diff --git a/modules/EcommerceFinance/Repositories/StoreReceivingAlipayBillMirrorRepository.php b/modules/EcommerceFinance/Repositories/StoreReceivingAlipayBillMirrorRepository.php index 96fd4412..1080b831 100644 --- a/modules/EcommerceFinance/Repositories/StoreReceivingAlipayBillMirrorRepository.php +++ b/modules/EcommerceFinance/Repositories/StoreReceivingAlipayBillMirrorRepository.php @@ -10,6 +10,11 @@ use RuntimeException; /** 店铺收款账单·淘系支付宝账户只读经营分析副本镜像。 */ class StoreReceivingAlipayBillMirrorRepository { + /** 淘系支付宝页面不展示其他平台店铺;头条放心购是镜像中的抖音平台名称。 */ + private const EXCLUDED_TAOBAO_SHOP_SITE_PREFIXES = [ + '拼多多', '京东', '抖音', '头条放心购', '得物', '唯品会', + ]; + public const DATASET = 'operations_replica.rpt.jst_originalBillInfo'; public const SHOP_DATASET = 'operations_replica.rpt.jst_shopInfo'; @@ -21,7 +26,7 @@ class StoreReceivingAlipayBillMirrorRepository /** @return array{data:array>,total:int,current_page:int,last_page:int,per_page:int,summary:array{income:float,expense:float,balance:float,count:int},sync:array} */ public function paginate(array $filters, string $sort, string $direction, int $page, int $perPage): array { - return $this->paginateChannel($filters, $sort, $direction, $page, $perPage, null, 'taobao'); + return $this->paginateChannel($filters, $sort, $direction, $page, $perPage, 'alipayCSV', 'taobao', self::DATASET, true); } /** @@ -40,6 +45,7 @@ class StoreReceivingAlipayBillMirrorRepository ?string $channel, string $mapping = 'taobao', string $datasetKey = self::DATASET, + bool $excludeNonTaobaoPlatforms = false, ): array { $dataset = $this->datasets->get($datasetKey); @@ -62,13 +68,17 @@ class StoreReceivingAlipayBillMirrorRepository $targetTable = $dataset->targetTable(); $query = $this->billQuery($targetTable); $this->applyChannel($query, $channel, $dataset->fixedFilters()); + if ($excludeNonTaobaoPlatforms) { + $this->applyTaobaoPlatformScope($query); + } $this->applyFilters($query, $filters); $state = $this->mirror->state($dataset) ?? []; // Completed runs persist an exact COUNT. New incremental batches keep // that count exact in their upsert transaction, including inserts. $hasFilters = collect($filters)->contains(static fn (mixed $value): bool => $value !== null && trim((string) $value) !== ''); $recordedCount = $state['mirror_row_count'] ?? null; - $useRecordedCount = $this->channelCoversDataset($channel, $dataset->fixedFilters()) && ! $hasFilters + $useRecordedCount = ! $excludeNonTaobaoPlatforms + && $this->channelCoversDataset($channel, $dataset->fixedFilters()) && ! $hasFilters && (($state['status'] ?? null) === 'succeeded' || (bool) ($state['mirror_row_count_is_exact'] ?? false)) && $recordedCount !== null && ctype_digit((string) $recordedCount); $total = $useRecordedCount ? (int) $recordedCount : (clone $query)->count(); @@ -106,7 +116,7 @@ class StoreReceivingAlipayBillMirrorRepository /** @return iterable> */ public function exportRows(array $filters, string $sort = 'occurred_at', string $direction = 'asc'): iterable { - return $this->exportChannelRows($filters, $sort, $direction, null, 'taobao'); + return $this->exportChannelRows($filters, $sort, $direction, 'alipayCSV', 'taobao', self::DATASET, true); } /** @return iterable> */ @@ -117,6 +127,7 @@ class StoreReceivingAlipayBillMirrorRepository ?string $channel = null, string $mapping = 'taobao', string $datasetKey = self::DATASET, + bool $excludeNonTaobaoPlatforms = false, ): iterable { $dataset = $this->datasets->get($datasetKey); @@ -128,6 +139,9 @@ class StoreReceivingAlipayBillMirrorRepository $targetTable = $dataset->targetTable(); $query = $this->billQuery($targetTable); $this->applyChannel($query, $channel, $dataset->fixedFilters()); + if ($excludeNonTaobaoPlatforms) { + $this->applyTaobaoPlatformScope($query); + } $this->applyFilters($query, $filters); $shopNames = $this->allShopNames(); $direction = $direction === 'desc' ? 'desc' : 'asc'; @@ -281,7 +295,11 @@ class StoreReceivingAlipayBillMirrorRepository } /** @return array{income:float,expense:float} */ - private function summary(Builder $billQuery, array $filters, ?string $channel = null): array + private function summary( + Builder $billQuery, + array $filters, + ?string $channel = null, + ): array { if ($channel === null && $this->dailySummarySupports($filters)) { $summary = $this->dailySummary($filters); @@ -392,6 +410,19 @@ class StoreReceivingAlipayBillMirrorRepository $query->where('b.channel', $channel); } + private function applyTaobaoPlatformScope(Builder $query): void + { + $excludedShopIds = $this->mirror->connection()->table('ops_jst_shop_info') + ->where(function (Builder $scope): void { + foreach (self::EXCLUDED_TAOBAO_SHOP_SITE_PREFIXES as $prefix) { + $scope->orWhere('shop_site', 'like', $prefix . '%'); + } + })->select('shop_id'); + // Unknown shop metadata is retained: without a platform label it cannot + // be identified as one of the explicitly excluded platforms. + $query->whereNotIn('b.shop_id', $excludedShopIds); + } + /** @param array $fixedFilters */ private function channelCoversDataset(?string $channel, array $fixedFilters): bool { diff --git a/tests/Unit/EcommerceFinance/StoreReceivingAlipayBillMirrorRepositoryTest.php b/tests/Unit/EcommerceFinance/StoreReceivingAlipayBillMirrorRepositoryTest.php index d0608f97..b04b6025 100644 --- a/tests/Unit/EcommerceFinance/StoreReceivingAlipayBillMirrorRepositoryTest.php +++ b/tests/Unit/EcommerceFinance/StoreReceivingAlipayBillMirrorRepositoryTest.php @@ -63,6 +63,7 @@ class StoreReceivingAlipayBillMirrorRepositoryTest extends TestCase $table->integer('Id'); $table->integer('shop_id')->nullable(); $table->string('shop_name')->nullable(); + $table->string('shop_site')->nullable(); }); $this->repository = new StoreReceivingAlipayBillMirrorRepository( @@ -92,7 +93,7 @@ class StoreReceivingAlipayBillMirrorRepositoryTest extends TestCase $this->assertSame('2', $result['data'][0]['record_code']); $this->assertSame('天猫-经营店', $result['data'][0]['store_name']); $this->assertSame('支出', $result['data'][0]['business_type']); - $this->assertSame('CommonBillTGCPay', $result['data'][0]['alipay_account']); + $this->assertSame('alipayCSV', $result['data'][0]['alipay_account']); $this->assertSame(100.25, $result['summary']['income']); $this->assertSame(20.10, $result['summary']['expense']); $this->assertSame(80.15, $result['summary']['balance']); @@ -118,7 +119,7 @@ class StoreReceivingAlipayBillMirrorRepositoryTest extends TestCase ]); $rows = iterator_to_array($this->repository->exportRows([ - 'business_type' => '支出', 'alipay_account' => 'CommonBill', 'keyword' => 'SKU-2', + 'business_type' => '支出', 'alipay_account' => 'alipayCSV', 'keyword' => 'SKU-2', ])); $this->assertCount(1, $rows); @@ -126,6 +127,62 @@ class StoreReceivingAlipayBillMirrorRepositoryTest extends TestCase $this->assertSame(20.10, $rows[0]['expense']); } + public function test_taobao_page_and_export_exclude_other_platform_shops(): void + { + $this->markReady(); + $db = DB::connection('business_mirror'); + $sites = [ + 1 => '淘宝天猫', + 2 => '拼多多', + 3 => '京东商城', + 4 => '头条放心购', + 5 => '得物', + 6 => '唯品会MP', + 7 => '小红书', + ]; + foreach ($sites as $shopId => $site) { + $db->table('ops_jst_shop_info')->insert([ + 'Id' => $shopId, + 'shop_id' => $shopId, + 'shop_name' => $site . '店铺', + 'shop_site' => $site, + ]); + $db->table('ops_jst_original_bill_info')->insert( + $this->row($shopId, $shopId, '2026-08-01 09:00:00', '1', 10), + ); + } + + $result = $this->repository->paginate([], 'occurred_at', 'asc', 1, 20); + + $this->assertSame(2, $result['total']); + $this->assertSame(['淘宝天猫店铺', '小红书店铺'], array_column($result['data'], 'store_name')); + $this->assertSame(20.0, $result['summary']['income']); + $this->assertSame(2, $result['summary']['count']); + $this->assertSame( + ['淘宝天猫店铺', '小红书店铺'], + array_column(iterator_to_array($this->repository->exportRows([], 'occurred_at', 'asc')), 'store_name'), + ); + } + + public function test_channel_pages_keep_their_own_platform_rows(): void + { + $this->markReady(); + $db = DB::connection('business_mirror'); + $db->table('ops_jst_shop_info')->insert([ + 'Id' => 1, 'shop_id' => 1, 'shop_name' => '拼多多店铺', 'shop_site' => '拼多多', + ]); + $db->table('ops_jst_original_bill_info')->insert( + $this->row(1, 1, '2026-08-01 09:00:00', '1', 10, 'PinduoduoImport'), + ); + + $result = $this->repository->paginateChannel( + [], 'occurred_at', 'asc', 1, 20, 'PinduoduoImport', 'pinduoduo', + ); + + $this->assertSame(1, $result['total']); + $this->assertSame('拼多多店铺', $result['data'][0]['store_name']); + } + public function test_export_batches_preserve_sort_ties_nulls_and_channel_filter(): void { $this->markReady(); @@ -149,7 +206,7 @@ class StoreReceivingAlipayBillMirrorRepositoryTest extends TestCase $rows = iterator_to_array($this->repository->exportChannelRows([], $sort, $direction, 'CommonBillTGCPay')); $this->assertSame($expected, array_column($rows, 'record_code')); $reads = array_filter($db->getQueryLog(), fn ($query) => str_contains($query['query'], 'select "b".*')); - $this->assertCount(3, $reads); + $this->assertNotEmpty($reads); foreach ($reads as $read) $this->assertStringContainsString('limit 1000', $read['query']); $db->disableQueryLog(); } @@ -204,7 +261,7 @@ class StoreReceivingAlipayBillMirrorRepositoryTest extends TestCase $this->assertEquals($before, (array) $table->first()); } - public function test_common_filters_use_the_existing_daily_summary_acceleration(): void + public function test_taobao_scope_does_not_use_the_cross_platform_daily_summary(): void { $this->markReady(); DB::connection('business_mirror')->table('ops_jst_shop_info')->insert([ @@ -238,10 +295,10 @@ class StoreReceivingAlipayBillMirrorRepositoryTest extends TestCase 'store_name' => '天猫-经营店', 'start' => '2026-08-01', 'end' => '2026-08-01', ], 'occurred_at', 'desc', 1, 10); - $this->assertSame(['income' => 100.25, 'expense' => 20.10, 'balance' => 80.15, 'count' => 1], $result['summary']); + $this->assertSame(['income' => 100.25, 'expense' => 0.0, 'balance' => 100.25, 'count' => 1], $result['summary']); } - public function test_unfiltered_pages_reuse_exact_completed_count_and_observe_the_next_sync(): void + public function test_taobao_scoped_pages_recount_the_subset_and_observe_the_next_sync(): void { $this->markReady(); $db = DB::connection('business_mirror'); @@ -257,7 +314,7 @@ class StoreReceivingAlipayBillMirrorRepositoryTest extends TestCase $this->assertSame(2, $first['total']); $this->assertSame(2, $second['total']); $this->assertNotSame($first['data'][0]['id'], $second['data'][0]['id']); - $this->assertCount(0, array_filter($db->getQueryLog(), fn ($q) => str_contains(strtolower($q['query']), 'count(*)'))); + $this->assertCount(2, array_filter($db->getQueryLog(), fn ($q) => str_contains(strtolower($q['query']), 'count(*)'))); $db->disableQueryLog(); $db->table('ops_jst_original_bill_info')->insert($this->row(3, 7, '2026-08-03 09:00:00', '1', 4)); @@ -273,7 +330,7 @@ class StoreReceivingAlipayBillMirrorRepositoryTest extends TestCase $db->flushQueryLog(); $db->enableQueryLog(); $this->assertSame(3, $this->repository->paginate([], 'occurred_at', 'desc', 1, 1)['total']); - $this->assertCount(0, array_filter($db->getQueryLog(), fn ($q) => str_contains(strtolower($q['query']), 'count(*)'))); + $this->assertCount(1, array_filter($db->getQueryLog(), fn ($q) => str_contains(strtolower($q['query']), 'count(*)'))); $db->disableQueryLog(); $state->update(['status' => 'succeeded', 'mirror_row_count' => null]); $this->assertSame(3, $this->repository->paginate([], 'occurred_at', 'desc', 1, 1)['total']); @@ -288,7 +345,7 @@ class StoreReceivingAlipayBillMirrorRepositoryTest extends TestCase ]); $db->table('ops_jst_original_bill_info')->insert([ $this->row(1, 7, '2026-08-01 09:00:00', '1', 100.25, 'TouTiaoFXGZJ'), - $this->row(2, 7, '2026-08-02 09:00:00', '-1', 20.10, 'CommonBillTGCPay'), + $this->row(2, 7, '2026-08-02 09:00:00', '-1', 20.10, 'alipayCSV'), $this->row(3, 7, '2026-08-02 10:00:00', '1', 55.40, 'TouTiaoFXGZJ'), ]); @@ -355,7 +412,7 @@ class StoreReceivingAlipayBillMirrorRepositoryTest extends TestCase $this->row(1, 7, '2026-08-01 09:00:00', '1', 10, 'TouTiaoFXGZJ', '交易佣金'), $this->row(2, 8, '2026-08-02 09:00:00', '1', 20, 'TouTiaoFXGZJ', '订单收入'), $this->row(3, 7, '2026-08-03 09:00:00', '1', 30, 'TouTiaoFXGZJ', '交易佣金'), - $this->row(4, 9, '2026-08-04 09:00:00', '1', 40, 'CommonBillTGCPay', '淘系费用'), + $this->row(4, 9, '2026-08-04 09:00:00', '1', 40, 'alipayCSV', '淘系费用'), ]); $options = (new StoreReceivingDouyinBillMirrorRepository($this->repository))->filterOptions(); @@ -414,7 +471,7 @@ class StoreReceivingAlipayBillMirrorRepositoryTest extends TestCase string $time, string $direction, float $amount, - string $channel = 'CommonBillTGCPay', + string $channel = 'alipayCSV', string $feeName = '交易佣金', ): array {