'农业银行', '中国工商银行' => '工商银行', '中国建设银行' => '建设银行', '江苏江南农村商业银行' => '江南农村商业银行', '江南农村商业银行' => '江南农村商业银行', '江南农商行' => '江南农村商业银行', '农村商业银行' => '江南农村商业银行', '农商银行' => '江南农村商业银行', '农业银行' => '农业银行', '工商银行' => '工商银行', '中国银行' => '中国银行', '招商银行' => '招商银行', '建设银行' => '建设银行', '交通银行' => '交通银行', '华夏银行' => '华夏银行', '华夏' => '华夏银行', '兴业银行' => '兴业银行', '中信银行' => '中信银行', '光大银行' => '光大银行', '浦发银行' => '浦发银行', '平安银行' => '平安银行', '网商银行' => '网商银行', '邮政储蓄银行' => '邮政储蓄银行', '农行' => '农业银行', '工行' => '工商银行', '中行' => '中国银行', '招行' => '招商银行', '建行' => '建设银行', '交行' => '交通银行', '华夏行' => '华夏银行', '农商' => '江南农村商业银行', '支付宝' => '支付宝', '微信' => '微信', ]; private const SOURCE_PREFIXES = [ '银行存款_公司账户_', '银行存款_个人账户_', '银行存款_个体工商户_', '银行存款_网商银行_', '银行存款_支付宝_', '银行存款_', '库存现金_', ]; /** @var array>|null */ private ?array $accounts = null; /** @var array|null */ private ?array $sourceMappings = null; /** @var array> */ private array $codeHints = []; /** @var array|null */ private ?array $usage = null; public function __construct(private readonly DatabaseManager $databases) { } public function reset(): void { $this->accounts = null; $this->sourceMappings = null; $this->codeHints = []; $this->usage = null; } public function remember(string $accountCode, string $accountShort): void { $accountCode = trim($accountCode); $accountShort = trim($accountShort); if ($accountCode === '' || $accountShort === '' || ! isset($this->accounts()[$accountShort])) { return; } $this->codeHints[$accountCode][$accountShort] = true; } public function resolve( string $sourceName, string $accountCode = '', string $rawAccountShort = '', ): string { $rawAccountShort = trim($rawAccountShort); if ($rawAccountShort !== '' && isset($this->accounts()[$rawAccountShort])) { return $rawAccountShort; } $codeHints = array_keys($this->codeHints[trim($accountCode)] ?? []); if (count($codeHints) === 1) { return (string) $codeHints[0]; } $mapped = trim($this->sourceMappings()[trim($sourceName)] ?? ''); if ($mapped !== '' && isset($this->accounts()[$mapped])) { return $mapped; } $sourceName = trim($sourceName); if ($sourceName === '') { return ''; } $sourceBank = $this->bankOf($sourceName); $sourceKey = $this->normalizeName($sourceName); if ($sourceKey === '') { return ''; } $candidates = []; foreach ($this->accounts() as $short => $account) { if ($sourceBank !== '' && $account['bank'] !== $sourceBank) { continue; } $shortKey = $this->normalizeName($short); $companyKey = $this->normalizeName((string) ($account['company_name'] ?? '')); if (! $this->sameKeyword($sourceKey, $shortKey) && ! $this->sameKeyword($sourceKey, $companyKey)) { continue; } $candidates[$short] = true; } $candidates = array_keys($candidates); if (count($candidates) === 1) { return (string) $candidates[0]; } if (count($candidates) < 2) { return ''; } $usage = $this->usage(); usort($candidates, static fn (string $left, string $right): int => ($usage[$right] ?? 0) <=> ($usage[$left] ?? 0) ); $top = $candidates[0]; $topUsage = (int) ($usage[$top] ?? 0); $secondUsage = (int) ($usage[$candidates[1]] ?? 0); // Normal RMB accounts normally dominate. Leave rare/currency-specific // accounts to explicit 账户简称 or 科目编码 instead of guessing. return $topUsage >= 3 && $topUsage >= max(3, $secondUsage * 3) ? $top : ''; } /** @return array> */ private function accounts(): array { if ($this->accounts !== null) { return $this->accounts; } $accounts = []; foreach ($this->connection()->table('fin_accounts') ->whereNotNull('account_short') ->where('account_short', '<>', '') ->get(['account_short', 'company_name', 'account_type', 'bank_name']) as $row) { $short = trim((string) $row->account_short); if ($short === '') { continue; } $bank = $this->bankOf( $short . ' ' . (string) ($row->bank_name ?? '') . ' ' . (string) ($row->company_name ?? ''), ); $accounts[$short] = [ 'company_name' => (string) ($row->company_name ?? ''), 'account_type' => (string) ($row->account_type ?? ''), 'bank' => $bank, ]; } return $this->accounts = $accounts; } /** @return array */ private function sourceMappings(): array { if ($this->sourceMappings !== null) { return $this->sourceMappings; } $mappings = []; foreach ($this->connection()->table('fin_account_map') ->where('source', 'dingtalk') ->whereNotNull('account_short') ->where('account_short', '<>', '') ->get(['source_value', 'account_short']) as $row) { $source = trim((string) $row->source_value); $short = trim((string) $row->account_short); if ($source !== '' && $short !== '' && isset($this->accounts()[$short])) { $mappings[$source] = $short; } } return $this->sourceMappings = $mappings; } /** @return array */ private function usage(): array { if ($this->usage !== null) { return $this->usage; } $usage = []; foreach ($this->connection()->table('fin_transactions') ->where('source', 'dingtalk') ->whereNotNull('account_short') ->where('account_short', '<>', '') ->selectRaw('account_short, COUNT(*) AS aggregate') ->groupBy('account_short') ->get() as $row) { $usage[(string) $row->account_short] = (int) $row->aggregate; } return $this->usage = $usage; } private function sameKeyword(string $source, string $candidate): bool { if ($source === '' || $candidate === '') { return false; } return $source === $candidate || (mb_strlen($source) >= 3 && str_contains($candidate, $source)) || (mb_strlen($candidate) >= 3 && str_contains($source, $candidate)); } private function bankOf(string $value): string { foreach (self::BANK_ALIASES as $alias => $canonical) { if (str_contains($value, $alias)) { return $canonical; } } return ''; } private function normalizeName(string $value): string { $value = mb_strtolower(trim($value), 'UTF-8'); foreach (self::SOURCE_PREFIXES as $prefix) { $value = str_replace(mb_strtolower($prefix, 'UTF-8'), '', $value); } foreach (array_keys(self::BANK_ALIASES) as $bank) { $value = str_replace(mb_strtolower($bank, 'UTF-8'), '', $value); } foreach ([ '股份有限公司', '有限责任公司', '有限公司', '基本户', '一般户', '公司账户', '个人账户', '个体工商户', '账户', '银行', '现金', ] as $suffix) { $value = str_replace($suffix, '', $value); } return preg_replace('/[\s_\-—()()【】\[\].,,。::\/\\\\"]+/u', '', $value) ?? ''; } private function connection() { return $this->databases->connection( (string) config('upstream_mirrors.connection', 'business_mirror'), ); } }