Station.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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\RfRecord as rfRecordModel;
  7. // use catchAdmin\device\model\Station as stationModel;
  8. // use catchAdmin\device\model\StationPhoto;
  9. use catcher\Utils;
  10. use catcher\library\excel\Excel;
  11. use PhpOffice\PhpSpreadsheet\IOFactory;
  12. use think\facade\Env;
  13. use think\facade\Db;
  14. use PDO;
  15. class Station extends CatchController
  16. {
  17. // protected $stationModel;
  18. // protected $rfRecordModel;
  19. // public function __construct(rfRecordModel $rfRecordModel,stationModel $stationModel)
  20. // {
  21. // $this->rfRecordModel = $rfRecordModel;
  22. // $this->stationModel = $stationModel;
  23. // }
  24. /**
  25. * 列表
  26. * @time 2022年01月20日 10:09
  27. * @param Request $request
  28. */
  29. public function index(Request $request)
  30. {
  31. $param=$request->param();
  32. $cond=[];
  33. if($param['shortcode']){
  34. $cond['a.DEVICE_CODE']=['like',$param['shortcode']];
  35. }
  36. if($param['name']){
  37. // $param['name'] = mb_convert_encoding($param['name'], 'GBK','UTF-8');
  38. $cond['a.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('(SELECT * FROM DSSC2.ADM_DEV ORDER BY ID DESC) a',$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.CREATE_DATE,\'yyyy-mm-dd hh24:mi:ss\') CREATE_DATE');
  44. foreach($rows as &$val){
  45. $val['IS_ONLINE_TEXT']=$val['IS_ONLINE']?'在线':'离线';
  46. $findCond=[
  47. 'DEVICE_CODE'=>['=',$val['DEVICE_CODE']]
  48. ];
  49. $info=queryOracleFind('DSSC2.ADM_DEV_RFID_CHN',$findCond);
  50. $val['longitude']=$info['GPS_X'];
  51. $val['latitude']=$info['GPS_Y'];
  52. }
  53. $response=[
  54. 'code'=>10000,
  55. 'message'=>'查询成功',
  56. 'count'=>$count,
  57. 'data'=>$rows,
  58. 'current'=>isset($param['page'])?(int)$param['page']:1,
  59. 'limit'=>isset($param['limit'])?(int)$param['limit']:10,
  60. ];
  61. return $response;
  62. }
  63. /**
  64. * 保存信息
  65. * @time 2022年01月20日 10:55
  66. * @param Request $request
  67. */
  68. public function save(Request $request)
  69. {
  70. $params=$request->param();
  71. $params['LOGIN_NAME']=strtoupper($params['LOGIN_NAME']);
  72. $r=$this->execSaveStation($params);
  73. return CatchResponse::success($r);
  74. }
  75. private function execSaveStation($data){
  76. $conn=getOracleConnect();
  77. $sql='select DEVICE_CODE from "DSSC2"."ADM_DEV" where DEVICE_CODE in (select max(DEVICE_CODE) from "DSSC2"."ADM_DEV")';
  78. $stid = oci_parse($conn, $sql);
  79. $r = oci_execute($stid);
  80. $result = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS);
  81. $deivce_code=$result['DEVICE_CODE']+1;
  82. $time=date('Y-m-d H:i:s');
  83. //插入基站信息表
  84. $sql='INSERT INTO "DSSC2"."ADM_DEV"("ID", "DEVICE_CODE","DEVICE_NAME", "DEVICE_CATEGORY", "DEVICE_TYPE","OWNER_CODE","LOGIN_TYPE","LOGIN_NAME","IS_ONLINE","CREATE_DATE","UPDATE_DATE")
  85. VALUES(DSSC2.SEQ_ADM_DEV.nextval,\''.$deivce_code.'\',\''.$data['DEVICE_NAME'].'\',32,\'13\',\'001\',\'6\',\''.$data['LOGIN_NAME'].'\',0,TO_DATE(\''.$time.'\', \'SYYYY-MM-DD HH24:MI:SS\'),TO_DATE(\''.$time.'\', \'SYYYY-MM-DD HH24:MI:SS\'))';
  86. $stid = oci_parse($conn, $sql);
  87. $r = oci_execute($stid);
  88. //插入基站信息表
  89. $sql='INSERT INTO "DSSC2"."ADM_DEV_RFID_CHN"("ID", "DEVICE_CODE","CHANNEL_NAME", "STAT" ,"GPS_X","GPS_Y","TYPE")
  90. VALUES(DSSC2.SEQ_ADM_DEV_RFID_CHN.nextval,\''.$deivce_code.'\',\''.$data['DEVICE_NAME'].'\',1,\''.$data['longitude'].'\',\''.$data['latitude'].'\',1)';
  91. $stid = oci_parse($conn, $sql);
  92. $r = oci_execute($stid);
  93. oci_free_statement($stid);
  94. return $r;
  95. }
  96. /**
  97. * 更新
  98. * @time 2022年01月20日 10:55
  99. * @param Request $request
  100. * @param $id
  101. */
  102. public function update(Request $request, $id)
  103. {
  104. $params=$request->param();
  105. $params['LOGIN_NAME']=strtoupper($params['LOGIN_NAME']);
  106. //UPDATE DSSC2.ADM_DEV A SET A.DEVICE_NAME = '1014测试插入4' WHERE A.DEVICE_CODE = '79933';
  107. //UPDATE DSSC2.ADM_DEV_RFID_CHN A SET A.CHANNEL_NAME = '1014测试插入4' WHERE A.DEVICE_CODE ='79933';
  108. $conn=getOracleConnect();
  109. //更新
  110. $sql='UPDATE DSSC2.ADM_DEV SET DEVICE_NAME = \''.$params['DEVICE_NAME'].'\',LOGIN_NAME=\''.$params['LOGIN_NAME'].'\' WHERE DEVICE_CODE = \''.$id.'\' ';
  111. $stid = oci_parse($conn, $sql);
  112. $r = oci_execute($stid);
  113. $sql='UPDATE DSSC2.ADM_DEV_RFID_CHN SET CHANNEL_NAME = \''.$params['DEVICE_NAME'].'\',GPS_X='.$params['longitude'].',GPS_Y='.$params['latitude'].' WHERE DEVICE_CODE = \''.$id.'\' ';
  114. $stid2 = oci_parse($conn, $sql);
  115. $r2 = oci_execute($stid2);
  116. return CatchResponse::success('修改成功');
  117. }
  118. /**
  119. * 删除
  120. * @time 2022年01月20日 10:55
  121. * @param $id
  122. */
  123. public function delete($id) : \think\Response
  124. {
  125. // $ids=Utils::stringToArrayBy($id);
  126. $conn=getOracleConnect();
  127. $sql='DELETE FROM DSSC2.ADM_DEV WHERE DEVICE_CODE in ('.$id.')';
  128. $stid = oci_parse($conn, $sql);
  129. $r = oci_execute($stid);
  130. $sql='DELETE FROM DSSC2.ADM_DEV_RFID_CHN WHERE DEVICE_CODE in ('.$id.')';
  131. $stid = oci_parse($conn, $sql);
  132. $r = oci_execute($stid);
  133. return CatchResponse::success($r);
  134. }
  135. public function getdeviceListByStation(Request $request){
  136. $params=$request->param();
  137. if(!$params['station_code']){
  138. return CatchResponse::success('');
  139. }
  140. $cond=[];
  141. if($params['station_code']){
  142. $cond['RF_ID']=['=',$params['station_code']];
  143. }
  144. $start_time = date('Y-m-d 00:00:00',time());
  145. $end_time = date('Y-m-d 23:59:59',time());
  146. if(isset($params['timeRange']) && $params['timeRange'] != ''){
  147. $start_time=date('Y-m-d H:i:s',strtotime($params['timeRange'][0]));
  148. $end_time=date('Y-m-d H:i:s',strtotime($params['timeRange'][1]));
  149. $cond['RF_DATE']=['timeRange',$start_time,$end_time];
  150. }
  151. $count=queryOracleCount('DSSC2.W_DW_RF_RECORD',$cond);
  152. $cond['page']=isset($param['page'])?$param['page']:1;
  153. $cond['limit']=isset($param['limit'])?$param['limit']:10;
  154. $rows=queryOracleSelect('(SELECT * FROM DSSC2.W_DW_RF_RECORD ORDER BY ID DESC) a',$cond,'a.RF_FLAGID,a.RF_STAT,to_char(a.RF_DATE,\'yyyy-mm-dd hh24:mi:ss\') RF_DATE');
  155. foreach($rows as &$val){
  156. //状态: 0- 未知,1 - 进入,2 - 离开
  157. if($val['RF_STAT']==1){
  158. $val['RF_STAT_TEXT']='进入';
  159. }elseif($val['RF_STAT']==2){
  160. $val['RF_STAT_TEXT']='离开';
  161. }else{
  162. $val['RF_STAT_TEXT']='未知';
  163. }
  164. }
  165. $response=[
  166. 'code'=>10000,
  167. 'message'=>'查询成功',
  168. 'count'=>$count,
  169. 'data'=>$rows,
  170. 'current'=>isset($params['page'])?(int)$params['page']:1,
  171. 'limit'=>isset($params['limit'])?(int)$params['limit']:10,
  172. ];
  173. return $response;
  174. // $cond=[];
  175. // $params=$request->param();
  176. // if(!$params['station_code']){
  177. // return CatchResponse::success('');
  178. // }
  179. // if($params['station_code']){
  180. // $cond[]=['RF_ID','=',$params['station_code']];
  181. // }
  182. // $start_time = date('Y-m-d 00:00:00',time());
  183. // $end_time = date('Y-m-d 23:59:59',time());
  184. // if(isset($params['timeRange']) && $params['timeRange'] != ''){
  185. // $start_time=date('Y-m-d H:i:s',strtotime($params['timeRange'][0]));
  186. // $end_time=date('Y-m-d H:i:s',strtotime($params['timeRange'][1]));
  187. // $cond[]=['RF_DATE','between',[$start_time,$end_time]];
  188. // }
  189. // $list=$this->rfRecordModel->getList($cond);
  190. // return CatchResponse::paginate($list);
  191. }
  192. /**
  193. * 导入设备
  194. *
  195. * @time 2022年02月15日
  196. * @param Excel $excel
  197. * @param DeviceExport $deviceExport
  198. * @throws \PhpOffice\PhpSpreadsheet\Exception
  199. * @return \think\response\Json
  200. */
  201. public function importStation(Request $request)
  202. {
  203. $url = $request->post('url');
  204. if (!$url) {
  205. return CatchResponse::fail('请上传文件');
  206. }
  207. $creator_id = $request->post('creator_id');
  208. //解析地址
  209. $parse_url = parse_url($url)['path'];
  210. //载入excel表格
  211. $objPHPExcel = IOFactory::load(public_path() . $parse_url);
  212. //获取表名,一维数组,值是表名。如:array('sheet1', 'sheet2', 'sheet3')
  213. // $nameArr = $objPHPExcel->getSheetNames();
  214. //获取表的数量
  215. $sheetCount = $objPHPExcel->getSheetCount();
  216. $fail = 0; //失败条数
  217. $success = 0; //成功条数
  218. $total = 0; //总共设备数
  219. $data = []; //设备数据
  220. //循环读取每一张表
  221. for ($index = 0; $index < $sheetCount; $index++) {
  222. //设置当前要读取的表
  223. $sheet = $objPHPExcel->getSheet($index); //excel中的第一张sheet
  224. // var_dump($sheet);exit;
  225. $highestRow = $sheet->getHighestRow(); // 取得总行数
  226. // var_dump($highestRow);
  227. if ($highestRow <= 1) {
  228. continue;
  229. }
  230. $total += $highestRow - 1;
  231. for ($j = 2; $j <= $highestRow; $j++) {
  232. $arr = array(); //每条设备信息
  233. $arr['LOGIN_NAME'] = strtoupper(trim($sheet->getCell("A" . $j)->getFormattedValue()));
  234. $arr['longitude'] = trim($sheet->getCell("B" . $j)->getFormattedValue());
  235. $arr['latitude'] = trim($sheet->getCell("C" . $j)->getFormattedValue());
  236. $arr['DEVICE_NAME'] = trim($sheet->getCell("D" . $j)->getFormattedValue());
  237. $r=$this->execSaveStation($arr);
  238. if($r){
  239. $success++;
  240. }else{
  241. $fail++;
  242. }
  243. // var_dump($arr);
  244. // array_push($data,$arr);
  245. }
  246. }
  247. // array_unique($data, SORT_REGULAR);
  248. // // var_dump($data);return CatchResponse::success();
  249. // $count = $this->deviceModel->limit(100)->insertAll($data);
  250. // if ($success = $count) {
  251. return CatchResponse::success('共' . $total . '条数据,成功' . $success . '条,失败' . $fail . '条');
  252. // }
  253. // return CatchResponse::success(['error' => true, 'msg' => '导入失败']);
  254. }
  255. }