diff --git a/inbound_verify/db_compare.py b/inbound_verify/db_compare.py index d674842..001f93e 100644 --- a/inbound_verify/db_compare.py +++ b/inbound_verify/db_compare.py @@ -142,7 +142,7 @@ def compare_site_date(site: str, target_date: str) -> CompareResult | None: 1. 取 scan_time::date = target_date 的实到运单(锚点) 2. 反推这些运单所属的交接批次(handover_no) 3. 展开批次全量应到运单 - 4. 查询批次全量实到扫描 + 4. 取目标日当天实际扫描的全部实到记录(不做批次加工) 5. 逐运单比对差缺(SF/non-SF 分支处理) Args: @@ -207,10 +207,10 @@ def compare_site_date(site: str, target_date: str) -> CompareResult | None: cur.execute( """ 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 """, - (site, all_wbs), + (site, target_date), ) 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 2. 不再依赖实到锚点反推;应到空时返回 None(明确"当日无应到") 3. 提前提交的批次按其出库日归属,自动归入正确日期 + 4. 实到 = 目标日当天实际扫描的全部记录(scan_time::date = 目标日,不做批次加工), + 跨日扫描不计入;差缺按运单匹配 Args: site: 站点名("顺心"/"中通"/"韵达"/"安能") @@ -284,10 +286,10 @@ def compare_site_outdate(site: str, target_date: str) -> CompareResult | None: cur.execute( """ 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 """, - (site, all_wbs), + (site, target_date), ) 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) if cfg is None: @@ -370,10 +374,10 @@ def _do_compare( ) -> CompareResult: """执行逐运单比对,产出统计 + 差缺明细。 - 与 compare.py:process() 口径一致: + 与业务口径一致: - 应到件数 = handover_pieces(交接件数) - - 实到件数 = SF ? COUNT(*) : COUNT(DISTINCT piece_no) - - arrived_cnt >= handover_pieces → 足额到货,跳过 + - 实到件数 = 实到记录全量(目标日当天扫描全量,不做批次加工) + - 差缺判断按运单匹配:arrived_cnt >= handover_pieces → 足额到货,跳过 """ # 构建实到索引: waybill_no → [piece_no, ...](保留所有行,不去重) act_by_wb: dict[str, list[str]] = {} @@ -383,6 +387,9 @@ def _do_compare( stats = CompareStats() rows: list[UndeliveredRow] = [] max_arrived = 0 + # 实到件数 = 实到记录全量(目标日当天扫描全量 / 批次全量),不做任何加工; + # 差缺判断仍按运单逐一匹配(act_by_wb 仅用于逐运单 arrived_cnt)。 + stats.arrived_pieces = len(act_rows) for wb, handover_no, handover_pcs in exp_rows: handover_pcs = handover_pcs or 0 @@ -408,8 +415,6 @@ def _do_compare( arrived_cnt = len(unique_pieces) arrived_list = unique_pieces - stats.arrived_pieces += arrived_cnt - if arrived_cnt >= handover_pcs: continue # 足额或溢到,不进差缺表