tongshanglei 2 years ago
parent
commit
1ce201353d
2 changed files with 80 additions and 2 deletions
  1. 77 1
      catch/alarm/controller/RfidWithBw.php
  2. 3 1
      catch/alarm/route.php

+ 77 - 1
catch/alarm/controller/RfidWithBw.php

@@ -6,7 +6,10 @@ use catcher\base\CatchRequest as Request;
 use catcher\CatchResponse;
 use catcher\base\CatchController;
 use catchAdmin\alarm\model\RfidWithBw as rfidWithBwModel;
-
+use catcher\Utils;
+use catcher\library\excel\Excel;
+use PhpOffice\PhpSpreadsheet\IOFactory;
+use think\facade\Db;
 class RfidWithBw extends CatchController
 {
     protected $rfidWithBwModel;
@@ -83,4 +86,77 @@ class RfidWithBw extends CatchController
     {
         return CatchResponse::success($this->rfidWithBwModel->deleteBy($id));
     }
+    /**
+     * 导入标签
+     *
+     * @time 2022年02月15日
+     * @param Excel $excel
+     * @param DeviceExport $deviceExport
+     * @throws \PhpOffice\PhpSpreadsheet\Exception
+     * @return \think\response\Json
+     */
+    public function importRfid(Request $request)
+    {
+        $url = $request->post('url');
+        if (!$url) {
+            return CatchResponse::fail('请上传文件');
+        }
+        $bw_id = $request->post('bw_id');
+        if (!$bw_id) {
+            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 <= 2) {
+                continue;
+            }
+            $total += $highestRow - 2;
+            for ($j = 3; $j <= $highestRow; $j++) {
+     
+                $arr = array(); //每条设备信息
+                $arr['rfid'] = strtoupper(trim($sheet->getCell("A" . $j)->getFormattedValue())); 
+                $arr['rfid_type'] = trim($sheet->getCell("B" . $j)->getFormattedValue());  
+
+                $arr['created_at']=time();
+                $arr['creator_id']=$creator_id;
+                $arr['bw_id']=$bw_id;
+                $exist=Db::table('rfid_with_bw')->where('rfid',$arr['rfid'])->where('bw_id',$bw_id)->count();
+                if($exist){
+                    continue;
+                }
+                array_push($data,$arr);
+               
+            }
+        }
+        
+        array_unique($data, SORT_REGULAR);
+        // var_dump($data);return CatchResponse::success();
+        $count = Db::table('rfid_with_bw')->limit(100)->insertAll($data);
+        if ($count) {
+
+            return CatchResponse::success('共' . $total . '条数据,成功' . $count . '条,失败' . ($total-$count) . '条');
+
+        }
+        return CatchResponse::success(['error' => true, 'msg' => '导入失败']);
+    }
 }

+ 3 - 1
catch/alarm/route.php

@@ -41,4 +41,6 @@ $router->group('alarmReport', function () use ($router){
 	$router->get('totalGrowth', '\catchAdmin\alarm\controller\AlarmReport@totalGrowth');
 	
 	
-})->middleware('auth');
+})->middleware('auth');
+
+$router->post('rfidWithBw/importRfid', '\catchAdmin\alarm\controller\RfidWithBw@importRfid');