OwnerLogisticFeeDetailService.php 3.8 KB

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