'近 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->installationCompanyFeeUsageTrend($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']); $orders = (int) $row['orders']; $reviewedOrders = (int) $row['reviewed_orders']; $settledAmount = (float) $row['settled_amount']; $periods[$index]['total_orders'] += $orders; $periods[$index]['reviewed_orders'] += $reviewedOrders; $periods[$index]['settled_amount'] += $settledAmount; $periods[$index]['vendors'][$vendorKey]['orders'] += $orders; $periods[$index]['vendors'][$vendorKey]['reviewed_orders'] += $reviewedOrders; $periods[$index]['vendors'][$vendorKey]['settled_amount'] += $settledAmount; } foreach ($periods as &$period) { $period['settled_amount'] = round((float) $period['settled_amount'], 2); $period['review_rate'] = $this->ratio($period['reviewed_orders'], $period['total_orders']); $period['unit_cost'] = $this->unitCost($period['settled_amount'], $period['reviewed_orders']); foreach ($period['vendors'] as &$vendor) { $vendor['settled_amount'] = round((float) $vendor['settled_amount'], 2); $vendor['review_rate'] = $this->ratio($vendor['reviewed_orders'], $vendor['orders']); $vendor['unit_cost'] = $this->unitCost($vendor['settled_amount'], $vendor['reviewed_orders']); $vendor['order_share'] = $this->ratio($vendor['orders'], $period['total_orders']); } unset($vendor); } 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, 'reviewed_orders' => 0, 'review_rate' => 0.0, 'settled_amount' => 0.0, 'unit_cost' => 0.0, 'vendors' => $this->emptyVendors(), ]; $cursor->addMonthNoOverflow(); } return $periods; } private function emptyVendors(): array { return [ 'wan' => [ 'key' => 'wan', 'label' => '万师傅', 'orders' => 0, 'reviewed_orders' => 0, 'settled_amount' => 0.0, 'review_rate' => 0.0, 'unit_cost' => 0.0, 'order_share' => 0.0, ], 'luban' => [ 'key' => 'luban', 'label' => '鲁班系列', 'orders' => 0, 'reviewed_orders' => 0, 'settled_amount' => 0.0, 'review_rate' => 0.0, 'unit_cost' => 0.0, 'order_share' => 0.0, ], 'other' => [ 'key' => 'other', 'label' => '其他安装公司', 'orders' => 0, 'reviewed_orders' => 0, 'settled_amount' => 0.0, 'review_rate' => 0.0, 'unit_cost' => 0.0, 'order_share' => 0.0, ], ]; } private function buildSummary(array $periods): array { $nonEmptyIndexes = []; foreach ($periods as $index => $period) { if ($period['total_orders'] > 0) { $nonEmptyIndexes[] = $index; } } if ($nonEmptyIndexes === []) { return $this->emptySummary(); } $currentIndex = end($nonEmptyIndexes); $previousIndex = count($nonEmptyIndexes) > 1 ? $nonEmptyIndexes[count($nonEmptyIndexes) - 2] : null; $current = $periods[$currentIndex]; $previous = $previousIndex === null ? null : $periods[$previousIndex]; $benchmarkIndexes = []; for ($index = $currentIndex - 1; $index >= 0; $index--) { if ($this->isCompleteReviewPeriod($periods[$index])) { $benchmarkIndexes[] = $index; if (count($benchmarkIndexes) === 2) { break; } } } if ($benchmarkIndexes === [] && $previousIndex !== null) { $benchmarkIndexes[] = $previousIndex; } $benchmarkPeriods = array_map( static fn (int $index): string => $periods[$index]['period'], $benchmarkIndexes, ); sort($benchmarkPeriods, SORT_STRING); $benchmarkRates = $this->benchmarkRates($periods, $benchmarkIndexes); $previousVendors = collect($previous['vendors'] ?? [])->keyBy('key'); $wanOrders = (int) $current['vendors']['wan']['orders']; $lubanOrders = (int) $current['vendors']['luban']['orders']; $switchOrders = $wanOrders + $lubanOrders; $wanRate = $benchmarkRates['wan'] ?? null; $lubanRate = $benchmarkRates['luban'] ?? null; $vendorRows = []; $estimatedMixedAmount = 0.0; $estimatedAllWanAmount = 0.0; foreach ($current['vendors'] as $vendor) { $rate = $benchmarkRates[$vendor['key']] ?? null; $estimatedAmount = $rate === null ? null : $vendor['orders'] * $rate; if ($vendor['key'] !== 'other' && $estimatedAmount !== null) { $estimatedMixedAmount += $estimatedAmount; } if ($vendor['key'] === 'wan' && $rate !== null) { $estimatedAllWanAmount = $switchOrders * $rate; } $prior = $previousVendors->get($vendor['key']); $vendorRows[] = [ ...$vendor, 'benchmark_rate' => $rate, 'estimated_amount' => $estimatedAmount === null ? null : round($estimatedAmount, 2), 'previous_orders' => (int) ($prior['orders'] ?? 0), 'previous_reviewed_orders' => (int) ($prior['reviewed_orders'] ?? 0), 'previous_review_rate' => (float) ($prior['review_rate'] ?? 0), 'previous_settled_amount' => (float) ($prior['settled_amount'] ?? 0), 'previous_unit_cost' => (float) ($prior['unit_cost'] ?? 0), ]; } $switchImpact = $switchOrders > 0 && $wanRate !== null && $lubanRate !== null ? round($estimatedMixedAmount - $estimatedAllWanAmount, 2) : null; return [ 'latest_period' => $current['period'], 'previous_period' => $previous['period'] ?? null, 'current_orders' => (int) $current['total_orders'], 'current_reviewed_orders' => (int) $current['reviewed_orders'], 'current_review_rate' => (float) $current['review_rate'], 'current_confirmed_amount' => (float) $current['settled_amount'], 'current_confirmed_unit_cost' => (float) $current['unit_cost'], 'previous_orders' => (int) ($previous['total_orders'] ?? 0), 'previous_reviewed_orders' => (int) ($previous['reviewed_orders'] ?? 0), 'previous_review_rate' => (float) ($previous['review_rate'] ?? 0), 'previous_confirmed_amount' => (float) ($previous['settled_amount'] ?? 0), 'previous_confirmed_unit_cost' => (float) ($previous['unit_cost'] ?? 0), 'benchmark_periods' => $benchmarkPeriods, 'benchmark_label' => $benchmarkPeriods === [] ? null : $benchmarkPeriods[0] . ' 至 ' . $benchmarkPeriods[count($benchmarkPeriods) - 1], 'benchmark_rates' => $benchmarkRates, 'estimated_orders' => $switchOrders, 'estimated_mixed_amount' => round($estimatedMixedAmount, 2), 'estimated_all_wan_amount' => round($estimatedAllWanAmount, 2), 'estimated_mixed_unit_cost' => $this->unitCost($estimatedMixedAmount, $switchOrders), 'estimated_all_wan_unit_cost' => $this->unitCost($estimatedAllWanAmount, $switchOrders), 'switch_impact' => $switchImpact, 'switch_impact_rate' => $estimatedAllWanAmount > 0.004 && $switchImpact !== null ? round(($switchImpact / $estimatedAllWanAmount) * 100, 1) : null, 'vendors' => $vendorRows, ]; } private function emptySummary(): array { return [ 'latest_period' => null, 'previous_period' => null, 'current_orders' => 0, 'current_reviewed_orders' => 0, 'current_review_rate' => 0.0, 'current_confirmed_amount' => 0.0, 'current_confirmed_unit_cost' => 0.0, 'previous_orders' => 0, 'previous_reviewed_orders' => 0, 'previous_review_rate' => 0.0, 'previous_confirmed_amount' => 0.0, 'previous_confirmed_unit_cost' => 0.0, 'benchmark_periods' => [], 'benchmark_label' => null, 'benchmark_rates' => [], 'estimated_orders' => 0, 'estimated_mixed_amount' => 0.0, 'estimated_all_wan_amount' => 0.0, 'estimated_mixed_unit_cost' => 0.0, 'estimated_all_wan_unit_cost' => 0.0, 'switch_impact' => null, 'switch_impact_rate' => null, 'vendors' => [], ]; } private function isCompleteReviewPeriod(array $period): bool { if ($period['total_orders'] <= 0) { return false; } foreach (['wan', 'luban'] as $vendorKey) { $vendor = $period['vendors'][$vendorKey]; if ($vendor['orders'] <= 0 || $vendor['review_rate'] < 70) { return false; } } return true; } /** @param list $periodIndexes */ private function benchmarkRates(array $periods, array $periodIndexes): array { $rates = [ 'wan' => ['reviewed_orders' => 0, 'settled_amount' => 0.0], 'luban' => ['reviewed_orders' => 0, 'settled_amount' => 0.0], ]; foreach ($periodIndexes as $periodIndex) { foreach ($rates as $vendorKey => &$rate) { $vendor = $periods[$periodIndex]['vendors'][$vendorKey]; $rate['reviewed_orders'] += (int) $vendor['reviewed_orders']; $rate['settled_amount'] += (float) $vendor['settled_amount']; } unset($rate); } return [ 'wan' => $rates['wan']['reviewed_orders'] > 0 ? $this->unitCost($rates['wan']['settled_amount'], $rates['wan']['reviewed_orders']) : null, 'luban' => $rates['luban']['reviewed_orders'] > 0 ? $this->unitCost($rates['luban']['settled_amount'], $rates['luban']['reviewed_orders']) : null, ]; } 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 unitCost(int|float $amount, int $orders): float { return $orders > 0 ? round((float) $amount / $orders, 2) : 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, ]; } }