'近 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->installationCompanyActualFeeTrend($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), [], '安装明细共享镜像尚未完成首次同步。', ); } $periods = $this->buildPeriods($fromMonth, $toMonth, $result['rows']); return $this->payload( $range, $rangeLabel, $fromMonth, $toMonth, $periods, $this->buildSummary($periods), ); } /** @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); $indexes = []; foreach ($periods as $index => $period) { $indexes[$period['period']] = $index; } foreach ($rows as $row) { $period = $row['period']; if (! isset($indexes[$period])) { continue; } $index = $indexes[$period]; $vendorKey = $this->vendorKey($row['installation_company']); $periods[$index]['total_orders'] += (int) $row['orders']; $periods[$index]['total_amount'] += (float) $row['amount']; $periods[$index]['vendors'][$vendorKey]['orders'] += (int) $row['orders']; $periods[$index]['vendors'][$vendorKey]['amount'] += (float) $row['amount']; } $previousAmount = null; $previousOrders = null; foreach ($periods as &$period) { $period['total_amount'] = round((float) $period['total_amount'], 2); $period['average_fee'] = $period['total_orders'] > 0 ? round($period['total_amount'] / $period['total_orders'], 2) : 0.0; foreach ($period['vendors'] as &$vendor) { $vendor['amount'] = round((float) $vendor['amount'], 2); $vendor['order_share'] = $this->ratio($vendor['orders'], $period['total_orders']); $vendor['average_fee'] = $vendor['orders'] > 0 ? round($vendor['amount'] / $vendor['orders'], 2) : 0.0; } unset($vendor); $period['fee_change_rate'] = $period['total_orders'] > 0 && $previousAmount !== null && $previousAmount > 0 ? round((($period['total_amount'] - $previousAmount) / $previousAmount) * 100, 1) : null; $period['order_change_rate'] = $period['total_orders'] > 0 && $previousOrders !== null && $previousOrders > 0 ? round((($period['total_orders'] - $previousOrders) / $previousOrders) * 100, 1) : null; if ($period['total_orders'] > 0) { $previousAmount = $period['total_amount']; $previousOrders = $period['total_orders']; } } 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('n') . '月', 'total_orders' => 0, 'total_amount' => 0.0, 'average_fee' => 0.0, 'fee_change_rate' => null, 'order_change_rate' => null, 'vendors' => $this->emptyVendors(), ]; $cursor->addMonthNoOverflow(); } return $periods; } private function emptyVendors(): array { return [ 'wan' => [ 'key' => 'wan', 'label' => '万师傅', 'orders' => 0, 'amount' => 0.0, 'order_share' => 0.0, 'average_fee' => 0.0, ], 'luban' => [ 'key' => 'luban', 'label' => '鲁班系列', 'orders' => 0, 'amount' => 0.0, 'order_share' => 0.0, 'average_fee' => 0.0, ], 'other' => [ 'key' => 'other', 'label' => '其他安装公司', 'orders' => 0, 'amount' => 0.0, 'order_share' => 0.0, 'average_fee' => 0.0, ], ]; } private function buildSummary(array $periods): array { $nonEmpty = array_values(array_filter( $periods, static fn (array $period): bool => $period['total_orders'] > 0, )); if ($nonEmpty === []) { return $this->emptySummary(); } $current = end($nonEmpty); $previous = count($nonEmpty) > 1 ? $nonEmpty[count($nonEmpty) - 2] : null; return [ 'latest_period' => $current['period'], 'previous_period' => $previous['period'] ?? null, 'current_orders' => (int) $current['total_orders'], 'current_amount' => (float) $current['total_amount'], 'current_average_fee' => (float) $current['average_fee'], 'current_fee_change_rate' => $current['fee_change_rate'], 'current_order_change_rate' => $current['order_change_rate'], 'previous_orders' => (int) ($previous['total_orders'] ?? 0), 'previous_amount' => (float) ($previous['total_amount'] ?? 0), 'previous_average_fee' => (float) ($previous['average_fee'] ?? 0), 'has_negative_amount' => collect($periods)->contains( static fn (array $period): bool => (float) $period['total_amount'] < -0.004, ), ]; } private function emptySummary(): array { return [ 'latest_period' => null, 'previous_period' => null, 'current_orders' => 0, 'current_amount' => 0.0, 'current_average_fee' => 0.0, 'current_fee_change_rate' => null, 'current_order_change_rate' => null, 'previous_orders' => 0, 'previous_amount' => 0.0, 'previous_average_fee' => 0.0, 'has_negative_amount' => false, ]; } private function vendorKey(string $company): string { if (str_contains($company, '万师傅')) { return 'wan'; } if (str_contains($company, '鲁班')) { return 'luban'; } return 'other'; } private function ratio(int|float $value, int|float $total): float { return abs((float) $total) > 0.00001 ? round(((float) $value / (float) $total) * 100, 1) : 0.0; } private function payload( string $range, string $rangeLabel, string $fromMonth, string $toMonth, array $periods, array $summary, ?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 ? $summary : [], 'installationFeeUsageError' => $error, ]; } }