SpecImport.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App\Imports;
  3. use App\Exceptions\ImportError;
  4. use App\Models\Device;
  5. use App\Models\DeviceName;
  6. use App\Models\InnerDevice;
  7. use App\Models\InnerDeviceNamesModel;
  8. use App\Models\Option;
  9. use App\Models\Project;
  10. use App\Models\ProjectZone;
  11. use App\Models\Road;
  12. use App\Models\Spec;
  13. use App\Models\WorkPoint;
  14. use App\Models\Zone;
  15. use Illuminate\Support\Collection;
  16. use Illuminate\Support\Facades\Auth;
  17. use Illuminate\Support\Facades\Log;
  18. use Maatwebsite\Excel\Concerns\ToCollection;
  19. use PhpOffice\PhpSpreadsheet\Shared\Date;
  20. class SpecImport implements ToCollection
  21. {
  22. protected $model;
  23. protected $zone;
  24. protected $road;
  25. protected $project_zone;
  26. public function __construct()
  27. {
  28. $this->model = new Spec();
  29. }
  30. public function collection(Collection $rows)
  31. {
  32. if(count($rows) <= 1) {
  33. /** @noinspection PhpUnhandledExceptionInspection */
  34. return false;
  35. };
  36. foreach ($rows as $key => $row) {
  37. if($key == 0) continue;
  38. if(empty($row[0])&&empty($row[1])&&empty($row[2]))
  39. {
  40. break;
  41. }
  42. //设备类型
  43. if ($row[1] == "项目自填(必须填写)" || $row[2] == "项目自填(必须填写)")
  44. {
  45. continue;
  46. }
  47. $device_id = null;
  48. $device_name_id = null;
  49. //设备类型
  50. if ($row[0])
  51. {
  52. $device_id = Device::firstOrCreate([
  53. 'name' => $row[0],
  54. 'sort' => 1,
  55. ]);
  56. }
  57. //设备名称
  58. if ($row[1])
  59. {
  60. if($device_id && $row[1]) {
  61. $device_name_id = DeviceName::firstOrCreate([
  62. 'device_id' => $device_id->id,
  63. 'name' => $row[1],
  64. 'sort' => 1,
  65. 'status' => 1
  66. ]);
  67. }
  68. }
  69. //规格型号
  70. if ($row[2])
  71. {
  72. $data = [
  73. 'name' => $row[2],
  74. 'device_name_id' => $device_name_id ? $device_name_id->id : '',
  75. 'device_id' => $device_id ? $device_id->id : '',
  76. 'sort' => 1
  77. ];
  78. $this->model->create($data);
  79. }
  80. }
  81. return true;
  82. }
  83. }