Station.php 16 KB

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