[['交易单号', '交易订单'], ['差异', '金额']], self::PAGE_OTHER_SURCHARGE => [['交易单号', '交易订单'], ['金额']], self::PAGE_RETURN_FEE => [['交易订单', '交易单号'], ['结算金额', '金额']], ]; private const CACHE_MINUTES = 10; private const AMOUNT_SCALE = 10; public function __construct(private readonly CourierPayableSummaryStore $store) {} /** * @param string $page self::PAGE_* 之一 * @return array 交易单号 => 金额(保留源文件里的原值字符串) */ public function map(int $companyId, string $page): array { if (! isset(self::PAGES[$page])) { return []; } $disk = Storage::disk('uploads'); $path = $this->store->path($companyId, $page); if (! $disk->exists($path)) { return []; } // 文件时间戳进缓存键:财务重新导入运费差后不用等缓存过期。 $key = sprintf('courier-payable-summary-map:v3:%s:%d:%d', $page, $companyId, (int) $disk->lastModified($path)); return Cache::remember($key, now()->addMinutes(self::CACHE_MINUTES), function () use ($companyId, $page): array { try { return $this->build($companyId, $page); } catch (Throwable $exception) { report($exception); return []; } }); } /** * 汇总快照按原始日期拆成月份,供财务经营分析表按月匹配。 * * 同一交易单号在同一月份可以出现多行,所有金额都累加,不做去重。 * * @return array> 月份 => [交易单号 => 金额] */ public function mapByMonth(int $companyId, string $page): array { if (! isset(self::PAGES[$page])) { return []; } $disk = Storage::disk('uploads'); $path = $this->store->path($companyId, $page); if (! $disk->exists($path)) { return []; } $key = sprintf( 'courier-payable-summary-month-map:v1:%s:%d:%d', $page, $companyId, (int) $disk->lastModified($path), ); return Cache::remember($key, now()->addMinutes(self::CACHE_MINUTES), function () use ($companyId, $page): array { try { return $this->buildByMonth($companyId, $page); } catch (Throwable $exception) { report($exception); return []; } }); } /** @return array */ private function build(int $companyId, string $page): array { $snapshot = $this->store->snapshot($companyId, $page); if ($snapshot === null) { return []; } [$orderHeaders, $valueHeaders] = self::PAGES[$page]; $headers = array_map(static fn ($value): string => trim((string) $value), (array) $snapshot['headers']); $orderColumn = $this->columnIndex($headers, $orderHeaders); $valueColumn = $this->columnIndex($headers, $valueHeaders); if ($orderColumn === null || $valueColumn === null) { return []; } $totals = []; $fallbacks = []; foreach ((array) $snapshot['rows'] as $row) { $orderNos = trim((string) ($row[$orderColumn] ?? '')); if ($orderNos === '') { continue; } $value = trim((string) ($row[$valueColumn] ?? '')); if ($value === '') { continue; } // 源表一个格子可能放多个交易单号(逗号分隔),逐个挂到同一个金额上。 foreach (preg_split('/[,,]/u', $orderNos) ?: [] as $orderNo) { $orderNo = trim($orderNo); if ($orderNo === '') { continue; } // 同一交易单号可能拆成多条运费差、附加费或退件费,按单号汇总, // 避免页面只取第一条而漏计同一订单的后续金额。 $numericValue = $this->numericDecimal($value); if ($numericValue !== null && function_exists('bcadd')) { $totals[$orderNo] = bcadd( $totals[$orderNo] ?? '0', $numericValue, self::AMOUNT_SCALE, ); } else { $fallbacks[$orderNo] ??= $value; } } } $map = []; foreach ($totals as $orderNo => $total) { $map[$orderNo] = $this->decimalString($total); } foreach ($fallbacks as $orderNo => $value) { if (! isset($map[$orderNo])) { $map[$orderNo] = $value; } } return $map; } /** @return array> */ private function buildByMonth(int $companyId, string $page): array { $snapshot = $this->store->snapshot($companyId, $page); if ($snapshot === null) { return []; } [$orderHeaders, $valueHeaders] = self::PAGES[$page]; $headers = array_map(static fn ($value): string => trim((string) $value), (array) $snapshot['headers']); $orderColumn = $this->columnIndex($headers, $orderHeaders); $valueColumn = $this->columnIndex($headers, $valueHeaders); $dateColumn = $this->columnIndex($headers, ['日期', '结算月份', '结算月', '月份']); if ($orderColumn === null || $valueColumn === null || $dateColumn === null) { return []; } $totals = []; foreach ((array) $snapshot['rows'] as $row) { $month = $this->month((string) ($row[$dateColumn] ?? '')); $orderNos = trim((string) ($row[$orderColumn] ?? '')); $value = trim((string) ($row[$valueColumn] ?? '')); $numericValue = $this->numericDecimal($value); if ($month === null || $orderNos === '' || $numericValue === null) { continue; } $totals[$month] ??= []; foreach (preg_split('/[,,]/u', $orderNos) ?: [] as $orderNo) { $orderNo = trim($orderNo); if ($orderNo === '') { continue; } $totals[$month][$orderNo] = function_exists('bcadd') ? bcadd($totals[$month][$orderNo] ?? '0', $numericValue, self::AMOUNT_SCALE) : (string) ((float) ($totals[$month][$orderNo] ?? 0) + (float) $numericValue); } } $map = []; foreach ($totals as $month => $orders) { foreach ($orders as $orderNo => $total) { $map[$month][$orderNo] = $this->decimalString($total); } } return $map; } private function month(string $value): ?string { if (preg_match( '/(? $needles */ private function columnIndex(array $headers, array $needles): ?int { foreach ($needles as $needle) { foreach ($headers as $index => $header) { if ($header === $needle) { return (int) $index; } } } foreach ($needles as $needle) { foreach ($headers as $index => $header) { if ($header !== '' && mb_strpos($header, $needle) !== false) { return (int) $index; } } } return null; } }