Vehicle.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. namespace catchAdmin\yunying\controller;
  3. use catcher\base\CatchRequest as Request;
  4. use catcher\CatchResponse;
  5. use catcher\base\CatchController;
  6. // use catchAdmin\yunying\model\Vehicle as vehicleModel;
  7. use catchAdmin\yunying\excel\VehiclesExport;
  8. use catchAdmin\system\model\SysDictData;
  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. class Vehicle extends CatchController
  15. {
  16. // protected $vehicleModel;
  17. // public function __construct(VehicleModel $vehicleModel)
  18. // {
  19. // $this->vehicleModel = $vehicleModel;
  20. // }
  21. /**
  22. * 列表
  23. * @time 2022年01月20日 10:42
  24. * @param Request $request
  25. */
  26. public function index(Request $request)
  27. {
  28. $param=$request->param();
  29. //联表条件o.rfid_id = s.id and o.owner_id = r.id
  30. $cond=[
  31. '_string'=>'o.rfid_id = s.id',
  32. ];
  33. if($param['PLATE_NO']){
  34. $cond['o.PLATE_NO']=['like',$param['PLATE_NO']];
  35. }
  36. if($param['ID_CARD_NUMBER']){
  37. $cond['r.ID_CARD_NUMBER']=['like',$param['ID_CARD_NUMBER']];
  38. }
  39. if($param['MOBILE_NUMBER']){
  40. $cond['r.MOBILE_NUMBER']=['like',$param['MOBILE_NUMBER']];
  41. }
  42. if($param['RFID_SN']){
  43. $cond['s.RFID_SN']=['like',$param['RFID_SN']];
  44. }
  45. $count=queryOracleCount('DSSC3.W_DW_NON_MOTOR o,DSSC3.W_DW_NON_MOTOR_OWNER r,DSSC3.W_DW_RFID_TAGS s',$cond);
  46. // $count=queryOracleCount('DSSC3.W_DW_NON_MOTOR o,DSSC3.W_DW_RFID_TAGS s',$cond);
  47. // $count=100000;
  48. $cond['page']=isset($param['page'])?$param['page']:1;
  49. $cond['limit']=isset($param['limit'])?$param['limit']:10;
  50. $rows=queryOracleSelect('DSSC3.W_DW_NON_MOTOR o,DSSC3.W_DW_NON_MOTOR_OWNER r,DSSC3.W_DW_RFID_TAGS s',$cond,'o.PLATE_NO,o.CAR_TYPE,o.CAR_BRAND,o.CAR_VIN,s.RFID_SN,s.INSTALLER,to_char(s.INSTA_DATE,\'yyyy-mm-dd hh24:mi:ss\') INSTA_DATE,r.NAME,r.ID_CARD_NUMBER,r.MOBILE_NUMBER,r.HOME_ADDRESS');
  51. // $rows=queryOracleSelect('DSSC3.W_DW_NON_MOTOR o,DSSC3.W_DW_RFID_TAGS s',$cond,'o.PLATE_NO,o.CAR_TYPE,o.CAR_BRAND,s.ID,s.RFID_SN,s.INSTALLER,to_char(s.INSTA_DATE,\'yyyy-mm-dd hh24:mi:ss\') INSTA_DATE');
  52. foreach($rows as &$val){
  53. $val['CAR_BRAND_TEXT']=(new SysDictData())->getValueByCode('CAR_BRAND_OPTION',$val['CAR_BRAND']);
  54. $val['CAR_TYPE_TEXT']=(new SysDictData())->getValueByCode('CAR_TYPE_OPTION',$val['CAR_TYPE']);
  55. $val['CARD_NUMBER_STR']=formatDataCardNumber($val['ID_CARD_NUMBER']);
  56. }
  57. $response=[
  58. 'code'=>10000,
  59. 'message'=>'查询成功',
  60. 'count'=>$count,
  61. 'data'=>$rows,
  62. 'current'=>isset($param['page'])?(int)$param['page']:1,
  63. 'limit'=>isset($param['limit'])?(int)$param['limit']:10,
  64. ];
  65. return $response;
  66. }
  67. public function getMapList(Request $request)
  68. {
  69. $param=$request->param();
  70. //联表条件o.rfid_id = s.id and o.owner_id = r.id
  71. $cond=[
  72. '_string'=>'o.rfid_id = s.id',
  73. ];
  74. $keywords=$param['keywords'];
  75. if($keywords){
  76. $cond['_string']= '(s.RFID_SN like \'%'.$keywords.'%\' OR o.PLATE_NO like \'%'.$keywords.'%\') AND o.rfid_id = s.id';
  77. }
  78. $count=queryOracleCount('DSSC3.W_DW_NON_MOTOR o,DSSC3.W_DW_RFID_TAGS s',$cond);
  79. $cond['page']=isset($param['page'])?$param['page']:1;
  80. $cond['limit']=isset($param['limit'])?$param['limit']:10;
  81. $rows=queryOracleSelect('DSSC3.W_DW_NON_MOTOR o,DSSC3.W_DW_RFID_TAGS s',$cond,'o.PLATE_NO,o.CAR_TYPE,o.CAR_BRAND,s.ID,s.RFID_SN,s.INSTALLER,to_char(s.INSTA_DATE,\'yyyy-mm-dd hh24:mi:ss\') INSTA_DATE');
  82. $conn=getOracleConnect();
  83. foreach($rows as &$val){
  84. $val['CAR_BRAND_TEXT']=(new SysDictData())->getValueByCode('CAR_BRAND_OPTION',$val['CAR_BRAND']);
  85. $val['CAR_TYPE_TEXT']=(new SysDictData())->getValueByCode('CAR_TYPE_OPTION',$val['CAR_TYPE']);
  86. $route_sql='select * from(SELECT RF_ID,to_char(RF_DATE,\'yyyy-mm-dd hh24:mi:ss\') RF_DATE FROM DSSC2.W_DW_RF_RECORD WHERE RF_FLAGID =\''.$val['RFID_SN'].'\' ORDER BY RF_DATE DESC )where rownum < 2';
  87. $stid = oci_parse($conn, $route_sql);
  88. oci_define_by_name($stid, 'RF_ID', $val['mac']);
  89. oci_define_by_name($stid, 'RF_DATE', $val['online_time']);
  90. oci_execute($stid);
  91. oci_fetch($stid);
  92. oci_free_statement($stid);
  93. $staion_info =Db::table('stations')->where('mac',$val['mac'])->find();
  94. if($staion_info){
  95. $wgsLoc = \algorithm\Geometry::gcj02ToWgs84((float)$staion_info['latitude'],(float)$staion_info['longitude']);
  96. $val['longitude']=$wgsLoc['lng'];
  97. $val['latitude']=$wgsLoc['lat'];
  98. $val['address']=$staion_info['name'];
  99. }else{
  100. $val['longitude']='';
  101. $val['latitude']='';
  102. $val['address']='';
  103. }
  104. }
  105. $response=[
  106. 'code'=>10000,
  107. 'message'=>'查询成功',
  108. 'count'=>$count,
  109. 'data'=>$rows,
  110. 'current'=>isset($param['page'])?(int)$param['page']:1,
  111. 'limit'=>isset($param['limit'])?(int)$param['limit']:10,
  112. ];
  113. return $response;
  114. }
  115. public function getRfidTagsList(Request $request) {
  116. $param=$request->param();
  117. //联表条件o.rfid_id = s.id and o.owner_id = r.id
  118. $cond=[];
  119. if($param['RFID_SN']){
  120. $cond['s.RFID_SN']=['like',$param['RFID_SN']];
  121. }
  122. $count=queryOracleCount('DSSC3.W_DW_RFID_TAGS s',$cond);
  123. $cond['page']=isset($param['page'])?$param['page']:1;
  124. $cond['limit']=isset($param['limit'])?$param['limit']:10;
  125. // $rows=queryOracleSelect('DSSC3.W_DW_NON_MOTOR o,DSSC3.W_DW_NON_MOTOR_OWNER r,DSSC3.W_DW_RFID_TAGS s',$cond,'o.PLATE_NO,s.RFID_SN,r. NAME,r.ID_CARD_NUMBER,r.MOBILE_NUMBER,r.HOME_ADDRESS');
  126. $rows=queryOracleSelect('DSSC3.W_DW_RFID_TAGS s',$cond,'s.ID,s.RFID_SN,s.RFID_TYPE');
  127. foreach($rows as &$val){
  128. // $val['CAR_BRAND_TEXT']=(new SysDictData())->getValueByCode('CAR_BRAND_OPTION',$val['CAR_BRAND']);
  129. $val['RFID_TYPE_TEXT']=(new SysDictData())->getValueByCode('RFID_TYPE_OPTION',$val['RFID_TYPE']);
  130. if( isset($param['bw_id']) && $param['bw_id'] ){
  131. $val['isAdd']=Db::table('rfid_with_bw')->where('bw_id',$param['bw_id'])->where('rfid',$val['RFID_SN'])->count();
  132. }
  133. }
  134. $response=[
  135. 'code'=>10000,
  136. 'message'=>'查询成功',
  137. 'count'=>$count,
  138. 'data'=>$rows,
  139. 'current'=>isset($param['page'])?(int)$param['page']:1,
  140. 'limit'=>isset($param['limit'])?(int)$param['limit']:10,
  141. ];
  142. return $response;
  143. }
  144. // public function indexAdd(Request $request)
  145. // {
  146. // ini_set('memory_limit','3072M');
  147. // set_time_limit(0);
  148. // $cond=[
  149. // '_string'=>'o.rfid_id = s.id',
  150. // ];
  151. // // $count=queryOracleCount('DSSC3.W_DW_NON_MOTOR o,DSSC3.W_DW_NON_MOTOR_OWNER r,DSSC3.W_DW_RFID_TAGS s',$cond);
  152. // $count=queryOracleCount('DSSC3.W_DW_NON_MOTOR o,DSSC3.W_DW_RFID_TAGS s',$cond);
  153. // $to=ceil($count/10000);
  154. // var_dump($to);
  155. // for($i=1;$i<=$to;$i++){
  156. // var_dump($i);
  157. // $cond['page']=$i;
  158. // $cond['limit']=10000;
  159. // // $rows=queryOracleSelect('DSSC3.W_DW_NON_MOTOR o,DSSC3.W_DW_NON_MOTOR_OWNER r,DSSC3.W_DW_RFID_TAGS s',$cond,'o.PLATE_NO,s.RFID_SN,r. NAME,r.ID_CARD_NUMBER,r.MOBILE_NUMBER,r.HOME_ADDRESS');
  160. // $rows=queryOracleSelect('DSSC3.W_DW_NON_MOTOR o,DSSC3.W_DW_RFID_TAGS s',$cond,'o.PLATE_NO,o.CAR_TYPE,o.CAR_BRAND,s.ID,s.RFID_SN,s.INSTALLER,to_char(s.INSTA_DATE,\'yyyy-mm-dd hh24:mi:ss\') INSTA_DATE');
  161. // // var_dump(count($rows));
  162. // $data=[];
  163. // foreach($rows as &$val){
  164. // $item=array(
  165. // 'license_plate'=>$val['PLATE_NO'],
  166. // 'car_type'=>$val['CAR_TYPE'],
  167. // 'brand_id'=>(int)$val['CAR_BRAND'],
  168. // 'rfid_sn'=>$val['RFID_SN'],
  169. // 'installer'=>$val['INSTALLER'],
  170. // 'license_time'=>$val['INSTA_DATE'],
  171. // );
  172. // array_push($data,$item);
  173. // }
  174. // $success = Db::table('vehicles')->limit(1000)->insertAll($data);
  175. // var_dump($success);
  176. // }
  177. // }
  178. /**
  179. * 保存信息
  180. * @time 2022年01月20日 10:42
  181. * @param Request $request
  182. */
  183. public function save(Request $request)
  184. {
  185. $installer=$request->user()->realname;
  186. $param=$request->param();
  187. $param['RFID_SN']=strtoupper($param['RFID_SN']);
  188. $param['PLATE_NO']=strtoupper($param['PLATE_NO']);
  189. $param['INSTA_DATE']=date('Y-m-d H:i:s',time());
  190. $param['INSTALLER']=$installer;
  191. $r=$this->execSaveVehicle($param);
  192. $param['DATA_TYPE']='vehicle_save';
  193. pushDataToRedisList($param);
  194. return CatchResponse::success($r);
  195. }
  196. private function execSaveVehicle($data){
  197. $conn=getOracleConnect();
  198. $sql='declare
  199. tagId number;
  200. begin
  201. INSERT INTO DSSC3.W_DW_RFID_TAGS("ID", "RFID_SN","INSTALLER","INSTA_DATE") VALUES (DSSC3.SEQ_W_DW_RFID_TAGS.nextval, \''.$data['RFID_SN'].'\',\''.$data['INSTALLER'].'\', TO_DATE(\''.$data['INSTA_DATE'].'\', \'SYYYY-MM-DD HH24:MI:SS\')) returning ID into tagId;
  202. INSERT INTO DSSC3.W_DW_NON_MOTOR("ID", "RFID_ID","PLATE_NO","CAR_BRAND","CAR_TYPE") VALUES (DSSC3.SEQ_W_DW_RFID_TAGS.nextval, tagId,\''.$data['PLATE_NO'].'\',\''.$data['CAR_BRAND'].'\',\''.$data['CAR_TYPE'].'\');
  203. end;';
  204. $stid = oci_parse($conn, $sql);
  205. $r = oci_execute($stid);
  206. oci_free_statement($stid);
  207. return $r;
  208. }
  209. /**
  210. * 更新
  211. * @time 2022年01月20日 10:42
  212. * @param Request $request
  213. * @param $id
  214. */
  215. public function update(Request $request, $id) : \think\Response
  216. {
  217. $params=$request->post();
  218. $conn=getOracleConnect();
  219. //查老的车牌
  220. $vehicle_sql='SELECT o.PLATE_NO FROM DSSC3.W_DW_NON_MOTOR o WHERE o.RFID_ID =\''.$id.'\' ';
  221. $OLD_NO='';
  222. $stid = oci_parse($conn, $vehicle_sql);
  223. oci_define_by_name($stid, 'PLATE_NO', $OLD_NO);
  224. oci_execute($stid);
  225. oci_fetch($stid);
  226. $params['OLD_NO']=$OLD_NO;
  227. $params['RFID_SN']=strtoupper($params['RFID_SN']);
  228. $params['PLATE_NO']=strtoupper($params['PLATE_NO']);
  229. // var_dump($params);
  230. // //更新
  231. $sql='UPDATE DSSC3.W_DW_RFID_TAGS SET RFID_SN = \''.$params['RFID_SN'].'\' WHERE ID = \''.$id.'\' ';
  232. $stid = oci_parse($conn, $sql);
  233. $r = oci_execute($stid);
  234. $sql='UPDATE DSSC3.W_DW_NON_MOTOR SET PLATE_NO = \''.$params['PLATE_NO'].'\',CAR_BRAND=\''.$params['CAR_BRAND'].'\',CAR_TYPE=\''.$params['CAR_TYPE'].'\' WHERE RFID_ID = \''.$id.'\' ';
  235. // var_dump($sql);
  236. $stid2 = oci_parse($conn, $sql);
  237. $r2 = oci_execute($stid2);
  238. $params['DATA_TYPE']='vehicle_update';
  239. pushDataToRedisList($params);
  240. return CatchResponse::success('修改成功');
  241. }
  242. /**
  243. * 删除
  244. * @time 2022年01月20日 10:42
  245. * @param $id
  246. */
  247. public function delete($id) : \think\Response
  248. {
  249. $conn=getOracleConnect();
  250. $sql='SELECT PLATE_NO FROM DSSC3.W_DW_NON_MOTOR WHERE RFID_ID in ('.$id.')';
  251. $stid = oci_parse($conn, $sql);
  252. $r = oci_execute($stid);
  253. $rows=[];
  254. while($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
  255. $rows[]=$row['PLATE_NO'];
  256. }
  257. $delete_data['PLATE_NO']=$rows;
  258. $delete_data['DATA_TYPE']='vehicle_delete';
  259. $sql='DELETE FROM DSSC3.W_DW_RFID_TAGS WHERE ID in ('.$id.')';
  260. $stid = oci_parse($conn, $sql);
  261. $r = oci_execute($stid);
  262. $sql='DELETE FROM DSSC3.W_DW_NON_MOTOR WHERE RFID_ID in ('.$id.')';
  263. $stid = oci_parse($conn, $sql);
  264. $r = oci_execute($stid);
  265. pushDataToRedisList($delete_data);
  266. return CatchResponse::success($r);
  267. }
  268. /**
  269. * 导出
  270. *
  271. * @time 2022年01月22日
  272. * @param Excel $excel
  273. * @param VehicleExport $vehicleExport
  274. * @throws \PhpOffice\PhpSpreadsheet\Exception
  275. * @return \think\response\Json
  276. */
  277. public function export_vehicle(Excel $excel, VehiclesExport $VehicleExport)
  278. {
  279. return CatchResponse::success($excel->save($VehicleExport, Utils::publicPath('export/vehicles'), 'local', '车辆列表'));
  280. }
  281. /**
  282. * 导入车辆
  283. *
  284. * @time 2022年02月15日
  285. * @param Excel $excel
  286. * @param DeviceExport $deviceExport
  287. * @throws \PhpOffice\PhpSpreadsheet\Exception
  288. * @return \think\response\Json
  289. */
  290. public function importVehicle(Request $request)
  291. {
  292. $url = $request->post('url');
  293. if (!$url) {
  294. return CatchResponse::fail('请上传文件');
  295. }
  296. $creator_id = $request->post('creator_id');
  297. //解析地址
  298. $parse_url = parse_url($url)['path'];
  299. //载入excel表格
  300. $objPHPExcel = IOFactory::load(public_path() . $parse_url);
  301. //获取表名,一维数组,值是表名。如:array('sheet1', 'sheet2', 'sheet3')
  302. // $nameArr = $objPHPExcel->getSheetNames();
  303. //获取表的数量
  304. $sheetCount = $objPHPExcel->getSheetCount();
  305. $fail = 0; //失败条数
  306. $success = 0; //成功条数
  307. $total = 0; //总共设备数
  308. $data = []; //设备数据
  309. //循环读取每一张表
  310. $conn=getOracleConnect();
  311. for ($index = 0; $index < $sheetCount; $index++) {
  312. //设置当前要读取的表
  313. $sheet = $objPHPExcel->getSheet($index); //excel中的第一张sheet
  314. // var_dump($sheet);exit;
  315. $highestRow = $sheet->getHighestRow(); // 取得总行数
  316. // var_dump($highestRow);
  317. if ($highestRow <= 1) {
  318. continue;
  319. }
  320. $brand_type_id = Db::table("sys_dict_type")->where('code', 'CAR_BRAND_OPTION')->value('id');
  321. $car_type_id = Db::table("sys_dict_type")->where('code', 'CAR_TYPE_OPTION')->value('id');
  322. $total += $highestRow - 1;
  323. // $sql='declare
  324. // tagId number;
  325. // begin
  326. // INSERT INTO DSSC3.W_DW_RFID_TAGS("ID", "RFID_SN","INSTALLER","INSTA_DATE") VALUES (DSSC3.SEQ_W_DW_RFID_TAGS.nextval, \''.$arr['RFID_SN'].'\',\''.$arr['INSTALLER'].'\', TO_DATE(\''.$arr['INSTA_DATE'].'\', \'SYYYY-MM-DD HH24:MI:SS\')) returning ID into tagId;
  327. // INSERT INTO DSSC3.W_DW_NON_MOTOR("ID", "RFID_ID","PLATE_NO","CAR_BRAND","CAR_TYPE") VALUES (DSSC3.SEQ_W_DW_RFID_TAGS.nextval, tagId,\''.$arr['PLATE_NO'].'\',\''.$arr['CAR_BRAND'].'\',\''.$arr['CAR_TYPE'].'\');
  328. // end;';
  329. // 预编译SQL语句
  330. $sql='declare
  331. tagId number;
  332. begin
  333. INSERT INTO DSSC3.W_DW_RFID_TAGS("ID", "RFID_SN","INSTALLER","INSTA_DATE") VALUES (DSSC3.SEQ_W_DW_RFID_TAGS.nextval, :col1,:col2, TO_DATE(:col3, \'SYYYY-MM-DD HH24:MI:SS\')) returning ID into tagId;
  334. INSERT INTO DSSC3.W_DW_NON_MOTOR("ID", "RFID_ID","PLATE_NO","CAR_BRAND","CAR_TYPE") VALUES (DSSC3.SEQ_W_DW_RFID_TAGS.nextval, tagId,:col4,:col5,:col6);
  335. end;';
  336. $stmt = oci_parse($conn, $sql);
  337. for ($j = 2; $j <= $highestRow; $j++) {
  338. $arr = array(); //每条设备信息
  339. $arr['PLATE_NO'] = strtoupper(trim($sheet->getCell("A" . $j)->getFormattedValue()));
  340. if(empty($arr['PLATE_NO']) || strlen($arr['PLATE_NO'])!=13){
  341. $fail++;
  342. debug_log('importVehicleError','车牌格式不正确,车牌号为:'.$arr['PLATE_NO']);
  343. continue;
  344. }
  345. $brand = trim($sheet->getCell("B" . $j)->getFormattedValue());
  346. $brand_id=Db::table("sys_dict_data")->where('type_id', $brand_type_id)->whereLike('value','%'.$brand.'%')->cache(60)->value('code');
  347. $arr['CAR_BRAND']=$brand_id?$brand_id:'99';
  348. $type = trim($sheet->getCell("C" . $j)->getFormattedValue());
  349. $car_type=Db::table("sys_dict_data")->where('type_id', $car_type_id)->whereLike('value','%'.$type.'%')->cache(60)->value('code');
  350. $arr['CAR_TYPE']=$car_type?$car_type:'4';
  351. $arr['RFID_SN'] = trim($sheet->getCell("D" . $j)->getFormattedValue());
  352. if(empty($arr['RFID_SN']) || strlen($arr['RFID_SN'])!=8){
  353. $fail++;
  354. debug_log('importVehicleError','标签格式格式不正确,标签为:'.$arr['RFID_SN']);
  355. continue;
  356. }
  357. $arr['INSTA_DATE'] = trim($sheet->getCell("E" . $j)->getFormattedValue());
  358. $arr['INSTALLER'] = trim($sheet->getCell("F" . $j)->getFormattedValue());
  359. //变量绑定
  360. oci_bind_by_name($stmt, ':col1', $arr['RFID_SN']);
  361. oci_bind_by_name($stmt, ':col2', $arr['INSTALLER']);
  362. oci_bind_by_name($stmt, ':col3', $arr['INSTA_DATE']);
  363. oci_bind_by_name($stmt, ':col4', $arr['PLATE_NO']);
  364. oci_bind_by_name($stmt, ':col5', $arr['CAR_BRAND']);
  365. oci_bind_by_name($stmt, ':col6', $arr['CAR_TYPE']);
  366. // 添加到批处理
  367. oci_execute($stmt, OCI_DEFAULT);
  368. $arr['DATA_TYPE']='vehicle_save';
  369. pushDataToRedisList($arr);
  370. $success++;
  371. if( ($j%1000 == 0) || $j==$highestRow){
  372. // 提交事务
  373. $res=oci_commit($conn);
  374. }
  375. }
  376. // 释放资源
  377. oci_free_statement($stmt);
  378. }
  379. //关闭连接
  380. oci_close($conn);
  381. return CatchResponse::success('共' . $total . '条数据,成功' . $success . '条,失败' . $fail . '条');
  382. }
  383. }