SpecImport.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. $device_id = null;
  44. $device_name_id = null;
  45. if ($row[1])
  46. {
  47. $device_id = Device::firstOrCreate([
  48. 'name' => $row[1],
  49. 'sort' => 1,
  50. ]);
  51. }
  52. //设备名称
  53. if ($row[2] == "项目自填(必须填写)")
  54. {
  55. continue;
  56. }else
  57. {
  58. if($device_id && $row[2]) {
  59. $device_name_id = DeviceName::firstOrCreate([
  60. 'device_id' => $device_id->id,
  61. 'name' => $row[2],
  62. 'sort' => 1,
  63. 'status' => 1
  64. ]);
  65. }
  66. }
  67. $name = null;
  68. if ($row[3] == "项目自填(必须填写)")
  69. {
  70. $name = '';
  71. }
  72. else{
  73. $name = $row[3];
  74. }
  75. $data = [
  76. 'name' => $name,
  77. 'device_name_id' => $device_name_id ? $device_name_id->id : '',
  78. 'device_id' => $device_id ? $device_id->id : '',
  79. 'sort' => 1
  80. ];
  81. $this->model->create($data);
  82. }
  83. return true;
  84. }
  85. }