From eb71aa7a2b36618d2164549e70d457052f11b08f Mon Sep 17 00:00:00 2001 From: 92451 <924518135@qq.com> Date: Mon, 26 Oct 2026 20:40:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E5=BA=97=E9=93=BA=E4=BD=99?= =?UTF-8?q?=E9=A2=9D=E5=90=8D=E7=A7=B0=E5=89=8D=E7=BC=80=E5=8C=B9=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/FinanceLedgerShopDirectory.php | 37 ++++++++++++++----- .../FinanceLedgerShopDirectoryTest.php | 11 ++++++ 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/modules/EcommerceFinance/Services/FinanceLedgerShopDirectory.php b/modules/EcommerceFinance/Services/FinanceLedgerShopDirectory.php index 4ffa8f6d..058c6562 100644 --- a/modules/EcommerceFinance/Services/FinanceLedgerShopDirectory.php +++ b/modules/EcommerceFinance/Services/FinanceLedgerShopDirectory.php @@ -5,6 +5,7 @@ namespace Modules\EcommerceFinance\Services; use Illuminate\Database\DatabaseManager; use Illuminate\Support\Facades\Schema; use Modules\EcommerceFinance\Repositories\BusinessInformationMirrorRepository; +use Modules\EcommerceFinance\Repositories\JushuitanShopBalanceRepository; use Throwable; /** @@ -81,7 +82,11 @@ class FinanceLedgerShopDirectory } } - $financeShop = $financeShops[$erpStoreName] ?? null; + $financeShop = $financeShops['exact'][$erpStoreName] ?? null; + if ($financeShop === null) { + $normalized = JushuitanShopBalanceRepository::normalizeStoreName($erpStoreName); + $financeShop = $financeShops['normalized'][$normalized] ?? null; + } $rows[] = [ 'id' => (int) ($store['id'] ?? 0), 'platform' => $this->nullableString($store['platform'] ?? null), @@ -101,24 +106,36 @@ class FinanceLedgerShopDirectory return $rows; } - /** @return array */ + /** @return array{exact:array,normalized:array} */ private function financeShops(): array { $connection = $this->databases->connection( (string) config('upstream_mirrors.connection', 'business_mirror'), ); if (! Schema::connection($connection->getName())->hasTable('fin_shops')) { - return []; + return ['exact' => [], 'normalized' => []]; } - return $connection->table('fin_shops') - ->get() - ->mapWithKeys(static function (object $row): array { - $name = trim((string) ($row->shop_name ?? '')); + $rows = $connection->table('fin_shops')->get(); + $exact = []; + $normalizedGroups = []; + foreach ($rows as $row) { + $name = trim((string) ($row->shop_name ?? '')); + if ($name === '') { + continue; + } + $exact[$name] = $row; + $normalizedGroups[JushuitanShopBalanceRepository::normalizeStoreName($name)][] = $row; + } - return $name === '' ? [] : [$name => $row]; - }) - ->all(); + return [ + 'exact' => $exact, + // 规范化名称重复时置空,避免跨平台误匹配。 + 'normalized' => array_map( + static fn (array $group): ?object => count($group) === 1 ? $group[0] : null, + $normalizedGroups, + ), + ]; } private function nullableString(mixed $value): ?string diff --git a/tests/Feature/Business/FinanceLedgerShopDirectoryTest.php b/tests/Feature/Business/FinanceLedgerShopDirectoryTest.php index cfa17815..88326eb3 100644 --- a/tests/Feature/Business/FinanceLedgerShopDirectoryTest.php +++ b/tests/Feature/Business/FinanceLedgerShopDirectoryTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Business; use App\Models\Auth\Permission; +use Illuminate\Support\Facades\DB; use Modules\EcommerceFinance\Repositories\BusinessInformationMirrorRepository; use Modules\EcommerceFinance\Services\StoreReportPeriodBuilder; use Tests\Feature\FeatureTestCase; @@ -20,6 +21,15 @@ class FinanceLedgerShopDirectoryTest extends FeatureTestCase public function test_shop_ledger_uses_store_report_current_subject_and_platform(): void { + DB::connection('business_mirror')->table('fin_shops')->insert([ + 'shop_name' => '报表测试店', + 'platform' => '天猫', + 'shop_balance' => 123.45, + 'shop_balance_date' => '2026-10-25', + 'created_at' => now(), + 'updated_at' => now(), + ]); + $openStore = [ 'id' => 901, 'platform' => '天猫', @@ -78,6 +88,7 @@ class FinanceLedgerShopDirectoryTest extends FeatureTestCase ->assertSeeText('淘宝-无主体店') ->assertSeeText('在营') ->assertSeeText('回款账户甲') + ->assertSeeText('123.45') ->assertDontSeeText('京东-已关店') ->assertDontSeeText('历史主体'); } -- 2.47.2