Ver código fonte

更新患者

ChenWuJie 4 anos atrás
pai
commit
9efbc3bae9

+ 15 - 0
app/Console/Commands/ImportUser.php

xqd xqd xqd
@@ -6,6 +6,7 @@ use App\Imports\chatOrder;
 use App\Imports\Docter\DocterInfo;
 use App\Imports\Docter\DocterOrganizationInfo;
 use App\Imports\Docter\OrganizationInfo;
+use App\Imports\User\PatientsInfo;
 use App\Imports\User\UserInfo;
 use App\Models\Organization;
 use Illuminate\Console\Command;
@@ -53,7 +54,11 @@ class ImportUser extends Command
         if($type == 'user'){
             $this->makeUser($file_path);
         }
+        if($type == 'patiens'){
+            $this->makePatiens($file_path);
+        }
     }
+    //导入用户
     public function makeUser($file_path)
     {
         $this->importsUser($file_path);
@@ -63,4 +68,14 @@ class ImportUser extends Command
         $filePath = './public/import/' . $filePath . '.xls';
         Excel::import(new UserInfo(), $filePath);
     }
+    //导入患者
+    public function makePatiens($file_path)
+    {
+        $this->importsPatiens($file_path);
+    }
+    public function importsPatiens($filePath)
+    {
+        $filePath = './public/import/' . $filePath . '.xls';
+        Excel::import(new PatientsInfo(), $filePath);
+    }
 }

+ 35 - 0
app/Imports/User/PatientsInfo.php

xqd
@@ -0,0 +1,35 @@
+<?php
+
+namespace App\Imports\User;
+
+use App\Models\User;
+use Maatwebsite\Excel\Concerns\WithBatchInserts;
+use Maatwebsite\Excel\Concerns\WithChunkReading;
+use Maatwebsite\Excel\Concerns\WithMultipleSheets;
+use Maatwebsite\Excel\Concerns\ToModel;
+use Maatwebsite\Excel\Concerns\SkipsUnknownSheets;
+
+
+class PatientsInfo implements WithMultipleSheets,WithBatchInserts,WithChunkReading
+{
+    /**
+    * @param array $row
+    *
+    * @return \Illuminate\Database\Eloquent\Model|null
+    */
+    public function sheets(): array
+    {
+        return [
+            new PatientsSheet()
+        ];
+    }
+    public function batchSize(): int
+    {
+        return 1000;
+    }
+
+    public function chunkSize(): int
+    {
+        return 1000;
+    }
+}

+ 82 - 0
app/Imports/User/PatientsSheet.php

xqd
@@ -0,0 +1,82 @@
+<?php
+
+namespace App\Imports\User;
+
+use App\Models\Patient;
+use App\Models\User;
+use Maatwebsite\Excel\Concerns\ToModel;
+use Illuminate\Support\Collection;
+use Maatwebsite\Excel\Concerns\ToCollection;
+use phpDocumentor\Reflection\DocBlock;
+
+class PatientsSheet implements ToCollection
+{
+    /**
+     * @param Collection $collection
+     */
+    public function collection(Collection $collection)
+    {
+        $patients_info = [];
+        foreach ($collection as $row) {
+            if ($row[0]=="患者姓名")
+            {
+                continue;
+            }
+            //姓名
+            if ($row[2] == null)
+            {
+                $user_name = '默认用户';
+                $nickname = '默认用户';
+            }else{
+                $user_name = $row[2] ;
+                $nickname = $row[2] ;
+            }
+            if ($row[1])
+            {
+                $has_user = User::where('phone',$row[1])->count();
+                if ($has_user == 0)
+                {
+                    $new_user = User::create([
+                        'nickname' =>$nickname,
+                        'user_name' => $user_name,
+                        'phone' => $row[1],
+                        'created_at' => date('Y-m-d H:i:s',time()),
+                        'balance' => 0
+                    ]);
+                }else{
+                    $new_user['id'] = User::where('phone',$row[1])->value('id');
+                }
+            }
+            if ($new_user['id'] != null)
+            {
+                $patients_info['user_id'] = $new_user['id'];
+            }else{
+                $patients_info['user_id'] = 0;
+            }
+
+            if ($row[4] == null)
+            {
+                $patients_info['sex'] = 0;
+            }else{
+                if ($row[4] == "男")
+                {
+                    $patients_info['sex'] = 1;
+                }else{
+                    $patients_info['sex'] = 2;
+                }
+            }
+            if ($row[0]==null){
+                $patients_info['name'] = '默认用户';
+            }else{
+                $patients_info['name'] = $row[0];
+            }
+            if ($row[1] == null){
+                $patients_info['phone'] = 0;
+            }else{
+                $patients_info['phone'] = $row[1];
+            }
+            //todo 患者生日没有导入
+            Patient::create($patients_info);
+        }
+    }
+}