'近 6 个月', '12' => '近 12 个月', 'ytd' => '本年至今', ]; public function __construct( private readonly InstallationDetailMirrorRepository $repository, ) { } public function page(): array { $range = (string) request()->query('range', '12'); if (! array_key_exists($range, self::RANGE_OPTIONS)) { $range = '12'; } [$fromMonth, $toMonth] = $this->monthRange($range); $rangeLabel = $fromMonth . ' 至 ' . $toMonth; try { $result = $this->repository->feeUsageTrend($fromMonth, $toMonth); } catch (Throwable $exception) { report($exception); return $this->payload( $range, $rangeLabel, $fromMonth, $toMonth, $this->emptyPeriods($fromMonth, $toMonth), [], '安装公司趋势暂时无法读取,请稍后重试。', ); } if (! $result['ready']) { return $this->payload( $range, $rangeLabel, $fromMonth, $toMonth, $this->emptyPeriods($fromMonth, $toMonth), [], '安装明细共享镜像尚未完成首次同步。', ); } return $this->payload( $range, $rangeLabel, $fromMonth, $toMonth, $this->buildPeriods($fromMonth, $toMonth, $result['rows']), $result['totals'], ); } /** @return array{0:string,1:string} */ private function monthRange(string $range): array { $current = Carbon::now()->startOfMonth(); $from = match ($range) { '6' => $current->copy()->subMonthsNoOverflow(5), 'ytd' => $current->copy()->startOfYear(), default => $current->copy()->subMonthsNoOverflow(11), }; return [$from->format('Y-m'), $current->format('Y-m')]; } /** @param list $rows */ private function buildPeriods(string $fromMonth, string $toMonth, array $rows): array { $periods = $this->emptyPeriods($fromMonth, $toMonth); $periodIndex = []; foreach ($periods as $index => $period) { $periodIndex[$period['period']] = $index; } foreach ($rows as $row) { $period = $row['period']; if (! isset($periodIndex[$period])) { continue; } $index = $periodIndex[$period]; $periods[$index]['total_fee_orders'] += (int) $row['orders']; $periods[$index]['total_amount'] += (float) $row['amount']; } foreach ($periods as &$period) { $period['total_amount'] = round((float) $period['total_amount'], 2); $period['average_fee'] = $period['total_fee_orders'] > 0 ? round($period['total_amount'] / $period['total_fee_orders'], 2) : 0.0; } unset($period); return $periods; } private function emptyPeriods(string $fromMonth, string $toMonth): array { $periods = []; $cursor = Carbon::parse($fromMonth . '-01')->startOfMonth(); $end = Carbon::parse($toMonth . '-01')->startOfMonth(); while ($cursor->lte($end)) { $periods[] = [ 'period' => $cursor->format('Y-m'), 'short_label' => $cursor->format('m') . '月', 'total_fee_orders' => 0, 'total_amount' => 0.0, 'average_fee' => 0.0, ]; $cursor->addMonthNoOverflow(); } return $periods; } /** @param list $feeTotals */ private function buildSummary(array $feeTotals, array $periods): array { $totalFeeOrders = (int) array_sum(array_column($feeTotals, 'orders')); $totalAmount = round(array_sum(array_column($feeTotals, 'amount')), 2); return [ 'total_fee_orders' => $totalFeeOrders, 'total_amount' => $totalAmount, 'average_fee' => $totalFeeOrders > 0 ? round($totalAmount / $totalFeeOrders, 2) : 0.0, 'has_negative_fee' => collect($periods)->contains( static fn (array $period): bool => (float) $period['total_amount'] < -0.004, ), ]; } private function payload( string $range, string $rangeLabel, string $fromMonth, string $toMonth, array $periods, array $totals, ?string $error = null, ): array { return [ 'view' => 'business.pages.financial-operations.installation-fee-usage', 'installationFeeUsageRange' => $range, 'installationFeeUsageRangeOptions' => self::RANGE_OPTIONS, 'installationFeeUsageRangeLabel' => $rangeLabel, 'installationFeeUsageFromMonth' => $fromMonth, 'installationFeeUsageToMonth' => $toMonth, 'installationFeeUsagePeriods' => $periods, 'installationFeeUsageSummary' => $error === null ? $this->buildSummary($totals, $periods) : [], 'installationFeeUsageError' => $error, ]; } }