OwnerLogisticFeeDetailService.php 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Services;
  3. use App\OwnerFeeDetail;
  4. use App\Traits\ServiceAppAop;
  5. use App\OwnerLogisticFeeDetail;
  6. use Carbon\Carbon;
  7. use Illuminate\Database\Eloquent\Builder;
  8. use Illuminate\Support\Collection;
  9. class OwnerLogisticFeeDetailService
  10. {
  11. use ServiceAppAop;
  12. /**
  13. * @var $modelClass OwnerLogisticFeeDetail
  14. */
  15. protected $modelClass = OwnerLogisticFeeDetail::class;
  16. //插入数据 ServiceAppAop的 insert 方法 支持批量
  17. /**
  18. * 根据货主查询 和时间段查询
  19. * @param string $owner_id
  20. * @param string $start
  21. * @param string $end
  22. * @return array
  23. */
  24. public function getDetails(string $owner_id, string $start, string $end): array
  25. {
  26. $ownerFeeDetailIds = OwnerFeeDetail::query()->selectRaw('id')
  27. ->where('type', '发货')
  28. ->where('outer_table_name', 'orders')
  29. ->whereHas('logistic', function (Builder $query) {
  30. $query->where('type', '快递');
  31. })
  32. ->where('owner_id', $owner_id)
  33. ->where('worked_at', '>=', Carbon::parse($start)->startOfDay())
  34. ->where('worked_at', '<=', Carbon::parse($end)->endOfDay())->pluck('id');
  35. $ownerLogisticFeeDetails = OwnerLogisticFeeDetail::query()->with(['ownerFeeDetail.logistic', 'ownerFeeDetailLogistic'])->whereIn('owner_fee_detail_id', $ownerFeeDetailIds)->paginate();
  36. dd($ownerLogisticFeeDetails);
  37. // $ownerFeeDetails = OwnerFeeDetail::query()
  38. // ->with(['ownerLogisticFeeDetail:id,logistic_bill,initial_weight_price,additional_price', 'items.logistic:id,name','items.ownerLogisticFeeDetail:id,logistic_bill,initial_weight_price,additional_price', 'logistic:id,name,type'])
  39. // ->where('type', '发货')
  40. // ->where('outer_table_name', 'orders')
  41. // ->whereHas('logistic', function (Builder $query) {
  42. // $query->where('type', '快递');
  43. // })
  44. // ->where('owner_id', $owner_id)
  45. // ->where('worked_at', '>=', Carbon::parse($start)->startOfDay())
  46. // ->where('worked_at', '<=', Carbon::parse($end)->endOfDay())
  47. // ->get();
  48. // $result = [];
  49. // foreach ($ownerFeeDetails as $ownerFeeDetail) {
  50. // if ($ownerFeeDetail->items->count()==0) {//只有一个包裹
  51. // $result[] = [
  52. // 'logistic_name' => $ownerFeeDetail->logistic->name ?? '',//快递公司
  53. // 'province' => $ownerFeeDetail->province,//省份
  54. // 'logistic_bill' => $ownerFeeDetail->logistic_bill ?? '',//快递单号
  55. // 'weight' => $ownerFeeDetail->weight,//重量
  56. // 'logistic_fee' => $ownerFeeDetail->logistic_fee,//快递费
  57. // 'initial_weight_price' => $ownerFeeDetail->ownerLogisticFeeDetail->initial_weight_price ?? '',//首重价格
  58. // 'additional_price' => $ownerFeeDetail->ownerLogisticFeeDetail->additional_price ?? '',//续重价格
  59. // ];
  60. // } else {//多个包裹
  61. // foreach ($ownerFeeDetail->items as $ownerFeeDetailLogistic) {
  62. // $result[] = [
  63. // 'logistic_name' => $ownerFeeDetail->logistic->name ?? '',//快递公司
  64. // 'province' => $ownerFeeDetail->province,//省份
  65. // 'logistic_bill' => $ownerFeeDetailLogistic->logistic_bill,//快递单号
  66. // 'weight' => $ownerFeeDetailLogistic->weight,//重量
  67. // 'logistic_fee' => $ownerFeeDetailLogistic->logistic_fee,//快递费
  68. // 'initial_weight_price' => $ownerFeeDetailLogistic->ownerLogisticFeeDetail->initial_weight_price ?? '',//首重价格
  69. // 'additional_price' => $ownerFeeDetailLogistic->ownerLogisticFeeDetail->additional_price ?? '',//续重价格
  70. // ];
  71. // }
  72. // }
  73. // }
  74. $result = [];
  75. foreach ($ownerLogisticFeeDetails->items() as $ownerLogisticFeeDetail) {
  76. $result[] = [
  77. 'logistic_name' => $ownerLogisticFeeDetail->ownerFeeDetail->logistic->name ?? '',//快递公司
  78. 'province' => $ownerLogisticFeeDetail->ownerFeeDetail->province,//省份
  79. 'logistic_bill' => $ownerLogisticFeeDetail->logistic_bill ?? '',//快递单号
  80. 'weight' => $ownerLogisticFeeDetail->ownerFeeDetailLogistic->weight,//重量
  81. 'logistic_fee' => $ownerLogisticFeeDetail->ownerFeeDetailLogistic->logistic_fee,//快递费
  82. 'initial_weight_price' => $ownerLogisticFeeDetail->initial_weight_price ?? '',//首重价格
  83. 'additional_price' => $ownerLogisticFeeDetail->additional_price ?? '',//续重价格
  84. ];
  85. }
  86. return $result;
  87. }
  88. }