dingdanquanliucheng/backend/migrations/pricing_integration.sql
2026-05-26 15:24:41 +08:00

91 lines
5.3 KiB
SQL
Raw 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.

-- 报价系统集成 - 数据库迁移脚本
-- 对应《产品报价系统集成方案.md》
-- 执行前请先备份现有数据库
-- ========== 新增表 ==========
-- 产品定价规则表
CREATE TABLE IF NOT EXISTS `product_pricing_rule` (
`id` bigint NOT NULL AUTO_INCREMENT,
`product_id` bigint NOT NULL COMMENT '产品ID关联 product 表)',
`product_name` varchar(128) NOT NULL COMMENT '产品名称冗余',
`pricing_type` varchar(32) NOT NULL DEFAULT 'area' COMMENT '显示类型area 按面积 / kg 按公斤 / unit 按件',
`base_unit_price` decimal(18,4) NOT NULL DEFAULT 0.0000 COMMENT '基准单价(元/㎡ 或 元/kg',
`pricing_unit` varchar(16) NOT NULL DEFAULT '' COMMENT '计价单位:㎡ / kg / 张 / 米',
`pricing_inputs` text COMMENT '输入字段声明JSON',
`formula_expr` text COMMENT '公式表达式',
`formula_constants` text COMMENT '公式常量JSON',
`surcharge_json` text COMMENT '附加费配置JSON',
`formula_note` text COMMENT '计算公式说明文本',
`status` tinyint NOT NULL DEFAULT 1,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`deleted` tinyint NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_product_pricing_rule_product_id` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='产品定价规则表';
-- 供应商产品成本表
CREATE TABLE IF NOT EXISTS `supplier_product_cost` (
`id` bigint NOT NULL AUTO_INCREMENT,
`product_id` bigint NOT NULL COMMENT '产品ID',
`supplier_id` bigint NOT NULL COMMENT '供应商ID',
`supplier_model` varchar(64) DEFAULT NULL COMMENT '供应商型号',
`our_model` varchar(64) DEFAULT NULL COMMENT '我司型号',
`thickness` varchar(32) DEFAULT NULL COMMENT '厚度(mm)',
`weight_gsm` int DEFAULT NULL COMMENT '克重(g/㎡)',
`base_fabric_weight` int DEFAULT NULL COMMENT '基布克重',
`cost_price` decimal(18,4) NOT NULL COMMENT '采购单价',
`cost_unit` varchar(16) NOT NULL DEFAULT '' COMMENT '成本单位',
`is_primary` tinyint NOT NULL DEFAULT 0 COMMENT '是否默认供应商',
`remark` varchar(255) DEFAULT NULL,
`status` tinyint NOT NULL DEFAULT 1,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`deleted` tinyint NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
KEY `idx_supplier_product_cost_product_id` (`product_id`),
KEY `idx_supplier_product_cost_supplier_id` (`supplier_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='供应商产品成本表';
-- 产品价格层级表
CREATE TABLE IF NOT EXISTS `product_price_tier` (
`id` bigint NOT NULL AUTO_INCREMENT,
`product_id` bigint NOT NULL COMMENT '产品ID',
`tier_code` varchar(32) NOT NULL COMMENT '层级编码special/tier1/tier2/default',
`tier_name` varchar(64) NOT NULL COMMENT '层级名称',
`price` decimal(18,4) NOT NULL COMMENT '销售单价',
`price_unit` varchar(16) NOT NULL DEFAULT '' COMMENT '价格单位',
`remark` varchar(255) DEFAULT NULL,
`status` tinyint NOT NULL DEFAULT 1,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`deleted` tinyint NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_product_price_tier` (`product_id`, `tier_code`),
KEY `idx_product_price_tier_product_id` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='产品价格层级表';
-- ========== 扩展现有表 ==========
-- product 表增加字段
ALTER TABLE `product` ADD COLUMN `pricing_type` varchar(32) DEFAULT NULL COMMENT '计价类型' AFTER `sale_price`;
ALTER TABLE `product` ADD COLUMN `pricing_unit` varchar(16) DEFAULT '' COMMENT '计价单位' AFTER `pricing_type`;
ALTER TABLE `product` ADD COLUMN `thickness` varchar(32) DEFAULT NULL COMMENT '厚度(mm)' AFTER `pricing_unit`;
ALTER TABLE `product` ADD COLUMN `weight_gsm` int DEFAULT NULL COMMENT '克重(g/㎡)' AFTER `thickness`;
ALTER TABLE `product` ADD COLUMN `default_width_m` decimal(10,4) DEFAULT NULL COMMENT '默认宽度(米)' AFTER `weight_gsm`;
-- crm_customer 表增加字段
ALTER TABLE `crm_customer` ADD COLUMN `price_tier` varchar(32) DEFAULT 'default' COMMENT '价格层级编码' AFTER `customer_type`;
-- sales_order_item 表增加字段
ALTER TABLE `sales_order_item` ADD COLUMN `pricing_type` varchar(32) DEFAULT NULL COMMENT '计价类型快照';
ALTER TABLE `sales_order_item` ADD COLUMN `length_m` decimal(10,4) DEFAULT NULL COMMENT '长度(米)';
ALTER TABLE `sales_order_item` ADD COLUMN `width_m` decimal(10,4) DEFAULT NULL COMMENT '宽度(米)';
ALTER TABLE `sales_order_item` ADD COLUMN `area_sqm` decimal(18,4) DEFAULT NULL COMMENT '面积(㎡)';
ALTER TABLE `sales_order_item` ADD COLUMN `surcharge_detail` text COMMENT '附加费明细JSON';
ALTER TABLE `sales_order_item` ADD COLUMN `processing_detail` text COMMENT '加工费明细JSON';
ALTER TABLE `sales_order_item` ADD COLUMN `supplier_id` bigint DEFAULT NULL COMMENT '供应商ID';
ALTER TABLE `sales_order_item` ADD COLUMN `supplier_model` varchar(64) DEFAULT NULL COMMENT '供应商型号';
ALTER TABLE `sales_order_item` ADD COLUMN `price_tier` varchar(32) DEFAULT NULL COMMENT '价格层级快照';