来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
207 lines
7.2 KiB
SQL
207 lines
7.2 KiB
SQL
-- 回填原有业务操作日志(改价审核 / 库存调整 / 售后处理);幂等
|
||
-- 见 docs/design/20260801-operation-logs-backfill-original.md
|
||
|
||
DO $$
|
||
DECLARE
|
||
sch text;
|
||
BEGIN
|
||
FOR sch IN
|
||
SELECT nspname
|
||
FROM pg_namespace
|
||
WHERE nspname IN ('public', 'legacy_public', 'tenant_template')
|
||
OR nspname LIKE 'tenant_%'
|
||
LOOP
|
||
IF NOT EXISTS (
|
||
SELECT 1 FROM information_schema.tables
|
||
WHERE table_schema = sch AND table_name = 'operation_logs'
|
||
) THEN
|
||
CONTINUE;
|
||
END IF;
|
||
|
||
-- 0) 历史异常工单日志补分类
|
||
EXECUTE format(
|
||
'UPDATE %I.operation_logs
|
||
SET action_category = ''exception''
|
||
WHERE operation_type IN (''resolve_ticket'', ''close_ticket'')
|
||
AND (action_category IS NULL OR BTRIM(action_category) = '''')',
|
||
sch
|
||
);
|
||
|
||
-- 1) 供应商改价确认/驳回
|
||
IF EXISTS (
|
||
SELECT 1 FROM information_schema.tables
|
||
WHERE table_schema = sch AND table_name = 'supplier_prep_amend_requests'
|
||
) THEN
|
||
EXECUTE format(
|
||
'INSERT INTO %I.operation_logs (
|
||
operator_id, operation_type, target_table, target_id,
|
||
old_value, new_value, branch_code, action_category, created_at
|
||
)
|
||
SELECT
|
||
r.reviewed_by,
|
||
CASE WHEN r.status = 20 THEN ''prep_amend_confirm'' ELSE ''prep_amend_reject'' END,
|
||
''supplier_prep_amend_requests'',
|
||
r.id::text,
|
||
jsonb_build_object(
|
||
''order_code'', r.order_code,
|
||
''sku_code'', r.sku_code,
|
||
''item_name'', r.item_name,
|
||
''supplier_code'', r.supplier_code,
|
||
''before_price'', r.before_price,
|
||
''before_qty'', r.before_qty,
|
||
''before_unit'', r.before_unit,
|
||
''proposed_price'', r.proposed_price,
|
||
''proposed_qty'', r.proposed_qty,
|
||
''proposed_unit'', r.proposed_unit
|
||
),
|
||
jsonb_build_object(
|
||
''final_price'', r.final_price,
|
||
''final_qty'', r.final_qty,
|
||
''final_unit'', r.final_unit,
|
||
''review_remark'', r.review_remark
|
||
),
|
||
NULLIF(BTRIM(COALESCE(r.branch_code, '''')), ''''),
|
||
''prep_amend'',
|
||
COALESCE(r.reviewed_at, r.updated_at, r.created_at, NOW())
|
||
FROM %I.supplier_prep_amend_requests r
|
||
WHERE r.status IN (20, 30)
|
||
AND NOT EXISTS (
|
||
SELECT 1 FROM %I.operation_logs ol
|
||
WHERE ol.target_table = ''supplier_prep_amend_requests''
|
||
AND ol.target_id = r.id::text
|
||
AND ol.operation_type IN (''prep_amend_confirm'', ''prep_amend_reject'')
|
||
)',
|
||
sch, sch, sch
|
||
);
|
||
END IF;
|
||
|
||
-- 2) 库存手工调整(inventory_logs.operation_type = adjust)
|
||
IF EXISTS (
|
||
SELECT 1 FROM information_schema.tables
|
||
WHERE table_schema = sch AND table_name = 'inventory_logs'
|
||
) THEN
|
||
IF EXISTS (
|
||
SELECT 1 FROM information_schema.tables
|
||
WHERE table_schema = sch AND table_name = 'inventory'
|
||
) AND EXISTS (
|
||
SELECT 1 FROM information_schema.columns
|
||
WHERE table_schema = sch AND table_name = 'inventory' AND column_name = 'branch_code'
|
||
) THEN
|
||
EXECUTE format(
|
||
'INSERT INTO %I.operation_logs (
|
||
operator_id, operation_type, target_table, target_id,
|
||
old_value, new_value, branch_code, action_category, created_at
|
||
)
|
||
SELECT
|
||
il.operator_id,
|
||
''inventory_adjust'',
|
||
''inventory'',
|
||
il.id::text,
|
||
jsonb_build_object(
|
||
''batch_no'', il.batch_no,
|
||
''sku_code'', il.sku_code,
|
||
''available_qty'', il.before_qty
|
||
),
|
||
jsonb_build_object(
|
||
''available_qty'', il.after_qty,
|
||
''delta'', il.quantity,
|
||
''remark'', il.remark,
|
||
''related_order_code'', il.related_order_code
|
||
),
|
||
(
|
||
SELECT NULLIF(BTRIM(COALESCE(i.branch_code, '''')), '''')
|
||
FROM %I.inventory i
|
||
WHERE i.batch_no IS NOT DISTINCT FROM il.batch_no
|
||
AND i.sku_code IS NOT DISTINCT FROM il.sku_code
|
||
LIMIT 1
|
||
),
|
||
''inventory'',
|
||
COALESCE(il.created_at, NOW())
|
||
FROM %I.inventory_logs il
|
||
WHERE il.operation_type = ''adjust''
|
||
AND NOT EXISTS (
|
||
SELECT 1 FROM %I.operation_logs ol
|
||
WHERE ol.operation_type = ''inventory_adjust''
|
||
AND ol.target_id = il.id::text
|
||
)',
|
||
sch, sch, sch, sch
|
||
);
|
||
ELSE
|
||
EXECUTE format(
|
||
'INSERT INTO %I.operation_logs (
|
||
operator_id, operation_type, target_table, target_id,
|
||
old_value, new_value, branch_code, action_category, created_at
|
||
)
|
||
SELECT
|
||
il.operator_id,
|
||
''inventory_adjust'',
|
||
''inventory'',
|
||
il.id::text,
|
||
jsonb_build_object(
|
||
''batch_no'', il.batch_no,
|
||
''sku_code'', il.sku_code,
|
||
''available_qty'', il.before_qty
|
||
),
|
||
jsonb_build_object(
|
||
''available_qty'', il.after_qty,
|
||
''delta'', il.quantity,
|
||
''remark'', il.remark,
|
||
''related_order_code'', il.related_order_code
|
||
),
|
||
NULL,
|
||
''inventory'',
|
||
COALESCE(il.created_at, NOW())
|
||
FROM %I.inventory_logs il
|
||
WHERE il.operation_type = ''adjust''
|
||
AND NOT EXISTS (
|
||
SELECT 1 FROM %I.operation_logs ol
|
||
WHERE ol.operation_type = ''inventory_adjust''
|
||
AND ol.target_id = il.id::text
|
||
)',
|
||
sch, sch, sch
|
||
);
|
||
END IF;
|
||
END IF;
|
||
|
||
-- 3) 已处理售后
|
||
IF EXISTS (
|
||
SELECT 1 FROM information_schema.tables
|
||
WHERE table_schema = sch AND table_name = 'after_sales'
|
||
) THEN
|
||
EXECUTE format(
|
||
'INSERT INTO %I.operation_logs (
|
||
operator_id, operation_type, target_table, target_id,
|
||
old_value, new_value, branch_code, action_category, created_at
|
||
)
|
||
SELECT
|
||
NULL,
|
||
''aftersale_handle'',
|
||
''after_sales'',
|
||
COALESCE(NULLIF(BTRIM(a.aftersale_code), ''''), a.id::text),
|
||
jsonb_build_object(
|
||
''order_code'', a.order_code,
|
||
''aftersale_type'', a.aftersale_type,
|
||
''amount'', a.amount
|
||
),
|
||
jsonb_build_object(
|
||
''status'', a.status,
|
||
''responsibility'', a.responsibility,
|
||
''handle_method'', a.handle_method,
|
||
''handle_amount'', a.handle_amount
|
||
),
|
||
NULLIF(BTRIM(COALESCE(a.branch_code, '''')), ''''),
|
||
''aftersale'',
|
||
COALESCE(a.closed_at, a.created_at, NOW())
|
||
FROM %I.after_sales a
|
||
WHERE COALESCE(a.status, 0) >= 30
|
||
AND NOT EXISTS (
|
||
SELECT 1 FROM %I.operation_logs ol
|
||
WHERE ol.operation_type = ''aftersale_handle''
|
||
AND ol.target_id = COALESCE(NULLIF(BTRIM(a.aftersale_code), ''''), a.id::text)
|
||
)',
|
||
sch, sch, sch
|
||
);
|
||
END IF;
|
||
END LOOP;
|
||
END $$;
|