diff --git a/initialize/migrate/database/02154_promo_system.down.sql b/initialize/migrate/database/02154_promo_system.down.sql new file mode 100644 index 0000000..bd4705f --- /dev/null +++ b/initialize/migrate/database/02154_promo_system.down.sql @@ -0,0 +1,41 @@ +-- Purpose: Rollback promo system tables and order promo snapshot columns + +SET @column_exists = ( + SELECT COUNT(*) + FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'order' + AND COLUMN_NAME = 'promo_discount' +); + +SET @sql = IF( + @column_exists = 1, + 'ALTER TABLE `order` DROP COLUMN `promo_discount`', + 'SELECT ''Column promo_discount does not exist in order table''' +); + +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @column_exists = ( + SELECT COUNT(*) + FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'order' + AND COLUMN_NAME = 'promo_rule_id' +); + +SET @sql = IF( + @column_exists = 1, + 'ALTER TABLE `order` DROP COLUMN `promo_rule_id`', + 'SELECT ''Column promo_rule_id does not exist in order table''' +); + +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +DROP TABLE IF EXISTS `promo_usage`; +DROP TABLE IF EXISTS `subscribe_promo`; +DROP TABLE IF EXISTS `promo_rule`; diff --git a/initialize/migrate/database/02154_promo_system.up.sql b/initialize/migrate/database/02154_promo_system.up.sql new file mode 100644 index 0000000..8c6e63c --- /dev/null +++ b/initialize/migrate/database/02154_promo_system.up.sql @@ -0,0 +1,78 @@ +-- Purpose: Add promo system tables and order promo snapshot columns + +CREATE TABLE IF NOT EXISTS `promo_rule` ( + `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, + `name` VARCHAR(100) NOT NULL DEFAULT '' COMMENT '规则名称,如"新客7天优惠"', + `type` VARCHAR(32) NOT NULL DEFAULT '' COMMENT '规则类型:new_user / inactive_user / campaign', + `params` JSON NOT NULL COMMENT '类型专属参数', + `priority` INT NOT NULL DEFAULT 0 COMMENT '优先级,数值越大越优先匹配', + `enabled` TINYINT(1) NOT NULL DEFAULT 1 COMMENT '是否启用', + `start_time` DATETIME DEFAULT NULL COMMENT '生效开始时间,NULL=立即生效', + `end_time` DATETIME DEFAULT NULL COMMENT '生效结束时间,NULL=永不过期', + `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `deleted_at` DATETIME DEFAULT NULL COMMENT '软删除时间', + PRIMARY KEY (`id`), + KEY `idx_enabled_priority` (`enabled`, `priority` DESC) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='促销规则表'; + +CREATE TABLE IF NOT EXISTS `subscribe_promo` ( + `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, + `subscribe_id` BIGINT UNSIGNED NOT NULL COMMENT '套餐规格 ID', + `promo_rule_id` BIGINT UNSIGNED NOT NULL COMMENT '促销规则 ID', + `promo_price` BIGINT NOT NULL DEFAULT 0 COMMENT '该规格在此规则下的优惠价(分)', + `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `uk_subscribe_rule` (`subscribe_id`, `promo_rule_id`), + KEY `idx_promo_rule_id` (`promo_rule_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='规格促销价表'; + +CREATE TABLE IF NOT EXISTS `promo_usage` ( + `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, + `user_id` BIGINT UNSIGNED NOT NULL COMMENT '用户 ID', + `promo_rule_id` BIGINT UNSIGNED NOT NULL COMMENT '使用的规则 ID', + `subscribe_id` BIGINT UNSIGNED NOT NULL COMMENT '购买的规格 ID', + `order_no` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '关联订单号', + `promo_price` BIGINT NOT NULL DEFAULT 0 COMMENT '使用时的促销单价(分)', + `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + KEY `idx_user_rule` (`user_id`, `promo_rule_id`), + KEY `idx_order_no` (`order_no`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='促销使用记录表'; + +SET @column_exists = ( + SELECT COUNT(*) + FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'order' + AND COLUMN_NAME = 'promo_rule_id' +); + +SET @sql = IF( + @column_exists = 0, + 'ALTER TABLE `order` ADD COLUMN `promo_rule_id` BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT ''促销规则ID, 0=未使用促销''', + 'SELECT ''Column promo_rule_id already exists in order table''' +); + +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @column_exists = ( + SELECT COUNT(*) + FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'order' + AND COLUMN_NAME = 'promo_discount' +); + +SET @sql = IF( + @column_exists = 0, + 'ALTER TABLE `order` ADD COLUMN `promo_discount` BIGINT NOT NULL DEFAULT 0 COMMENT ''促销优惠金额(分)''', + 'SELECT ''Column promo_discount already exists in order table''' +); + +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt;