diff --git a/app/Business/JstOrderLabelImporter.php b/app/Business/JstOrderLabelImporter.php index c5564a3f..886decc2 100644 --- a/app/Business/JstOrderLabelImporter.php +++ b/app/Business/JstOrderLabelImporter.php @@ -15,11 +15,16 @@ use RuntimeException; * 两个数据源: * - 共享镜像 ops_jst_orders(常规,覆盖镜像有数据的区间); * - 聚水潭「导出订单」Excel(镜像没有的历史区间,例如 2026-03,只能靠财务导出补)。 + * + * 卖家备注包含「SSDD」时同时生成 SSDD 标签。卖家备注不是聚水潭系统标签, + * 但种菜判断需要把这类订单纳入标签明细。 */ class JstOrderLabelImporter { public const NO_LABEL = '(无标签)'; + public const SELLER_REMARK_LABEL = 'SSDD'; + private const READ_CHUNK = 2000; private const INSERT_CHUNK = 2000; @@ -27,6 +32,8 @@ class JstOrderLabelImporter /** 聚水潭导出文件里用到的表头。 */ private const EXPORT_COLUMNS = ['线上订单号', '店铺名称', '下单时间', '标签']; + private const EXPORT_OPTIONAL_COLUMNS = ['卖家备注']; + public function __construct( private readonly DatabaseManager $databases, private readonly ?XlsxSheetReader $exportReader = null, @@ -98,7 +105,10 @@ class JstOrderLabelImporter continue; } - $labels = $this->splitLabels((string) ($record->labels ?? '')); + $labels = $this->labelsForRecord( + (string) ($record->labels ?? ''), + (string) ($record->remark ?? ''), + ); if ($labels === []) { $labels = [self::NO_LABEL]; $noLabelOrders++; @@ -221,9 +231,12 @@ class JstOrderLabelImporter // 明细行带标签;不按订单判断就会给本来有标签的订单多写一行「(无标签)」。 $labeledOrders = []; $anyRow = false; - foreach ($reader->rows($path, self::EXPORT_COLUMNS) as $line) { + foreach ($reader->rows($path, self::EXPORT_COLUMNS, self::EXPORT_OPTIONAL_COLUMNS) as $line) { $anyRow = true; - if ($this->splitLabels((string) ($line['标签'] ?? '')) === []) { + if ($this->labelsForRecord( + (string) ($line['标签'] ?? ''), + (string) ($line['卖家备注'] ?? ''), + ) === []) { continue; } $orderNo = trim((string) ($line['线上订单号'] ?? '')); @@ -241,7 +254,7 @@ class JstOrderLabelImporter } // 第二遍:按「线上订单号 + 标签」去重后写入。 - foreach ($reader->rows($path, self::EXPORT_COLUMNS) as $line) { + foreach ($reader->rows($path, self::EXPORT_COLUMNS, self::EXPORT_OPTIONAL_COLUMNS) as $line) { $orderNo = trim((string) ($line['线上订单号'] ?? '')); $orderedAt = $this->normalizeOrderedAt((string) ($line['下单时间'] ?? '')); if ($orderNo === '' || $orderedAt === null) { @@ -255,7 +268,10 @@ class JstOrderLabelImporter continue; } - $labels = $this->splitLabels((string) ($line['标签'] ?? '')); + $labels = $this->labelsForRecord( + (string) ($line['标签'] ?? ''), + (string) ($line['卖家备注'] ?? ''), + ); $withoutLabel = $labels === []; if ($withoutLabel) { if (isset($labeledOrders[$orderNo])) { @@ -350,6 +366,18 @@ class JstOrderLabelImporter return array_values(array_unique($out)); } + /** @return list */ + private function labelsForRecord(string $labels, string $sellerRemark): array + { + $labels = $this->splitLabels($labels); + if (stripos($sellerRemark, self::SELLER_REMARK_LABEL) !== false + && ! in_array(self::SELLER_REMARK_LABEL, $labels, true)) { + $labels[] = self::SELLER_REMARK_LABEL; + } + + return $labels; + } + private function normalizeDate(string $value, string $label): string { $value = trim($value); diff --git a/tests/Feature/Business/JstOrderLabelsPageTest.php b/tests/Feature/Business/JstOrderLabelsPageTest.php index 023a6935..ae4c1c68 100644 --- a/tests/Feature/Business/JstOrderLabelsPageTest.php +++ b/tests/Feature/Business/JstOrderLabelsPageTest.php @@ -33,6 +33,7 @@ class JstOrderLabelsPageTest extends FeatureTestCase $table->string('shop_name')->nullable(); $table->dateTime('order_date')->nullable(); $table->text('labels')->nullable(); + $table->text('remark')->nullable(); }); DB::connection('business_mirror')->table('ops_jst_orders')->insert([ @@ -109,6 +110,43 @@ class JstOrderLabelsPageTest extends FeatureTestCase $this->assertSame(3, $result['orders']); } + public function test_import_uses_ssdd_seller_remark_as_a_planting_label(): void + { + $this->allowPage(); + DB::connection('business_mirror')->table('ops_jst_orders')->insert([ + 'Id' => 5, + 'o_id' => 105, + 'so_id' => '260915-SSDD-REMARK', + 'shop_name' => '拼多多-索菲亚书房官方旗舰店', + 'order_date' => '2026-09-15 11:00:00', + 'labels' => '', + 'remark' => 'SSDD。特殊单', + ]); + DB::table('jst_order_label_planting_rules')->insert([ + 'company_id' => $this->company->id, + 'label' => 'SSDD', + 'is_planting' => '是', + 'created_at' => now(), + 'updated_at' => now(), + ]); + app(\App\Business\JstOrderLabelPlantingRules::class)->forget($this->company->id); + + app(JstOrderLabelImporter::class)->import('2026-09-01', '2026-09-15', $this->company->id, true); + + $labels = JstOrderLabel::query() + ->where('company_id', $this->company->id) + ->where('order_no', '260915-SSDD-REMARK') + ->pluck('label') + ->all(); + + $this->assertSame([JstOrderLabelImporter::SELLER_REMARK_LABEL], $labels); + $this->assertSame( + '是', + app(\App\Business\JstOrderLabelPlantingRules::class) + ->flag($this->company->id, JstOrderLabelImporter::SELLER_REMARK_LABEL), + ); + } + public function test_page_filters_and_export(): void { $this->allowPage(); @@ -241,6 +279,31 @@ class JstOrderLabelsPageTest extends FeatureTestCase $this->assertSame('得物-米客米家家居旗舰店', $noLabel->shop_name); } + public function test_import_from_file_uses_optional_ssdd_seller_remark(): void + { + $this->allowPage(); + + $path = $this->makeExportFile([ + ['线上订单号', '店铺名称', '下单时间', '标签', '卖家备注'], + ['260915-FILE-SSDD', '拼多多-索菲亚书房官方旗舰店', '2026-09-15 12:00:00', '', 'SSDD。特殊单'], + ]); + + try { + app(JstOrderLabelImporter::class)->importFromFile($path, null, null, $this->company->id, false); + } finally { + @unlink($path); + } + + $this->assertSame( + [JstOrderLabelImporter::SELLER_REMARK_LABEL], + JstOrderLabel::query() + ->where('company_id', $this->company->id) + ->where('order_no', '260915-FILE-SSDD') + ->pluck('label') + ->all(), + ); + } + public function test_import_from_file_respects_window_and_replace(): void { $this->allowPage();