Files
SupplierSystem/scripts/repair-ord-web-20260910-br002-0008-restore-original.sql
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库: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 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

184 lines
5.8 KiB
PL/PgSQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 一次性:本地还原 ORD_WEB_20260910_BR002_0008 被 0001 明细覆盖
-- 设计:docs/design/20260911-restore-br002-0008-from-overlay.md
-- 禁止在生产执行,除非已备份且人工确认。
BEGIN;
SELECT set_config('search_path', 'tenant_br002, public', true);
SELECT set_config('app.branch_code', 'BR002', true);
DO $$
BEGIN
IF EXISTS (
SELECT 1 FROM inventory_logs
WHERE related_order_code = 'ORD_WEB_20260910_BR002_0008'
AND operation_type = 'return_undo'
) THEN
RAISE EXCEPTION 'already restored locally (return_undo exists)';
END IF;
END $$;
CREATE TEMP TABLE overlay_0008_items ON COMMIT DROP AS
SELECT id, sku_code
FROM order_items
WHERE order_code = 'ORD_WEB_20260910_BR002_0008'
AND created_at >= timestamptz '2026-09-11 00:45:49+08'
AND created_at < timestamptz '2026-09-11 00:45:50+08'
AND COALESCE(is_returned, false) = false;
-- 1) overlay 履约 / 拣货 / 待验采购 / 备货
DELETE FROM picking_items
WHERE order_item_id IN (SELECT id FROM overlay_0008_items);
DELETE FROM order_item_fulfillments
WHERE order_item_id IN (SELECT id FROM overlay_0008_items);
DELETE FROM purchase_orders
WHERE order_code = 'ORD_WEB_20260910_BR002_0008'
AND purchase_code LIKE 'PURX%'
AND purchase_status = 30;
DELETE FROM supplier_preparations
WHERE order_code = 'ORD_WEB_20260910_BR002_0008'
AND created_at >= timestamptz '2026-09-11 00:45:49+08';
DELETE FROM cost_details
WHERE order_code = 'ORD_WEB_20260910_BR002_0008'
AND cost_source = 'extra_dish';
DELETE FROM statement_lines
WHERE statement_no IN ('SS20260911001', 'SS20260911002', 'SS20260911003')
AND order_code = 'ORD_WEB_20260910_BR002_0008';
DELETE FROM supplier_statements
WHERE statement_no IN ('SS20260911001', 'SS20260911002', 'SS20260911003')
AND statement_status = 10
AND NOT EXISTS (
SELECT 1 FROM statement_lines sl WHERE sl.statement_no = supplier_statements.statement_no
);
DELETE FROM order_items
WHERE id IN (SELECT id FROM overlay_0008_items);
-- 2) 撤销 14 行误退货
UPDATE order_items
SET is_returned = false, return_code = NULL
WHERE order_code = 'ORD_WEB_20260910_BR002_0008'
AND COALESCE(is_returned, false) = true
AND return_code LIKE 'OIR_0BR0020008_%';
DELETE FROM after_sales
WHERE order_code = 'ORD_WEB_20260910_BR002_0008'
AND aftersale_code LIKE 'OIR_0BR0020008_%'
AND created_at >= timestamptz '2026-09-11 00:45:49+08'
AND created_at < timestamptz '2026-09-11 00:45:50+08';
DELETE FROM order_item_returns
WHERE order_code = 'ORD_WEB_20260910_BR002_0008'
AND return_code LIKE 'OIR_0BR0020008_%';
UPDATE cost_details cd
SET amount = ROUND((oi.quantity * oi.unit_cost)::numeric, 2),
allocated_amount = ROUND((oi.quantity * oi.unit_cost)::numeric, 2),
remark = regexp_replace(COALESCE(cd.remark, ''), '(;)?订单退货红冲$', '')
FROM order_items oi
WHERE cd.order_code = 'ORD_WEB_20260910_BR002_0008'
AND cd.order_item_id = oi.id
AND cd.cost_type = 'purchase'
AND cd.cost_source IS DISTINCT FROM 'extra_dish'
AND oi.order_code = 'ORD_WEB_20260910_BR002_0008'
AND oi.id NOT IN (
'ec2dc347-3666-49c4-aa32-5836954096b5'::uuid,
'1c78112e-1a0a-455a-b94b-218683b464c8'::uuid,
'1f6d0586-2872-408e-9a7d-fe32950c8311'::uuid
);
UPDATE inventory i
SET available_qty = i.available_qty + il.quantity,
quantity = i.quantity + il.quantity
FROM inventory_logs il
WHERE il.related_order_code = 'ORD_WEB_20260910_BR002_0008'
AND il.operation_type = 'order_item_return'
AND i.batch_no = il.batch_no;
INSERT INTO inventory_logs (
batch_no, sku_code, operation_type, quantity, before_qty, after_qty, related_order_code, remark
)
SELECT
il.batch_no,
il.sku_code,
'return_undo',
il.quantity,
i.available_qty - il.quantity,
i.available_qty,
'ORD_WEB_20260910_BR002_0008',
'本地还原误退货,加回库存'
FROM inventory_logs il
JOIN inventory i ON i.batch_no = il.batch_no
WHERE il.related_order_code = 'ORD_WEB_20260910_BR002_0008'
AND il.operation_type = 'order_item_return';
-- 3) 硬删 3 行(原 UUID + 孤儿采购账)
INSERT INTO order_items (
id, order_code, sku_code, item_name, quantity, ordered_quantity, unit, ordered_unit,
price, locked_price, amount, unit_cost, supplier_code,
processing_method, processing_owner, category_code, category_name,
item_status, sort_order, branch_code, is_extra_dish, is_returned,
created_at, price_locked_at
) VALUES
(
'ec2dc347-3666-49c4-aa32-5836954096b5',
'ORD_WEB_20260910_BR002_0008',
'SKU78660111794',
'凉粉刮条',
21, 21, '斤', '斤',
2.50, 2.50, 52.50, 1.30,
'INTERNAL',
'刮条', 'warehouse', 'BR002_CAT78370107537', '豆制品',
10, 90, 'BR002', false, false,
timestamptz '2026-09-10 15:33:26.348239+08',
timestamptz '2026-09-11 00:44:29+08'
),
(
'1c78112e-1a0a-455a-b94b-218683b464c8',
'ORD_WEB_20260910_BR002_0008',
'SKU78665602457',
'绞碎肉',
20, 20, '斤', '斤',
12.00, 12.00, 240.00, 8.30,
'SUP35143228',
'剁碎', 'supplier', 'BR002_CAT78367186561', '猪肉',
10, 91, 'BR002', false, false,
timestamptz '2026-09-10 15:33:26.348239+08',
timestamptz '2026-09-11 00:44:29+08'
),
(
'1f6d0586-2872-408e-9a7d-fe32950c8311',
'ORD_WEB_20260910_BR002_0008',
'SKU78668570381',
'鸡',
21, 21, '斤', '斤',
15.00, 15.00, 315.00, 10.00,
'SUP51419717',
'原材料', NULL, 'BR002_CAT78360388699', '家禽',
10, 92, 'BR002', false, false,
timestamptz '2026-09-10 15:33:26.348239+08',
timestamptz '2026-09-11 00:44:29+08'
);
-- 4) 订单头
UPDATE orders o
SET total_amount = sub.amt,
total_weight = sub.wt
FROM (
SELECT
ROUND(SUM(amount)::numeric, 2) AS amt,
ROUND(SUM(quantity)::numeric, 2) AS wt
FROM order_items
WHERE order_code = 'ORD_WEB_20260910_BR002_0008'
AND COALESCE(is_returned, false) = false
) sub
WHERE o.order_code = 'ORD_WEB_20260910_BR002_0008';
COMMIT;