| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 | <?phpnamespace App\Imports;use App\Http\Models\SiteOptimizePage;use Illuminate\Support\Collection;use Maatwebsite\Excel\Concerns\Importable;use Maatwebsite\Excel\Concerns\ToCollection;class OptimizePageImport implements ToCollection{    use Importable;    protected $siteId;    public function __construct($siteId)    {        $this->siteId = $siteId;    }    const FIELD_MAP = [        0 => 'keyword',        1 => 'url',        2 => 'is_optimize',    ];    public function collection(Collection $collections)    {        $insertData = [];        foreach ($collections as $key => $collection) {            if ($key < 1) {                continue;            }            $temp = $this->fieldMap($collection);            $insertData[] = $temp;        }        SiteOptimizePage::query()->insert($insertData);    }    public function fieldMap($collect): array    {        $result = [            'created_at' => date('Y-m-d H:i:s'),            'updated_at' => date('Y-m-d H:i:s'),            'site_id' => $this->siteId        ];        foreach ($collect as $inx => $val) {            if (empty(self::FIELD_MAP[$inx])) continue;            if (in_array(self::FIELD_MAP[$inx], ['is_optimize'])) {                $result[self::FIELD_MAP[$inx]] = ($val == "是" ? 1 : 0);            } else {                $result[self::FIELD_MAP[$inx]] = $val;            }        }        return $result;    }}
 |