feat(fulfillment): 自动核销任务展示并搜索核销码
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
-- 核销任务直接保存目标券码,供后台“最近核销任务”展示和检索。
|
||||
-- 任务创建时会先解析候选券码;执行时再刷新一次,兼容券码晚于任务到达的场景。
|
||||
|
||||
CREATE EXTENSION IF NOT EXISTS pg_trgm;
|
||||
|
||||
ALTER TABLE voucher_consume_jobs
|
||||
ADD COLUMN IF NOT EXISTS voucher_codes TEXT[] NOT NULL DEFAULT ARRAY[]::TEXT[];
|
||||
|
||||
COMMENT ON COLUMN voucher_consume_jobs.voucher_codes IS
|
||||
'核销任务关联的券码;快手电子凭证保存 voucher_code,抖音码速达保存我方与平台侧券码';
|
||||
|
||||
-- array_to_string 在 PostgreSQL 中被标记为 STABLE,不能直接用于表达式索引;
|
||||
-- text[] 的拼接是本项目的确定性展示口径,用 IMMUTABLE 包装支持核销码模糊检索。
|
||||
CREATE OR REPLACE FUNCTION fulfillment_voucher_codes_search_text(codes TEXT[])
|
||||
RETURNS TEXT
|
||||
LANGUAGE sql
|
||||
IMMUTABLE
|
||||
PARALLEL SAFE
|
||||
RETURN ARRAY_TO_STRING(codes, ' ');
|
||||
|
||||
COMMENT ON FUNCTION fulfillment_voucher_codes_search_text(TEXT[]) IS
|
||||
'核销任务券码模糊搜索文本(用于 GIN trigram 表达式索引)';
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_voucher_consume_jobs_voucher_codes
|
||||
ON voucher_consume_jobs
|
||||
USING GIN ((fulfillment_voucher_codes_search_text(voucher_codes)) gin_trgm_ops);
|
||||
|
||||
-- 回填已有任务。快手优先按任务/单元定位;没有任务时退回订单号维度。
|
||||
UPDATE voucher_consume_jobs job
|
||||
SET voucher_codes = COALESCE((
|
||||
SELECT ARRAY_AGG(DISTINCT voucher.voucher_code ORDER BY voucher.voucher_code)
|
||||
FROM kuaishou_industry_vouchers voucher
|
||||
WHERE job.provider = 'kuaishou_industry'
|
||||
AND voucher.oid = job.platform_order_id
|
||||
AND BTRIM(voucher.voucher_code) <> ''
|
||||
AND (
|
||||
job.task_id IS NULL
|
||||
OR voucher.task_id = job.task_id
|
||||
OR voucher.unit_index = (
|
||||
SELECT task.unit_index FROM fulfillment_tasks task WHERE task.id = job.task_id
|
||||
)
|
||||
)
|
||||
), ARRAY[]::TEXT[])
|
||||
WHERE job.provider = 'kuaishou_industry'
|
||||
AND CARDINALITY(job.voucher_codes) = 0;
|
||||
|
||||
-- 抖音码速达可能发生平台侧换码,因此两个券码都保留并可搜索。
|
||||
UPDATE voucher_consume_jobs job
|
||||
SET voucher_codes = COALESCE((
|
||||
SELECT ARRAY_AGG(DISTINCT code ORDER BY code)
|
||||
FROM (
|
||||
SELECT certificate.cert_no AS code
|
||||
FROM redeem_certificates certificate
|
||||
WHERE certificate.provider = 'douyin_mashida'
|
||||
AND (
|
||||
certificate.local_order_id = job.order_id
|
||||
OR (job.order_id IS NULL AND certificate.order_id = job.platform_order_id)
|
||||
)
|
||||
AND BTRIM(certificate.cert_no) <> ''
|
||||
AND (
|
||||
job.order_item_id IS NULL
|
||||
OR certificate.order_item_id IS NULL
|
||||
OR certificate.order_item_id = job.order_item_id
|
||||
)
|
||||
UNION
|
||||
SELECT certificate.platform_cert_no AS code
|
||||
FROM redeem_certificates certificate
|
||||
WHERE certificate.provider = 'douyin_mashida'
|
||||
AND (
|
||||
certificate.local_order_id = job.order_id
|
||||
OR (job.order_id IS NULL AND certificate.order_id = job.platform_order_id)
|
||||
)
|
||||
AND BTRIM(certificate.platform_cert_no) <> ''
|
||||
AND (
|
||||
job.order_item_id IS NULL
|
||||
OR certificate.order_item_id IS NULL
|
||||
OR certificate.order_item_id = job.order_item_id
|
||||
)
|
||||
) codes
|
||||
), ARRAY[]::TEXT[])
|
||||
WHERE job.provider = 'douyin_mashida'
|
||||
AND CARDINALITY(job.voucher_codes) = 0;
|
||||
Reference in New Issue
Block a user