137 lines
9.2 KiB
SQL
137 lines
9.2 KiB
SQL
-- 报价系统集成 - 数据库迁移脚本
|
||
-- 对应《产品报价系统集成方案.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 表增加字段(使用存储过程避免重复执行报错)
|
||
-- 如果列已存在则跳过
|
||
SET @dbname = DATABASE();
|
||
|
||
-- product 表
|
||
SET @exist = (SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=@dbname AND table_name='product' AND column_name='pricing_type');
|
||
SET @sql = IF(@exist=0, 'ALTER TABLE `product` ADD COLUMN `pricing_type` varchar(32) DEFAULT NULL COMMENT ''计价类型'' AFTER `sale_price`', 'SELECT 1');
|
||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||
|
||
SET @exist = (SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=@dbname AND table_name='product' AND column_name='pricing_unit');
|
||
SET @sql = IF(@exist=0, 'ALTER TABLE `product` ADD COLUMN `pricing_unit` varchar(16) DEFAULT ''㎡'' COMMENT ''计价单位'' AFTER `pricing_type`', 'SELECT 1');
|
||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||
|
||
SET @exist = (SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=@dbname AND table_name='product' AND column_name='thickness');
|
||
SET @sql = IF(@exist=0, 'ALTER TABLE `product` ADD COLUMN `thickness` varchar(32) DEFAULT NULL COMMENT ''厚度(mm)'' AFTER `pricing_unit`', 'SELECT 1');
|
||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||
|
||
SET @exist = (SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=@dbname AND table_name='product' AND column_name='weight_gsm');
|
||
SET @sql = IF(@exist=0, 'ALTER TABLE `product` ADD COLUMN `weight_gsm` int DEFAULT NULL COMMENT ''克重(g/㎡)'' AFTER `thickness`', 'SELECT 1');
|
||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||
|
||
SET @exist = (SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=@dbname AND table_name='product' AND column_name='default_width_m');
|
||
SET @sql = IF(@exist=0, 'ALTER TABLE `product` ADD COLUMN `default_width_m` decimal(10,4) DEFAULT NULL COMMENT ''默认宽度(米)'' AFTER `weight_gsm`', 'SELECT 1');
|
||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||
|
||
-- customer 表(注意:实际表名是 customer,不是 crm_customer)
|
||
SET @exist = (SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=@dbname AND table_name='customer' AND column_name='price_tier');
|
||
SET @sql = IF(@exist=0, 'ALTER TABLE `customer` ADD COLUMN `price_tier` varchar(32) DEFAULT ''default'' COMMENT ''价格层级编码'' AFTER `customer_type`', 'SELECT 1');
|
||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||
|
||
-- sales_order_item 表
|
||
SET @exist = (SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=@dbname AND table_name='sales_order_item' AND column_name='pricing_type');
|
||
SET @sql = IF(@exist=0, 'ALTER TABLE `sales_order_item` ADD COLUMN `pricing_type` varchar(32) DEFAULT NULL COMMENT ''计价类型快照''', 'SELECT 1');
|
||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||
|
||
SET @exist = (SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=@dbname AND table_name='sales_order_item' AND column_name='length_m');
|
||
SET @sql = IF(@exist=0, 'ALTER TABLE `sales_order_item` ADD COLUMN `length_m` decimal(10,4) DEFAULT NULL COMMENT ''长度(米)''', 'SELECT 1');
|
||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||
|
||
SET @exist = (SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=@dbname AND table_name='sales_order_item' AND column_name='width_m');
|
||
SET @sql = IF(@exist=0, 'ALTER TABLE `sales_order_item` ADD COLUMN `width_m` decimal(10,4) DEFAULT NULL COMMENT ''宽度(米)''', 'SELECT 1');
|
||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||
|
||
SET @exist = (SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=@dbname AND table_name='sales_order_item' AND column_name='area_sqm');
|
||
SET @sql = IF(@exist=0, 'ALTER TABLE `sales_order_item` ADD COLUMN `area_sqm` decimal(18,4) DEFAULT NULL COMMENT ''面积(㎡)''', 'SELECT 1');
|
||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||
|
||
SET @exist = (SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=@dbname AND table_name='sales_order_item' AND column_name='surcharge_detail');
|
||
SET @sql = IF(@exist=0, 'ALTER TABLE `sales_order_item` ADD COLUMN `surcharge_detail` text COMMENT ''附加费明细JSON''', 'SELECT 1');
|
||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||
|
||
SET @exist = (SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=@dbname AND table_name='sales_order_item' AND column_name='processing_detail');
|
||
SET @sql = IF(@exist=0, 'ALTER TABLE `sales_order_item` ADD COLUMN `processing_detail` text COMMENT ''加工费明细JSON''', 'SELECT 1');
|
||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||
|
||
SET @exist = (SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=@dbname AND table_name='sales_order_item' AND column_name='supplier_id');
|
||
SET @sql = IF(@exist=0, 'ALTER TABLE `sales_order_item` ADD COLUMN `supplier_id` bigint DEFAULT NULL COMMENT ''供应商ID''', 'SELECT 1');
|
||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||
|
||
SET @exist = (SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=@dbname AND table_name='sales_order_item' AND column_name='supplier_model');
|
||
SET @sql = IF(@exist=0, 'ALTER TABLE `sales_order_item` ADD COLUMN `supplier_model` varchar(64) DEFAULT NULL COMMENT ''供应商型号''', 'SELECT 1');
|
||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||
|
||
SET @exist = (SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=@dbname AND table_name='sales_order_item' AND column_name='price_tier');
|
||
SET @sql = IF(@exist=0, 'ALTER TABLE `sales_order_item` ADD COLUMN `price_tier` varchar(32) DEFAULT NULL COMMENT ''价格层级快照''', 'SELECT 1');
|
||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|