Files
SupplierSystem/migrations/20260615_product_catalog_unified.sql
T
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

155 lines
5.8 KiB
PL/PgSQL

-- 统一商品目录表:分类 + 商品合并存储,商品管理与订单录入共用
CREATE TABLE IF NOT EXISTS product_catalog (
id SERIAL PRIMARY KEY,
item_type VARCHAR(20) NOT NULL CHECK (item_type IN ('category', 'product')),
code VARCHAR(50) NOT NULL UNIQUE,
name VARCHAR(200) NOT NULL,
parent_code VARCHAR(50),
level INTEGER,
sort_order INTEGER DEFAULT 0,
loss_threshold DECIMAL(5,2) DEFAULT 5.00,
is_fresh BOOLEAN DEFAULT true,
unit VARCHAR(20),
spec VARCHAR(100),
barcode VARCHAR(50),
image_url TEXT,
default_loss_threshold DECIMAL(5,2),
sale_price DECIMAL(10,2) DEFAULT 0,
cost_price DECIMAL(10,2) DEFAULT 0,
status INTEGER DEFAULT 1,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_product_catalog_type ON product_catalog(item_type);
CREATE INDEX IF NOT EXISTS idx_product_catalog_parent ON product_catalog(parent_code);
CREATE INDEX IF NOT EXISTS idx_product_catalog_name ON product_catalog(name);
COMMENT ON TABLE product_catalog IS '统一商品目录:分类与商品共用,商品管理与订单录入均可维护';
-- 从现有 categories / skus 迁移(幂等)
INSERT INTO product_catalog (
item_type, code, name, parent_code, level, sort_order, loss_threshold, is_fresh, status, created_at, updated_at
)
SELECT
'category', category_code, category_name, parent_code, level, sort_order, loss_threshold, is_fresh, status, created_at, created_at
FROM categories
ON CONFLICT (code) DO UPDATE SET
name = EXCLUDED.name,
parent_code = EXCLUDED.parent_code,
level = EXCLUDED.level,
sort_order = EXCLUDED.sort_order,
loss_threshold = EXCLUDED.loss_threshold,
is_fresh = EXCLUDED.is_fresh,
status = EXCLUDED.status,
updated_at = NOW();
INSERT INTO product_catalog (
item_type, code, name, parent_code, unit, spec, barcode, image_url,
default_loss_threshold, sale_price, cost_price, status, created_at, updated_at
)
SELECT
'product', sku_code, sku_name, category_code, unit, spec, barcode, image_url,
default_loss_threshold,
COALESCE(sale_price, 0),
COALESCE(cost_price, 0),
status, created_at, created_at
FROM skus
ON CONFLICT (code) DO UPDATE SET
name = EXCLUDED.name,
parent_code = EXCLUDED.parent_code,
unit = EXCLUDED.unit,
spec = EXCLUDED.spec,
barcode = EXCLUDED.barcode,
image_url = EXCLUDED.image_url,
default_loss_threshold = EXCLUDED.default_loss_threshold,
sale_price = EXCLUDED.sale_price,
cost_price = EXCLUDED.cost_price,
status = EXCLUDED.status,
updated_at = NOW();
-- 写入 product_catalog 时同步到 categories / skus(兼容现有外键与查询)
CREATE OR REPLACE FUNCTION sync_product_catalog_to_legacy()
RETURNS TRIGGER AS $$
BEGIN
IF TG_OP = 'DELETE' THEN
IF OLD.item_type = 'category' THEN
DELETE FROM categories WHERE category_code = OLD.code;
ELSE
DELETE FROM skus WHERE sku_code = OLD.code;
END IF;
RETURN OLD;
END IF;
IF NEW.item_type = 'category' THEN
INSERT INTO categories (
category_code, parent_code, category_name, level, sort_order,
loss_threshold, is_fresh, status, created_at
) VALUES (
NEW.code, NEW.parent_code, NEW.name, COALESCE(NEW.level, 1), COALESCE(NEW.sort_order, 0),
COALESCE(NEW.loss_threshold, 5.00), COALESCE(NEW.is_fresh, true), COALESCE(NEW.status, 1), COALESCE(NEW.created_at, NOW())
)
ON CONFLICT (category_code) DO UPDATE SET
parent_code = EXCLUDED.parent_code,
category_name = EXCLUDED.category_name,
level = EXCLUDED.level,
sort_order = EXCLUDED.sort_order,
loss_threshold = EXCLUDED.loss_threshold,
is_fresh = EXCLUDED.is_fresh,
status = EXCLUDED.status;
ELSE
INSERT INTO skus (
sku_code, sku_name, category_code, unit, spec, barcode, image_url,
default_loss_threshold, sale_price, cost_price, status, created_at
) VALUES (
NEW.code, NEW.name, NEW.parent_code, COALESCE(NEW.unit, '斤'), NEW.spec, NEW.barcode, NEW.image_url,
NEW.default_loss_threshold, COALESCE(NEW.sale_price, 0), COALESCE(NEW.cost_price, 0),
COALESCE(NEW.status, 1), COALESCE(NEW.created_at, NOW())
)
ON CONFLICT (sku_code) DO UPDATE SET
sku_name = EXCLUDED.sku_name,
category_code = EXCLUDED.category_code,
unit = EXCLUDED.unit,
spec = EXCLUDED.spec,
barcode = EXCLUDED.barcode,
image_url = EXCLUDED.image_url,
default_loss_threshold = EXCLUDED.default_loss_threshold,
sale_price = EXCLUDED.sale_price,
cost_price = EXCLUDED.cost_price,
status = EXCLUDED.status;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION set_product_catalog_updated_at()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at := NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS trg_product_catalog_updated_at ON product_catalog;
CREATE TRIGGER trg_product_catalog_updated_at
BEFORE UPDATE ON product_catalog
FOR EACH ROW EXECUTE FUNCTION set_product_catalog_updated_at();
DROP TRIGGER IF EXISTS trg_sync_product_catalog_to_legacy ON product_catalog;
CREATE TRIGGER trg_sync_product_catalog_to_legacy
AFTER INSERT OR UPDATE OR DELETE ON product_catalog
FOR EACH ROW EXECUTE FUNCTION sync_product_catalog_to_legacy();
ALTER TABLE product_catalog ENABLE ROW LEVEL SECURITY;
DROP POLICY IF EXISTS anon_select_product_catalog ON product_catalog;
DROP POLICY IF EXISTS anon_insert_product_catalog ON product_catalog;
DROP POLICY IF EXISTS anon_update_product_catalog ON product_catalog;
DROP POLICY IF EXISTS anon_delete_product_catalog ON product_catalog;
CREATE POLICY anon_select_product_catalog ON product_catalog FOR SELECT USING (true);
CREATE POLICY anon_insert_product_catalog ON product_catalog FOR INSERT WITH CHECK (true);
CREATE POLICY anon_update_product_catalog ON product_catalog FOR UPDATE USING (true);
CREATE POLICY anon_delete_product_catalog ON product_catalog FOR DELETE USING (true);