Vehicle.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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,s.RFID_SN,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. }
  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. }
  66. public function getMapList(Request $request)
  67. {
  68. $param=$request->param();
  69. //联表条件o.rfid_id = s.id and o.owner_id = r.id
  70. $cond=[
  71. '_string'=>'o.rfid_id = s.id',
  72. ];
  73. $keywords=$param['keywords'];
  74. if($keywords){
  75. $cond['_string']= '(s.RFID_SN like \'%'.$keywords.'%\' OR o.PLATE_NO like \'%'.$keywords.'%\') AND o.rfid_id = s.id';
  76. }
  77. $count=queryOracleCount('DSSC3.W_DW_NON_MOTOR o,DSSC3.W_DW_RFID_TAGS s',$cond);
  78. $cond['page']=isset($param['page'])?$param['page']:1;
  79. $cond['limit']=isset($param['limit'])?$param['limit']:10;
  80. $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');
  81. $conn=getOracleConnect();
  82. foreach($rows as &$val){
  83. $val['CAR_BRAND_TEXT']=(new SysDictData())->getValueByCode('CAR_BRAND_OPTION',$val['CAR_BRAND']);
  84. $val['CAR_TYPE_TEXT']=(new SysDictData())->getValueByCode('CAR_TYPE_OPTION',$val['CAR_TYPE']);
  85. $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';
  86. $stid = oci_parse($conn, $route_sql);
  87. oci_define_by_name($stid, 'RF_ID', $val['mac']);
  88. oci_define_by_name($stid, 'RF_DATE', $val['online_time']);
  89. oci_execute($stid);
  90. oci_fetch($stid);
  91. oci_free_statement($stid);
  92. $staion_info =Db::table('stations')->where('mac',$val['mac'])->find();
  93. if($staion_info){
  94. $wgsLoc = \algorithm\Geometry::gcj02ToWgs84((float)$staion_info['latitude'],(float)$staion_info['longitude']);
  95. $val['longitude']=$wgsLoc['lng'];
  96. $val['latitude']=$wgsLoc['lat'];
  97. $val['address']=$staion_info['name'];
  98. }else{
  99. $val['longitude']='';
  100. $val['latitude']='';
  101. $val['address']='';
  102. }
  103. }
  104. $response=[
  105. 'code'=>10000,
  106. 'message'=>'查询成功',
  107. 'count'=>$count,
  108. 'data'=>$rows,
  109. 'current'=>isset($param['page'])?(int)$param['page']:1,
  110. 'limit'=>isset($param['limit'])?(int)$param['limit']:10,
  111. ];
  112. return $response;
  113. }
  114. public function getRfidTagsList(Request $request) {
  115. $param=$request->param();
  116. //联表条件o.rfid_id = s.id and o.owner_id = r.id
  117. $cond=[];
  118. if($param['RFID_SN']){
  119. $cond['s.RFID_SN']=['like',$param['RFID_SN']];
  120. }
  121. $count=queryOracleCount('DSSC3.W_DW_RFID_TAGS s',$cond);
  122. $cond['page']=isset($param['page'])?$param['page']:1;
  123. $cond['limit']=isset($param['limit'])?$param['limit']:10;
  124. // $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');
  125. $rows=queryOracleSelect('DSSC3.W_DW_RFID_TAGS s',$cond,'s.ID,s.RFID_SN,s.RFID_TYPE');
  126. foreach($rows as &$val){
  127. // $val['CAR_BRAND_TEXT']=(new SysDictData())->getValueByCode('CAR_BRAND_OPTION',$val['CAR_BRAND']);
  128. $val['RFID_TYPE_TEXT']=(new SysDictData())->getValueByCode('RFID_TYPE_OPTION',$val['RFID_TYPE']);
  129. if( isset($param['bw_id']) && $param['bw_id'] ){
  130. $val['isAdd']=Db::table('rfid_with_bw')->where('bw_id',$param['bw_id'])->where('rfid',$val['RFID_SN'])->count();
  131. }
  132. }
  133. $response=[
  134. 'code'=>10000,
  135. 'message'=>'查询成功',
  136. 'count'=>$count,
  137. 'data'=>$rows,
  138. 'current'=>isset($param['page'])?(int)$param['page']:1,
  139. 'limit'=>isset($param['limit'])?(int)$param['limit']:10,
  140. ];
  141. return $response;
  142. }
  143. // public function indexAdd(Request $request)
  144. // {
  145. // ini_set('memory_limit','3072M');
  146. // set_time_limit(0);
  147. // $cond=[
  148. // '_string'=>'o.rfid_id = s.id',
  149. // ];
  150. // // $count=queryOracleCount('DSSC3.W_DW_NON_MOTOR o,DSSC3.W_DW_NON_MOTOR_OWNER r,DSSC3.W_DW_RFID_TAGS s',$cond);
  151. // $count=queryOracleCount('DSSC3.W_DW_NON_MOTOR o,DSSC3.W_DW_RFID_TAGS s',$cond);
  152. // $to=ceil($count/10000);
  153. // var_dump($to);
  154. // for($i=1;$i<=$to;$i++){
  155. // var_dump($i);
  156. // $cond['page']=$i;
  157. // $cond['limit']=10000;
  158. // // $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');
  159. // $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');
  160. // // var_dump(count($rows));
  161. // $data=[];
  162. // foreach($rows as &$val){
  163. // $item=array(
  164. // 'license_plate'=>$val['PLATE_NO'],
  165. // 'car_type'=>$val['CAR_TYPE'],
  166. // 'brand_id'=>(int)$val['CAR_BRAND'],
  167. // 'rfid_sn'=>$val['RFID_SN'],
  168. // 'installer'=>$val['INSTALLER'],
  169. // 'license_time'=>$val['INSTA_DATE'],
  170. // );
  171. // array_push($data,$item);
  172. // }
  173. // $success = Db::table('vehicles')->limit(1000)->insertAll($data);
  174. // var_dump($success);
  175. // }
  176. // }
  177. /**
  178. * 保存信息
  179. * @time 2022年01月20日 10:42
  180. * @param Request $request
  181. */
  182. public function save(Request $request)
  183. {
  184. $installer=$request->user()->realname;
  185. $param=$request->param();
  186. $param['RFID_SN']=strtoupper($param['RFID_SN']);
  187. $param['PLATE_NO']=strtoupper($param['PLATE_NO']);
  188. $param['INSTA_DATE']=date('Y-m-d H:i:s',time());
  189. $param['INSTALLER']=$installer;
  190. $r=$this->execSaveVehicle($param);
  191. $param['DATA_TYPE']='vehicle_save';
  192. pushDataToRedisList($param);
  193. return CatchResponse::success($r);
  194. }
  195. private function execSaveVehicle($data){
  196. $conn=getOracleConnect();
  197. $sql='declare
  198. tagId number;
  199. begin
  200. 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;
  201. 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'].'\');
  202. end;';
  203. $stid = oci_parse($conn, $sql);
  204. $r = oci_execute($stid);
  205. oci_free_statement($stid);
  206. return $r;
  207. }
  208. /**
  209. * 更新
  210. * @time 2022年01月20日 10:42
  211. * @param Request $request
  212. * @param $id
  213. */
  214. public function update(Request $request, $id) : \think\Response
  215. {
  216. $params=$request->post();
  217. $conn=getOracleConnect();
  218. //查老的车牌
  219. $vehicle_sql='SELECT o.PLATE_NO FROM DSSC3.W_DW_NON_MOTOR o WHERE o.RFID_ID =\''.$id.'\' ';
  220. $OLD_NO='';
  221. $stid = oci_parse($conn, $vehicle_sql);
  222. oci_define_by_name($stid, 'PLATE_NO', $OLD_NO);
  223. oci_execute($stid);
  224. oci_fetch($stid);
  225. $params['OLD_NO']=$OLD_NO;
  226. $params['RFID_SN']=strtoupper($params['RFID_SN']);
  227. $params['PLATE_NO']=strtoupper($params['PLATE_NO']);
  228. // var_dump($params);
  229. // //更新
  230. $sql='UPDATE DSSC3.W_DW_RFID_TAGS SET RFID_SN = \''.$params['RFID_SN'].'\' WHERE ID = \''.$id.'\' ';
  231. $stid = oci_parse($conn, $sql);
  232. $r = oci_execute($stid);
  233. $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.'\' ';
  234. // var_dump($sql);
  235. $stid2 = oci_parse($conn, $sql);
  236. $r2 = oci_execute($stid2);
  237. $params['DATA_TYPE']='vehicle_update';
  238. pushDataToRedisList($params);
  239. return CatchResponse::success('修改成功');
  240. }
  241. /**
  242. * 删除
  243. * @time 2022年01月20日 10:42
  244. * @param $id
  245. */
  246. public function delete($id) : \think\Response
  247. {
  248. $conn=getOracleConnect();
  249. $sql='SELECT PLATE_NO FROM DSSC3.W_DW_NON_MOTOR WHERE RFID_ID in ('.$id.')';
  250. $stid = oci_parse($conn, $sql);
  251. $r = oci_execute($stid);
  252. $rows=[];
  253. while($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
  254. $rows[]=$row['PLATE_NO'];
  255. }
  256. $delete_data['PLATE_NO']=$rows;
  257. $delete_data['DATA_TYPE']='vehicle_delete';
  258. $sql='DELETE FROM DSSC3.W_DW_RFID_TAGS WHERE ID in ('.$id.')';
  259. $stid = oci_parse($conn, $sql);
  260. $r = oci_execute($stid);
  261. $sql='DELETE FROM DSSC3.W_DW_NON_MOTOR WHERE RFID_ID in ('.$id.')';
  262. $stid = oci_parse($conn, $sql);
  263. $r = oci_execute($stid);
  264. pushDataToRedisList($delete_data);
  265. return CatchResponse::success($r);
  266. }
  267. /**
  268. * 导出
  269. *
  270. * @time 2022年01月22日
  271. * @param Excel $excel
  272. * @param VehicleExport $vehicleExport
  273. * @throws \PhpOffice\PhpSpreadsheet\Exception
  274. * @return \think\response\Json
  275. */
  276. public function export_vehicle(Excel $excel, VehiclesExport $VehicleExport)
  277. {
  278. return CatchResponse::success($excel->save($VehicleExport, Utils::publicPath('export/vehicles'), 'local', '车辆列表'));
  279. }
  280. /**
  281. * 导入车辆
  282. *
  283. * @time 2022年02月15日
  284. * @param Excel $excel
  285. * @param DeviceExport $deviceExport
  286. * @throws \PhpOffice\PhpSpreadsheet\Exception
  287. * @return \think\response\Json
  288. */
  289. public function importVehicle(Request $request)
  290. {
  291. $url = $request->post('url');
  292. if (!$url) {
  293. return CatchResponse::fail('请上传文件');
  294. }
  295. $creator_id = $request->post('creator_id');
  296. //解析地址
  297. $parse_url = parse_url($url)['path'];
  298. //载入excel表格
  299. $objPHPExcel = IOFactory::load(public_path() . $parse_url);
  300. //获取表名,一维数组,值是表名。如:array('sheet1', 'sheet2', 'sheet3')
  301. // $nameArr = $objPHPExcel->getSheetNames();
  302. //获取表的数量
  303. $sheetCount = $objPHPExcel->getSheetCount();
  304. $fail = 0; //失败条数
  305. $success = 0; //成功条数
  306. $total = 0; //总共设备数
  307. $data = []; //设备数据
  308. //循环读取每一张表
  309. for ($index = 0; $index < $sheetCount; $index++) {
  310. //设置当前要读取的表
  311. $sheet = $objPHPExcel->getSheet($index); //excel中的第一张sheet
  312. // var_dump($sheet);exit;
  313. $highestRow = $sheet->getHighestRow(); // 取得总行数
  314. // var_dump($highestRow);
  315. if ($highestRow <= 1) {
  316. continue;
  317. }
  318. $brand_type_id = Db::table("sys_dict_type")->where('code', 'CAR_BRAND_OPTION')->value('id');
  319. $car_type_id = Db::table("sys_dict_type")->where('code', 'CAR_TYPE_OPTION')->value('id');
  320. // return $this->where('type_id', $type_id)
  321. // ->where('code', $value)
  322. // ->cache(true, 60)
  323. // ->value('remark');
  324. // $r=$this->execSaveVehicle($param);
  325. $total += $highestRow - 1;
  326. for ($j = 2; $j <= $highestRow; $j++) {
  327. $arr = array(); //每条设备信息
  328. $arr['PLATE_NO'] = strtoupper(trim($sheet->getCell("A" . $j)->getFormattedValue()));
  329. $brand = trim($sheet->getCell("B" . $j)->getFormattedValue());
  330. $brand_id=Db::table("sys_dict_data")->where('type_id', $brand_type_id)->whereLike('value','%'.$brand.'%')->cache(60)->value('code');
  331. $arr['CAR_BRAND']=$brand_id?$brand_id:'99';
  332. $type = trim($sheet->getCell("C" . $j)->getFormattedValue());
  333. $car_type=Db::table("sys_dict_data")->where('type_id', $car_type_id)->whereLike('value','%'.$type.'%')->cache(60)->value('code');
  334. $arr['CAR_BRAND']=$car_type?$car_type:'4';
  335. $arr['RFID_SN'] = trim($sheet->getCell("D" . $j)->getFormattedValue());
  336. $arr['INSTA_DATE'] = trim($sheet->getCell("E" . $j)->getFormattedValue());
  337. $arr['INSTALLER'] = trim($sheet->getCell("F" . $j)->getFormattedValue());
  338. // var_dump($arr);
  339. $r=$this->execSaveVehicle($arr);
  340. if($r){
  341. $arr['DATA_TYPE']='vehicle_save';
  342. pushDataToRedisList($arr);
  343. $success++;
  344. }else{
  345. $fail++;
  346. }
  347. // var_dump($arr);
  348. // array_push($data,$arr);
  349. }
  350. }
  351. // array_unique($data, SORT_REGULAR);
  352. // // var_dump($data);return CatchResponse::success();
  353. // $count = $this->deviceModel->limit(100)->insertAll($data);
  354. // if ($success = $count) {
  355. return CatchResponse::success('共' . $total . '条数据,成功' . $success . '条,失败' . $fail . '条');
  356. // }
  357. // return CatchResponse::success(['error' => true, 'msg' => '导入失败']);
  358. }
  359. }