subMonth()->startOfMonth()->toDateTimeString(); } $this->reportDate = $counting_month; $start = $this->reportDate; $end = Carbon::parse($this->reportDate)->endOfMonth()->toDateTimeString(); $ownerLogisticFeeDetails = OwnerLogisticFeeDetail::query() ->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") ->whereBetween('created_at', [$start, $end]) ->groupBy('initial_weight', 'initial_weight_price', 'additional_price', 'additional_weight', 'logistic_id', 'province', 'counted_date', 'owner_id') ->get(); $ownerLogisticFeeReportArray = []; foreach ($ownerLogisticFeeDetails as $ownerLogisticFeeDetail) { $ownerLogisticFeeReportArray[] = [ 'logistic_id' => $ownerLogisticFeeDetail->logistic_id, 'province' => $ownerLogisticFeeDetail->province, 'counted_date' => Carbon::parse($ownerLogisticFeeDetail->counted_date)->firstOfMonth()->toDateString(), 'initial_weight' => $ownerLogisticFeeDetail->initial_weight, 'initial_weight_price' => $ownerLogisticFeeDetail->initial_weight_price, 'initial_amount' => $ownerLogisticFeeDetail->initial_amount, 'additional_weight' => $ownerLogisticFeeDetail->additional_weight, 'additional_price' => $ownerLogisticFeeDetail->additional_price, 'additional_amount' => $ownerLogisticFeeDetail->additional_amount, 'fee' => ($ownerLogisticFeeDetail['initial_weight_price'] * $ownerLogisticFeeDetail['initial_amount']) + ($ownerLogisticFeeDetail['additional_amount'] * $ownerLogisticFeeDetail['additional_price']), 'owner_id' => $ownerLogisticFeeDetail->owner_id, ]; } OwnerLogisticFeeReport::query()->insertOrIgnore($ownerLogisticFeeReportArray); } /** * 订单统计分页查询 * @param $owner_id * @param $counting_month string 查询的年月 2021-05-01 * @param $paginateParams * @return LengthAwarePaginator */ private function getRecordPagination($owner_id, string $counting_month, $paginateParams): LengthAwarePaginator { return $this->getSql($owner_id, $counting_month) ->paginate($paginateParams['paginate'] ?? 50); } public function get(array $kvPairs): array { $this->archiveService = app('OwnerBillReportArchiveService'); if ($this->archiveService->isArchived($kvPairs['counting_month'], $kvPairs['owner_id'], $kvPairs['type']) == 1) { $archived = $this->archiveService->get($kvPairs); $reports =collect($archived->information['reports']); $recordTotal = $archived->information['recordTotal']; } else { $recordTotal = $this->getRecordTotal($kvPairs['owner_id'], $kvPairs['counting_month']); $reports = $this->getRecordPagination($kvPairs['owner_id'], $kvPairs['counting_month'], $kvPairs['paginateParams']); } return array($reports, $recordTotal); } /** * 订单统计查询 * @param $owner_id * @param $counting_month string 查询的年月 2021-05-01 * @return Builder[]|Collection */ public function getRecords($owner_id, string $counting_month) { return $this->getSql($owner_id, $counting_month)->get(); } /** * 订单总计查询 * @param $owner_id * @param $counting_month string 查询的年月 2021-05-01 * @return array */ public function getRecordTotal($owner_id, string $counting_month): array { $logistic_fee = OwnerLogisticFeeReport::query() ->where('owner_id', $owner_id) ->where('counted_date', $counting_month) ->sum('fee'); $order_count = (int)OwnerLogisticFeeReport::query() ->where('owner_id', $owner_id) ->where('counted_date', $counting_month) ->sum('initial_amount'); return [ 'logistic_fee' => $logistic_fee, 'order_count' => $order_count, ]; } /** * @param $owner_id * @param string $counting_month * @return Builder */ public function getSql($owner_id, string $counting_month): Builder { return OwnerLogisticFeeReport::query() ->with('logistic:id,name') ->where('owner_id', $owner_id) ->where('counted_date', $counting_month) ->orderByDesc('logistic_id') ->orderByDesc('province'); } }