Station.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace catchAdmin\device\controller;
  3. use catcher\base\CatchRequest as Request;
  4. use catcher\CatchResponse;
  5. use catcher\base\CatchController;
  6. use catchAdmin\device\model\Station as stationModel;
  7. use catcher\Utils;
  8. use catcher\library\excel\Excel;
  9. use PhpOffice\PhpSpreadsheet\IOFactory;
  10. use think\facade\Env;
  11. use think\facade\Db;
  12. use PDO;
  13. use think\facade\Cache;
  14. class Station extends CatchController
  15. {
  16. protected $stationModel;
  17. public function __construct(StationModel $stationModel)
  18. {
  19. $this->stationModel = $stationModel;
  20. }
  21. /**
  22. * 列表
  23. * @time 2022年10月26日 10:32
  24. * @param Request $request
  25. */
  26. public function index(Request $request) : \think\Response
  27. {
  28. return CatchResponse::paginate($this->stationModel->getList());
  29. }
  30. /**
  31. * 保存信息
  32. * @time 2022年10月26日 10:32
  33. * @param Request $request
  34. */
  35. public function save(Request $request) : \think\Response
  36. {
  37. $params=$request->post();
  38. $shortcode=$this->stationModel->order('shortcode','desc')->value('shortcode');
  39. $params['shortcode']=$shortcode+1;
  40. // $gcjLoc = \algorithm\Geometry::convertBd09ToGcj02((float)$params['latitude'],(float)$params['longitude']);
  41. $gcjLoc = \algorithm\Geometry::wgsTOgcj((float)$params['latitude'],(float)$params['longitude']);
  42. $params['longitude']=$gcjLoc['lng'];
  43. $params['latitude']=$gcjLoc['lat'];
  44. $params['DATA_TYPE']='station_save';
  45. pushDataToRedisList($params);
  46. return CatchResponse::success($this->stationModel->storeBy($params));
  47. }
  48. /**
  49. * 读取
  50. * @time 2022年10月26日 10:32
  51. * @param $id
  52. */
  53. public function read($id) : \think\Response
  54. {
  55. return CatchResponse::success($this->stationModel->findBy($id));
  56. }
  57. /**
  58. * 更新
  59. * @time 2022年10月26日 10:32
  60. * @param Request $request
  61. * @param $id
  62. */
  63. public function update(Request $request, $id) : \think\Response
  64. {
  65. $params=$request->post();
  66. // $gcjLoc = \algorithm\Geometry::convertBd09ToGcj02((float)$params['latitude'],(float)$params['longitude']);
  67. $gcjLoc = \algorithm\Geometry::wgsTOgcj((float)$params['latitude'],(float)$params['longitude']);
  68. $params['longitude']=$gcjLoc['lng'];
  69. $params['latitude']=$gcjLoc['lat'];
  70. $params['update_id']=$id;
  71. $params['DATA_TYPE']='station_update';
  72. pushDataToRedisList($params);
  73. return CatchResponse::success($this->stationModel->updateBy($id, $params));
  74. }
  75. /**
  76. * 删除
  77. * @time 2022年10月26日 10:32
  78. * @param $id
  79. */
  80. public function delete($id) : \think\Response
  81. {
  82. $ids=explode(',',$id);
  83. $shortcode=$this->stationModel->whereIn('id',$ids)->column('shortcode');
  84. $params['delete_code']=$shortcode;
  85. $params['DATA_TYPE']='station_save';
  86. pushDataToRedisList($params);
  87. return CatchResponse::success($this->stationModel->deleteBy($id,true));
  88. }
  89. public function getdeviceListByStation(Request $request){
  90. $params=$request->param();
  91. if(!$params['station_code']){
  92. return CatchResponse::success([]);
  93. }
  94. $cond=[];
  95. if($params['station_code']){
  96. $cond['RF_ID']=['=',$params['station_code']];
  97. }
  98. $start_time = date('Y-m-d 00:00:00',time());
  99. $end_time = date('Y-m-d 23:59:59',time());
  100. if(isset($params['timeRange']) && $params['timeRange'] != ''){
  101. $start_time=date('Y-m-d H:i:s',strtotime($params['timeRange'][0]));
  102. $end_time=date('Y-m-d H:i:s',strtotime($params['timeRange'][1]));
  103. $cond['RF_DATE']=['timeRange',$start_time,$end_time];
  104. }
  105. // $count=queryOracleCount('DSSC2.W_DW_RF_RECORD',$cond);
  106. $cond['page']=isset($params['page'])?$params['page']:1;
  107. $cond['limit']=isset($params['limit'])?$params['limit']:10;
  108. $rows=queryOracleSelect('(SELECT * FROM DSSC2.W_DW_RF_RECORD ORDER BY RF_DATE DESC) a',$cond,'a.RF_FLAGID,a.RF_STAT,to_char(a.RF_DATE,\'yyyy-mm-dd hh24:mi:ss\') RF_DATE');
  109. foreach($rows as &$val){
  110. //状态: 0- 未知,1 - 进入,2 - 离开
  111. if($val['RF_STAT']==1){
  112. $val['RF_STAT_TEXT']='进入';
  113. }elseif($val['RF_STAT']==2){
  114. $val['RF_STAT_TEXT']='离开';
  115. }else{
  116. $val['RF_STAT_TEXT']='未知';
  117. }
  118. }
  119. $response=[
  120. 'code'=>10000,
  121. 'message'=>'查询成功',
  122. // 'count'=>$count,
  123. 'data'=>$rows,
  124. 'current'=>isset($params['page'])?(int)$params['page']:1,
  125. 'limit'=>isset($params['limit'])?(int)$params['limit']:10,
  126. ];
  127. return $response;
  128. // $cond=[];
  129. // $params=$request->param();
  130. // if(!$params['station_code']){
  131. // return CatchResponse::success('');
  132. // }
  133. // if($params['station_code']){
  134. // $cond[]=['RF_ID','=',$params['station_code']];
  135. // }
  136. // $start_time = date('Y-m-d 00:00:00',time());
  137. // $end_time = date('Y-m-d 23:59:59',time());
  138. // if(isset($params['timeRange']) && $params['timeRange'] != ''){
  139. // $start_time=date('Y-m-d H:i:s',strtotime($params['timeRange'][0]));
  140. // $end_time=date('Y-m-d H:i:s',strtotime($params['timeRange'][1]));
  141. // $cond[]=['RF_DATE','between',[$start_time,$end_time]];
  142. // }
  143. // $list=$this->rfRecordModel->getList($cond);
  144. // return CatchResponse::paginate($list);
  145. }
  146. /**
  147. * 导入设备
  148. *
  149. * @time 2022年02月15日
  150. * @param Excel $excel
  151. * @param DeviceExport $deviceExport
  152. * @throws \PhpOffice\PhpSpreadsheet\Exception
  153. * @return \think\response\Json
  154. */
  155. public function importStation(Request $request)
  156. {
  157. $url = $request->post('url');
  158. if (!$url) {
  159. return CatchResponse::fail('请上传文件');
  160. }
  161. $creator_id = $request->post('creator_id');
  162. //解析地址
  163. $parse_url = parse_url($url)['path'];
  164. //载入excel表格
  165. $objPHPExcel = IOFactory::load(public_path() . $parse_url);
  166. //获取表名,一维数组,值是表名。如:array('sheet1', 'sheet2', 'sheet3')
  167. // $nameArr = $objPHPExcel->getSheetNames();
  168. //获取表的数量
  169. $sheetCount = $objPHPExcel->getSheetCount();
  170. $fail = 0; //失败条数
  171. $success = 0; //成功条数
  172. $total = 0; //总共设备数
  173. $data = []; //设备数据
  174. //循环读取每一张表
  175. $shortcode=$this->stationModel->order('shortcode','desc')->value('shortcode');
  176. for ($index = 0; $index < $sheetCount; $index++) {
  177. //设置当前要读取的表
  178. $sheet = $objPHPExcel->getSheet($index); //excel中的第一张sheet
  179. // var_dump($sheet);exit;
  180. $highestRow = $sheet->getHighestRow(); // 取得总行数
  181. // var_dump($highestRow);
  182. if ($highestRow <= 1) {
  183. continue;
  184. }
  185. $total += $highestRow - 1;
  186. for ($j = 2; $j <= $highestRow; $j++) {
  187. $arr = array(); //每条设备信息
  188. $arr['mac'] = strtoupper(trim($sheet->getCell("A" . $j)->getFormattedValue()));
  189. if(empty($arr['mac']) || strlen($arr['mac'])!=8){
  190. $fail++;
  191. debug_log('importStationError','MAC格式格式不正确,MAC为:'.$arr['mac']);
  192. continue;
  193. }
  194. $arr['longitude'] = trim($sheet->getCell("B" . $j)->getFormattedValue());
  195. $arr['latitude'] = trim($sheet->getCell("C" . $j)->getFormattedValue());
  196. $arr['name'] = trim($sheet->getCell("D" . $j)->getFormattedValue());
  197. $arr['created_at']=time();
  198. $shortcode++;
  199. $arr['shortcode']=$shortcode;
  200. array_push($data,$arr);
  201. $arr['DATA_TYPE']='station_save';
  202. pushDataToRedisList($arr);
  203. }
  204. }
  205. // array_unique($data, SORT_REGULAR);
  206. // // var_dump($data);return CatchResponse::success();
  207. $count = $this->stationModel->limit(100)->insertAll($data);
  208. if ($count) {
  209. return CatchResponse::success('共' . $total . '条数据,成功' . $count . '条,失败' . ($total-$count) . '条');
  210. }
  211. return CatchResponse::success(['error' => true, 'msg' => '导入失败']);
  212. }
  213. /**
  214. * 基站地图额
  215. * @time 2022年01月20日 10:09
  216. * @param Request $request
  217. */
  218. public function getMapList(Request $request)
  219. {
  220. return CatchResponse::paginate($this->stationModel->getMapList());
  221. }
  222. /**
  223. * 所有列表
  224. * @time 2022年01月20日 10:09
  225. * @param Request $request
  226. */
  227. public function getAllList(Request $request)
  228. {
  229. $params=$request->param();
  230. $start=$params['start']?$params['start']*4000:0;
  231. $limit=$params['limit']?:4000;
  232. $total=$this->stationModel->count();
  233. return CatchResponse::success(['list'=>$this->stationModel->getAllList($start,$limit),'total'=>$total]);
  234. }
  235. /**
  236. * 导出列表
  237. * @time 2022年01月20日 10:09
  238. * @param Request $request
  239. */
  240. public function getExportList(Request $request)
  241. {
  242. return CatchResponse::success($this->stationModel->getExportList());
  243. }
  244. public function getAllListForTrans(Request $request) {
  245. $start=0;
  246. $limit=40000;
  247. $list=$this->stationModel->getAllList($start,$limit);
  248. $rows=[];
  249. foreach($list as $val){
  250. $item=['key'=>$val['mac'],'label'=>$val['name'] ];
  251. $rows[]=$item;
  252. }
  253. return CatchResponse::success($rows);
  254. }
  255. }