InnerDeviceImport.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Imports;
  3. use App\Exceptions\ImportError;
  4. use App\Models\DeviceName;
  5. use App\Models\InnerDevice;
  6. use App\Models\InnerDeviceNamesModel;
  7. use App\Models\Option;
  8. use App\Models\Project;
  9. use App\Models\ProjectZone;
  10. use App\Models\Road;
  11. use App\Models\Spec;
  12. use App\Models\WorkPoint;
  13. use App\Models\Zone;
  14. use Illuminate\Support\Collection;
  15. use Illuminate\Support\Facades\Auth;
  16. use Illuminate\Support\Facades\Log;
  17. use Maatwebsite\Excel\Concerns\ToCollection;
  18. use PhpOffice\PhpSpreadsheet\Shared\Date;
  19. class InnerDeviceImport implements ToCollection
  20. {
  21. protected $model;
  22. protected $zone;
  23. protected $road;
  24. protected $project_zone;
  25. public function __construct()
  26. {
  27. $this->model = new InnerDevice();
  28. }
  29. function excelTime($date, $time = false) {
  30. if(function_exists('GregorianToJD')){
  31. if (is_numeric( $date )) {
  32. $jd = GregorianToJD( 1, 1, 1970 );
  33. $gregorian = JDToGregorian( $jd + intval ( $date ) - 25569 );
  34. $date = explode( '/', $gregorian );
  35. $date_str = str_pad( $date [2], 4, '0', STR_PAD_LEFT )
  36. ."-". str_pad( $date [0], 2, '0', STR_PAD_LEFT )
  37. ."-". str_pad( $date [1], 2, '0', STR_PAD_LEFT )
  38. . ($time ? " 00:00:00" : '');
  39. return $date_str;
  40. }
  41. }else{
  42. $date=$date>25568?$date+1:25569;
  43. /*There was a bug if Converting date before 1-1-1970 (tstamp 0)*/
  44. $ofs=(70 * 365 + 17+2) * 86400;
  45. $date = date("Y-m-d",($date * 86400) - $ofs).($time ? " 00:00:00" : '');
  46. }
  47. // dd($date);
  48. return $date;
  49. }
  50. public function collection(Collection $rows)
  51. {
  52. if(count($rows) <= 1) {
  53. /** @noinspection PhpUnhandledExceptionInspection */
  54. return false;
  55. };
  56. foreach ($rows as $key => $row) {
  57. if($key == 0) continue;
  58. if(empty($row[0])&&empty($row[1])&&empty($row[2]))
  59. {
  60. break;
  61. }
  62. //设备名称
  63. if ($row[1])
  64. {
  65. $device_name = InnerDeviceNamesModel::firstOrCreate([
  66. 'name' => $row[1],
  67. 'sort' => 1,
  68. 'status' => 1
  69. ]);
  70. }
  71. $spec_name = $row[2];
  72. $project = null;
  73. $work_point = null;
  74. //项目
  75. if($row[7]) {
  76. $project = Project::firstOrCreate([
  77. 'name' => $row[7],
  78. 'active' => 1
  79. ]);
  80. }
  81. // //工点
  82. // if($project && $row[8]) {
  83. // $work_point = WorkPoint::firstOrCreate([
  84. // 'project_id' => $project->id,
  85. // 'name' => $row[8]
  86. // ]);
  87. // }
  88. $status = Option::where([
  89. ['table', '=', 'inner_devices'],
  90. ['column', '=', 'status'],
  91. ['name', '=', $row[8]]
  92. ])->first();
  93. $data = [
  94. 'number' => $row[0],
  95. 'device_name_id' => $device_name ? $device_name->id : '',
  96. 'spec_name' => $spec_name ? $spec_name : '',
  97. 'produce_date' => $this->excelTime($row[3]),
  98. 'buy_origin' => $row[4],
  99. 'manufacturer' => $row[5],
  100. 'shape' => $row[6],
  101. 'project_id' => $project ? $project->id : '',
  102. // 'work_point_id' => $work_point ? $work_point->id : '',
  103. 'status' => $status ? $status->id : ''
  104. ];
  105. $this->model->create($data);
  106. }
  107. return true;
  108. }
  109. }