tongshanglei 2 years ago
parent
commit
68d2cbe364
2 changed files with 162 additions and 57 deletions
  1. 161 56
      catch/yunying/controller/Vehicle.php
  2. 1 1
      catch/yunying/route.php

+ 161 - 56
catch/yunying/controller/Vehicle.php

@@ -5,74 +5,61 @@ namespace catchAdmin\yunying\controller;
 use catcher\base\CatchRequest as Request;
 use catcher\CatchResponse;
 use catcher\base\CatchController;
-use catchAdmin\yunying\model\Vehicle as vehicleModel;
-use catcher\library\excel\Excel;
-use catchAdmin\yunying\excel\VehiclesExport;
-use catcher\Utils;
+// use catchAdmin\yunying\model\Vehicle as vehicleModel;
 
 
+use catchAdmin\yunying\excel\VehiclesExport;
+use catchAdmin\system\model\SysDictData;
+use catcher\Utils;
+use catcher\library\excel\Excel;
+use PhpOffice\PhpSpreadsheet\IOFactory;
+use think\facade\Env;
+use think\facade\Db;
 class Vehicle extends CatchController
 {
-    protected $vehicleModel;
+    // protected $vehicleModel;
     
-    public function __construct(VehicleModel $vehicleModel)
-    {
-        $this->vehicleModel = $vehicleModel;
-    }
+    // public function __construct(VehicleModel $vehicleModel)
+    // {
+    //     $this->vehicleModel = $vehicleModel;
+    // }
     
     /**
      * 列表
      * @time 2022年01月20日 10:42
      * @param Request $request 
      */
-    public function index(Request $request) : \think\Response
-    {
-        $field = $request->get('field')?:'id';
-        $order = $request->get('order')?:'desc';
-        return CatchResponse::paginate($this->vehicleModel->getVehicleList($field,$order));
-    }
-    
-    public function getVehicleListOracle(Request $request) 
+    public function index(Request $request)
     {
-
         $param=$request->param();
         //联表条件o.rfid_id = s.id and o.owner_id = r.id 
         $cond=[
-            '_string'=>'o.rfid_id = s.id and o.owner_id = r.id ',
+            '_string'=>'o.rfid_id = s.id',
         ];
         if($param['PLATE_NO']){
             $cond['o.PLATE_NO']=['like',$param['PLATE_NO']];
         }
-        if($param['ID_CARD_NUMBER']){
-            $cond['r.ID_CARD_NUMBER']=['like',$param['ID_CARD_NUMBER']];
-        }
-        if($param['MOBILE_NUMBER']){
-            $cond['r.MOBILE_NUMBER']=['like',$param['MOBILE_NUMBER']];
-        }
+        // if($param['ID_CARD_NUMBER']){
+        //     $cond['r.ID_CARD_NUMBER']=['like',$param['ID_CARD_NUMBER']];
+        // }
+        // if($param['MOBILE_NUMBER']){
+        //     $cond['r.MOBILE_NUMBER']=['like',$param['MOBILE_NUMBER']];
+        // }
         if($param['RFID_SN']){
             $cond['s.RFID_SN']=['like',$param['RFID_SN']];
         }
 
-        $count=queryOracleCount('DSSC3.W_DW_NON_MOTOR o,DSSC3.W_DW_NON_MOTOR_OWNER r,DSSC3.W_DW_RFID_TAGS s',$cond);
+        // $count=queryOracleCount('DSSC3.W_DW_NON_MOTOR o,DSSC3.W_DW_NON_MOTOR_OWNER r,DSSC3.W_DW_RFID_TAGS s',$cond);
+        $count=queryOracleCount('DSSC3.W_DW_NON_MOTOR o,DSSC3.W_DW_RFID_TAGS s',$cond);
 
         $cond['page']=isset($param['page'])?$param['page']:1;
         $cond['limit']=isset($param['limit'])?$param['limit']:10;
-        $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');
-        
-        // foreach($rows as &$val){
-        //     // $val['DEVICE_NAME'] = mb_convert_encoding($val['DEVICE_NAME'], 'UTF-8', 'GBK');
-        //     $val['IS_ONLINE_TEXT']=$val['IS_ONLINE']?'在线':'离线';
-        //     // $val['UPDATE_DATE'] = mb_convert_encoding($val['UPDATE_DATE'], 'UTF-8', 'GBK');
-        //     // $val['CREATE_DATE'] = mb_convert_encoding($val['CREATE_DATE'], 'UTF-8', 'GBK');
-        //     $findCond=[
-        //         'DEVICE_CODE'=>['=',$val['DEVICE_CODE']]
-        //     ];
-        //     $info=queryOracleFind('DSSC2.ADM_DEV_RFID_CHN',$findCond);
-        //     $val['longitude']=$info['GPS_X'];
-        //     $val['latitude']=$info['GPS_Y'];
-           
-        // }
-        
+        // $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');
+        $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');
+        foreach($rows as &$val){
+            $val['CAR_BRAND_TEXT']=(new SysDictData())->getValueByCode('CAR_BRAND_OPTION',$val['CAR_BRAND']);
+            $val['CAR_TYPE_TEXT']=(new SysDictData())->getValueByCode('CAR_TYPE_OPTION',$val['CAR_TYPE']);
+        }
         $response=[
             'code'=>10000,
             'message'=>'查询成功',
@@ -83,26 +70,42 @@ class Vehicle extends CatchController
         ];
         return $response;
     }
+    
+
     /**
      * 保存信息
      * @time 2022年01月20日 10:42
      * @param Request $request 
      */
-    public function save(Request $request) : \think\Response
+    public function save(Request $request)
     {
-        return CatchResponse::success($this->vehicleModel->storeBy($request->post()));
+  
+        $installer=$request->user()->realname;
+        $param=$request->param();
+        $param['RFID_SN']=strtoupper($param['RFID_SN']);
+        $param['PLATE_NO']=strtoupper($param['PLATE_NO']);
+        $param['INSTA_DATE']=date('Y-m-d H:i:s',time());
+        $param['INSTALLER']=$installer;
+        $r=$this->execSaveVehicle($param);
+        return CatchResponse::success($r);
     }
-    
-    /**
-     * 读取
-     * @time 2022年01月20日 10:42
-     * @param $id 
-     */
-    public function read($id) : \think\Response
-    {
-        return CatchResponse::success($this->vehicleModel->findBy($id));
+    private function execSaveVehicle($data){
+        $conn=getOracleConnect();
+        
+        
+        $sql='declare
+            tagId number;
+        begin
+            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;
+            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'].'\');
+        end;';
+        $stid = oci_parse($conn, $sql);
+        $r = oci_execute($stid);
+        oci_free_statement($stid);
+        return $r;
     }
     
+    
     /**
      * 更新
      * @time 2022年01月20日 10:42
@@ -111,7 +114,23 @@ class Vehicle extends CatchController
      */
     public function update(Request $request, $id) : \think\Response
     {
-        return CatchResponse::success($this->vehicleModel->updateBy($id, $request->post()));
+        $params=$request->post();
+        $params['RFID_SN']=strtoupper($params['RFID_SN']);
+        $params['PLATE_NO']=strtoupper($params['PLATE_NO']);
+        // var_dump($params);
+        $conn=getOracleConnect();
+        // //更新
+        $sql='UPDATE DSSC3.W_DW_RFID_TAGS  SET RFID_SN = \''.$params['RFID_SN'].'\' WHERE ID = \''.$id.'\' ';
+        $stid = oci_parse($conn, $sql);
+        $r = oci_execute($stid);
+        
+        $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.'\' ';
+        // var_dump($sql);
+        $stid2 = oci_parse($conn, $sql);
+        $r2 = oci_execute($stid2);
+        
+        return CatchResponse::success('修改成功');
+
     }
     
     /**
@@ -121,7 +140,16 @@ class Vehicle extends CatchController
      */
     public function delete($id) : \think\Response
     {
-        return CatchResponse::success($this->vehicleModel->deleteBy($id));
+        $conn=getOracleConnect();
+        $sql='DELETE FROM DSSC3.W_DW_RFID_TAGS WHERE ID in ('.$id.')';
+      
+        $stid = oci_parse($conn, $sql);
+        $r = oci_execute($stid);
+
+        $sql='DELETE FROM DSSC3.W_DW_NON_MOTOR WHERE RFID_ID in ('.$id.')';
+        $stid = oci_parse($conn, $sql);
+        $r = oci_execute($stid);
+        return CatchResponse::success($r);
     }
     /**
      * 导出
@@ -134,7 +162,84 @@ class Vehicle extends CatchController
      */
     public function export_vehicle(Excel $excel, VehiclesExport $VehicleExport)
     {
-        //  var_dump(Utils::publicPath('export/users'));//导出路径
         return CatchResponse::success($excel->save($VehicleExport, Utils::publicPath('export/vehicles'), 'local', '车辆列表'));
     }
+    /**
+     * 导入车辆
+     *
+     * @time 2022年02月15日
+     * @param Excel $excel
+     * @param DeviceExport $deviceExport
+     * @throws \PhpOffice\PhpSpreadsheet\Exception
+     * @return \think\response\Json
+     */
+    public function importVehicle(Request $request)
+    {
+        $url = $request->post('url');
+        if (!$url) {
+            return CatchResponse::fail('请上传文件');
+        }
+        
+        $creator_id = $request->post('creator_id');
+        //解析地址
+        $parse_url = parse_url($url)['path'];
+        //载入excel表格 
+        $objPHPExcel = IOFactory::load(public_path() . $parse_url);
+        //获取表名,一维数组,值是表名。如:array('sheet1', 'sheet2', 'sheet3')
+        // $nameArr = $objPHPExcel->getSheetNames();
+        //获取表的数量
+        $sheetCount = $objPHPExcel->getSheetCount();
+        $fail = 0; //失败条数
+        $success = 0; //成功条数
+        $total = 0; //总共设备数
+        $data = []; //设备数据
+        //循环读取每一张表
+        for ($index = 0; $index < $sheetCount; $index++) {
+            //设置当前要读取的表
+            $sheet = $objPHPExcel->getSheet($index);   //excel中的第一张sheet
+            // var_dump($sheet);exit;
+            $highestRow  = $sheet->getHighestRow();       // 取得总行数
+            // var_dump($highestRow);
+            if ($highestRow <= 1) {
+                continue;
+            }
+            $brand_type_id = Db::table("sys_dict_type")->where('code', 'CAR_BRAND_OPTION')->value('id');
+            $car_type_id = Db::table("sys_dict_type")->where('code', 'CAR_TYPE_OPTION')->value('id');
+            
+        // return $this->where('type_id', $type_id)
+        //             ->where('code', $value)
+        //             ->cache(true, 60)
+        //             ->value('remark');
+            $total += $highestRow - 1;
+            for ($j = 2; $j <= $highestRow; $j++) {
+                $arr = array(); //每条设备信息
+                $arr['PLATE_NO'] = strtoupper(trim($sheet->getCell("A" . $j)->getFormattedValue())); 
+                $brand = trim($sheet->getCell("B" . $j)->getFormattedValue());  
+
+                // $brand_id=Db::table("sys_dict_data")->where('type_id', $type_id)->where('code', $value)->value('remark');
+                $type = trim($sheet->getCell("C" . $j)->getFormattedValue());  
+                $arr['RFID_SN'] = trim($sheet->getCell("D" . $j)->getFormattedValue());  
+                $arr['INSTA_DATE'] = trim($sheet->getCell("E" . $j)->getFormattedValue());  
+                $arr['INSTALLER'] = trim($sheet->getCell("F" . $j)->getFormattedValue()); 
+                var_dump($arr);
+                // $r=$this->execSaveStation($arr);
+                // if($r){
+                //     $success++;
+                // }else{
+                //     $fail++;
+                // }
+                // var_dump($arr);
+            //    array_push($data,$arr);
+            }
+        }
+        // array_unique($data, SORT_REGULAR);
+        // // var_dump($data);return CatchResponse::success();
+        // $count = $this->deviceModel->limit(100)->insertAll($data);
+        // if ($success = $count) {
+
+            return CatchResponse::success('共' . $total . '条数据,成功' . $success . '条,失败' . $fail . '条');
+
+        // }
+        // return CatchResponse::success(['error' => true, 'msg' => '导入失败']);
+    }
 }

+ 1 - 1
catch/yunying/route.php

@@ -12,7 +12,7 @@
 // you should use `$router`
 $router->group(function () use ($router){
 	// vehicle路由
-	$router->get('vehicle/getVehicleList','\catchAdmin\yunying\controller\Vehicle@getVehicleListOracle');
+	$router->post('vehicle/importVehicle', '\catchAdmin\yunying\controller\Vehicle@importVehicle');
 	$router->resource('vehicle', '\catchAdmin\yunying\controller\Vehicle');
 	
 	// 导出车辆