OwnerLogisticFeeReportService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. use ServiceAppAop;
  13. protected $modelClass = OwnerLogisticFeeReport::class;
  14. private $reportDate;
  15. /**
  16. * 生成报表数据
  17. * 如果参数$date为空 统计上一个月的
  18. * 如果参数$date为2021-01-01 则统计2021-01-01 -- 2021-01-31之间的数据
  19. * @param null $date 统计月份,默认统计上个月的 2021-05-01
  20. */
  21. public function recordReport($date = null)
  22. {
  23. if (is_null($date)) {
  24. //默认统计上个月的数据
  25. $date = now()->subMonth()->startOfMonth()->toDateTimeString();
  26. }
  27. $this->reportDate = $date;
  28. $start = $this->reportDate;
  29. $end = Carbon::parse($this->reportDate)->endOfMonth()->toDateTimeString();
  30. $ownerLogisticFeeDetails = OwnerLogisticFeeDetail::query()
  31. ->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")
  32. ->whereBetween('created_at', [$start, $end])
  33. ->groupBy('initial_weight', 'initial_weight_price', 'additional_price', 'additional_weight', 'logistic_id', 'province', 'counted_date', 'owner_id')
  34. ->get();
  35. $ownerLogisticFeeReportArray = [];
  36. foreach ($ownerLogisticFeeDetails as $ownerLogisticFeeDetail) {
  37. $ownerLogisticFeeReportArray[] = [
  38. 'logistic_id' => $ownerLogisticFeeDetail->logistic_id,
  39. 'province' => $ownerLogisticFeeDetail->province,
  40. 'counted_date' => Carbon::parse($ownerLogisticFeeDetail->counted_date)->firstOfMonth()->toDateString(),
  41. 'initial_weight' => $ownerLogisticFeeDetail->initial_weight,
  42. 'initial_weight_price' => $ownerLogisticFeeDetail->initial_weight_price,
  43. 'initial_amount' => $ownerLogisticFeeDetail->initial_amount,
  44. 'additional_weight' => $ownerLogisticFeeDetail->additional_weight,
  45. 'additional_price' => $ownerLogisticFeeDetail->additional_price,
  46. 'additional_amount' => $ownerLogisticFeeDetail->additional_amount,
  47. 'fee' => ($ownerLogisticFeeDetail['initial_weight_price'] * $ownerLogisticFeeDetail['initial_amount']) + ($ownerLogisticFeeDetail['additional_amount'] * $ownerLogisticFeeDetail['additional_price']),
  48. 'owner_id' => $ownerLogisticFeeDetail->owner_id,
  49. ];
  50. }
  51. OwnerLogisticFeeReport::query()->insertOrIgnore($ownerLogisticFeeReportArray);
  52. }
  53. /**
  54. * 订单统计分页查询
  55. * @param $owner_id
  56. * @param $date string 查询的年月 2021-05-01
  57. * @param $paginateParams
  58. * @return LengthAwarePaginator
  59. */
  60. public function getRecordPagination($owner_id, string $date,$paginateParams): LengthAwarePaginator
  61. {
  62. return $this->getSql($owner_id, $date)
  63. ->paginate($paginateParams['paginate']??50);
  64. }
  65. /**
  66. * 订单总计查询
  67. * @param $owner_id
  68. * @param $date string 查询的年月 2021-05-01
  69. * @return array
  70. */
  71. public function getRecordTotal($owner_id, string $date): array
  72. {
  73. $logistic_fee = OwnerLogisticFeeReport::query()
  74. ->where('owner_id', $owner_id)
  75. ->where('counted_date', $date)
  76. ->sum('fee');
  77. $order_count = (int)OwnerLogisticFeeReport::query()
  78. ->where('owner_id', $owner_id)
  79. ->where('counted_date', $date)
  80. ->sum('initial_amount');
  81. return [
  82. 'logistic_fee' => $logistic_fee,
  83. 'order_count' => $order_count,
  84. ];
  85. }
  86. /**
  87. * @param $owner_id
  88. * @param string $date
  89. * @return Builder
  90. */
  91. public function getSql($owner_id, string $date): Builder
  92. {
  93. return OwnerLogisticFeeReport::query()
  94. ->with('logistic:id,name')
  95. ->where('owner_id', $owner_id)
  96. ->where('counted_date', $date)
  97. ->orderByDesc('logistic_id')
  98. ->orderByDesc('province');
  99. }
  100. }