diff --git a/modules/EcommerceFinance/Repositories/AfterSalesCostRowRepository.php b/modules/EcommerceFinance/Repositories/AfterSalesCostRowRepository.php index 57fa7358..2a28da84 100644 --- a/modules/EcommerceFinance/Repositories/AfterSalesCostRowRepository.php +++ b/modules/EcommerceFinance/Repositories/AfterSalesCostRowRepository.php @@ -43,7 +43,30 @@ class AfterSalesCostRowRepository return false; } - return $this->filtered($filters)->limit(1)->exists(); + // 指定单号时,导入表里命中该单就完整(同单的所有明细行都在导入数据里)。 + if (trim((string) ($filters['order_no'] ?? '')) !== '') { + return $this->filtered($filters)->limit(1)->exists(); + } + + $from = trim((string) ($filters['from'] ?? '')); + $to = trim((string) ($filters['to'] ?? '')); + + // 不限日期时无法保证覆盖范围,回退镜像,避免把导入数据当成全量。 + if ($from === '' || $to === '') { + return false; + } + + $range = $this->filtered(['shop_names' => $filters['shop_names'] ?? []]) + ->selectRaw('MIN(order_time) as min_time, MAX(order_time) as max_time') + ->first(); + + if (! $range || $range->min_time === null) { + return false; + } + + // 只有导入范围完整覆盖所选日期时才使用导入数据,避免跨月时数据不全。 + return substr((string) $range->min_time, 0, 10) <= $from + && substr((string) $range->max_time, 0, 10) >= $to; } /** diff --git a/tests/Feature/Business/AfterSalesCostImportTest.php b/tests/Feature/Business/AfterSalesCostImportTest.php index 6378ad3c..97a26d58 100644 --- a/tests/Feature/Business/AfterSalesCostImportTest.php +++ b/tests/Feature/Business/AfterSalesCostImportTest.php @@ -27,6 +27,26 @@ class AfterSalesCostImportTest extends FeatureTestCase 'combine_type' => '组合单品', ])); + DB::table('ops_after_sales_cost_rows')->insert(AfterSalesCostRowRepository::toRecord([ + 'shop_name' => '天猫-亿家达旗舰店', + 'online_order_no' => '5120748758529014000', + 'product_code' => 'SJ038999N1410V01', + 'order_time' => '2026-06-01 00:00:00', + 'product_quantity' => 1, + 'product_amount' => 10, + 'product_cost' => 5, + ])); + + DB::table('ops_after_sales_cost_rows')->insert(AfterSalesCostRowRepository::toRecord([ + 'shop_name' => '天猫-亿家达旗舰店', + 'online_order_no' => '5120748758529014001', + 'product_code' => 'SJ038998N1410V01', + 'order_time' => '2026-06-30 23:00:00', + 'product_quantity' => 1, + 'product_amount' => 10, + 'product_cost' => 5, + ])); + $result = app(OrderDataPageData::class)->afterSalesRows([ 'from' => '2026-06-01', 'to' => '2026-06-30', @@ -36,9 +56,11 @@ class AfterSalesCostImportTest extends FeatureTestCase ], 1, 20); $this->assertSame('imported', $result['status']['X-Caiwu-Data-Status']); - $this->assertCount(1, $result['rows']); - $this->assertSame('SJ038020N1410V02', $result['rows'][0]['product_code']); - $this->assertEquals(110.53, $result['rows'][0]['product_cost']); + $this->assertCount(3, $result['rows']); + + $target = collect($result['rows'])->firstWhere('product_code', 'SJ038020N1410V02'); + $this->assertNotNull($target); + $this->assertEquals(110.53, $target['product_cost']); } public function test_import_command_reads_the_jst_export_file(): void