Station.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. use think\facade\Cache;
  16. class Station extends CatchController
  17. {
  18. // protected $stationModel;
  19. // protected $rfRecordModel;
  20. // public function __construct(rfRecordModel $rfRecordModel,stationModel $stationModel)
  21. // {
  22. // $this->rfRecordModel = $rfRecordModel;
  23. // $this->stationModel = $stationModel;
  24. // }
  25. /**
  26. * 列表
  27. * @time 2022年01月20日 10:09
  28. * @param Request $request
  29. */
  30. public function index(Request $request)
  31. {
  32. $param=$request->param();
  33. $cond=[];
  34. if($param['shortcode']){
  35. $cond['a.DEVICE_CODE']=['like',$param['shortcode']];
  36. }
  37. if($param['name']){
  38. // $param['name'] = mb_convert_encoding($param['name'], 'GBK','UTF-8');
  39. $cond['a.DEVICE_NAME']=['like',$param['name']];
  40. }
  41. $count=queryOracleCount('DSSC2.ADM_DEV',$cond);
  42. $cond['page']=isset($param['page'])?$param['page']:1;
  43. $cond['limit']=isset($param['limit'])?$param['limit']:10;
  44. $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');
  45. foreach($rows as &$val){
  46. $val['IS_ONLINE_TEXT']=$val['IS_ONLINE']?'在线':'离线';
  47. $findCond=[
  48. 'DEVICE_CODE'=>['=',$val['DEVICE_CODE']]
  49. ];
  50. $info=queryOracleFind('DSSC2.ADM_DEV_RFID_CHN',$findCond);
  51. $val['longitude']=$info['GPS_X'];
  52. $val['latitude']=$info['GPS_Y'];
  53. }
  54. $response=[
  55. 'code'=>10000,
  56. 'message'=>'查询成功',
  57. 'count'=>$count,
  58. 'data'=>$rows,
  59. 'current'=>isset($param['page'])?(int)$param['page']:1,
  60. 'limit'=>isset($param['limit'])?(int)$param['limit']:10,
  61. ];
  62. return $response;
  63. }
  64. /**
  65. * 所有列表
  66. * @time 2022年01月20日 10:09
  67. * @param Request $request
  68. */
  69. public function getAllList(Request $request)
  70. {
  71. $list=Cache::get('stationList');
  72. if($list){
  73. return [
  74. 'code'=>10000,
  75. 'message'=>'查询成功',
  76. 'data'=>$list,
  77. ];
  78. }
  79. $cond=[
  80. '_string'=>'a.DEVICE_CODE = r.DEVICE_CODE',
  81. ];
  82. $cond['page']=1;
  83. $cond['limit']=30000;
  84. $conn = null;
  85. $tableName='(SELECT * FROM DSSC2.ADM_DEV ORDER BY ID DESC) a,DSSC2.ADM_DEV_RFID_CHN r';
  86. $field='a.ID,a.IS_ONLINE,a.LOGIN_NAME,a.DEVICE_CODE,a.DEVICE_NAME,a.OWNER_CODE,to_char(a.UPDATE_DATE,\'yyyy-mm-dd hh24:mi:ss\') UPDATE_DATE,r.GPS_X,r.GPS_Y';
  87. $host= Env::get('oracle.hostname', '127.0.0.1');
  88. $port= Env::get('oracle.hostport', '1521');
  89. $instance_name= Env::get('oracle.instance', 'ORCL');
  90. $username= Env::get('oracle.username', 'root');
  91. $password= Env::get('oracle.password', 'root');
  92. // $conn = oci_connect('用户名', '密码', '远程数据库名(eg.//192.168.1.133/orcl)');
  93. $conn = oci_connect($username, $password, $host.':'.$port.'/'. $instance_name,'AL32UTF8');
  94. if (!$conn) {
  95. $e = oci_error();
  96. trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
  97. }
  98. $start=($cond['page']-1)*$cond['limit'];
  99. $end=$cond['page']*$cond['limit'];
  100. $whereStr=buildQueryCond2($cond);
  101. $sql='SELECT * FROM ( SELECT '.$field.',ROWNUM RN FROM '.$tableName.' WHERE ROWNUM <='.$end.' '.$whereStr.' ) WHERE RN >'. $start;
  102. // $sql='SELECT '.$field.' FROM '.$tableName.' WHERE '.$whereStr;
  103. // var_dump($sql);
  104. // 查询并渲染
  105. $stid = oci_parse($conn, $sql);
  106. $r = oci_execute($stid);
  107. $rows=[];
  108. while($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
  109. if($row['GPS_Y'] && $row['GPS_X']){
  110. // $wgsLoc = \algorithm\Geometry::wgsTOgcj((float)$row['GPS_Y'],(float)$row['GPS_X']);
  111. // $lngLat = \algorithm\Geometry::convertGcj02ToBd09($wgsLoc['lat'],$wgsLoc['lng']);
  112. // $row['GPS_X']=$lngLat['lng'];
  113. // $row['GPS_Y']=$lngLat['lat'];
  114. $rows[]=$row;
  115. }
  116. }
  117. oci_free_statement($stid);
  118. Cache::set('stationList', $rows, 300);
  119. $response=[
  120. 'code'=>10000,
  121. 'message'=>'查询成功',
  122. 'data'=>$rows,
  123. ];
  124. return $response;
  125. }
  126. /**
  127. * 所有列表给智能交通使用
  128. * @time 2022年01月20日 10:09
  129. * @param Request $request
  130. */
  131. public function getAllListForTrans(Request $request)
  132. {
  133. // $param=$request->param();
  134. // $cond['page']=isset($param['page'])?$param['page']:1;
  135. // $cond['limit']=isset($param['limit'])?$param['limit']:100;
  136. $cond['page']=1;
  137. $cond['limit']=30000;
  138. $conn = null;
  139. $tableName='(SELECT * FROM DSSC2.ADM_DEV ORDER BY ID DESC) a';
  140. $field='a.ID,a.LOGIN_NAME,a.DEVICE_CODE,a.DEVICE_NAME,a.OWNER_CODE';
  141. $host= Env::get('oracle.hostname', '127.0.0.1');
  142. $port= Env::get('oracle.hostport', '1521');
  143. $instance_name= Env::get('oracle.instance', 'ORCL');
  144. $username= Env::get('oracle.username', 'root');
  145. $password= Env::get('oracle.password', 'root');
  146. // $count=queryOracleCount('DSSC2.ADM_DEV',$cond);
  147. // $conn = oci_connect('用户名', '密码', '远程数据库名(eg.//192.168.1.133/orcl)');
  148. $conn = oci_connect($username, $password, $host.':'.$port.'/'. $instance_name,'AL32UTF8');
  149. if (!$conn) {
  150. $e = oci_error();
  151. trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
  152. }
  153. $start=($cond['page']-1)*$cond['limit'];
  154. $end=$cond['page']*$cond['limit'];
  155. $whereStr=buildQueryCond2($cond);
  156. $sql='SELECT * FROM ( SELECT '.$field.',ROWNUM RN FROM '.$tableName.' WHERE ROWNUM <='.$end.' '.$whereStr.' ) WHERE RN >'. $start;
  157. // var_dump($sql);
  158. // 查询并渲染
  159. $stid = oci_parse($conn, $sql);
  160. $r = oci_execute($stid);
  161. $rows=[];
  162. while($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
  163. $item=['key'=>$row['LOGIN_NAME'],'label'=>$row['DEVICE_NAME']];
  164. $rows[]=$item;
  165. }
  166. oci_free_statement($stid);
  167. // $response=[
  168. // 'code'=>10000,
  169. // 'message'=>'查询成功',
  170. // 'count'=>$count,
  171. // 'data'=>$rows,
  172. // 'current'=>isset($param['page'])?(int)$param['page']:1,
  173. // 'limit'=>isset($param['limit'])?(int)$param['limit']:100,
  174. // ];
  175. // return $response;
  176. $response=[
  177. 'code'=>10000,
  178. 'message'=>'查询成功',
  179. 'data'=>$rows,
  180. ];
  181. return $response;
  182. }
  183. /**
  184. * 保存信息
  185. * @time 2022年01月20日 10:55
  186. * @param Request $request
  187. */
  188. public function save(Request $request)
  189. {
  190. $params=$request->param();
  191. $params['LOGIN_NAME']=strtoupper($params['LOGIN_NAME']);
  192. $r=$this->execSaveStation($params);
  193. return CatchResponse::success($r);
  194. }
  195. private function execSaveStation($data){
  196. $conn=getOracleConnect();
  197. $sql='select DEVICE_CODE from "DSSC2"."ADM_DEV" where DEVICE_CODE in (select max(DEVICE_CODE) from "DSSC2"."ADM_DEV")';
  198. $stid = oci_parse($conn, $sql);
  199. $r = oci_execute($stid);
  200. $result = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS);
  201. $deivce_code=$result['DEVICE_CODE']+1;
  202. $time=date('Y-m-d H:i:s');
  203. //插入基站信息表
  204. $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")
  205. 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\'))';
  206. $stid = oci_parse($conn, $sql);
  207. $r = oci_execute($stid);
  208. //插入基站信息表
  209. $sql='INSERT INTO "DSSC2"."ADM_DEV_RFID_CHN"("ID", "DEVICE_CODE","CHANNEL_NAME", "STAT" ,"GPS_X","GPS_Y","TYPE")
  210. VALUES(DSSC2.SEQ_ADM_DEV_RFID_CHN.nextval,\''.$deivce_code.'\',\''.$data['DEVICE_NAME'].'\',1,\''.$data['longitude'].'\',\''.$data['latitude'].'\',1)';
  211. $stid = oci_parse($conn, $sql);
  212. $r = oci_execute($stid);
  213. oci_free_statement($stid);
  214. return $r;
  215. }
  216. /**
  217. * 更新
  218. * @time 2022年01月20日 10:55
  219. * @param Request $request
  220. * @param $id
  221. */
  222. public function update(Request $request, $id)
  223. {
  224. $params=$request->param();
  225. $params['LOGIN_NAME']=strtoupper($params['LOGIN_NAME']);
  226. //UPDATE DSSC2.ADM_DEV A SET A.DEVICE_NAME = '1014测试插入4' WHERE A.DEVICE_CODE = '79933';
  227. //UPDATE DSSC2.ADM_DEV_RFID_CHN A SET A.CHANNEL_NAME = '1014测试插入4' WHERE A.DEVICE_CODE ='79933';
  228. $conn=getOracleConnect();
  229. //更新
  230. $sql='UPDATE DSSC2.ADM_DEV SET DEVICE_NAME = \''.$params['DEVICE_NAME'].'\',LOGIN_NAME=\''.$params['LOGIN_NAME'].'\' WHERE DEVICE_CODE = \''.$id.'\' ';
  231. $stid = oci_parse($conn, $sql);
  232. $r = oci_execute($stid);
  233. $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.'\' ';
  234. $stid2 = oci_parse($conn, $sql);
  235. $r2 = oci_execute($stid2);
  236. return CatchResponse::success('修改成功');
  237. }
  238. /**
  239. * 删除
  240. * @time 2022年01月20日 10:55
  241. * @param $id
  242. */
  243. public function delete($id) : \think\Response
  244. {
  245. // $ids=Utils::stringToArrayBy($id);
  246. $conn=getOracleConnect();
  247. $sql='DELETE FROM DSSC2.ADM_DEV WHERE DEVICE_CODE in ('.$id.')';
  248. $stid = oci_parse($conn, $sql);
  249. $r = oci_execute($stid);
  250. $sql='DELETE FROM DSSC2.ADM_DEV_RFID_CHN WHERE DEVICE_CODE in ('.$id.')';
  251. $stid = oci_parse($conn, $sql);
  252. $r = oci_execute($stid);
  253. return CatchResponse::success($r);
  254. }
  255. public function getdeviceListByStation(Request $request){
  256. $params=$request->param();
  257. if(!$params['station_code']){
  258. return CatchResponse::success('');
  259. }
  260. $cond=[];
  261. if($params['station_code']){
  262. $cond['RF_ID']=['=',$params['station_code']];
  263. }
  264. $start_time = date('Y-m-d 00:00:00',time());
  265. $end_time = date('Y-m-d 23:59:59',time());
  266. if(isset($params['timeRange']) && $params['timeRange'] != ''){
  267. $start_time=date('Y-m-d H:i:s',strtotime($params['timeRange'][0]));
  268. $end_time=date('Y-m-d H:i:s',strtotime($params['timeRange'][1]));
  269. $cond['RF_DATE']=['timeRange',$start_time,$end_time];
  270. }
  271. $count=queryOracleCount('DSSC2.W_DW_RF_RECORD',$cond);
  272. $cond['page']=isset($param['page'])?$param['page']:1;
  273. $cond['limit']=isset($param['limit'])?$param['limit']:10;
  274. $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');
  275. foreach($rows as &$val){
  276. //状态: 0- 未知,1 - 进入,2 - 离开
  277. if($val['RF_STAT']==1){
  278. $val['RF_STAT_TEXT']='进入';
  279. }elseif($val['RF_STAT']==2){
  280. $val['RF_STAT_TEXT']='离开';
  281. }else{
  282. $val['RF_STAT_TEXT']='未知';
  283. }
  284. }
  285. $response=[
  286. 'code'=>10000,
  287. 'message'=>'查询成功',
  288. 'count'=>$count,
  289. 'data'=>$rows,
  290. 'current'=>isset($params['page'])?(int)$params['page']:1,
  291. 'limit'=>isset($params['limit'])?(int)$params['limit']:10,
  292. ];
  293. return $response;
  294. // $cond=[];
  295. // $params=$request->param();
  296. // if(!$params['station_code']){
  297. // return CatchResponse::success('');
  298. // }
  299. // if($params['station_code']){
  300. // $cond[]=['RF_ID','=',$params['station_code']];
  301. // }
  302. // $start_time = date('Y-m-d 00:00:00',time());
  303. // $end_time = date('Y-m-d 23:59:59',time());
  304. // if(isset($params['timeRange']) && $params['timeRange'] != ''){
  305. // $start_time=date('Y-m-d H:i:s',strtotime($params['timeRange'][0]));
  306. // $end_time=date('Y-m-d H:i:s',strtotime($params['timeRange'][1]));
  307. // $cond[]=['RF_DATE','between',[$start_time,$end_time]];
  308. // }
  309. // $list=$this->rfRecordModel->getList($cond);
  310. // return CatchResponse::paginate($list);
  311. }
  312. /**
  313. * 导入设备
  314. *
  315. * @time 2022年02月15日
  316. * @param Excel $excel
  317. * @param DeviceExport $deviceExport
  318. * @throws \PhpOffice\PhpSpreadsheet\Exception
  319. * @return \think\response\Json
  320. */
  321. public function importStation(Request $request)
  322. {
  323. $url = $request->post('url');
  324. if (!$url) {
  325. return CatchResponse::fail('请上传文件');
  326. }
  327. $creator_id = $request->post('creator_id');
  328. //解析地址
  329. $parse_url = parse_url($url)['path'];
  330. //载入excel表格
  331. $objPHPExcel = IOFactory::load(public_path() . $parse_url);
  332. //获取表名,一维数组,值是表名。如:array('sheet1', 'sheet2', 'sheet3')
  333. // $nameArr = $objPHPExcel->getSheetNames();
  334. //获取表的数量
  335. $sheetCount = $objPHPExcel->getSheetCount();
  336. $fail = 0; //失败条数
  337. $success = 0; //成功条数
  338. $total = 0; //总共设备数
  339. $data = []; //设备数据
  340. //循环读取每一张表
  341. for ($index = 0; $index < $sheetCount; $index++) {
  342. //设置当前要读取的表
  343. $sheet = $objPHPExcel->getSheet($index); //excel中的第一张sheet
  344. // var_dump($sheet);exit;
  345. $highestRow = $sheet->getHighestRow(); // 取得总行数
  346. // var_dump($highestRow);
  347. if ($highestRow <= 1) {
  348. continue;
  349. }
  350. $total += $highestRow - 1;
  351. for ($j = 2; $j <= $highestRow; $j++) {
  352. $arr = array(); //每条设备信息
  353. $arr['LOGIN_NAME'] = strtoupper(trim($sheet->getCell("A" . $j)->getFormattedValue()));
  354. $arr['longitude'] = trim($sheet->getCell("B" . $j)->getFormattedValue());
  355. $arr['latitude'] = trim($sheet->getCell("C" . $j)->getFormattedValue());
  356. $arr['DEVICE_NAME'] = trim($sheet->getCell("D" . $j)->getFormattedValue());
  357. $r=$this->execSaveStation($arr);
  358. if($r){
  359. $success++;
  360. }else{
  361. $fail++;
  362. }
  363. // var_dump($arr);
  364. // array_push($data,$arr);
  365. }
  366. }
  367. // array_unique($data, SORT_REGULAR);
  368. // // var_dump($data);return CatchResponse::success();
  369. // $count = $this->deviceModel->limit(100)->insertAll($data);
  370. // if ($success = $count) {
  371. return CatchResponse::success('共' . $total . '条数据,成功' . $success . '条,失败' . $fail . '条');
  372. // }
  373. // return CatchResponse::success(['error' => true, 'msg' => '导入失败']);
  374. }
  375. }