按文件导入改为两遍:先挑出真正有标签的订单,再拆行写入
聚水潭导出是一单多行,实测有 97 单是「部分明细行带标签、部分为空」;单遍处理会给这些单多写一行「(无标签)」。改成第一遍收集有标签的订单号,第二遍只对整单无标签的订单写「(无标签)」,并补上对应断言。
This commit is contained in:
parent
8a70fd4482
commit
4bcfde58f7
@ -140,8 +140,9 @@ class JstOrderLabelImporter
|
||||
/**
|
||||
* 从聚水潭「导出订单」Excel 导入。
|
||||
*
|
||||
* 导出文件是一单多行(商品明细),这里按「线上订单号 + 标签」去重后拆行写入;
|
||||
* 同时兼容「导出订单」直接勾选 4 列导出的极简表头。
|
||||
* 导出文件是一单多行(商品明细),且同一单里可能只有部分明细行带标签:
|
||||
* 第一遍先挑出真正有标签的订单号,第二遍再按「线上订单号 + 标签」去重拆行写入,
|
||||
* 只对整单没标签的订单写「(无标签)」。同时兼容只勾 4 列导出的极简表头。
|
||||
*
|
||||
* 传 $from/$to 时会顺带按窗口过滤;带 $replace 时必须给窗口,
|
||||
* 以便在写入前先清掉该区间,避免重复导入翻倍。
|
||||
@ -200,15 +201,32 @@ class JstOrderLabelImporter
|
||||
};
|
||||
|
||||
$reader = $this->exportReader ?? app(JstOrderExportReader::class);
|
||||
$anyRow = false;
|
||||
|
||||
// 第一遍:挑出「有标签」的订单号。聚水潭导出是一单多行,同一单常常只有部分
|
||||
// 明细行带标签;不按订单判断就会给本来有标签的订单多写一行「(无标签)」。
|
||||
$labeledOrders = [];
|
||||
$anyRow = false;
|
||||
foreach ($reader->rows($path, self::EXPORT_COLUMNS) as $line) {
|
||||
$anyRow = true;
|
||||
if (! $cleared) {
|
||||
$clearWindow();
|
||||
$cleared = true;
|
||||
if ($this->splitLabels((string) ($line['标签'] ?? '')) === []) {
|
||||
continue;
|
||||
}
|
||||
$orderNo = trim((string) ($line['线上订单号'] ?? ''));
|
||||
if ($orderNo !== '') {
|
||||
$labeledOrders[$orderNo] = true;
|
||||
}
|
||||
}
|
||||
if (! $anyRow) {
|
||||
throw new RuntimeException('订单导出文件里没有数据行:' . basename($path));
|
||||
}
|
||||
|
||||
if (! $cleared) {
|
||||
$clearWindow();
|
||||
$cleared = true;
|
||||
}
|
||||
|
||||
// 第二遍:按「线上订单号 + 标签」去重后写入。
|
||||
foreach ($reader->rows($path, self::EXPORT_COLUMNS) as $line) {
|
||||
$orderNo = trim((string) ($line['线上订单号'] ?? ''));
|
||||
$orderedAt = $this->normalizeOrderedAt((string) ($line['下单时间'] ?? ''));
|
||||
if ($orderNo === '' || $orderedAt === null) {
|
||||
@ -222,17 +240,21 @@ class JstOrderLabelImporter
|
||||
continue;
|
||||
}
|
||||
|
||||
$labels = $this->splitLabels((string) ($line['标签'] ?? ''));
|
||||
$withoutLabel = $labels === [];
|
||||
if ($withoutLabel) {
|
||||
if (isset($labeledOrders[$orderNo])) {
|
||||
continue;
|
||||
}
|
||||
$labels = [self::NO_LABEL];
|
||||
}
|
||||
|
||||
if (! isset($orderSeen[$orderNo])) {
|
||||
$orderSeen[$orderNo] = true;
|
||||
$orders++;
|
||||
}
|
||||
|
||||
$shopName = mb_substr(trim((string) ($line['店铺名称'] ?? '')), 0, 191);
|
||||
$labels = $this->splitLabels((string) ($line['标签'] ?? ''));
|
||||
$withoutLabel = $labels === [];
|
||||
if ($labels === []) {
|
||||
$labels = [self::NO_LABEL];
|
||||
}
|
||||
|
||||
foreach ($labels as $label) {
|
||||
$fingerprint = hash('xxh3', $orderNo . "\0" . $label);
|
||||
@ -261,13 +283,6 @@ class JstOrderLabelImporter
|
||||
}
|
||||
}
|
||||
|
||||
if (! $anyRow) {
|
||||
throw new RuntimeException('订单导出文件里没有数据行:' . basename($path));
|
||||
}
|
||||
if (! $cleared) {
|
||||
$clearWindow();
|
||||
}
|
||||
|
||||
$flush();
|
||||
|
||||
$labelQuery = $target->table('jst_order_labels')->where('company_id', $companyId);
|
||||
|
||||
@ -124,6 +124,8 @@ class JstOrderLabelsPageTest extends FeatureTestCase
|
||||
['6476896', '3424493003753167', '京东-柯诺苏家具旗舰店', '2026-03-01 01:30:52', '发货后售后,店铺-京东,晚发赔'],
|
||||
['6476897', '3424493003753167', '京东-柯诺苏家具旗舰店', '2026-03-01 01:30:52', '发货后售后,店铺-京东,晚发赔'],
|
||||
['6476898', '3424493003753167', '京东-柯诺苏家具旗舰店', '2026-03-01 01:30:52', '发货后售后'],
|
||||
// 同一单里只有部分明细行带标签:不能因此多写一行「(无标签)」
|
||||
['6476899', '3424493003753167', '京东-柯诺苏家具旗舰店', '2026-03-01 01:30:52', ''],
|
||||
['6477350', '110197275064171099', '得物-米客米家家居旗舰店', '2026-03-01 08:05:36', ''],
|
||||
['9535498', '3290154852795260451', '淘宝-亿家达家居鞋柜电脑桌', '2026-03-11 19:01:30', '历史订单'],
|
||||
]);
|
||||
@ -134,11 +136,16 @@ class JstOrderLabelsPageTest extends FeatureTestCase
|
||||
@unlink($path);
|
||||
}
|
||||
|
||||
// 同一订单的多条明细行只算一单,重复标签只写一行
|
||||
// 同一订单的多条明细行只算一单,重复标签只写一行,
|
||||
// 且「部分明细行没标签」不会污染成「(无标签)」
|
||||
$this->assertSame(3, $result['orders']);
|
||||
$this->assertSame(5, $result['rows']);
|
||||
$this->assertSame(1, $result['no_label_orders']);
|
||||
|
||||
$this->assertSame(0, JstOrderLabel::query()->where('company_id', $this->company->id)
|
||||
->where('order_no', '3424493003753167')
|
||||
->where('label', JstOrderLabelImporter::NO_LABEL)->count());
|
||||
|
||||
$labels = JstOrderLabel::query()->where('company_id', $this->company->id)
|
||||
->where('order_no', '3424493003753167')->pluck('label')->sort()->values()->all();
|
||||
$this->assertSame(['发货后售后', '店铺-京东', '晚发赔'], $labels);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user