OwnerLogisticFeeDetailService.php 4.2 KB

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