Vehicle.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 index(Request $request)
  66. // {
  67. // ini_set('memory_limit','3072M');
  68. // set_time_limit(0);
  69. // $cond=[
  70. // '_string'=>'o.rfid_id = s.id',
  71. // ];
  72. // // $count=queryOracleCount('DSSC3.W_DW_NON_MOTOR o,DSSC3.W_DW_NON_MOTOR_OWNER r,DSSC3.W_DW_RFID_TAGS s',$cond);
  73. // $count=queryOracleCount('DSSC3.W_DW_NON_MOTOR o,DSSC3.W_DW_RFID_TAGS s',$cond);
  74. // $to=ceil($count/10000);
  75. // var_dump($to);
  76. // for($i=1;$i<=$to;$i++){
  77. // var_dump($i);
  78. // $cond['page']=$i;
  79. // $cond['limit']=10000;
  80. // // $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');
  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. // // var_dump(count($rows));
  83. // $data=[];
  84. // foreach($rows as &$val){
  85. // $item=array(
  86. // 'license_plate'=>$val['PLATE_NO'],
  87. // 'car_type'=>$val['CAR_TYPE'],
  88. // 'brand_id'=>(int)$val['CAR_BRAND'],
  89. // 'rfid_sn'=>$val['RFID_SN'],
  90. // 'installer'=>$val['INSTALLER'],
  91. // 'license_time'=>$val['INSTA_DATE'],
  92. // );
  93. // array_push($data,$item);
  94. // }
  95. // $success = Db::table('vehicles')->limit(1000)->insertAll($data);
  96. // var_dump($success);
  97. // }
  98. // }
  99. /**
  100. * 保存信息
  101. * @time 2022年01月20日 10:42
  102. * @param Request $request
  103. */
  104. public function save(Request $request)
  105. {
  106. $installer=$request->user()->realname;
  107. $param=$request->param();
  108. $param['RFID_SN']=strtoupper($param['RFID_SN']);
  109. $param['PLATE_NO']=strtoupper($param['PLATE_NO']);
  110. $param['INSTA_DATE']=date('Y-m-d H:i:s',time());
  111. $param['INSTALLER']=$installer;
  112. $r=$this->execSaveVehicle($param);
  113. return CatchResponse::success($r);
  114. }
  115. private function execSaveVehicle($data){
  116. $conn=getOracleConnect();
  117. $sql='declare
  118. tagId number;
  119. begin
  120. 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;
  121. 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'].'\');
  122. end;';
  123. $stid = oci_parse($conn, $sql);
  124. $r = oci_execute($stid);
  125. oci_free_statement($stid);
  126. return $r;
  127. }
  128. /**
  129. * 更新
  130. * @time 2022年01月20日 10:42
  131. * @param Request $request
  132. * @param $id
  133. */
  134. public function update(Request $request, $id) : \think\Response
  135. {
  136. $params=$request->post();
  137. $params['RFID_SN']=strtoupper($params['RFID_SN']);
  138. $params['PLATE_NO']=strtoupper($params['PLATE_NO']);
  139. // var_dump($params);
  140. $conn=getOracleConnect();
  141. // //更新
  142. $sql='UPDATE DSSC3.W_DW_RFID_TAGS SET RFID_SN = \''.$params['RFID_SN'].'\' WHERE ID = \''.$id.'\' ';
  143. $stid = oci_parse($conn, $sql);
  144. $r = oci_execute($stid);
  145. $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.'\' ';
  146. // var_dump($sql);
  147. $stid2 = oci_parse($conn, $sql);
  148. $r2 = oci_execute($stid2);
  149. return CatchResponse::success('修改成功');
  150. }
  151. /**
  152. * 删除
  153. * @time 2022年01月20日 10:42
  154. * @param $id
  155. */
  156. public function delete($id) : \think\Response
  157. {
  158. $conn=getOracleConnect();
  159. $sql='DELETE FROM DSSC3.W_DW_RFID_TAGS WHERE ID in ('.$id.')';
  160. $stid = oci_parse($conn, $sql);
  161. $r = oci_execute($stid);
  162. $sql='DELETE FROM DSSC3.W_DW_NON_MOTOR WHERE RFID_ID in ('.$id.')';
  163. $stid = oci_parse($conn, $sql);
  164. $r = oci_execute($stid);
  165. return CatchResponse::success($r);
  166. }
  167. /**
  168. * 导出
  169. *
  170. * @time 2022年01月22日
  171. * @param Excel $excel
  172. * @param VehicleExport $vehicleExport
  173. * @throws \PhpOffice\PhpSpreadsheet\Exception
  174. * @return \think\response\Json
  175. */
  176. public function export_vehicle(Excel $excel, VehiclesExport $VehicleExport)
  177. {
  178. return CatchResponse::success($excel->save($VehicleExport, Utils::publicPath('export/vehicles'), 'local', '车辆列表'));
  179. }
  180. /**
  181. * 导入车辆
  182. *
  183. * @time 2022年02月15日
  184. * @param Excel $excel
  185. * @param DeviceExport $deviceExport
  186. * @throws \PhpOffice\PhpSpreadsheet\Exception
  187. * @return \think\response\Json
  188. */
  189. public function importVehicle(Request $request)
  190. {
  191. $url = $request->post('url');
  192. if (!$url) {
  193. return CatchResponse::fail('请上传文件');
  194. }
  195. $creator_id = $request->post('creator_id');
  196. //解析地址
  197. $parse_url = parse_url($url)['path'];
  198. //载入excel表格
  199. $objPHPExcel = IOFactory::load(public_path() . $parse_url);
  200. //获取表名,一维数组,值是表名。如:array('sheet1', 'sheet2', 'sheet3')
  201. // $nameArr = $objPHPExcel->getSheetNames();
  202. //获取表的数量
  203. $sheetCount = $objPHPExcel->getSheetCount();
  204. $fail = 0; //失败条数
  205. $success = 0; //成功条数
  206. $total = 0; //总共设备数
  207. $data = []; //设备数据
  208. //循环读取每一张表
  209. for ($index = 0; $index < $sheetCount; $index++) {
  210. //设置当前要读取的表
  211. $sheet = $objPHPExcel->getSheet($index); //excel中的第一张sheet
  212. // var_dump($sheet);exit;
  213. $highestRow = $sheet->getHighestRow(); // 取得总行数
  214. // var_dump($highestRow);
  215. if ($highestRow <= 1) {
  216. continue;
  217. }
  218. $brand_type_id = Db::table("sys_dict_type")->where('code', 'CAR_BRAND_OPTION')->value('id');
  219. $car_type_id = Db::table("sys_dict_type")->where('code', 'CAR_TYPE_OPTION')->value('id');
  220. // return $this->where('type_id', $type_id)
  221. // ->where('code', $value)
  222. // ->cache(true, 60)
  223. // ->value('remark');
  224. $r=$this->execSaveVehicle($param);
  225. $total += $highestRow - 1;
  226. for ($j = 2; $j <= $highestRow; $j++) {
  227. $arr = array(); //每条设备信息
  228. $arr['PLATE_NO'] = strtoupper(trim($sheet->getCell("A" . $j)->getFormattedValue()));
  229. $brand = trim($sheet->getCell("B" . $j)->getFormattedValue());
  230. $brand_id=Db::table("sys_dict_data")->where('type_id', $brand_type_id)->whereLike('value','%'.$brand.'%')->cache(60)->value('code');
  231. $arr['CAR_BRAND']=$brand_id?$brand_id:'99';
  232. $type = trim($sheet->getCell("C" . $j)->getFormattedValue());
  233. $car_type=Db::table("sys_dict_data")->where('type_id', $car_type_id)->whereLike('value','%'.$type.'%')->cache(60)->value('code');
  234. $arr['CAR_BRAND']=$car_type?$car_type:'4';
  235. $arr['RFID_SN'] = trim($sheet->getCell("D" . $j)->getFormattedValue());
  236. $arr['INSTA_DATE'] = trim($sheet->getCell("E" . $j)->getFormattedValue());
  237. $arr['INSTALLER'] = trim($sheet->getCell("F" . $j)->getFormattedValue());
  238. var_dump($arr);
  239. $r=$this->execSaveStation($arr);
  240. if($r){
  241. $success++;
  242. }else{
  243. $fail++;
  244. }
  245. // var_dump($arr);
  246. // array_push($data,$arr);
  247. }
  248. }
  249. // array_unique($data, SORT_REGULAR);
  250. // // var_dump($data);return CatchResponse::success();
  251. // $count = $this->deviceModel->limit(100)->insertAll($data);
  252. // if ($success = $count) {
  253. return CatchResponse::success('共' . $total . '条数据,成功' . $success . '条,失败' . $fail . '条');
  254. // }
  255. // return CatchResponse::success(['error' => true, 'msg' => '导入失败']);
  256. }
  257. }