OwnerLogisticFeeReportService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Services;
  3. use App\OwnerLogisticFeeDetail;
  4. use App\Traits\ServiceAppAop;
  5. use App\OwnerLogisticFeeReport;
  6. use Carbon\Carbon;
  7. use Illuminate\Contracts\Pagination\LengthAwarePaginator;
  8. use Illuminate\Database\Eloquent\Builder;
  9. use Illuminate\Database\Eloquent\Collection;
  10. class OwnerLogisticFeeReportService
  11. {
  12. const TYPE = '快递费-合计';
  13. use ServiceAppAop;
  14. protected $modelClass = OwnerLogisticFeeReport::class;
  15. private $reportDate;
  16. /** @var $archiveService OwnerBillReportArchiveService */
  17. private $archiveService;
  18. /**
  19. * 生成报表数据
  20. * 如果参数$counting_month为空 统计上一个月的
  21. * 如果参数$counting_month为2021-01-01 则统计2021-01-01 -- 2021-01-31之间的数据
  22. * @param null $counting_month 统计月份,默认统计上个月的 2021-05-01
  23. */
  24. public function recordReport($counting_month = null)
  25. {
  26. if (is_null($counting_month)) {
  27. //默认统计上个月的数据
  28. $counting_month = now()->subMonth()->startOfMonth()->toDateTimeString();
  29. }
  30. $this->reportDate = $counting_month;
  31. $start = $this->reportDate;
  32. $end = Carbon::parse($this->reportDate)->endOfMonth()->toDateTimeString();
  33. $ownerLogisticFeeDetails = OwnerLogisticFeeDetail::query()
  34. ->selectRaw("logistic_id,province,DATE_FORMAT(created_at,'%Y-%m-%d') as counted_date,initial_weight,initial_weight_price,count(1) as initial_amount,additional_price,additional_weight,sum(additional_weigh_weight) as additional_amount,created_at,owner_id")
  35. ->whereBetween('created_at', [$start, $end])
  36. ->groupBy('initial_weight', 'initial_weight_price', 'additional_price', 'additional_weight', 'logistic_id', 'province', 'counted_date', 'owner_id')
  37. ->get();
  38. $ownerLogisticFeeReportArray = [];
  39. foreach ($ownerLogisticFeeDetails as $ownerLogisticFeeDetail) {
  40. $ownerLogisticFeeReportArray[] = [
  41. 'logistic_id' => $ownerLogisticFeeDetail->logistic_id,
  42. 'province' => $ownerLogisticFeeDetail->province,
  43. 'counted_date' => Carbon::parse($ownerLogisticFeeDetail->counted_date)->firstOfMonth()->toDateString(),
  44. 'initial_weight' => $ownerLogisticFeeDetail->initial_weight,
  45. 'initial_weight_price' => $ownerLogisticFeeDetail->initial_weight_price,
  46. 'initial_amount' => $ownerLogisticFeeDetail->initial_amount,
  47. 'additional_weight' => $ownerLogisticFeeDetail->additional_weight,
  48. 'additional_price' => $ownerLogisticFeeDetail->additional_price,
  49. 'additional_amount' => $ownerLogisticFeeDetail->additional_amount,
  50. 'fee' => ($ownerLogisticFeeDetail['initial_weight_price'] * $ownerLogisticFeeDetail['initial_amount']) + ($ownerLogisticFeeDetail['additional_amount'] * $ownerLogisticFeeDetail['additional_price']),
  51. 'owner_id' => $ownerLogisticFeeDetail->owner_id,
  52. ];
  53. }
  54. OwnerLogisticFeeReport::query()->insertOrIgnore($ownerLogisticFeeReportArray);
  55. }
  56. /**
  57. * 订单统计分页查询
  58. * @param $owner_id
  59. * @param $counting_month string 查询的年月 2021-05-01
  60. * @param $paginateParams
  61. * @return LengthAwarePaginator
  62. */
  63. private function getPagination($owner_id, string $counting_month, $paginateParams): LengthAwarePaginator
  64. {
  65. return $this->getSql($owner_id, $counting_month)
  66. ->paginate($paginateParams['paginate'] ?? 50);
  67. }
  68. public function get(array $kvPairs): array
  69. {
  70. $this->archiveService = app('OwnerBillReportArchiveService');
  71. if ($this->archiveService->isArchived($kvPairs['counting_month'], $kvPairs['owner_id'], $kvPairs['type']) == 1) {
  72. //查询存档数据
  73. $archived = $this->archiveService->get($kvPairs);
  74. $reports =collect($archived->information['reports']);
  75. $recordTotal = $archived->information['recordTotal'];
  76. } else {
  77. $recordTotal = $this->getRecordTotal($kvPairs['owner_id'], $kvPairs['counting_month']);
  78. $reports = $this->getPagination($kvPairs['owner_id'], $kvPairs['counting_month'], $kvPairs['paginateParams']);
  79. }
  80. return array($reports, $recordTotal);
  81. }
  82. /**
  83. * 订单统计查询
  84. * @param $owner_id
  85. * @param $counting_month string 查询的年月 2021-05-01
  86. * @return Builder[]|Collection
  87. */
  88. public function getRecords($owner_id, string $counting_month)
  89. {
  90. return $this->getSql($owner_id, $counting_month)->get();
  91. }
  92. /**
  93. * 订单总计查询
  94. * @param $owner_id
  95. * @param $counting_month string 查询的年月 2021-05-01
  96. * @return array
  97. */
  98. public function getRecordTotal($owner_id, string $counting_month): array
  99. {
  100. $logistic_fee = OwnerLogisticFeeReport::query()
  101. ->where('owner_id', $owner_id)
  102. ->where('counted_date', $counting_month)
  103. ->sum('fee');
  104. $order_count = (int)OwnerLogisticFeeReport::query()
  105. ->where('owner_id', $owner_id)
  106. ->where('counted_date', $counting_month)
  107. ->sum('initial_amount');
  108. return [
  109. 'logistic_fee' => $logistic_fee,
  110. 'order_count' => $order_count,
  111. ];
  112. }
  113. /**
  114. * @param $owner_id
  115. * @param string $counting_month
  116. * @return Builder
  117. */
  118. public function getSql($owner_id, string $counting_month): Builder
  119. {
  120. return OwnerLogisticFeeReport::query()
  121. ->with('logistic:id,name')
  122. ->where('owner_id', $owner_id)
  123. ->where('counted_date', $counting_month)
  124. ->orderByDesc('logistic_id')
  125. ->orderByDesc('province');
  126. }
  127. }