feat(worker-platform): 支持客户退款冲正并关闭订单
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
-- 134_customer_refund_operations.sql
|
||||
--
|
||||
-- 客户退款编排:
|
||||
-- 1. 平台售后单落库,支持抖音从 refund_RefundCreated 消息自动发现;
|
||||
-- 2. 一键退款操作记录平台调用、本地退款和业务关闭步骤,保证可重试且不重复退款。
|
||||
|
||||
INSERT INTO admin_permissions (permission_key, permission_name, permission_group, description)
|
||||
VALUES (
|
||||
'worker_order.customer_refund',
|
||||
'客户退款并关闭订单',
|
||||
'接单管理',
|
||||
'同意平台用户售后,冲正核销,并关闭订单关联履约、资料和领取任务'
|
||||
)
|
||||
ON CONFLICT (permission_key) DO UPDATE SET
|
||||
permission_name = EXCLUDED.permission_name,
|
||||
permission_group = EXCLUDED.permission_group,
|
||||
description = EXCLUDED.description,
|
||||
updated_at = NOW();
|
||||
|
||||
-- 客服和运营默认具备前台退款处置能力;高风险管理员仍可在角色权限里收回。
|
||||
INSERT INTO admin_role_permissions (role_key, permission_key)
|
||||
SELECT role_key, 'worker_order.customer_refund'
|
||||
FROM (VALUES ('admin'), ('operator'), ('support')) AS roles(role_key)
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS platform_after_sales (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
provider TEXT NOT NULL,
|
||||
platform_order_id TEXT NOT NULL,
|
||||
sub_order_id TEXT NOT NULL DEFAULT '',
|
||||
external_after_sale_id TEXT NOT NULL,
|
||||
local_order_id BIGINT REFERENCES orders(id) ON DELETE SET NULL,
|
||||
local_order_item_id BIGINT REFERENCES order_items(id) ON DELETE SET NULL,
|
||||
amount_fen BIGINT NOT NULL DEFAULT 0 CHECK (amount_fen >= 0),
|
||||
after_sale_status INTEGER,
|
||||
after_sale_type INTEGER,
|
||||
refund_status INTEGER,
|
||||
platform_update_time TEXT NOT NULL DEFAULT '',
|
||||
state TEXT NOT NULL DEFAULT 'open'
|
||||
CHECK (state IN ('open', 'agreed', 'refused', 'closed', 'success', 'failed')),
|
||||
raw_json JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
applied_at TIMESTAMPTZ,
|
||||
last_event_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_platform_after_sales_external
|
||||
ON platform_after_sales(provider, external_after_sale_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_platform_after_sales_order
|
||||
ON platform_after_sales(provider, platform_order_id, state, last_event_at DESC);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_platform_after_sales_local_order
|
||||
ON platform_after_sales(local_order_id, state, last_event_at DESC)
|
||||
WHERE local_order_id IS NOT NULL;
|
||||
|
||||
COMMENT ON TABLE platform_after_sales IS
|
||||
'平台电商售后单:抖音由退款消息落库,供后台自动选择待处理退款;不是打手履约售后单';
|
||||
COMMENT ON COLUMN platform_after_sales.platform_update_time IS
|
||||
'平台售后版本时间;抖音 afterSale.operate 必须回传该值防止版本过期';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS refund_cancel_operations (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
operation_no TEXT NOT NULL UNIQUE,
|
||||
order_id BIGINT NOT NULL REFERENCES orders(id) ON DELETE CASCADE,
|
||||
order_item_id BIGINT NOT NULL REFERENCES order_items(id) ON DELETE CASCADE,
|
||||
work_order_id BIGINT REFERENCES work_orders(id) ON DELETE SET NULL,
|
||||
provider TEXT NOT NULL,
|
||||
external_after_sale_id TEXT NOT NULL,
|
||||
amount_fen BIGINT NOT NULL CHECK (amount_fen > 0),
|
||||
status TEXT NOT NULL DEFAULT 'pending'
|
||||
CHECK (status IN (
|
||||
'pending', 'platform_approved', 'refund_recorded', 'closed', 'failed'
|
||||
)),
|
||||
current_step TEXT NOT NULL DEFAULT 'created',
|
||||
error_message TEXT NOT NULL DEFAULT '',
|
||||
result_json JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
created_by_admin_user_id BIGINT REFERENCES admin_users(id) ON DELETE SET NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
completed_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_refund_cancel_operations_external
|
||||
ON refund_cancel_operations(provider, external_after_sale_id, order_item_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_refund_cancel_operations_order
|
||||
ON refund_cancel_operations(order_id, created_at DESC);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_refund_cancel_operations_status
|
||||
ON refund_cancel_operations(status, updated_at DESC);
|
||||
|
||||
COMMENT ON TABLE refund_cancel_operations IS
|
||||
'客户退款并关闭订单的幂等编排记录:平台同意、本地退款、业务关闭分步留痕,可安全重试';
|
||||
|
||||
COMMENT ON COLUMN work_order_shares.cancelled_reason IS
|
||||
'取消来源:timeout/worker_request/admin/admin_partial/problem/settlement/customer_refund';
|
||||
Reference in New Issue
Block a user