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. // $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. return CatchResponse::success($r);
  192. }
  193. private function execSaveVehicle($data){
  194. $conn=getOracleConnect();
  195. $sql='declare
  196. tagId number;
  197. begin
  198. 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;
  199. 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'].'\');
  200. end;';
  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:42
  209. * @param Request $request
  210. * @param $id
  211. */
  212. public function update(Request $request, $id) : \think\Response
  213. {
  214. $params=$request->post();
  215. $params['RFID_SN']=strtoupper($params['RFID_SN']);
  216. $params['PLATE_NO']=strtoupper($params['PLATE_NO']);
  217. // var_dump($params);
  218. $conn=getOracleConnect();
  219. // //更新
  220. $sql='UPDATE DSSC3.W_DW_RFID_TAGS SET RFID_SN = \''.$params['RFID_SN'].'\' WHERE ID = \''.$id.'\' ';
  221. $stid = oci_parse($conn, $sql);
  222. $r = oci_execute($stid);
  223. $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.'\' ';
  224. // var_dump($sql);
  225. $stid2 = oci_parse($conn, $sql);
  226. $r2 = oci_execute($stid2);
  227. return CatchResponse::success('修改成功');
  228. }
  229. /**
  230. * 删除
  231. * @time 2022年01月20日 10:42
  232. * @param $id
  233. */
  234. public function delete($id) : \think\Response
  235. {
  236. $conn=getOracleConnect();
  237. $sql='DELETE FROM DSSC3.W_DW_RFID_TAGS WHERE ID in ('.$id.')';
  238. $stid = oci_parse($conn, $sql);
  239. $r = oci_execute($stid);
  240. $sql='DELETE FROM DSSC3.W_DW_NON_MOTOR WHERE RFID_ID in ('.$id.')';
  241. $stid = oci_parse($conn, $sql);
  242. $r = oci_execute($stid);
  243. return CatchResponse::success($r);
  244. }
  245. /**
  246. * 导出
  247. *
  248. * @time 2022年01月22日
  249. * @param Excel $excel
  250. * @param VehicleExport $vehicleExport
  251. * @throws \PhpOffice\PhpSpreadsheet\Exception
  252. * @return \think\response\Json
  253. */
  254. public function export_vehicle(Excel $excel, VehiclesExport $VehicleExport)
  255. {
  256. return CatchResponse::success($excel->save($VehicleExport, Utils::publicPath('export/vehicles'), 'local', '车辆列表'));
  257. }
  258. /**
  259. * 导入车辆
  260. *
  261. * @time 2022年02月15日
  262. * @param Excel $excel
  263. * @param DeviceExport $deviceExport
  264. * @throws \PhpOffice\PhpSpreadsheet\Exception
  265. * @return \think\response\Json
  266. */
  267. public function importVehicle(Request $request)
  268. {
  269. $url = $request->post('url');
  270. if (!$url) {
  271. return CatchResponse::fail('请上传文件');
  272. }
  273. $creator_id = $request->post('creator_id');
  274. //解析地址
  275. $parse_url = parse_url($url)['path'];
  276. //载入excel表格
  277. $objPHPExcel = IOFactory::load(public_path() . $parse_url);
  278. //获取表名,一维数组,值是表名。如:array('sheet1', 'sheet2', 'sheet3')
  279. // $nameArr = $objPHPExcel->getSheetNames();
  280. //获取表的数量
  281. $sheetCount = $objPHPExcel->getSheetCount();
  282. $fail = 0; //失败条数
  283. $success = 0; //成功条数
  284. $total = 0; //总共设备数
  285. $data = []; //设备数据
  286. //循环读取每一张表
  287. for ($index = 0; $index < $sheetCount; $index++) {
  288. //设置当前要读取的表
  289. $sheet = $objPHPExcel->getSheet($index); //excel中的第一张sheet
  290. // var_dump($sheet);exit;
  291. $highestRow = $sheet->getHighestRow(); // 取得总行数
  292. // var_dump($highestRow);
  293. if ($highestRow <= 1) {
  294. continue;
  295. }
  296. $brand_type_id = Db::table("sys_dict_type")->where('code', 'CAR_BRAND_OPTION')->value('id');
  297. $car_type_id = Db::table("sys_dict_type")->where('code', 'CAR_TYPE_OPTION')->value('id');
  298. // return $this->where('type_id', $type_id)
  299. // ->where('code', $value)
  300. // ->cache(true, 60)
  301. // ->value('remark');
  302. // $r=$this->execSaveVehicle($param);
  303. $total += $highestRow - 1;
  304. for ($j = 2; $j <= $highestRow; $j++) {
  305. $arr = array(); //每条设备信息
  306. $arr['PLATE_NO'] = strtoupper(trim($sheet->getCell("A" . $j)->getFormattedValue()));
  307. $brand = trim($sheet->getCell("B" . $j)->getFormattedValue());
  308. $brand_id=Db::table("sys_dict_data")->where('type_id', $brand_type_id)->whereLike('value','%'.$brand.'%')->cache(60)->value('code');
  309. $arr['CAR_BRAND']=$brand_id?$brand_id:'99';
  310. $type = trim($sheet->getCell("C" . $j)->getFormattedValue());
  311. $car_type=Db::table("sys_dict_data")->where('type_id', $car_type_id)->whereLike('value','%'.$type.'%')->cache(60)->value('code');
  312. $arr['CAR_BRAND']=$car_type?$car_type:'4';
  313. $arr['RFID_SN'] = trim($sheet->getCell("D" . $j)->getFormattedValue());
  314. $arr['INSTA_DATE'] = trim($sheet->getCell("E" . $j)->getFormattedValue());
  315. $arr['INSTALLER'] = trim($sheet->getCell("F" . $j)->getFormattedValue());
  316. var_dump($arr);
  317. $r=$this->execSaveStation($arr);
  318. if($r){
  319. $success++;
  320. }else{
  321. $fail++;
  322. }
  323. // var_dump($arr);
  324. // array_push($data,$arr);
  325. }
  326. }
  327. // array_unique($data, SORT_REGULAR);
  328. // // var_dump($data);return CatchResponse::success();
  329. // $count = $this->deviceModel->limit(100)->insertAll($data);
  330. // if ($success = $count) {
  331. return CatchResponse::success('共' . $total . '条数据,成功' . $success . '条,失败' . $fail . '条');
  332. // }
  333. // return CatchResponse::success(['error' => true, 'msg' => '导入失败']);
  334. }
  335. }