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; } }