商品利润镜像:7 月前与只有费用行的历史订单回退导入数据 (#525)

This commit is contained in:
renshen 2026-10-24 20:44:43 +08:00
parent d22cc9e279
commit 8c5ec8b34f
2 changed files with 27 additions and 8 deletions

View File

@ -10,6 +10,10 @@
*/
return [
// 聚水潭「商品利润 → 订单售后费用数据」原表可靠覆盖的起始日期:
// 更早月份在原表里不完整(例如 6 月订单只有费用行、没有组件/成本),走导入文件。
'goods_profit_from' => '2026-07-01',
'headers' => [
'内部订单号' => 'internal_order_no',
'线上订单号' => 'online_order_no',

View File

@ -184,15 +184,22 @@ class GoodsProfitAfsRepository
// 只按单号(不限日期)查询:原表命中该单就用原表,否则交给导入历史数据兜底。
if ($from === '' && $to === '' && $orderNo !== '') {
$dateColumn = $columns['order_time'] ?? null;
foreach ($this->orderNumberFields($orderNo) as $field) {
if (! isset($columns[$field])) {
continue;
}
if ($this->connection()->table(self::MAIN_TABLE)
->where($columns[$field], 'like', $orderNo . '%')
->limit(1)
->exists()) {
$query = $this->connection()->table(self::MAIN_TABLE)
->where($columns[$field], 'like', $orderNo . '%');
// 只有带订单时间的行才是完整明细;只有费用行的历史订单交给导入数据。
if ($dateColumn !== null) {
$query->whereNotNull($dateColumn)->where($dateColumn, '<>', '');
}
if ($query->limit(1)->exists()) {
return true;
}
}
@ -200,6 +207,12 @@ class GoodsProfitAfsRepository
return false;
}
$start = (string) config('order_after_sales_cost.goods_profit_from', '2026-07-01');
if ($start !== '' && $from !== '' && $from < $start) {
return false;
}
// “是否覆盖”只看日期范围,不掺业务筛选:筛选条件由 paginate 应用,
// 筛不到就该显示空结果,而不是回退到旧订单镜像(否则筛选会被忽略)。
if ($from === '' && $to === '') {
@ -212,15 +225,17 @@ class GoodsProfitAfsRepository
$expression = $this->dateExpression($columns['order_time']);
$range = $this->connection()->table(self::MAIN_TABLE)
->selectRaw("MIN({$expression}) as min_time, MAX({$expression}) as max_time")
->selectRaw("MAX({$expression}) as max_time")
->first();
if (! $range || $range->min_time === null) {
if (! $range || $range->max_time === null) {
return false;
}
return ($from === '' || substr((string) $range->min_time, 0, 10) <= $from)
&& ($to === '' || substr((string) $range->max_time, 0, 10) >= $to);
// 起始日受 goods_profit_from 约束,结束日按原表实际最大日期判断(今天往往晚于最后一次同步)。
$anchor = $from !== '' ? $from : $to;
return $anchor === '' || substr((string) $range->max_time, 0, 10) >= $anchor;
}
/**