From 304bb3dc8e12c3dbbfb148cee4c01021549f6a66 Mon Sep 17 00:00:00 2001 From: qiuxiang <394405259@qq.com> Date: Wed, 30 Sep 2026 09:55:51 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=8D=E8=8F=9C=E6=98=8E=E7=BB=86=E8=A1=A8?= =?UTF-8?q?=EF=BC=9AID=20=E5=88=97=E6=94=BE=E5=AE=BD=E4=B8=BA=20text=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=A4=9A=E8=A1=8C=20ID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 源表「ID」列存在一个单元格多行放多个 ID 的行(导入时报 Data too long for column external_id)。新增迁移把 external_id 放宽为 text,导入器按 500 字符保留原文,并补用例覆盖多行 ID。 --- app/Business/PlantingDetailImporter.php | 7 ++--- ...000_widen_planting_details_external_id.php | 26 +++++++++++++++++++ .../Business/PlantingDetailsPageTest.php | 17 ++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 database/migrations/2026_09_30_130000_widen_planting_details_external_id.php 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');