Station.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 catchAdmin\device\model\StationPhoto;
  8. use catcher\Utils;
  9. use catcher\library\excel\Excel;
  10. use PhpOffice\PhpSpreadsheet\IOFactory;
  11. use catchAdmin\device\excel\StationExport;
  12. use think\facade\Db;
  13. use PDO;
  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年01月20日 10:09
  24. * @param Request $request
  25. */
  26. public function index(Request $request)
  27. {
  28. // header('Content-Type: text/html; charset=utf-8');
  29. // header('Content-Type: text/html; charset=gb2312');
  30. $param=$request->param();
  31. // var_dump($param);
  32. $cond=[];
  33. if($param['shortcode']){
  34. $cond['DEVICE_CODE']=['like',$param['shortcode']];
  35. }
  36. if($param['name']){
  37. $param['name'] = mb_convert_encoding($param['name'], 'GBK','UTF-8');
  38. $cond['DEVICE_NAME']=['like',$param['name']];
  39. }
  40. $count=queryOracleCount('DSSC2.ADM_DEV',$cond);
  41. $cond['page']=isset($param['page'])?$param['page']:1;
  42. $cond['limit']=isset($param['limit'])?$param['limit']:10;
  43. $rows=queryOracleSelect('DSSC2.ADM_DEV',$cond,'a.ID,a.IS_ONLINE,a.LOGIN_NAME,a.DEVICE_CODE,a.DEVICE_NAME,a.OWNER_CODE,a.DEVICE_IP,a.DEVICE_PORT,to_char(a.UPDATE_DATE,\'yyyy-mm-dd hh24:mi:ss\') UPDATE_DATE');
  44. foreach($rows as &$val){
  45. $val['DEVICE_NAME'] = mb_convert_encoding($val['DEVICE_NAME'], 'UTF-8', 'GBK');
  46. $val['IS_ONLINE_TEXT']=$val['IS_ONLINE']?'在线':'离线';
  47. // $val['UPDATE_DATE'] = mb_convert_encoding($val['UPDATE_DATE'], 'UTF-8', 'GBK');
  48. // $val['CREATE_DATE'] = mb_convert_encoding($val['CREATE_DATE'], 'UTF-8', 'GBK');
  49. $findCond=[
  50. 'DEVICE_CODE'=>['=',$val['DEVICE_CODE']]
  51. ];
  52. $info=queryOracleFind('DSSC2.ADM_DEV_RFID_CHN',$findCond);
  53. $val['longitude']=$info['GPS_X'];
  54. $val['latitude']=$info['GPS_Y'];
  55. }
  56. $response=[
  57. 'code'=>10000,
  58. 'message'=>'查询成功',
  59. 'count'=>$count,
  60. 'data'=>$rows,
  61. 'current'=>isset($param['page'])?(int)$param['page']:1,
  62. 'limit'=>isset($param['limit'])?(int)$param['limit']:10,
  63. ];
  64. return $response;
  65. // $field = $request->get('field')?:'id';
  66. // $order = $request->get('order')?:'desc';
  67. // return CatchResponse::paginate($this->stationModel->getStationList($field,$order));
  68. }
  69. public function getdeviceListByStation(Request $request){
  70. $params=$request->param();
  71. if(!$params['station_code']){
  72. return CatchResponse::success('');
  73. }
  74. $cond=[];
  75. if($params['station_code']){
  76. $cond['RF_ID']=['=',$params['station_code']];
  77. }
  78. $start_time = date('Y-m-d 00:00:00',time());
  79. $end_time = date('Y-m-d 23:59:59',time());
  80. if(isset($params['timeRange']) && $params['timeRange'] != ''){
  81. $start_time=date('Y-m-d H:i:s',strtotime($params['timeRange'][0]));
  82. $end_time=date('Y-m-d H:i:s',strtotime($params['timeRange'][1]));
  83. $cond['RF_DATE']=['timeRange',$start_time,$end_time];
  84. }
  85. $count=queryOracleCount('DSSC2.W_DW_RF_RECORD',$cond);
  86. $cond['page']=isset($param['page'])?$param['page']:1;
  87. $cond['limit']=isset($param['limit'])?$param['limit']:10;
  88. $rows=queryOracleSelect('DSSC2.W_DW_RF_RECORD',$cond,'a.RF_FLAGID,a.RF_STAT,to_char(a.RF_DATE,\'yyyy-mm-dd hh24:mi:ss\') RF_DATE');
  89. foreach($rows as &$val){
  90. $val['RF_DATE'] = mb_convert_encoding($val['RF_DATE'], 'UTF-8', 'GBK');
  91. }
  92. $response=[
  93. 'code'=>10000,
  94. 'message'=>'查询成功',
  95. 'count'=>$count,
  96. 'data'=>$rows,
  97. 'current'=>isset($params['page'])?(int)$params['page']:1,
  98. 'limit'=>isset($params['limit'])?(int)$params['limit']:10,
  99. ];
  100. return $response;
  101. }
  102. /**
  103. * 保存信息
  104. * @time 2022年01月20日 10:09
  105. * @param Request $request
  106. */
  107. public function save(Request $request) : \think\Response
  108. {
  109. $data = $request->post();
  110. //判断参数
  111. if(!$data['department_id']){
  112. return CatchResponse::fail('请选择所属部门');
  113. }
  114. if(!$data['model']){
  115. return CatchResponse::fail('请选择基站型号');
  116. }
  117. $data['mac'] = trim($data['mac']);
  118. if(!$data['mac']){
  119. return CatchResponse::fail('请输入基站Mac');
  120. }
  121. if (!preg_match('/^[0-9a-zA-Z]{6,15}$/i',$data['mac'])){
  122. return CatchResponse::fail('基站为6-15位字母数字');
  123. }
  124. //基站简码
  125. $data['shortcode'] = substr($data['mac'],-6,6);
  126. //校验重复
  127. if($this->stationModel->where('mac',$data['mac'])->count()){
  128. return CatchResponse::fail('该基站已存在');
  129. }
  130. //开启事务
  131. Db::startTrans();
  132. //开局状态
  133. if($data['type'] == 1){
  134. $data['open_status'] = 0;
  135. }else{
  136. $data['open_status'] = 1;
  137. //开局时间
  138. $data['open_time'] = date('Y-m-d H:i:s');
  139. //开局员工
  140. $data['open_user_id'] = $data['creator_id'];
  141. }
  142. $station_id = $this->stationModel->storeBy($data);
  143. if(!$station_id){
  144. return CatchResponse::fail('添加失败');
  145. }
  146. //基站图片
  147. if(isset($data['install_photo'])){
  148. $photo = array(
  149. 'url'=>$data['install_photo'],
  150. 'type'=>'station',
  151. 'station_id'=>$station_id,
  152. 'creator_id'=>$data['creator_id']
  153. );
  154. $res = (new StationPhoto())->storeBy($photo);
  155. if(!$res){
  156. Db::rollback();
  157. return CatchResponse::fail('添加失败');
  158. }
  159. }
  160. Db::commit();
  161. return CatchResponse::success();
  162. }
  163. /**
  164. * 读取
  165. * @time 2022年01月20日 10:09
  166. * @param $id
  167. */
  168. public function read($id) : \think\Response
  169. {
  170. return CatchResponse::success($this->stationModel->findBy($id));
  171. }
  172. /**
  173. * 更新
  174. * @time 2022年01月20日 10:09
  175. * @param Request $request
  176. * @param $id
  177. */
  178. public function update(Request $request, $id) : \think\Response
  179. {
  180. return CatchResponse::success($this->stationModel->updateBy($id, $request->post()));
  181. }
  182. /**
  183. * 删除
  184. * @time 2022年01月20日 10:09
  185. * @param $id
  186. */
  187. public function delete($id) : \think\Response
  188. {
  189. return CatchResponse::success($this->stationModel->deleteBy($id,true));
  190. }
  191. /**
  192. * 导出
  193. *
  194. * @time 2022年02月15日
  195. * @param Excel $excel
  196. * @param StationExport $stationExport
  197. * @throws \PhpOffice\PhpSpreadsheet\Exception
  198. * @return \think\response\Json
  199. */
  200. public function export_station(Excel $excel, StationExport $StationExport)
  201. {
  202. // var_dump(Utils::publicPath('export/users'));//导出路径
  203. return CatchResponse::success($excel->save($StationExport, Utils::publicPath('export/stations'), 'local', '基站列表'));
  204. }
  205. /**
  206. * 导入设备
  207. *
  208. * @time 2022年02月15日
  209. * @param Excel $excel
  210. * @param DeviceExport $deviceExport
  211. * @throws \PhpOffice\PhpSpreadsheet\Exception
  212. * @return \think\response\Json
  213. */
  214. public function import_station(Request $request)
  215. {
  216. $url = $request->post('url');
  217. if (!$url) {
  218. return CatchResponse::fail('请上传文件');
  219. }
  220. $depart_id = $request->post('depart_id');
  221. if (!$depart_id) {
  222. return CatchResponse::fail('请选择部门');
  223. }
  224. $model_id = $request->post('model_id');
  225. if (!$model_id) {
  226. return CatchResponse::fail('请选择型号');
  227. }
  228. $creator_id = $request->post('creator_id');
  229. //解析地址
  230. $parse_url = parse_url($url)['path'];
  231. //载入excel表格
  232. $objPHPExcel = IOFactory::load(public_path() . $parse_url);
  233. // var_dump($objPHPExcel);
  234. //获取表名,一维数组,值是表名。如:array('sheet1', 'sheet2', 'sheet3')
  235. // $nameArr = $objPHPExcel->getSheetNames();
  236. // var_dump($nameArr);
  237. //获取表的数量
  238. $sheetCount = $objPHPExcel->getSheetCount();
  239. $fail = 0; //失败条数
  240. $success = 0; //成功条数
  241. $total = 0; //总共设备数
  242. $data = []; //基站数据
  243. //循环读取每一张表
  244. for ($index = 0; $index < $sheetCount; $index++) {
  245. //设置当前要读取的表
  246. $sheet = $objPHPExcel->getSheet($index); //excel中的第一张sheet
  247. // var_dump($sheet);exit;
  248. $highestRow = $sheet->getHighestRow(); // 取得总行数
  249. // var_dump($highestRow);
  250. if ($highestRow <= 2) {
  251. continue;
  252. }
  253. $total += $highestRow - 2;
  254. for ($j = 3; $j <= $highestRow; $j++) {
  255. $arr = array(); //每条基站信息
  256. $arr['mac'] = trim($sheet->getCell("A" . $j)->getFormattedValue()); //imei
  257. if ($arr['mac'] && mb_strlen($arr['mac']) != 12) {
  258. $fail++;
  259. $msg = '导入基站:' . $arr['mac'] . '失败:mac编号格式不正确';
  260. $this->importFailLog($msg);
  261. continue;
  262. }
  263. if ($arr['mac']) {
  264. $isHas = $this->stationModel->where('mac',$arr['mac'])->count();
  265. if($isHas){
  266. $fail++;
  267. $msg = '导入基站:' . $arr['mac'] . '失败:mac编号已存在';
  268. $this->importFailLog($msg);
  269. continue;
  270. }
  271. }
  272. $arr['remark'] = trim($sheet->getCell("B" . $j)->getFormattedValue()); //备注
  273. $arr['department_id'] = $depart_id;
  274. $arr['shortcode'] = substr($arr['mac'],-6);
  275. $arr['creator_id'] = $creator_id;
  276. $arr['model'] = $model_id;
  277. $arr['name'] = '基站'.substr($arr['mac'],-4);
  278. $arr['created_at'] = time();
  279. $arr['updated_at'] = time();
  280. array_push($data,$arr);
  281. }
  282. }
  283. array_unique($data, SORT_REGULAR);
  284. // var_dump($data);return CatchResponse::success();
  285. $count = $this->stationModel->limit(100)->insertAll($data);
  286. if ($success = $count) {
  287. return CatchResponse::success('共' . $total . '条数据,成功' . $success . '条,失败' . $fail . '条');
  288. }
  289. return CatchResponse::success(['error' => true, 'msg' => '导入失败']);
  290. }
  291. /**
  292. * 导入设备失败日志
  293. */
  294. public function importFailLog($msg)
  295. {
  296. $file = runtime_path() . '/log/' . date("Ymd", time()) . "/import_stations_fail.log";
  297. $folder = dirname($file);
  298. if (!is_dir($folder)) {
  299. mkdir($folder, 0777, true);
  300. }
  301. file_put_contents($file, '[' . date('Y-m-d H:i:s') . ']' . $msg . PHP_EOL, FILE_APPEND);
  302. }
  303. // ┏┛ ┻━━━━━┛ ┻┓
  304. // ┃       ┃
  305. // ┃   ━   ┃
  306. // ┃ ┳┛  ┗┳ ┃
  307. // ┃       ┃
  308. // ┃   ┻   ┃
  309. // ┃       ┃
  310. // ┗━┓   ┏━━━┛
  311. // ┃   ┃ 神兽保佑
  312. // ┃   ┃ 代码无BUG!
  313. // ┃   ┗━━━━━━━━━┓
  314. // ┃        ┣┓
  315. // ┃     ┏┛
  316. // ┗━┓ ┓ ┏━━━┳ ┓ ┏━┛
  317. // ┃ ┫ ┫ ┃ ┫ ┫
  318. // ┗━┻━┛ ┗━┻━┛
  319. }