detailService = app('OwnerStoreFeeDetailService'); if (is_null($counting_month)) { //默认统计上个月的数据 $counting_month = now()->subMonth()->startOfMonth()->toDateString(); } $this->reportDate = $counting_month; $start = $this->reportDate; $end = Carbon::parse($this->reportDate)->endOfMonth()->toDateString(); $details = DB::table('owner_store_fee_details') ->leftJoin('owner_fee_details', 'owner_fee_detail_id', '=', 'owner_fee_details.id') ->selectRaw("DATE_FORMAT(owner_store_fee_details.created_at,'%Y-%m') as counting_month, unit_id, unit_price, sum(amount) as amounts , owner_store_fee_details.owner_id, owner_fee_detail_id, sum(owner_fee_details.work_fee) as work_fee, owner_store_fee_details.owner_price_operation_id") ->whereBetween('owner_store_fee_details.created_at', [$start, $end]) ->groupBy('counting_month', 'owner_store_fee_details.owner_id', 'owner_store_fee_details.owner_price_operation_id', 'unit_id', 'unit_price') ->get(); $reports = []; foreach ($details as $detail) { $counting_month = Carbon::parse($detail->counting_month)->startOfMonth()->toDateString(); $ownerBillReport = OwnerBillReport::query() ->selectRaw("id") ->where('owner_id', $detail->owner_id) ->where('counting_month', $counting_month)->first(); $reports[] = [ 'owner_bill_report_id' => $ownerBillReport->id ?? null, 'owner_price_operation_id' => $detail->owner_price_operation_id, 'owner_id' => $detail->owner_id, 'counting_month' => $counting_month, 'unit_id' => $detail->unit_id, 'unit_price' => $detail->unit_price, 'amount' => $detail->amounts, 'fee' => $detail->work_fee, ]; } $reports_chunked = array_chunk($reports,1000 ); foreach ($reports_chunked as $items) { OwnerStoreFeeReport::query()->insertOrIgnore($items); } } public function get(array $kvPairs): array { $this->archiveService = app('OwnerBillReportArchiveService'); $this->detailService = app('OwnerStoreFeeDetailService'); if ($this->archiveService->isArchived($kvPairs['counting_month'], $kvPairs['owner_id'], $kvPairs['type']) == 1) { //查询存档数据 $archived = $this->archiveService->get($kvPairs); $reports = collect($archived->information['reports']); $totalAmount = $archived->information['totalAmount']; $totalFee = $archived->information['totalFee']; $owner_price_operation_fees = collect($archived->information['owner_price_operation_fees']); } else { $reports = $this->getSql($kvPairs['owner_id'], $kvPairs['counting_month'])->orderByDesc('owner_price_operation_id')->get(); $totalAmount = $reports->sum('amount'); $totalFee = number_format($reports->sum('fee'),2); $owner_price_operation_fees = OwnerStoreFeeReport::query() ->with('ownerPriceOperation:id,name') ->selectRaw("sum(fee) as fee,owner_price_operation_id") ->where('owner_id', $kvPairs['owner_id']) ->where('counting_month', $kvPairs['counting_month']) ->groupBy('owner_price_operation_id')->get(); } return array($reports, $totalAmount, $totalFee,$owner_price_operation_fees); } /** * @param $owner_id * @param $counting_month * @return Builder */ public function getSql($owner_id, $counting_month): Builder { return OwnerStoreFeeReport::query() ->with(['unit:id,name']) ->where('owner_id', $owner_id) ->where('counting_month', $counting_month); } }