OwnerLogisticFeeDetailService.php 3.8 KB

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