优化履约待办分桶与补载体扫描
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
-- 162_fulfillment_queue_buckets.sql
|
||||
--
|
||||
-- 把履约待办从单一 pending 队列拆成只读派生桶:
|
||||
-- actionable 正常等待补载体,允许自动扫描
|
||||
-- configuration 缺内部 SKU、渠道或渠道配置
|
||||
-- operator_review 失败重试、人工复核、已关闭/取消、系统不一致
|
||||
-- terminal 已退款、退款申请、平台取消或商业订单已关闭
|
||||
--
|
||||
-- 本迁移不新增可写状态字段,也不改 fulfillments 的权威状态。
|
||||
-- 分类只由订单商业状态、正式履约字段和结构化 issue 派生,供扫描器与后台共用。
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_fulfillments_pending_unlocked_item
|
||||
ON fulfillments (order_item_id)
|
||||
WHERE status = 'pending' AND locked = FALSE;
|
||||
|
||||
COMMENT ON INDEX idx_fulfillments_pending_unlocked_item IS
|
||||
'履约队列扫描:仅覆盖待处理且未锁定的正式履约记录';
|
||||
|
||||
CREATE OR REPLACE VIEW v_fulfillment_queue AS
|
||||
WITH classified AS (
|
||||
SELECT
|
||||
state.*,
|
||||
CASE
|
||||
WHEN state.pay_status = 'refunded'
|
||||
OR state.order_status IN ('refunded', 'cancelled', 'closed')
|
||||
OR state.issue_code IN ('user_refund', 'platform_cancelled')
|
||||
THEN 'terminal'
|
||||
WHEN state.issue_category IN ('configuration', 'classification')
|
||||
OR state.issue_code IN (
|
||||
'missing_internal_sku',
|
||||
'missing_channel',
|
||||
'manual_config_missing',
|
||||
'work_rule_missing',
|
||||
'affiliate_mapping_missing'
|
||||
)
|
||||
OR COALESCE(state.fulfillment_pending_detail ->> 'carrierIssue', '') IN (
|
||||
'manual_config_missing',
|
||||
'work_rule_missing',
|
||||
'affiliate_mapping_missing',
|
||||
'missing_internal_sku',
|
||||
'missing_channel'
|
||||
)
|
||||
THEN 'configuration'
|
||||
WHEN state.issue_category = 'manual_review'
|
||||
OR state.issue_code IN (
|
||||
'carrier_manual_review',
|
||||
'carrier_retry_pending',
|
||||
'carrier_expired',
|
||||
'carrier_closed'
|
||||
)
|
||||
OR COALESCE(state.fulfillment_pending_detail ->> 'carrierIssue', '') IN (
|
||||
'carrier_task_closed',
|
||||
'carrier_task_user_refund',
|
||||
'carrier_task_retry_pending',
|
||||
'carrier_task_cancelled',
|
||||
'carrier_task_expired',
|
||||
'carrier_task_manual_review'
|
||||
)
|
||||
THEN 'operator_review'
|
||||
WHEN state.issue_code = 'state_inconsistent'
|
||||
AND state.issue_category = 'system'
|
||||
AND COALESCE(state.fulfillment_pending_detail ->> 'carrierIssue', '')
|
||||
IN ('', 'carrier_not_attempted')
|
||||
THEN 'actionable'
|
||||
WHEN state.issue_category = 'system'
|
||||
THEN 'operator_review'
|
||||
ELSE 'actionable'
|
||||
END AS queue_bucket
|
||||
FROM v_fulfillment_item_state state
|
||||
),
|
||||
eligible AS (
|
||||
SELECT
|
||||
classified.*,
|
||||
(
|
||||
classified.fulfillment_status = 'pending'
|
||||
AND classified.pay_status = 'paid'
|
||||
AND classified.order_status NOT IN ('refunded', 'cancelled', 'closed')
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM fulfillment_tasks task
|
||||
WHERE task.order_item_id = classified.order_item_id
|
||||
AND task.executor_key = 'affiliate_dash'
|
||||
AND task.task_status NOT IN (
|
||||
'closed', 'expired', 'failed', 'manual_review', 'retry_pending', 'cancelled'
|
||||
)
|
||||
)
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM work_orders work_order
|
||||
WHERE work_order.order_item_id = classified.order_item_id
|
||||
AND work_order.status <> 'cancelled'
|
||||
)
|
||||
) AS carrier_queue_candidate
|
||||
FROM classified
|
||||
)
|
||||
SELECT
|
||||
eligible.order_item_id AS id,
|
||||
eligible.*,
|
||||
(
|
||||
eligible.carrier_queue_candidate
|
||||
AND eligible.queue_bucket = 'actionable'
|
||||
AND eligible.fulfillment_locked = FALSE
|
||||
) AS actionable_for_carrier,
|
||||
item.manual_sku_key
|
||||
FROM eligible
|
||||
JOIN order_items item ON item.id = eligible.order_item_id;
|
||||
|
||||
COMMENT ON VIEW v_fulfillment_queue IS
|
||||
'履约待办只读投影:actionable/configuration/operator_review/terminal;actionable_for_carrier 为补载体自动扫描唯一入口';
|
||||
Reference in New Issue
Block a user