| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 | <?phpnamespace App\Imports;use App\Http\Models\PrKeywordExtend;use Illuminate\Support\Collection;use Illuminate\Support\Facades\DB;use Maatwebsite\Excel\Concerns\Importable;use Maatwebsite\Excel\Concerns\ToCollection;class KeywordExtend implements ToCollection{    use Importable;    protected $siteId;    protected $oldId;    public function __construct($siteId, $oldId)    {        $this->siteId = $siteId;        $this->oldId = $oldId;    }////    const FIELD_MAP = [//        0 => 'keyword',//        1 => 'chinese_keyword',//        2 => 'page_url',//        3 => 'search_num',//    ];    const FIELD_MAP = [        0 => 'keyword',        1 => 'search_num',        2 => 'index_num',        3 => 'land_page',    ];    public function collection(Collection $collections)    {        $all = collect([]);        $rankKeyword=collect([]);        $rankConnection = DB::connection('rank');        $maxSn = $rankConnection->table('project_keyword')->where(['project_id' => $this->oldId])->max('sn') ?? 0;        foreach ($collections as $key => $collection) {            if ($key < 1) {                continue;            }            $temp = $this->fieldMap($collection);            if (mb_strlen($temp['keyword']) < 1) {                continue;            }            $all->push($temp);            $rank=$this->fieldMapRank($collection);            $maxSn++;            $rankKeyword->push($rank+['sn' => $maxSn]);        }        $allItems = $all->chunk(2500)->toArray();        foreach ($allItems as $item) {            PrKeywordExtend::query()->insert($item);        }        $allRank=$rankKeyword->chunk(2500)->toArray();        foreach ($allRank as $item) {            $rankConnection->table('project_keyword')->insert($item);        }    }    public function fieldMapRank($collect): array    {        $result = [            'google_rank' => 9999,            'allintitle' => -1,            'sync_time' => strtotime('+1 day'),            'project_id' => $this->oldId        ];        foreach ($collect as $inx => $val) {            if (empty(self::FIELD_MAP[$inx])) continue;            if ($inx==0) {                $result['keyword']=$val;                $result['chinese_keyword']='';            }            if ($inx==1) {                $result['search_num']=intval($val);            }            if ($inx==3) {                $result['page_url']=$val;            }        }        return $result;    }    public function fieldMap($collect): array    {//        $ym = date('Ym', strtotime('first day of -1 month'));        $ym = date('Ym');        $result = [            'old_id' => $this->oldId,            'ym' => $ym,            'site_id' => $this->siteId,            'created_at' => date('Y-m-d H:i:s'),            'updated_at' => date('Y-m-d H:i:s'),        ];        foreach ($collect as $inx => $val) {            if (empty(self::FIELD_MAP[$inx])) continue;            if (in_array(self::FIELD_MAP[$inx], ['index_num', 'search_num'])) {                $result[self::FIELD_MAP[$inx]] = intval($val);            } else {                $result[self::FIELD_MAP[$inx]] = $val;            }        }        return $result;    }}
 |