123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- namespace App\Imports;
- use App\Http\Models\Link;
- use App\Http\Models\LinkTaskDetail;
- use Illuminate\Support\Collection;
- use Maatwebsite\Excel\Concerns\Importable;
- use Maatwebsite\Excel\Concerns\ToCollection;
- class LinkImport implements ToCollection
- {
- use Importable;
- protected $siteId;
- public function __construct($siteId)
- {
- $this->siteId = $siteId;
- }
- const FIELD_MAP = [
- 0 => 'link_id',
- 1 => 'username',
- 2 => 'password',
- 3 => 'email',
- 4 => 'url',
- 5 => 'created_at'
- ];
- public function collection(Collection $collections)
- {
- $allLinks = Link::all();
- $all = collect([]);
- foreach ($collections as $key => $collection) {
- if ($key < 1 || empty($collection[0]) || empty($collection[2])
- || empty($collection[3]) || empty($collection[4])) {
- continue;
- }
- $matchLink = $allLinks->firstWhere('url', $collection[0]);
- if (!$matchLink) {
- continue;
- }
- $collection[0] = $matchLink->id;
- $temp = $this->fieldMap($collection);
- $all->push($temp);
- }
- $allRecords = $all->toArray();
- foreach ($allRecords as $record) {
- if (!LinkTaskDetail::query()->where([
- 'redundant_site_id' => $this->siteId,
- 'link_id' => $record['link_id'],
- 'enable' => 1,
- 'status' => 5
- ])->exists()) {
- LinkTaskDetail::query()->insert($record);
- }
- }
- // $allItems = $all->chunk(5000)->toArray();
- // foreach ($allItems as $item) {
- // LinkTaskDetail::query()->insert($item);
- // }
- }
- public function fieldMap($collect): array
- {
- $result = [];
- foreach ($collect as $inx => $val) {
- if (empty(self::FIELD_MAP[$inx])) continue;
- if (self::FIELD_MAP[$inx] == 'created_at') {
- $time = strtotime(str_replace('#', '', $val));
- $result[self::FIELD_MAP[$inx]] = $time ? date(' Y-m-d H:i:s', $time) : date('Y-m-d H:i:s');
- } else {
- $result[self::FIELD_MAP[$inx]] = $val;
- }
- $result['redundant_site_id'] = $this->siteId;
- $result['status'] = 5; //审核通过的
- $result['enable'] = 1;
- }
- return $result;
- }
- }
|