'近 6 个月', '12' => '近 12 个月', 'ytd' => '本年至今', ]; private const PLATFORM_ORDER = [ '淘系' => 10, '京东' => 20, '拼多多' => 30, '抖音' => 40, '唯品会' => 50, '得物' => 60, '京喜' => 70, '其他' => 80, ]; 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->platformFeeUsageTrend($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]; $platform = $row['platform'] !== '' ? $row['platform'] : '其他'; $vendor = $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'][$vendor]['orders'] += $orders; $periods[$index]['vendors'][$vendor]['reviewed_orders'] += $reviewedOrders; $periods[$index]['vendors'][$vendor]['settled_amount'] += $settledAmount; if (! isset($periods[$index]['platforms'][$platform])) { $periods[$index]['platforms'][$platform] = [ 'platform' => $platform, 'orders' => 0, 'reviewed_orders' => 0, 'settled_amount' => 0.0, 'review_rate' => 0.0, 'unit_cost' => 0.0, 'vendors' => $this->emptyVendors(), ]; } $platformData = &$periods[$index]['platforms'][$platform]; $platformData['orders'] += $orders; $platformData['reviewed_orders'] += $reviewedOrders; $platformData['settled_amount'] += $settledAmount; $platformData['vendors'][$vendor]['orders'] += $orders; $platformData['vendors'][$vendor]['reviewed_orders'] += $reviewedOrders; $platformData['vendors'][$vendor]['settled_amount'] += $settledAmount; unset($platformData); } 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); foreach ($period['platforms'] as &$platform) { $platform['settled_amount'] = round((float) $platform['settled_amount'], 2); $platform['review_rate'] = $this->ratio($platform['reviewed_orders'], $platform['orders']); $platform['unit_cost'] = $this->unitCost($platform['settled_amount'], $platform['reviewed_orders']); foreach ($platform['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'], $platform['orders']); } unset($vendor); } unset($platform); uasort($period['platforms'], static function (array $left, array $right): int { return ($right['orders'] <=> $left['orders']) ?: ((self::PLATFORM_ORDER[$left['platform']] ?? 99) <=> (self::PLATFORM_ORDER[$right['platform']] ?? 99)); }); $period['platforms'] = array_values($period['platforms']); } 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(), 'platforms' => [], ]; $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); $rates = $this->benchmarkRates($periods, $benchmarkIndexes); $previousPlatforms = collect($previous['platforms'] ?? [])->keyBy('platform'); $platformRows = []; $estimatedOrders = 0; $estimatedMixedAmount = 0.0; $estimatedAllWanAmount = 0.0; foreach ($current['platforms'] as $platform) { $wanOrders = (int) ($platform['vendors']['wan']['orders'] ?? 0); $lubanOrders = (int) ($platform['vendors']['luban']['orders'] ?? 0); $wanRate = $this->benchmarkRate($rates, $platform['platform'], 'wan'); $lubanRate = $this->benchmarkRate($rates, $platform['platform'], 'luban'); $mixedAmount = null; $allWanAmount = null; $switchImpact = null; if ($wanOrders + $lubanOrders > 0 && $wanRate !== null && $lubanRate !== null) { $mixedAmount = $wanOrders * $wanRate + $lubanOrders * $lubanRate; $allWanAmount = ($wanOrders + $lubanOrders) * $wanRate; $switchImpact = $mixedAmount - $allWanAmount; $estimatedOrders += $wanOrders + $lubanOrders; $estimatedMixedAmount += $mixedAmount; $estimatedAllWanAmount += $allWanAmount; } $prior = $previousPlatforms->get($platform['platform']); $platformRows[] = [ 'platform' => $platform['platform'], 'orders' => (int) $platform['orders'], 'wan_orders' => $wanOrders, 'luban_orders' => $lubanOrders, 'other_orders' => (int) ($platform['vendors']['other']['orders'] ?? 0), 'luban_share' => $this->ratio( $lubanOrders, $wanOrders + $lubanOrders + (int) ($platform['vendors']['other']['orders'] ?? 0), ), 'reviewed_orders' => (int) $platform['reviewed_orders'], 'review_rate' => (float) $platform['review_rate'], 'settled_amount' => (float) $platform['settled_amount'], 'confirmed_unit_cost' => (float) $platform['unit_cost'], 'wan_benchmark_rate' => $wanRate, 'luban_benchmark_rate' => $lubanRate, 'estimated_amount' => $mixedAmount === null ? null : round($mixedAmount, 2), 'all_wan_amount' => $allWanAmount === null ? null : round($allWanAmount, 2), 'switch_impact' => $switchImpact === null ? null : round($switchImpact, 2), 'previous_orders' => (int) ($prior['orders'] ?? 0), 'previous_settled_amount' => round((float) ($prior['settled_amount'] ?? 0), 2), 'previous_unit_cost' => round((float) ($prior['unit_cost'] ?? 0), 2), ]; } usort($platformRows, static fn (array $left, array $right): int => ($right['orders'] <=> $left['orders']) ?: ((self::PLATFORM_ORDER[$left['platform']] ?? 99) <=> (self::PLATFORM_ORDER[$right['platform']] ?? 99))); $switchImpact = $estimatedOrders > 0 ? 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], 'estimated_orders' => $estimatedOrders, 'estimated_mixed_amount' => round($estimatedMixedAmount, 2), 'estimated_all_wan_amount' => round($estimatedAllWanAmount, 2), 'estimated_mixed_unit_cost' => $this->unitCost($estimatedMixedAmount, $estimatedOrders), 'estimated_all_wan_unit_cost' => $this->unitCost($estimatedAllWanAmount, $estimatedOrders), 'switch_impact' => $switchImpact, 'switch_impact_rate' => $estimatedAllWanAmount > 0.004 && $switchImpact !== null ? round(($switchImpact / $estimatedAllWanAmount) * 100, 1) : null, 'platforms' => $platformRows, ]; } 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, '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, 'platforms' => [], ]; } 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 { $platforms = []; foreach ($periodIndexes as $periodIndex) { foreach ($periods[$periodIndex]['platforms'] as $platform) { foreach (['wan', 'luban'] as $vendorKey) { $vendor = $platform['vendors'][$vendorKey]; $platforms[$platform['platform']][$vendorKey] ??= [ 'reviewed_orders' => 0, 'settled_amount' => 0.0, ]; $platforms[$platform['platform']][$vendorKey]['reviewed_orders'] += (int) $vendor['reviewed_orders']; $platforms[$platform['platform']][$vendorKey]['settled_amount'] += (float) $vendor['settled_amount']; } } } $companies = []; foreach ($platforms as &$vendors) { foreach ($vendors as $vendorKey => &$rate) { $rate['rate'] = $this->unitCost($rate['settled_amount'], $rate['reviewed_orders']); $companies[$vendorKey]['reviewed_orders'] = ($companies[$vendorKey]['reviewed_orders'] ?? 0) + $rate['reviewed_orders']; $companies[$vendorKey]['settled_amount'] = ($companies[$vendorKey]['settled_amount'] ?? 0.0) + $rate['settled_amount']; } unset($rate); } unset($vendors); foreach ($companies as &$company) { $company['rate'] = $this->unitCost($company['settled_amount'], $company['reviewed_orders']); } unset($company); return ['platforms' => $platforms, 'companies' => $companies]; } private function benchmarkRate(array $rates, string $platform, string $vendorKey): ?float { $platformRate = $rates['platforms'][$platform][$vendorKey] ?? null; if ($platformRate !== null && $platformRate['reviewed_orders'] >= 10) { return $platformRate['rate'] > 0 ? (float) $platformRate['rate'] : null; } $companyRate = $rates['companies'][$vendorKey]['rate'] ?? null; return $companyRate !== null && $companyRate > 0 ? (float) $companyRate : 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, ]; } }