Keine Beschreibung

StarInformationImport.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Imports;
  3. use App\Models\Sys\SysStarInformation;
  4. use Maatwebsite\Excel\Concerns\ToModel;
  5. use Maatwebsite\Excel\Concerns\WithStartRow;
  6. use Maatwebsite\Excel\Concerns\WithUpserts;
  7. class StarInformationImport implements ToModel, WithUpserts, WithStartRow
  8. {
  9. public $ptype;
  10. public $groupId;
  11. public $sysUserId;
  12. private $rows = 0;
  13. public function __construct($ptype, $groupId, $sysUserId)
  14. {
  15. $this->ptype = $ptype;
  16. $this->groupId = $groupId;
  17. $this->sysUserId = $sysUserId;
  18. }
  19. /**
  20. * @param array $row
  21. *
  22. * @return \Illuminate\Database\Eloquent\Model|null
  23. */
  24. public function model(array $row)
  25. {
  26. // 跳过空数据
  27. if (empty($row[3]) || !is_numeric($row[3])) return null;
  28. $fieldParam = [
  29. 'group_id' => $this->groupId,
  30. 'ptype' => $this->ptype,
  31. 'sys_user_id' => $this->sysUserId,
  32. 'platform' => $row[0],
  33. 'cat_id' => isset($row[1]) ? hash('fnv164', $row[1], false) : null,
  34. 'category' => $row[1],
  35. 'nickname' => $row[2],
  36. 'accid' => $row[3],
  37. 'fans' => $row[4],
  38. ];
  39. switch ($this->ptype) {
  40. case 1:
  41. case 2:
  42. $fieldParam['quoted_price_1'] = $row[5];
  43. $fieldParam['ins_id'] = isset($row[6]) ? hash('fnv164', $row[6], false) : null;
  44. $fieldParam['institution'] = $row[6];
  45. $fieldParam['contact'] = $row[7];
  46. $fieldParam['home_link'] = $row[8];
  47. $fieldParam['cooperate_type'] = $row[9];
  48. $fieldParam['gender_ratio'] = $row[13];
  49. $fieldParam['age_ratio'] = $row[14];
  50. $fieldParam['avg_play'] = $row[10];
  51. $fieldParam['avg_interact'] = $row[11];
  52. $fieldParam['cpm'] = $row[12];
  53. break;
  54. case 3:
  55. $fieldParam['quoted_price_1'] = $row[6];
  56. $fieldParam['quoted_price_2'] = $row[5];
  57. $fieldParam['ins_id'] = isset($row[7]) ? hash('fnv164', $row[7], false) : null;
  58. $fieldParam['institution'] = $row[7];
  59. $fieldParam['contact'] = $row[8];
  60. $fieldParam['home_link'] = $row[9];
  61. $fieldParam['cooperate_type'] = $row[10];
  62. $fieldParam['gender_ratio'] = $row[14];
  63. $fieldParam['age_ratio'] = $row[15];
  64. $fieldParam['med_read'] = $row[11];
  65. $fieldParam['med_interact'] = $row[12];
  66. $fieldParam['interact_rate'] = $row[13];
  67. break;
  68. default:
  69. return null;
  70. }
  71. ++$this->rows;
  72. return new SysStarInformation($fieldParam);
  73. }
  74. /**
  75. * @return string|array
  76. */
  77. public function uniqueBy()
  78. {
  79. return ['group_id', 'ins_id'];
  80. }
  81. /**
  82. * @return int
  83. */
  84. public function startRow(): int
  85. {
  86. return 2;
  87. }
  88. public function getRowCount(): int
  89. {
  90. return $this->rows;
  91. }
  92. }