123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace App\Imports;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Validation\Rule;
- use Maatwebsite\Excel\Concerns\Importable;
- use Maatwebsite\Excel\Concerns\ToCollection;
- use Maatwebsite\Excel\Concerns\WithMappedCells;
- use Maatwebsite\Excel\Concerns\WithValidation;
- class Keyword implements ToCollection
- {
- use Importable;
- protected $projectId;
- public function __construct($projectId)
- {
- $this->projectId = $projectId;
- }
- // const FIELD_MAP = [
- // 0 => 'sn',
- // 1 => 'level',
- // 2 => 'keyword',
- // 3 => 'chinese_keyword',
- // 4 => 'page_title',
- // 5 => 'page_url',
- // 6 => 'search_num',
- // ];
- const FIELD_MAP = [
- 0 => 'keyword',
- 1 => 'chinese_keyword',
- 2 => 'page_url',
- 3 => 'search_num',
- ];
- public function collection(Collection $collections)
- {
- $all = collect([]);
- $rankConnection = DB::connection('rank');
- $maxSn = $rankConnection->table('project_keyword')->where(['project_id' => $this->projectId])->max('sn') ?? 0;
- foreach ($collections as $key => $collection) {
- if ($key < 1) {
- continue;
- }
- $temp = $this->fieldMap($collection);
- if (mb_strlen($temp['keyword']) < 1) {
- continue;
- }
- $maxSn++;
- $all->push(($temp + ['sn' => $maxSn]));
- }
- $allItems = $all->chunk(2500)->toArray();
- foreach ($allItems as $item) {
- $rankConnection->table('project_keyword')->insert($item);
- }
- }
- public function fieldMap($collect): array
- {
- $result = [
- 'google_rank' => 9999,
- 'allintitle' => -1,
- 'sync_time' => strtotime('+1 day'),
- 'project_id' => $this->projectId
- ];
- foreach ($collect as $inx => $val) {
- if (empty(self::FIELD_MAP[$inx])) continue;
- if (in_array(self::FIELD_MAP[$inx], ['sn', 'search_num'])) {
- $result[self::FIELD_MAP[$inx]] = intval($val);
- } else {
- $result[self::FIELD_MAP[$inx]] = $val;
- }
- }
- return $result;
- }
- }
|