diff --git a/app/Business/PlantingDetailImporter.php b/app/Business/PlantingDetailImporter.php index fd7d9b13..06d4f2a6 100644 --- a/app/Business/PlantingDetailImporter.php +++ b/app/Business/PlantingDetailImporter.php @@ -124,7 +124,8 @@ class PlantingDetailImporter 'tag' => $this->toText($this->cell($gridRow, $map, 'tag')), 'profit_center' => $this->toText($this->cell($gridRow, $map, 'profit_center')), 'responsible_platform' => $this->toText($this->cell($gridRow, $map, 'responsible_platform')), - 'external_id' => $this->toText($this->cell($gridRow, $map, 'external_id')), + // 源表存在一个单元格放多个 ID(换行分隔)的情况,这里保留到 500 字符。 + 'external_id' => $this->toText($this->cell($gridRow, $map, 'external_id'), 500), 'created_from' => $source, 'created_by' => $userId, 'created_at' => $now, @@ -271,7 +272,7 @@ class PlantingDetailImporter return $timestamp === false ? null : date('Y-m-d', $timestamp); } - private function toText(mixed $value): ?string + private function toText(mixed $value, int $limit = 191): ?string { if ($value === null) { return null; @@ -279,7 +280,7 @@ class PlantingDetailImporter $text = trim((string) (is_scalar($value) ? $value : '')); - return $text === '' ? null : mb_substr($text, 0, 191); + return $text === '' ? null : mb_substr($text, 0, $limit); } private function toNumber(mixed $value): ?float diff --git a/database/migrations/2026_09_30_130000_widen_planting_details_external_id.php b/database/migrations/2026_09_30_130000_widen_planting_details_external_id.php new file mode 100644 index 00000000..72621696 --- /dev/null +++ b/database/migrations/2026_09_30_130000_widen_planting_details_external_id.php @@ -0,0 +1,26 @@ +text('external_id')->nullable()->change(); + }); + } + + public function down(): void + { + Schema::table('planting_details', function (Blueprint $table) { + $table->string('external_id', 100)->nullable()->change(); + }); + } +}; diff --git a/tests/Feature/Business/PlantingDetailsPageTest.php b/tests/Feature/Business/PlantingDetailsPageTest.php index de9c4916..03bc704e 100644 --- a/tests/Feature/Business/PlantingDetailsPageTest.php +++ b/tests/Feature/Business/PlantingDetailsPageTest.php @@ -104,6 +104,23 @@ class PlantingDetailsPageTest extends FeatureTestCase $this->assertSame('运营中心A', $record->profit_center); } + /** 源表 ID 列存在「一个格子多个 ID、换行分隔」的行,导入要保留而不是报错截断。 */ + public function test_importer_keeps_multi_line_external_ids(): void + { + $longId = implode("\n", ['10205993844217', '10205981846604', '10205993844217', '10210832117878']); + $path = $this->writeFile('planting-multi-id.csv'); + $handle = fopen($path, 'wb'); + fputcsv($handle, ['日期', '订单编号', '财务售价', '财务运费', '财务佣金', '京喜差价', '扣款', '财务空包费', '财务打款金额', '笔数', '备注', '正确店铺', '负责人', '账户', '类型', '创建人', '标签', '所属利润中心', '负责平台', 'ID']); + fputcsv($handle, ['2026-03-05', 'ORDER-9', '1356', '0', '0', '0', '0', '0', '1356', '1', '', '京喜-招财邸喜店工厂店', '李金林', '6228760405003956861', '', '骆冰', '', '运营中心A', '', $longId]); + fclose($handle); + + $result = app(PlantingDetailImporter::class)->import($path, $this->company->id, true, null, 'test-multi-id'); + $this->assertSame(1, $result['inserted']); + + $record = PlantingDetail::query()->where('company_id', $this->company->id)->firstOrFail(); + $this->assertSame($longId, $record->external_id); + } + private function csvFixture(): string { $path = $this->writeFile('planting.csv');