Browse Source

用户导入

ChenWuJie 4 years ago
parent
commit
e6d059c3ca
3 changed files with 173 additions and 0 deletions
  1. 66 0
      app/Console/Commands/ImportUser.php
  2. 35 0
      app/Imports/User/UserInfo.php
  3. 72 0
      app/Imports/User/UserSheet.php

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

xqd
@@ -0,0 +1,66 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Imports\chatOrder;
+use App\Imports\Docter\DocterInfo;
+use App\Imports\Docter\DocterOrganizationInfo;
+use App\Imports\Docter\OrganizationInfo;
+use App\Imports\User\UserInfo;
+use App\Models\Organization;
+use Illuminate\Console\Command;
+use Maatwebsite\Excel\Facades\Excel;
+class ImportUser extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'import:importUser {type} {filepath}';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '用户数据导入';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $type = $this->argument('type');
+        $file_path = $this->argument('filepath');
+        //路径相对于项目根目录即 /public
+        if(empty($type) || empty($file_path)){
+            dd('请输入完整参数');
+        }
+        //医生信息导入
+        if($type == 'user'){
+            $this->makeUser($file_path);
+        }
+    }
+    public function makeUser($file_path)
+    {
+        $this->importsUser($file_path);
+    }
+    public function importsUser($filePath)
+    {
+        $filePath = './public/import/' . $filePath . '.xls';
+        Excel::import(new UserInfo(), $filePath);
+    }
+}

+ 35 - 0
app/Imports/User/UserInfo.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 UserInfo implements WithMultipleSheets,WithBatchInserts,WithChunkReading
+{
+    /**
+    * @param array $row
+    *
+    * @return \Illuminate\Database\Eloquent\Model|null
+    */
+    public function sheets(): array
+    {
+        return [
+            new UserSheet()
+        ];
+    }
+    public function batchSize(): int
+    {
+        return 1000;
+    }
+
+    public function chunkSize(): int
+    {
+        return 1000;
+    }
+}

+ 72 - 0
app/Imports/User/UserSheet.php

xqd
@@ -0,0 +1,72 @@
+<?php
+
+namespace App\Imports\User;
+
+use App\Models\User;
+use Maatwebsite\Excel\Concerns\ToModel;
+use Illuminate\Support\Collection;
+use Maatwebsite\Excel\Concerns\ToCollection;
+
+class UserSheet implements ToCollection
+{
+    /**
+     * @param Collection $collection
+     */
+    public function collection(Collection $collection)
+    {
+        $user_info = [];
+        foreach ($collection as $row) {
+            if ($row[0]=="userId")
+            {
+                continue;
+            }
+            //用户名
+            if ($row[1] == null)
+            {
+                $user_info['user_name'] = '';
+            }else{
+                $user_info['user_name'] = $row[1];
+            }
+            //昵称
+            if ($row[2] == null)
+            {
+                $user_info['nickname'] = '';
+            }else{
+                $user_info['nickname'] = $row[2];
+            }
+            //电话
+            if ($row[3] == null)
+            {
+                $user_info['phone'] = '';
+            }else{
+                $user_info['phone'] = $row[3];
+            }
+
+            if ($row[4] == null)
+            {
+                $user_info['created_at'] =self::excelTime(0);
+            }else{
+                $user_info['created_at'] = self::excelTime($row[4]);
+            }
+
+            if ($row[5] == null)
+            {
+                $user_info['balance'] = '';
+            }else{
+                $user_info['balance'] = $row[5];
+            }
+            User::insert($user_info);
+        }
+
+    }
+    public static function excelTime($date, $time = false) {
+        $date=$date>25568?$date+1:25569;
+        $ofs=(70 * 365 + 17+2) * 86400;
+        $d1 = date("Y-m-d H:i:s",($date * 86400) - $ofs);
+        $d1 = strtotime("-8hours",strtotime($d1));
+        $d1 = date('Y-m-d H:i:s',$d1);
+        $date = $d1;
+        return $date;
+
+    }
+}