WorkPointImport.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 WorkPointImport 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 WorkPoint();
  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]))
  39. {
  40. break;
  41. }
  42. $project_id = null;
  43. $work_point = null;
  44. //项目
  45. if ($row[0])
  46. {
  47. $project_id = Project::firstOrCreate([
  48. 'name' => $row[0],
  49. ],[
  50. 'name' => $row[0],
  51. 'active' => 1
  52. ]);
  53. }
  54. //项目挂钩工点
  55. if ($row[1])
  56. {
  57. if($project_id && $row[1]) {
  58. $work_point = WorkPoint::firstOrCreate([
  59. 'project_id' => $project_id->id,
  60. 'name' => $row[1],
  61. ],[
  62. 'project_id' => $project_id->id,
  63. 'name' => $row[1],
  64. 'status' => 1
  65. ]);
  66. }
  67. }
  68. }
  69. return true;
  70. }
  71. }