| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- namespace App\Imports;
- use App\Exceptions\ImportError;
- use App\Models\Device;
- use App\Models\DeviceName;
- use App\Models\InnerDevice;
- use App\Models\InnerDeviceNamesModel;
- use App\Models\Option;
- use App\Models\Project;
- use App\Models\ProjectUser;
- use App\Models\ProjectZone;
- use App\Models\Road;
- use App\Models\Spec;
- use App\Models\User;
- use App\Models\WorkPoint;
- use App\Models\Zone;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Log;
- use Maatwebsite\Excel\Concerns\ToCollection;
- use PhpOffice\PhpSpreadsheet\Shared\Date;
- class ProjectImport implements ToCollection
- {
- protected $model;
- protected $zone;
- protected $road;
- protected $project_zone;
- public function __construct()
- {
- $this->model = new Project();
- }
- public function collection(Collection $rows)
- {
- if(count($rows) <= 1) {
- /** @noinspection PhpUnhandledExceptionInspection */
- return false;
- };
- foreach ($rows as $key => $row) {
- if($key == 0) continue;
- if(empty($row[0])&&empty($row[1])&&empty($row[2]))
- {
- break;
- }
- //项目id
- $project_id = null;
- //用户id
- $user_id = null;
- $password = '123456';
- $password = bcrypt($password);
- /*
- * 项目名称
- * 逻辑
- * 1.如果不存在该项目,那么就创建一个项目,并且active为激活状态
- * 2.如果存在该项目,返回项目id
- * */
- if ($row[0])
- {
- $project_id = Project::firstOrCreate([
- 'name' => $row[0],
- ],[
- 'name' => $row[0],
- 'active' => 1
- ]);
- }
- //手机号
- if ($row[1]&&$row[2])
- {
- $user_id = User::firstOrCreate([
- 'phone' => $row[2]
- ],[
- 'name' => $row[1],
- 'phone'=> $row[2],
- 'password' => $password
- ]);
- }
- //如果相同电话号码,不同名字的话就替换原来的名字
- if ($user_id->name != $row[1])
- {
- User::where('id',$user_id->id)->update(['name'=>$row[1]]);
- }
- /*
- * 导入到项目成员表里
- * 如果原本这个项目存在项目经理,那就把这个项目经理替换成项目副经理
- * 然后再插入新的数据
- * */
- $manager_num = ProjectUser::where('project_id',$project_id['id'])->where('project_role_id',4)->count();
- if ($manager_num != 0)
- {
- $manager_user = ProjectUser::where('project_id',$project_id['id'])->where('project_role_id',4)->first();
- ProjectUser::where('id',$manager_user['id'])->update(['project_role_id'=>3]);
- }
- $data =[
- 'project_id' => $project_id['id'],
- 'user_id' => $user_id['id'],
- 'project_role_id' => 4
- ];
- ProjectUser::create($data);
- }
- return true;
- }
- }
|