refactor(db_compare): actual arrival = full scans on target date, no extra processing

This commit is contained in:
Misaka_Company
2026-08-04 17:57:21 +08:00
parent db06776ff7
commit c708bff924

View File

@@ -142,7 +142,7 @@ def compare_site_date(site: str, target_date: str) -> CompareResult | None:
1. 取 scan_time::date = target_date 的实到运单(锚点) 1. 取 scan_time::date = target_date 的实到运单(锚点)
2. 反推这些运单所属的交接批次handover_no 2. 反推这些运单所属的交接批次handover_no
3. 展开批次全量应到运单 3. 展开批次全量应到运单
4. 查询批次全量实到扫描 4. 取目标日当天实际扫描的全部实到记录(不做批次加工)
5. 逐运单比对差缺SF/non-SF 分支处理) 5. 逐运单比对差缺SF/non-SF 分支处理)
Args: Args:
@@ -207,10 +207,10 @@ def compare_site_date(site: str, target_date: str) -> CompareResult | None:
cur.execute( cur.execute(
""" """
SELECT waybill_no, piece_no FROM actual_record SELECT waybill_no, piece_no FROM actual_record
WHERE site = %s AND waybill_no = ANY(%s) WHERE site = %s AND scan_time::date = %s
ORDER BY waybill_no, piece_no ORDER BY waybill_no, piece_no
""", """,
(site, all_wbs), (site, target_date),
) )
act_rows = cur.fetchall() # [(waybill_no, piece_no), ...] act_rows = cur.fetchall() # [(waybill_no, piece_no), ...]
@@ -231,6 +231,8 @@ def compare_site_outdate(site: str, target_date: str) -> CompareResult | None:
1. 应到来源 = expected_record WHERE batch_out_date = target_date 1. 应到来源 = expected_record WHERE batch_out_date = target_date
2. 不再依赖实到锚点反推;应到空时返回 None明确"当日无应到" 2. 不再依赖实到锚点反推;应到空时返回 None明确"当日无应到"
3. 提前提交的批次按其出库日归属,自动归入正确日期 3. 提前提交的批次按其出库日归属,自动归入正确日期
4. 实到 = 目标日当天实际扫描的全部记录scan_time::date = 目标日,不做批次加工),
跨日扫描不计入;差缺按运单匹配
Args: Args:
site: 站点名("顺心"/"中通"/"韵达"/"安能" site: 站点名("顺心"/"中通"/"韵达"/"安能"
@@ -284,10 +286,10 @@ def compare_site_outdate(site: str, target_date: str) -> CompareResult | None:
cur.execute( cur.execute(
""" """
SELECT waybill_no, piece_no FROM actual_record SELECT waybill_no, piece_no FROM actual_record
WHERE site = %s AND waybill_no = ANY(%s) WHERE site = %s AND scan_time::date = %s
ORDER BY waybill_no, piece_no ORDER BY waybill_no, piece_no
""", """,
(site, all_wbs), (site, target_date),
) )
act_rows = cur.fetchall() act_rows = cur.fetchall()
@@ -305,6 +307,8 @@ def compare_site_batch(site: str, handover_no: str) -> CompareResult | None:
"""按指定交接单号执行全批次比对(不依赖实到锚点)。 """按指定交接单号执行全批次比对(不依赖实到锚点)。
用于已知交接单号后精确比对某一批次。 用于已知交接单号后精确比对某一批次。
注意:本入口为批次审计工具、无日期语境,实到取该批次运单的**全量**扫描
(不按扫描日过滤),与按归属日统计的 compare_site_outdate/compare_site_date 口径不同。
""" """
cfg = SITE_COMPARE_CONFIG.get(site) cfg = SITE_COMPARE_CONFIG.get(site)
if cfg is None: if cfg is None:
@@ -370,10 +374,10 @@ def _do_compare(
) -> CompareResult: ) -> CompareResult:
"""执行逐运单比对,产出统计 + 差缺明细。 """执行逐运单比对,产出统计 + 差缺明细。
compare.py:process() 口径一致: 业务口径一致:
- 应到件数 = handover_pieces交接件数 - 应到件数 = handover_pieces交接件数
- 实到件数 = SF ? COUNT(*) : COUNT(DISTINCT piece_no) - 实到件数 = 实到记录全量(目标日当天扫描全量,不做批次加工)
- arrived_cnt >= handover_pieces → 足额到货,跳过 - 差缺判断按运单匹配:arrived_cnt >= handover_pieces → 足额到货,跳过
""" """
# 构建实到索引: waybill_no → [piece_no, ...](保留所有行,不去重) # 构建实到索引: waybill_no → [piece_no, ...](保留所有行,不去重)
act_by_wb: dict[str, list[str]] = {} act_by_wb: dict[str, list[str]] = {}
@@ -383,6 +387,9 @@ def _do_compare(
stats = CompareStats() stats = CompareStats()
rows: list[UndeliveredRow] = [] rows: list[UndeliveredRow] = []
max_arrived = 0 max_arrived = 0
# 实到件数 = 实到记录全量(目标日当天扫描全量 / 批次全量),不做任何加工;
# 差缺判断仍按运单逐一匹配act_by_wb 仅用于逐运单 arrived_cnt
stats.arrived_pieces = len(act_rows)
for wb, handover_no, handover_pcs in exp_rows: for wb, handover_no, handover_pcs in exp_rows:
handover_pcs = handover_pcs or 0 handover_pcs = handover_pcs or 0
@@ -408,8 +415,6 @@ def _do_compare(
arrived_cnt = len(unique_pieces) arrived_cnt = len(unique_pieces)
arrived_list = unique_pieces arrived_list = unique_pieces
stats.arrived_pieces += arrived_cnt
if arrived_cnt >= handover_pcs: if arrived_cnt >= handover_pcs:
continue # 足额或溢到,不进差缺表 continue # 足额或溢到,不进差缺表