KeywordExtend.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace App\Imports;
  3. use App\Http\Models\PrKeywordExtend;
  4. use Illuminate\Support\Collection;
  5. use Illuminate\Support\Facades\DB;
  6. use Maatwebsite\Excel\Concerns\Importable;
  7. use Maatwebsite\Excel\Concerns\ToCollection;
  8. class KeywordExtend implements ToCollection
  9. {
  10. use Importable;
  11. protected $siteId;
  12. protected $oldId;
  13. public function __construct($siteId, $oldId)
  14. {
  15. $this->siteId = $siteId;
  16. $this->oldId = $oldId;
  17. }
  18. //
  19. // const FIELD_MAP = [
  20. // 0 => 'keyword',
  21. // 1 => 'chinese_keyword',
  22. // 2 => 'page_url',
  23. // 3 => 'search_num',
  24. // ];
  25. const FIELD_MAP = [
  26. 0 => 'keyword',
  27. 1 => 'search_num',
  28. 2 => 'index_num',
  29. 3 => 'land_page',
  30. ];
  31. public function collection(Collection $collections)
  32. {
  33. $all = collect([]);
  34. $rankKeyword=collect([]);
  35. $rankConnection = DB::connection('rank');
  36. $maxSn = $rankConnection->table('project_keyword')->where(['project_id' => $this->oldId])->max('sn') ?? 0;
  37. foreach ($collections as $key => $collection) {
  38. if ($key < 1) {
  39. continue;
  40. }
  41. $temp = $this->fieldMap($collection);
  42. if (mb_strlen($temp['keyword']) < 1) {
  43. continue;
  44. }
  45. $all->push($temp);
  46. $rank=$this->fieldMapRank($collection);
  47. $maxSn++;
  48. $rankKeyword->push($rank+['sn' => $maxSn]);
  49. }
  50. $allItems = $all->chunk(2500)->toArray();
  51. foreach ($allItems as $item) {
  52. PrKeywordExtend::query()->insert($item);
  53. }
  54. $allRank=$rankKeyword->chunk(2500)->toArray();
  55. foreach ($allRank as $item) {
  56. $rankConnection->table('project_keyword')->insert($item);
  57. }
  58. }
  59. public function fieldMapRank($collect): array
  60. {
  61. $result = [
  62. 'google_rank' => 9999,
  63. 'allintitle' => -1,
  64. 'sync_time' => strtotime('+1 day'),
  65. 'project_id' => $this->oldId
  66. ];
  67. foreach ($collect as $inx => $val) {
  68. if (empty(self::FIELD_MAP[$inx])) continue;
  69. if ($inx==0) {
  70. $result['keyword']=$val;
  71. $result['chinese_keyword']='';
  72. }
  73. if ($inx==1) {
  74. $result['search_num']=intval($val);
  75. }
  76. if ($inx==3) {
  77. $result['page_url']=$val;
  78. }
  79. }
  80. return $result;
  81. }
  82. public function fieldMap($collect): array
  83. {
  84. // $ym = date('Ym', strtotime('first day of -1 month'));
  85. $ym = date('Ym');
  86. $result = [
  87. 'old_id' => $this->oldId,
  88. 'ym' => $ym,
  89. 'site_id' => $this->siteId,
  90. 'created_at' => date('Y-m-d H:i:s'),
  91. 'updated_at' => date('Y-m-d H:i:s'),
  92. ];
  93. foreach ($collect as $inx => $val) {
  94. if (empty(self::FIELD_MAP[$inx])) continue;
  95. if (in_array(self::FIELD_MAP[$inx], ['index_num', 'search_num'])) {
  96. $result[self::FIELD_MAP[$inx]] = intval($val);
  97. } else {
  98. $result[self::FIELD_MAP[$inx]] = $val;
  99. }
  100. }
  101. return $result;
  102. }
  103. }