| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 | <?phpnamespace App\Imports;use App\Http\Models\Link;use App\Http\Models\LinkTaskDetail;use App\Http\Models\Site;use Illuminate\Support\Collection;use Maatwebsite\Excel\Concerns\Importable;use Maatwebsite\Excel\Concerns\ToCollection;class DemoLinkImport implements ToCollection{    use Importable;    protected $siteId;    public function __construct()    {    }    const FIELD_MAP = [        0 => 'domain',        4 => 'type',        5 => 'type_domain',        6 => 'username',        7 => 'account',        8 => 'password',        9 => 'page',        10 => 'create_time',        11 => 'upload_time'    ];    public function collection(Collection $collections)    {        $allSites = Site::query()->select(['id', 'domain'])->get();        $allLinks = Link::all();        $all = collect([]);        $noSiteList = [];        $noLink = [];        foreach ($collections as $key => $collection) {            if ($key < 1) {                continue;            }            $temp = $this->fieldMap($collection);            $hasSite = $allSites->firstWhere('domain', $temp['domain']);            if ($hasSite) {                $hasLink = $allLinks->firstWhere('url', $temp['type_domain']);                if (!$hasLink) {                    $noLink[] = $temp;                }                $createdAt = $temp['create_time'] ? date('Y-m-d H:i:s', $temp['create_time']) : null;                $inserted = [                    'link_type' => $temp['type'],                    'link_url' => $temp['type_domain'],                    'link_id' => $hasLink->id ?? 0,                    'redundant_site_id' => $hasSite->id,                    'username' => $temp['username'],                    'email' => $temp['account'],                    'password' => $temp['password'],                    'url' => $temp['page'],                    'created_at' => $createdAt,                    'enable' => 1,                    'status' => 5                ];                $all->push(collect($inserted));            } else {                $noSiteList[] = $temp;            }        }        $allItems = $all->chunk(5000)->toArray();        foreach ($allItems as $item) {            LinkTaskDetail::query()->insert($item);        }    }    public function collection2(Collection $collections)    {        $allLinks = Link::all();        $all = collect([]);        foreach ($collections as $key => $collection) {            if ($key < 1) {                continue;            }            $temp = $this->fieldMap($collection);            $linkTypes = Link::TYPES;            array_walk($linkTypes, function ($item) {                return strtolower($item);            });            $typeIndex = array_search(strtolower($temp['type']), $linkTypes);            $noTypeList = [];            if ($typeIndex === false) {//                continue;                $noTypeList[] = $temp['type'];            }            dd($noTypeList);            $hasLink = $allLinks->where('url', $temp['type_domain'])->firstWhere('type', $typeIndex);            if (!$hasLink) {                $inserted = [                    'is_export' => 1,                    'type' => $typeIndex,                    'url' => $temp['type_domain'],                    'created_at' => date('Y-m-d H:i:s')                ];                $all->push(collect($inserted));            }        }        $allItems = $all->chunk(5000)->toArray();//        foreach ($allItems as $item) {//            Link::query()->insert($item);//        }    }    public function fieldMap($collect): array    {        $result = [];        foreach ($collect as $inx => $val) {            if (empty(self::FIELD_MAP[$inx])) continue;            $result[self::FIELD_MAP[$inx]] = $val;        }        return $result;    }}
 |