GoogleTrendsKeywordImport.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Imports;
  3. use App\Http\Models\GoogleTrendsKeyword;
  4. use Illuminate\Support\Collection;
  5. use Maatwebsite\Excel\Concerns\Importable;
  6. use Maatwebsite\Excel\Concerns\ToCollection;
  7. class GoogleTrendsKeywordImport implements ToCollection
  8. {
  9. use Importable;
  10. public function collection(Collection $collections)
  11. {
  12. foreach ($collections as $key => $collection) {
  13. if ($key < 1) {
  14. continue;
  15. }
  16. $enable = 0;
  17. $type = $this->replace(trim($collection[1]));
  18. if ($type == '禁用') {
  19. $enable = 1;
  20. }
  21. $naturalRankingPercentage = '';
  22. if (is_numeric($collection[5])) {
  23. $naturalRankingPercentage = round($collection[5] * 100, 2) . '%';
  24. }
  25. $proportionOfBidding = '';
  26. if (is_numeric($collection[6])) {
  27. $proportionOfBidding = round($collection[6] * 100, 2) . '%';
  28. }
  29. $simultaneousProportion = '';
  30. if (is_numeric($collection[7])) {
  31. $simultaneousProportion = round($collection[7] * 100, 2) . '%';
  32. }
  33. $proportionNotClicked = '';
  34. if (is_numeric($collection[8])) {
  35. $proportionNotClicked = round($collection[8] * 100, 2) . '%';
  36. }
  37. $mobileDevices = '';
  38. if (is_numeric($collection[9])) {
  39. $mobileDevices = round($collection[9] * 100, 2) . '%';
  40. }
  41. $tabletPc = '';
  42. if (is_numeric($collection[10])) {
  43. $tabletPc = round($collection[10] * 100, 2) . '%';
  44. }
  45. $desktopDevice = '';
  46. if (is_numeric($collection[11])) {
  47. $desktopDevice = round($collection[11] * 100, 2) . '%';
  48. }
  49. $data = [
  50. 'keyword' => $collection[0],
  51. 'enable' => $enable,
  52. 'monthly_searches' => $collection[2] ?? '',
  53. 'mumber_of_search_results' => $collection[3] ?? '',
  54. 'competition_index' => $collection[4] ?? '',
  55. 'natural_ranking_percentage' => $naturalRankingPercentage ?? '',
  56. 'proportion_of_bidding' => $proportionOfBidding ?? '',
  57. 'simultaneous_proportion' => $simultaneousProportion ?? '',
  58. 'proportion_not_clicked' => $proportionNotClicked ?? '',
  59. 'mobile_devices' => $mobileDevices ?? '',
  60. 'tablet_pc' => $tabletPc ?? '',
  61. 'desktop_device' => $desktopDevice ?? '',
  62. 'order_conditions' => $collection[12] ?? '',
  63. 'industry_difficulty' => $collection[13] ?? '',
  64. 'package_suggestion' => $collection[14] ?? '',
  65. 'product_main_dimension' => $this->replace(trim($collection[15])),
  66. 'product_secondary_dimension' => $this->replace(trim($collection[16])),
  67. 'website_function' => $this->replace(trim($collection[17])),
  68. 'seo_code_optimization' => $this->replace(trim($collection[18])),
  69. 'keyword_screening_suggestions' => $this->replace(trim($collection[19])),
  70. 'keyword_layout_suggestions' => $this->replace(trim($collection[20])),
  71. 'img' => $this->replace(trim($collection[21])),
  72. 'parameter_table' => $this->replace(trim($collection[22])),
  73. 'product_copywriting' => $this->replace(trim($collection[23])),
  74. 'pdf_resources' => $this->replace(trim($collection[24])),
  75. 'video_resources' => $this->replace(trim($collection[25])),
  76. 'external_link_assessment' => $this->replace(trim($collection[26])),
  77. 'social_promotion' => $this->replace(trim($collection[27])),
  78. 'industry_difficulties_analysis' => $this->replace(trim($collection[28])),
  79. 'website' => $this->replace(trim($collection[29])),
  80. 'remarks' => $this->replace(trim($collection[30])),
  81. 'created_at' => date('Y-m-d H:i:s'),
  82. 'user_id' => auth()->user()->id ?? '',
  83. ];
  84. $result = GoogleTrendsKeyword::query()->where('keyword', $collection[0])->first();
  85. if (empty($result)) {
  86. GoogleTrendsKeyword::query()->insert($data);
  87. } else {
  88. $data['user_id'] = $result->user_id;
  89. GoogleTrendsKeyword::query()->where('keyword', $collection[0])->update($data);
  90. }
  91. }
  92. }
  93. public function replace($str)
  94. {
  95. return str_replace(["\r\n", "\r", "\n"], "", $str);
  96. }
  97. }