| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 | <?phpnamespace App\Imports;use App\Exceptions\ImportError;use App\Models\DeviceName;use App\Models\InnerDevice;use App\Models\Option;use App\Models\Project;use App\Models\ProjectZone;use App\Models\Road;use App\Models\Spec;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 InnerDeviceImport implements ToCollection{    protected $model;    protected $zone;    protected $road;    protected $project_zone;    public function __construct()    {        $this->model = new InnerDevice();    }    public function formatDate($date)    {        if(empty($date)) return '';        return Date::excelToDateTimeObject($date)->format('Y-m-d');    }    public function collection(Collection $rows)    {        if(count($rows) <= 1) {            /** @noinspection PhpUnhandledExceptionInspection *///            throw new ImportError('表格为空');            return false;        };        foreach ($rows as $key => $row) {            if($key == 0) continue;            if(!isset($row[0])) break;            $device_name = DeviceName::firstOrCreate([                'name' => $row[1]            ]);            $spec = Spec::firstOrCreate([                'device_name_id' => $device_name->id,                'name' => $row[2]            ]);            $work_point = WorkPoint::firstOrCreate([                'name' => $row[8]            ])->first();            $status = Option::where([                ['table', '=', 'inner_devices'],                ['column', '=', 'status'],                ['name', '=', $row[9]]            ])->first();            $data = [                'number' => $row[0],                'device_name_id' => $device_name ? $device_name->id : '',                'spec_id' => $spec ? $spec->id : '',                'produce_date' => $this->formatDate($row[3]),                'buy_origin' => $row[4],                'manufacturer' => $row[5],                'shape' => $row[6],                'work_point_id' => $work_point ? $work_point->id : '',                'status' => $status ? $status->id : '',                'quantity' => (int)$row[7]            ];            $this->model->create($data);//            $num = (int)$row[7];//            for($i = 0; $i < $num; ++$i) {//                $this->model->create($data);//            }        }        return true;    }}
 |