refactor(ingest): unique key (site,handover_no,waybill_no) + business_date purge + shortfall-sum undelivered
- schema: expected_record unique key (site,waybill_no) -> (site,handover_no,waybill_no), so the same waybill can belong to multiple handover batches without being overwritten - store: dedup by (handover_no,waybill_no) instead of waybill_no alone; UPSERT conflict key matches the new unique key; handover_no no longer force-overwritten on conflict - store: add purge_expected, called on force re-download; deletes by business_date (download day, same source as task date) rather than batch_out_date, which is offset from the download day for sites like yunda where download day != outbound day - store/runtime: thread force + target_date through ingest_task / _persist_to_db / _site_undelivered_handler so force re-download purges then re-ingests in one tx - db_compare: undelivered_pieces now sums per-waybill shortfall (total - arrived) instead of expected_total - arrived_total, unaffected by unrelated/extra scans
This commit is contained in:
20
schema.sql
20
schema.sql
@@ -10,11 +10,11 @@
|
||||
CREATE SCHEMA IF NOT EXISTS inbound_verify;
|
||||
SET search_path TO inbound_verify;
|
||||
|
||||
-- 应到货物(运单级:一运单一行;按运单号去重 keep-first 后入库)
|
||||
-- 应到货物(运单级:一运单一批次一行;同运单可跨批次,按 交接单号+运单号 去重后入库)
|
||||
CREATE TABLE IF NOT EXISTS expected_record (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
site TEXT NOT NULL, -- 站点:顺心 / 中通 / 韵达 / 安能
|
||||
waybill_no TEXT NOT NULL, -- 运单基号(业务唯一键,去重键)
|
||||
waybill_no TEXT NOT NULL, -- 运单基号
|
||||
handover_no TEXT, -- 交接单号
|
||||
handover_pieces INTEGER, -- 交接件数(应到件数口径)
|
||||
order_pieces INTEGER, -- 录单件数
|
||||
@@ -23,8 +23,22 @@ CREATE TABLE IF NOT EXISTS expected_record (
|
||||
batch_out_date DATE, -- 批次归属日(同交接单号出库日众数,并列取较早)
|
||||
raw JSONB NOT NULL, -- 站点原始全列(key=原列名)
|
||||
ingested_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
UNIQUE (site, waybill_no)
|
||||
UNIQUE (site, handover_no, waybill_no)
|
||||
);
|
||||
-- 迁移:旧唯一键 (site, waybill_no) → 新 (site, handover_no, waybill_no)。
|
||||
-- CREATE TABLE IF NOT EXISTS 不改已存在的表,故用幂等 ALTER 切换约束。
|
||||
ALTER TABLE expected_record DROP CONSTRAINT IF EXISTS expected_record_site_waybill_no_key;
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM pg_constraint
|
||||
WHERE conname = 'expected_record_site_handover_waybill_key'
|
||||
) THEN
|
||||
ALTER TABLE expected_record
|
||||
ADD CONSTRAINT expected_record_site_handover_waybill_key
|
||||
UNIQUE (site, handover_no, waybill_no);
|
||||
END IF;
|
||||
END $$;
|
||||
CREATE INDEX IF NOT EXISTS idx_expected_site_date ON expected_record (site, business_date);
|
||||
CREATE INDEX IF NOT EXISTS idx_expected_handover ON expected_record (site, handover_no);
|
||||
CREATE INDEX IF NOT EXISTS idx_expected_out_date ON expected_record (site, out_date);
|
||||
|
||||
Reference in New Issue
Block a user