OptimizePageImport.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Imports;
  3. use App\Http\Models\SiteOptimizePage;
  4. use Illuminate\Support\Collection;
  5. use Maatwebsite\Excel\Concerns\Importable;
  6. use Maatwebsite\Excel\Concerns\ToCollection;
  7. class OptimizePageImport implements ToCollection
  8. {
  9. use Importable;
  10. protected $siteId;
  11. public function __construct($siteId)
  12. {
  13. $this->siteId = $siteId;
  14. }
  15. const FIELD_MAP = [
  16. 0 => 'keyword',
  17. 1 => 'url',
  18. 2 => 'is_optimize',
  19. ];
  20. public function collection(Collection $collections)
  21. {
  22. $insertData = [];
  23. foreach ($collections as $key => $collection) {
  24. if ($key < 1) {
  25. continue;
  26. }
  27. $temp = $this->fieldMap($collection);
  28. $insertData[] = $temp;
  29. }
  30. SiteOptimizePage::query()->insert($insertData);
  31. }
  32. public function fieldMap($collect): array
  33. {
  34. $result = [
  35. 'created_at' => date('Y-m-d H:i:s'),
  36. 'updated_at' => date('Y-m-d H:i:s'),
  37. 'site_id' => $this->siteId
  38. ];
  39. foreach ($collect as $inx => $val) {
  40. if (empty(self::FIELD_MAP[$inx])) continue;
  41. if (in_array(self::FIELD_MAP[$inx], ['is_optimize'])) {
  42. $result[self::FIELD_MAP[$inx]] = ($val == "是" ? 1 : 0);
  43. } else {
  44. $result[self::FIELD_MAP[$inx]] = $val;
  45. }
  46. }
  47. return $result;
  48. }
  49. }