BikesImport.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App\Imports;
  3. use App\Models\AdminUserArea;
  4. use App\Models\Area;
  5. use App\Models\Bike;
  6. use App\Utils\Admin;
  7. use Illuminate\Support\Facades\Log;
  8. use Maatwebsite\Excel\Concerns\OnEachRow;
  9. use Maatwebsite\Excel\Concerns\ToModel;
  10. use Maatwebsite\Excel\Row;
  11. class BikesImport implements OnEachRow
  12. {
  13. /**
  14. * @param array $row
  15. *
  16. * @return \Illuminate\Database\Eloquent\Model|null
  17. */
  18. public function onRow(Row $row)
  19. {
  20. // Log::info($row);
  21. $admin_id = Admin::user()->id;
  22. $area = Area::query()->where('admin_id', $admin_id)->first();
  23. // 投放区域id 在导入excle时 默认为改该管理员第一个创建得区域id
  24. $put_area_id = 0;
  25. if(!empty($area)){
  26. $put_area_id = $area->id;
  27. }else{
  28. // 如果是普通管理员 则显示其自己管理的区域id
  29. $area_id = AdminUserArea::query()->where('admin_id',$admin_id)->first('area_id');
  30. $put_area_id = $area_id->area_id ?? 0;
  31. }
  32. $rowIndex = $row->getIndex();
  33. $row = $row->toArray();
  34. return Bike::firstOrCreate([
  35. 'bike_no' => $row[0],//车辆编号
  36. // 'box_no' => $row[1],//控制设备
  37. 'put_area_id' => $put_area_id
  38. ]);
  39. }
  40. }