韩盟盟 před 3 dny
rodič
revize
c792eb41b0
1 změnil soubory, kde provedl 48 přidání a 47 odebrání
  1. 48 47
      app/Admin/Actions/Post/ImportExcel.php

+ 48 - 47
app/Admin/Actions/Post/ImportExcel.php

@@ -3,6 +3,7 @@
 namespace App\Admin\Actions\Post;
 
 use App\Model\TextLabel;
+use App\Model\XyZskDisease;
 use Encore\Admin\Actions\Action;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Storage;
@@ -14,54 +15,54 @@ class ImportExcel extends Action
     public function handle(Request $request)
     {
         $oFile = $request->file('file');
-        Storage::disk('local')->put('text.xlsx',file_get_contents($oFile));
-        $url = storage_path().'/app/text.xlsx';
-        $excel = PHPExcel_IOFactory::load($url);
-        $sheet = $excel->getSheet(0);
-        $row_num = $sheet->getHighestRow();
-        $data = [];
-        for ($row = 2;$row <= $row_num;$row++){
-            $text = $excel->getActiveSheet()->getCell("A".$row)->getValue();
-            $length = mb_strlen($text);
-            if ($length <= 510){
-                $data[]['text'] = $text;
-            }elseif ($length > 510 && $length <= 1020){
-                $textArray = preg_split('/。/',$text);
-                $count = count($textArray);
-                $pos = ceil($count / 2);
-                $text1 = '';
-                $text2 = '';
-                foreach ($textArray as $key => $item){
-                    if ($key <= $pos){
-                        $text1 .= $item.'。';
-                    }else{
-                        $text2 .= $item.'。';
-                    }
-                }
-                $data[]['text'] = $text1;
-                $data[]['text'] = $text2;
-            }elseif ($length > 1020){
-                $textArray = preg_split('/。/',$text);
-                $count = count($textArray);
-                $pos = ceil($count / 3);
-                $text1 = '';
-                $text2 = '';
-                $text3 = '';
-                foreach ($textArray as $key => $item){
-                    if ($key <= $pos){
-                        $text1 .= $item;
-                    }elseif($key <= $pos*2){
-                        $text2 .= $item.'。';
-                    }else{
-                        $text3 .= $item.'。';
-                    }
-                }
-                $data[]['text'] = $text1;
-                $data[]['text'] = $text2;
-                $data[]['text'] = $text3;
-            }
+        if(!$oFile){
+            return $this->error('请选择上传文件');
         }
-        TextLabel::query()->insert($data);
+        Storage::disk('local')->put('text.csv',file_get_contents($oFile));
+        $url = storage_path().'/app/text.csv';
+
+        $file = fopen($url, 'r');
+        if(!$file)
+        {
+            return $this->error('文件打开错误');
+        }
+        while ($data = fgetcsv($file)) { //每次读取CSV里面的一行内容
+            array_walk($data, function(&$item){
+                $item = mb_convert_encoding($item, "UTF-8", "GBK");
+            });
+            $list[] = $data;
+        }
+        fclose($file);
+
+        foreach ($list as $k=>$v) {
+            if($k == 0) continue;
+            $data = [
+                'department_1' => $v[0],
+                'department_2' => $v[1],
+                'name' => $v[2],
+                'alias' => $v[3],
+                'clinicalFeature' => $v[4],
+                'diagnosis' => $v[5],
+                'treatment' => $v[6],
+                'regularMedication' => $v[7],
+                'pathogenesis' => $v[8],
+                'inspection' => $v[9],
+                'relevantOperation' => $v[10],
+                'laboratoryInspection' => $v[11],
+                'icd' => $v[12],
+                'etiology' => $v[13],
+                'auxiliaryExamination' => $v[14],
+                'antidiastole' => $v[15],
+                'prognosis' => $v[16],
+                'complicationsOverview' => $v[17],
+                'epidemiology' => $v[18],
+                'precaution' => $v[19],
+                'symptom' => $v[20],
+                'examination' => $v[21]
+            ];
+            XyZskDisease::query()->insert($data);
+        }
+
         return $this->response()->success('Success message...')->refresh();
     }