-- 统一商品目录表:分类 + 商品合并存储,商品管理与订单录入共用 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);