diff --git a/app/Business/SupplierSummaryInboundImporter.php b/app/Business/SupplierSummaryInboundImporter.php index b7fba151..d792c784 100644 --- a/app/Business/SupplierSummaryInboundImporter.php +++ b/app/Business/SupplierSummaryInboundImporter.php @@ -16,7 +16,8 @@ use ZipArchive; /** * 供应商汇总表月度增量导入。 * - * 仅追加目标月份的新行,不删除其他月份;已存在的同一业务行按内容摘要跳过。 + * 仅追加目标月份的新行,不删除其他月份。内容相同的源明细按出现次序分配 + * 稳定标识后全部保留;重复执行时只跳过已经写入的同一源行。 * 单行字段异常会进入失败清单,不阻断其他有效行。 */ class SupplierSummaryInboundImporter @@ -84,7 +85,7 @@ class SupplierSummaryInboundImporter $mapped = 0; $failed = 0; $reasons = []; - $seen = []; + $occurrences = []; $batch = []; DB::connection($connection)->transaction(function () use ( @@ -98,7 +99,7 @@ class SupplierSummaryInboundImporter &$mapped, &$failed, &$reasons, - &$seen, + &$occurrences, &$batch, ): void { $flush = function () use ( @@ -197,11 +198,15 @@ class SupplierSummaryInboundImporter continue; } - if (isset($seen[$record['row_key']])) { - $duplicates++; - continue; + $baseKey = (string) $record['row_key']; + $occurrence = ($occurrences[$baseKey] ?? 0) + 1; + $occurrences[$baseKey] = $occurrence; + if ($occurrence > 1) { + $record['row_key'] = hash( + 'sha256', + $baseKey . '|occurrence:' . $occurrence, + ); } - $seen[$record['row_key']] = true; $mapped++; $batch[] = $record; diff --git a/tests/Feature/Console/SupplierSummaryInboundImportTest.php b/tests/Feature/Console/SupplierSummaryInboundImportTest.php index 315aaec8..02ffd930 100644 --- a/tests/Feature/Console/SupplierSummaryInboundImportTest.php +++ b/tests/Feature/Console/SupplierSummaryInboundImportTest.php @@ -10,7 +10,7 @@ use Tests\Feature\FeatureTestCase; class SupplierSummaryInboundImportTest extends FeatureTestCase { - public function test_monthly_import_appends_rows_and_reports_duplicates_and_failures(): void + public function test_monthly_import_preserves_identical_rows_and_reports_duplicates_and_failures(): void { $provider = '测试仓储方-' . uniqid(); InventoryWarehouseNameMapping::query()->create([ @@ -47,15 +47,15 @@ class SupplierSummaryInboundImportTest extends FeatureTestCase '--month' => '2026-09', '--result-json' => $firstResult, ]) - ->expectsOutputToContain('成功=1') + ->expectsOutputToContain('成功=2') ->expectsOutputToContain('另有 2 条失败原因') ->assertExitCode(1); $result = json_decode((string) file_get_contents($firstResult), true); $this->assertSame('partial', $result['status']); $this->assertSame('2026-09-01', $result['report_month']); - $this->assertSame(1, $result['success']); - $this->assertSame(1, $result['duplicates']); + $this->assertSame(2, $result['success']); + $this->assertSame(0, $result['duplicates']); $this->assertSame(12, $result['failed']); $this->assertSame(12, $result['failure_reason_count']); $this->assertSame(4, $result['failure_reasons'][0]['row']); @@ -73,12 +73,16 @@ class SupplierSummaryInboundImportTest extends FeatureTestCase $september = DB::connection($connection)->table('inventory_inbound_detail_rows') ->where('report_month', '2026-09-01') ->get(); - $this->assertCount(1, $september); + $this->assertCount(2, $september); $this->assertSame('BARCODE-1', $september->first()->barcode); $this->assertSame('测试简称', $september->first()->warehouse_short_name); $this->assertSame('2026-09-03 10:00:00', $september->first()->created_at_raw); $this->assertEquals(20, $september->first()->amount); $this->assertEquals(22, $september->first()->estimated_amount); + $this->assertNotSame( + $september[0]->row_key, + $september[1]->row_key, + ); $this->artisan('inventory:supplier-summary:import', [ 'file' => $path, @@ -92,7 +96,7 @@ class SupplierSummaryInboundImportTest extends FeatureTestCase $this->assertSame(2, $second['duplicates']); $this->assertSame(12, $second['failed']); $this->assertSame( - 1, + 2, DB::connection($connection)->table('inventory_inbound_detail_rows') ->where('report_month', '2026-09-01')->count(), );