From 773681773fbff1b307c9245a6cb3039956865bd1 Mon Sep 17 00:00:00 2001 From: yueyang <2495544247@qq.com> Date: Sat, 5 Dec 2026 09:53:10 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E7=A7=91=E7=9B=AE=E6=98=A0=E5=B0=84):=20?= =?UTF-8?q?=E6=B7=98=E5=AE=9D=E5=A4=A9=E7=8C=AB=E6=8C=89=E5=90=8C=E4=B8=80?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0=E5=A4=84=E7=90=86=20(#1747)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/AccountMappingCoverage.php | 75 ++++++++++++++++-- .../Services/AccountMappingMatcher.php | 24 ++++-- .../Services/AccountMappingPlatform.php | 48 ++++++++++++ .../Services/AccountMappingSuggestion.php | 4 +- .../financial-rules/account-mapping.blade.php | 2 +- .../Business/AccountMappingMatcherTest.php | 27 +++++++ .../AccountMappingCoverageTest.php | 77 ++++++++++++++++++- .../AccountMappingSuggestionTest.php | 12 +++ 8 files changed, 253 insertions(+), 16 deletions(-) create mode 100644 modules/EcommerceFinance/Services/AccountMappingPlatform.php diff --git a/modules/EcommerceFinance/Services/AccountMappingCoverage.php b/modules/EcommerceFinance/Services/AccountMappingCoverage.php index d362708dd..efba6b549 100644 --- a/modules/EcommerceFinance/Services/AccountMappingCoverage.php +++ b/modules/EcommerceFinance/Services/AccountMappingCoverage.php @@ -46,8 +46,8 @@ class AccountMappingCoverage ]; private const PLATFORM_PREFIX = [ - '天猫' => '天猫', - '淘宝' => '淘宝', + '天猫' => AccountMappingPlatform::TAOBAO_TMALL, + '淘宝' => AccountMappingPlatform::TAOBAO_TMALL, '京东' => '京东', '京喜' => '京喜', '京喜小店' => '京喜', @@ -330,7 +330,7 @@ class AccountMappingCoverage $items = []; $missingRows = 0; $excludedRows = 0; - foreach ($raw['pairs'] as $pair) { + foreach ($this->canonicalPairs((array) ($raw['pairs'] ?? [])) as $pair) { if (! $this->isPlatformBill($pair)) { $excludedRows += (int) $pair['source_rows']; continue; @@ -376,7 +376,7 @@ class AccountMappingCoverage $missingRows = 0; $excludedRows = 0; - foreach ($raw['pairs'] as $pair) { + foreach ($this->canonicalPairs((array) ($raw['pairs'] ?? [])) as $pair) { if (! $this->isPlatformBill($pair)) { $excludedRows += (int) $pair['source_rows']; continue; @@ -971,7 +971,7 @@ class AccountMappingCoverage { $keys = []; foreach ($rules as $rule) { - $platform = trim((string) ($rule['platform'] ?? '')); + $platform = AccountMappingPlatform::canonical((string) ($rule['platform'] ?? '')); $item = trim((string) ($rule['item'] ?? '')); if ($platform === '' || $item === '') { continue; @@ -984,7 +984,7 @@ class AccountMappingCoverage private function isMapped(array $pair, array $mapping): bool { - $platform = (string) ($pair['platform'] ?? ''); + $platform = AccountMappingPlatform::canonical((string) ($pair['platform'] ?? '')); $item = (string) ($pair['item'] ?? ''); if ($platform === '' || $item === '') { return false; @@ -1001,6 +1001,69 @@ class AccountMappingCoverage return $platform !== '' && $platform !== '支付宝'; } + /** + * 历史快照仍可能保存“淘宝”“天猫”两组;读取时统一口径并合并同名项目。 + * + * @param list> $pairs + * @return list> + */ + private function canonicalPairs(array $pairs): array + { + $normalized = []; + foreach ($pairs as $pair) { + if (! is_array($pair)) { + continue; + } + + $platform = AccountMappingPlatform::canonical((string) ($pair['platform'] ?? '')); + $item = trim((string) ($pair['item'] ?? '')); + if ($platform === '' && $item === '') { + continue; + } + + $pair['platform'] = $platform; + $pair['platform_label'] = $platform; + $key = $platform . "\0" . $item; + if (! isset($normalized[$key])) { + $normalized[$key] = $pair; + continue; + } + + $normalized[$key]['source_rows'] += (int) ($pair['source_rows'] ?? 0); + $normalized[$key]['store_count'] += (int) ($pair['store_count'] ?? 0); + $normalized[$key]['source_labels'] = array_values(array_unique(array_merge( + (array) ($normalized[$key]['source_labels'] ?? []), + (array) ($pair['source_labels'] ?? []), + ))); + $normalized[$key]['raw_items'] = array_values(array_unique(array_merge( + (array) ($normalized[$key]['raw_items'] ?? []), + (array) ($pair['raw_items'] ?? []), + ))); + $normalized[$key]['months'] = array_values(array_unique(array_merge( + (array) ($normalized[$key]['months'] ?? []), + (array) ($pair['months'] ?? []), + ))); + if (isset($pair['month_rows']) || isset($normalized[$key]['month_rows'])) { + $monthRows = (array) ($normalized[$key]['month_rows'] ?? []); + foreach ((array) ($pair['month_rows'] ?? []) as $month => $rows) { + $monthRows[$month] = (int) ($monthRows[$month] ?? 0) + (int) $rows; + } + ksort($monthRows); + $normalized[$key]['month_rows'] = $monthRows; + $normalized[$key]['months'] = array_keys($monthRows); + } + } + + foreach ($normalized as &$pair) { + if (isset($pair['months'])) { + sort($pair['months']); + } + } + unset($pair); + + return array_values($normalized); + } + /** @return list */ private function generations(string $periodsTable, string $start, string $end): array { diff --git a/modules/EcommerceFinance/Services/AccountMappingMatcher.php b/modules/EcommerceFinance/Services/AccountMappingMatcher.php index 1c77930f3..a76f83b67 100644 --- a/modules/EcommerceFinance/Services/AccountMappingMatcher.php +++ b/modules/EcommerceFinance/Services/AccountMappingMatcher.php @@ -35,7 +35,8 @@ class AccountMappingMatcher { $candidates = []; foreach ($this->store->all() as $rule) { - if ($this->sameText((string) $rule['platform'], $platform) && $this->sameText((string) $rule['item'], $item)) { + if (AccountMappingPlatform::equivalent((string) $rule['platform'], $platform) + && $this->sameText((string) $rule['item'], $item)) { $candidates[] = $rule; } } @@ -45,13 +46,14 @@ class AccountMappingMatcher } $hits = []; - $fallback = null; + $fallbacks = []; foreach ($candidates as $rule) { $memo = trim((string) ($rule['memo'] ?? '')); $goods = trim((string) ($rule['goods'] ?? '')); if ($memo === '' && $goods === '') { - $fallback ??= $rule; + $rank = AccountMappingPlatform::matchRank((string) $rule['platform'], $platform); + $fallbacks[$rank] ??= $rule; continue; } @@ -62,11 +64,21 @@ class AccountMappingMatcher continue; } - $hits[] = ['rule' => $rule, 'score' => ($goods !== '' ? 2 : 0) + ($memo !== '' ? 1 : 0)]; + $hits[] = [ + 'rule' => $rule, + 'score' => ($goods !== '' ? 2 : 0) + ($memo !== '' ? 1 : 0), + 'platform_rank' => AccountMappingPlatform::matchRank((string) $rule['platform'], $platform), + ]; } if ($hits !== []) { - usort($hits, static fn (array $a, array $b): int => $b['score'] <=> $a['score'] ?: ((int) $a['rule']['id'] <=> (int) $b['rule']['id'])); + usort($hits, static fn (array $a, array $b): int => ( + $b['score'] <=> $a['score'] + ) ?: ( + $a['platform_rank'] <=> $b['platform_rank'] + ) ?: ( + (int) $a['rule']['id'] <=> (int) $b['rule']['id'] + )); return [ 'rule' => $hits[0]['rule'], @@ -75,6 +87,8 @@ class AccountMappingMatcher ]; } + ksort($fallbacks); + $fallback = reset($fallbacks) ?: null; $rule = $fallback ?? $candidates[0]; return [ diff --git a/modules/EcommerceFinance/Services/AccountMappingPlatform.php b/modules/EcommerceFinance/Services/AccountMappingPlatform.php new file mode 100644 index 000000000..6cc7a21fc --- /dev/null +++ b/modules/EcommerceFinance/Services/AccountMappingPlatform.php @@ -0,0 +1,48 @@ + self::TAOBAO_TMALL, + default => $platform, + }; + } + + public static function equivalent(string $left, string $right): bool + { + return self::canonical($left) === self::canonical($right); + } + + /** + * 排序优先级:原平台精确匹配优先,同口径平台其次,其它平台最后。 + */ + public static function matchRank(string $candidate, string $target): int + { + $candidate = trim($candidate); + $target = trim($target); + + if (mb_strtolower($candidate) === mb_strtolower($target)) { + return 0; + } + + return self::equivalent($candidate, $target) ? 1 : 2; + } +} diff --git a/modules/EcommerceFinance/Services/AccountMappingSuggestion.php b/modules/EcommerceFinance/Services/AccountMappingSuggestion.php index d63941cd1..6db809be0 100644 --- a/modules/EcommerceFinance/Services/AccountMappingSuggestion.php +++ b/modules/EcommerceFinance/Services/AccountMappingSuggestion.php @@ -80,8 +80,8 @@ class AccountMappingSuggestion } usort($matches, static fn (array $left, array $right): int => ( - ((string) ($left['platform'] ?? '') === $platform ? 0 : 1) - <=> ((string) ($right['platform'] ?? '') === $platform ? 0 : 1) + AccountMappingPlatform::matchRank((string) ($left['platform'] ?? ''), $platform) + <=> AccountMappingPlatform::matchRank((string) ($right['platform'] ?? ''), $platform) ) ?: (((int) ($left['id'] ?? 0)) <=> ((int) ($right['id'] ?? 0)))); $templates = []; diff --git a/resources/views/business/pages/financial-rules/account-mapping.blade.php b/resources/views/business/pages/financial-rules/account-mapping.blade.php index 9c371f833..be1f12920 100644 --- a/resources/views/business/pages/financial-rules/account-mapping.blade.php +++ b/resources/views/business/pages/financial-rules/account-mapping.blade.php @@ -894,7 +894,7 @@ >
- 统计最近 60 个月;清单生成后持续保留,每月自动更新一次 + 统计最近 60 个月;清单生成后持续保留,每月自动更新一次;淘宝、天猫按“淘宝天猫”同一平台口径统计
diff --git a/tests/Feature/Business/AccountMappingMatcherTest.php b/tests/Feature/Business/AccountMappingMatcherTest.php index 7d350199d..26ea48470 100644 --- a/tests/Feature/Business/AccountMappingMatcherTest.php +++ b/tests/Feature/Business/AccountMappingMatcherTest.php @@ -150,6 +150,33 @@ class AccountMappingMatcherTest extends FeatureTestCase $this->assertSame($rule['id'], $hit['rule']['id']); } + /** 淘宝、天猫按同一平台匹配;原平台精确规则仍优先 */ + public function test_taobao_and_tmall_share_rules_with_exact_platform_priority(): void + { + $tmallOnly = $this->rule([ + 'platform' => '天猫', + 'item' => '共享项目', + 'memo' => '技术服务费', + ]); + + $fallback = $this->matcher->match('淘宝', '共享项目', ['技术服务费']); + $this->assertNotNull($fallback); + $this->assertTrue($fallback['matched']); + $this->assertSame($tmallOnly['id'], $fallback['rule']['id']); + + $taobaoExact = $this->rule([ + 'platform' => '淘宝', + 'item' => '共享项目', + 'memo' => '技术服务费', + 'dAcct' => '管理费用', + 'dSub' => '软件服务费', + ]); + + $exact = $this->matcher->match('淘宝', '共享项目', ['技术服务费']); + $this->assertNotNull($exact); + $this->assertSame($taobaoExact['id'], $exact['rule']['id']); + } + /** 校验:同项目下有兜底规则时给出提示,提醒它只做兜底 */ public function test_validator_hints_fallback_rule(): void { diff --git a/tests/Unit/EcommerceFinance/AccountMappingCoverageTest.php b/tests/Unit/EcommerceFinance/AccountMappingCoverageTest.php index 8f8f97f4a..b93977488 100644 --- a/tests/Unit/EcommerceFinance/AccountMappingCoverageTest.php +++ b/tests/Unit/EcommerceFinance/AccountMappingCoverageTest.php @@ -97,9 +97,9 @@ class AccountMappingCoverageTest extends TestCase $this->assertSame(3, $first['missing_item_count']); $this->assertSame(4, $first['missing_row_count']); $this->assertSame([ - '商家货款入账|天猫|2', + '商家货款入账|淘宝天猫|2', '未映射售后费|京东|1', - '(未填写账单项目)|天猫|1', + '(未填写账单项目)|淘宝天猫|1', ], $this->missingSignature($first['items'])); $this->assertNotContains('上期项目', array_column($first['items'], 'item')); $this->assertNotContains('未知平台项目', array_column($first['items'], 'item')); @@ -259,6 +259,79 @@ class AccountMappingCoverageTest extends TestCase $this->assertSame('平台账单项目', $summary['items'][0]['item']); } + public function test_retained_snapshot_treats_taobao_and_tmall_as_one_platform(): void + { + $store = app(AccountMappingStore::class); + $this->rule($store, '天猫', '两边共同项目'); + + $directory = $this->storageRoot . '/app/account-mapping/coverage-snapshots'; + File::ensureDirectoryExists($directory); + File::put($directory . '/all-taobao-tmall.json', json_encode([ + 'version' => 1, + 'month' => 'all', + 'source_key' => 'taobao-tmall', + 'refreshed_at' => '2026-12-01 08:00:00', + 'raw' => [ + 'status' => 'ready', + 'source_rows' => 5, + 'pending_sources' => [], + 'pairs' => [ + [ + 'platform' => '天猫', + 'platform_label' => '天猫', + 'item' => '两边共同项目', + 'source_rows' => 2, + 'store_count' => 1, + 'source_labels' => ['淘系支付宝'], + 'raw_items' => ['两边共同项目'], + 'months' => ['2026-11'], + ], + [ + 'platform' => '淘宝', + 'platform_label' => '淘宝', + 'item' => '两边共同项目', + 'source_rows' => 1, + 'store_count' => 1, + 'source_labels' => ['淘系支付宝'], + 'raw_items' => ['两边共同项目'], + 'months' => ['2026-10'], + ], + [ + 'platform' => '天猫', + 'platform_label' => '天猫', + 'item' => '统一口径未映射项目', + 'source_rows' => 1, + 'store_count' => 1, + 'source_labels' => ['淘系支付宝'], + 'raw_items' => ['统一口径未映射项目'], + 'months' => ['2026-10'], + ], + [ + 'platform' => '淘宝', + 'platform_label' => '淘宝', + 'item' => '统一口径未映射项目', + 'source_rows' => 1, + 'store_count' => 1, + 'source_labels' => ['淘系微信'], + 'raw_items' => ['统一口径未映射项目'], + 'months' => ['2026-11'], + ], + ], + ], + ], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR)); + + $summary = app(AccountMappingCoverage::class)->missingAcrossMonths(); + + $this->assertSame('ready', $summary['status']); + $this->assertSame(5, $summary['source_rows']); + $this->assertSame(1, $summary['missing_item_count']); + $this->assertSame(2, $summary['missing_row_count']); + $this->assertSame('淘宝天猫', $summary['items'][0]['platform']); + $this->assertSame('统一口径未映射项目', $summary['items'][0]['item']); + $this->assertSame(['2026-10', '2026-11'], $summary['items'][0]['months']); + $this->assertSame(['淘系支付宝', '淘系微信'], $summary['items'][0]['source_labels']); + } + public function test_interrupted_refresh_request_is_recovered_after_stale_timeout(): void { $coverage = app(AccountMappingCoverage::class); diff --git a/tests/Unit/EcommerceFinance/AccountMappingSuggestionTest.php b/tests/Unit/EcommerceFinance/AccountMappingSuggestionTest.php index 5d3472db4..c6f37c916 100644 --- a/tests/Unit/EcommerceFinance/AccountMappingSuggestionTest.php +++ b/tests/Unit/EcommerceFinance/AccountMappingSuggestionTest.php @@ -30,6 +30,18 @@ class AccountMappingSuggestionTest extends TestCase $this->assertSame('冲减收入', $result['values']['memo']); } + public function test_taobao_and_tmall_are_preferred_as_the_same_platform(): void + { + $result = app(AccountMappingSuggestion::class)->suggestFromRules([ + ['id' => 10] + $this->rule('拼多多', '平台技术服务费', '技术服务费', '费用', '销售费用'), + ['id' => 20] + $this->rule('天猫', '平台技术服务费', '技术服务费', '费用', '销售费用'), + ], '淘宝天猫', '平台技术服务费'); + + $this->assertTrue($result['auto_fill']); + $this->assertSame(20, $result['sources'][0]['id']); + $this->assertSame('天猫', $result['sources'][0]['platform']); + } + public function test_conflicting_templates_are_not_applied_automatically(): void { $result = app(AccountMappingSuggestion::class)->suggestFromRules([