Station.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. $shortcode=$this->stationModel->where('id',$id)->value('shortcode');
  71. $params['update_code']=$shortcode;
  72. $params['DATA_TYPE']='station_update';
  73. pushDataToRedisList($params);
  74. return CatchResponse::success($this->stationModel->updateBy($id, $params));
  75. }
  76. /**
  77. * 删除
  78. * @time 2022年10月26日 10:32
  79. * @param $id
  80. */
  81. public function delete($id) : \think\Response
  82. {
  83. $ids=explode(',',$id);
  84. $shortcode=$this->stationModel->whereIn('id',$ids)->column('shortcode');
  85. $params['delete_code']=$shortcode;
  86. $params['DATA_TYPE']='station_save';
  87. pushDataToRedisList($params);
  88. return CatchResponse::success($this->stationModel->deleteBy($id,true));
  89. }
  90. public function getdeviceListByStation(Request $request){
  91. $params=$request->param();
  92. if(!$params['station_code']){
  93. return CatchResponse::success([]);
  94. }
  95. $cond=[];
  96. if($params['station_code']){
  97. $cond['RF_ID']=['=',$params['station_code']];
  98. }
  99. $start_time = date('Y-m-d 00:00:00',time());
  100. $end_time = date('Y-m-d 23:59:59',time());
  101. if(isset($params['timeRange']) && $params['timeRange'] != ''){
  102. $start_time=date('Y-m-d H:i:s',strtotime($params['timeRange'][0]));
  103. $end_time=date('Y-m-d H:i:s',strtotime($params['timeRange'][1]));
  104. $cond['RF_DATE']=['timeRange',$start_time,$end_time];
  105. }
  106. // $count=queryOracleCount('DSSC2.W_DW_RF_RECORD',$cond);
  107. $cond['page']=isset($params['page'])?$params['page']:1;
  108. $cond['limit']=isset($params['limit'])?$params['limit']:10;
  109. $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');
  110. foreach($rows as &$val){
  111. //状态: 0- 未知,1 - 进入,2 - 离开
  112. if($val['RF_STAT']==1){
  113. $val['RF_STAT_TEXT']='进入';
  114. }elseif($val['RF_STAT']==2){
  115. $val['RF_STAT_TEXT']='离开';
  116. }else{
  117. $val['RF_STAT_TEXT']='未知';
  118. }
  119. }
  120. $response=[
  121. 'code'=>10000,
  122. 'message'=>'查询成功',
  123. // 'count'=>$count,
  124. 'data'=>$rows,
  125. 'current'=>isset($params['page'])?(int)$params['page']:1,
  126. 'limit'=>isset($params['limit'])?(int)$params['limit']:10,
  127. ];
  128. return $response;
  129. // $cond=[];
  130. // $params=$request->param();
  131. // if(!$params['station_code']){
  132. // return CatchResponse::success('');
  133. // }
  134. // if($params['station_code']){
  135. // $cond[]=['RF_ID','=',$params['station_code']];
  136. // }
  137. // $start_time = date('Y-m-d 00:00:00',time());
  138. // $end_time = date('Y-m-d 23:59:59',time());
  139. // if(isset($params['timeRange']) && $params['timeRange'] != ''){
  140. // $start_time=date('Y-m-d H:i:s',strtotime($params['timeRange'][0]));
  141. // $end_time=date('Y-m-d H:i:s',strtotime($params['timeRange'][1]));
  142. // $cond[]=['RF_DATE','between',[$start_time,$end_time]];
  143. // }
  144. // $list=$this->rfRecordModel->getList($cond);
  145. // return CatchResponse::paginate($list);
  146. }
  147. /**
  148. * 导入设备
  149. *
  150. * @time 2022年02月15日
  151. * @param Excel $excel
  152. * @param DeviceExport $deviceExport
  153. * @throws \PhpOffice\PhpSpreadsheet\Exception
  154. * @return \think\response\Json
  155. */
  156. public function importStation(Request $request)
  157. {
  158. $url = $request->post('url');
  159. if (!$url) {
  160. return CatchResponse::fail('请上传文件');
  161. }
  162. $creator_id = $request->post('creator_id');
  163. //解析地址
  164. $parse_url = parse_url($url)['path'];
  165. //载入excel表格
  166. $objPHPExcel = IOFactory::load(public_path() . $parse_url);
  167. //获取表名,一维数组,值是表名。如:array('sheet1', 'sheet2', 'sheet3')
  168. // $nameArr = $objPHPExcel->getSheetNames();
  169. //获取表的数量
  170. $sheetCount = $objPHPExcel->getSheetCount();
  171. $fail = 0; //失败条数
  172. $success = 0; //成功条数
  173. $total = 0; //总共设备数
  174. $data = []; //设备数据
  175. //循环读取每一张表
  176. $shortcode=$this->stationModel->order('shortcode','desc')->value('shortcode');
  177. for ($index = 0; $index < $sheetCount; $index++) {
  178. //设置当前要读取的表
  179. $sheet = $objPHPExcel->getSheet($index); //excel中的第一张sheet
  180. // var_dump($sheet);exit;
  181. $highestRow = $sheet->getHighestRow(); // 取得总行数
  182. // var_dump($highestRow);
  183. if ($highestRow <= 1) {
  184. continue;
  185. }
  186. $total += $highestRow - 1;
  187. for ($j = 2; $j <= $highestRow; $j++) {
  188. $shortcode++;
  189. $arr = array(); //每条设备信息
  190. $arr['mac'] = strtoupper(trim($sheet->getCell("A" . $j)->getFormattedValue()));
  191. $arr['longitude'] = trim($sheet->getCell("B" . $j)->getFormattedValue());
  192. $arr['latitude'] = trim($sheet->getCell("C" . $j)->getFormattedValue());
  193. $arr['name'] = trim($sheet->getCell("D" . $j)->getFormattedValue());
  194. $arr['created_at']=time();
  195. $arr['shortcode']=$shortcode;
  196. $arr['DATA_TYPE']='station_save';
  197. pushDataToRedisList($arr);
  198. array_push($data,$arr);
  199. }
  200. }
  201. // array_unique($data, SORT_REGULAR);
  202. // // var_dump($data);return CatchResponse::success();
  203. $count = $this->stationModel->limit(100)->insertAll($data);
  204. if ($count) {
  205. return CatchResponse::success('共' . $total . '条数据,成功' . $count . '条,失败' . ($total-$count) . '条');
  206. }
  207. return CatchResponse::success(['error' => true, 'msg' => '导入失败']);
  208. }
  209. /**
  210. * 基站地图额
  211. * @time 2022年01月20日 10:09
  212. * @param Request $request
  213. */
  214. public function getMapList(Request $request)
  215. {
  216. return CatchResponse::paginate($this->stationModel->getMapList());
  217. }
  218. /**
  219. * 所有列表
  220. * @time 2022年01月20日 10:09
  221. * @param Request $request
  222. */
  223. public function getAllList(Request $request)
  224. {
  225. $params=$request->param();
  226. $start=$params['start']?$params['start']*4000:0;
  227. $limit=$params['limit']?:4000;
  228. $total=$this->stationModel->count();
  229. return CatchResponse::success(['list'=>$this->stationModel->getAllList($start,$limit),'total'=>$total]);
  230. }
  231. /**
  232. * 导出列表
  233. * @time 2022年01月20日 10:09
  234. * @param Request $request
  235. */
  236. public function getExportList(Request $request)
  237. {
  238. return CatchResponse::success($this->stationModel->getExportList());
  239. }
  240. public function getAllListForTrans(Request $request) {
  241. $start=0;
  242. $limit=40000;
  243. $list=$this->stationModel->getAllList($start,$limit);
  244. $rows=[];
  245. foreach($list as $val){
  246. $item=['key'=>$val['mac'],'label'=>$val['name'] ];
  247. $rows[]=$item;
  248. }
  249. return CatchResponse::success($rows);
  250. }
  251. }