兼容店铺余额名称前缀匹配 #650

Merged
yutu merged 1 commits from fix/92451-ledger-shop-balance-name-match into main 2026-10-26 20:40:53 +08:00
2 changed files with 38 additions and 10 deletions

View File

@ -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<string,object> */
/** @return array{exact:array<string,object>,normalized:array<string,object>} */
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

View File

@ -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('历史主体');
}