BikesImport.php 1.6 KB

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