种菜明细表:ID 列放宽为 text 支持多行 ID

源表「ID」列存在一个单元格多行放多个 ID 的行(导入时报 Data too long for column external_id)。新增迁移把 external_id 放宽为 text,导入器按 500 字符保留原文,并补用例覆盖多行 ID。
This commit is contained in:
qiuxiang 2026-09-30 09:55:51 +08:00
parent b106569905
commit 304bb3dc8e
3 changed files with 47 additions and 3 deletions

View File

@ -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

View File

@ -0,0 +1,26 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* 源表「ID」列存在一个单元格里放多个 ID(换行分隔)的情况,varchar(100) 装不下,
* 放宽为 text,保证导入不丢原始内容。
*/
public function up(): void
{
Schema::table('planting_details', function (Blueprint $table) {
$table->text('external_id')->nullable()->change();
});
}
public function down(): void
{
Schema::table('planting_details', function (Blueprint $table) {
$table->string('external_id', 100)->nullable()->change();
});
}
};

View File

@ -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');