Vehicle.php 15 KB

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