Files
mp-java/src/main/resources/sql/coupon_tables.sql

79 lines
4.9 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.

-- 优惠券功能相关数据库表
-- 1. 更新优惠券模板表
ALTER TABLE `shop_coupon`
ADD COLUMN `description` varchar(500) COMMENT '优惠券描述' AFTER `name`,
ADD COLUMN `total_count` int(11) DEFAULT -1 COMMENT '发放总数量(-1表示无限制)',
ADD COLUMN `issued_count` int(11) DEFAULT 0 COMMENT '已发放数量',
ADD COLUMN `limit_per_user` int(11) DEFAULT -1 COMMENT '每人限领数量(-1表示无限制)',
ADD COLUMN `enabled` tinyint(1) DEFAULT 1 COMMENT '是否启用(0禁用 1启用)',
MODIFY COLUMN `apply_range` int(11) DEFAULT 10 COMMENT '适用范围(10全部商品 20指定商品 30指定分类)',
MODIFY COLUMN `status` int(11) DEFAULT 0 COMMENT '状态, 0正常, 1禁用',
MODIFY COLUMN `user_id` int(11) COMMENT '创建用户ID';
-- 2. 创建用户优惠券表
CREATE TABLE `shop_user_coupon` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
`coupon_id` int(11) NOT NULL COMMENT '优惠券模板ID',
`user_id` int(11) NOT NULL COMMENT '用户ID',
`name` varchar(100) NOT NULL COMMENT '优惠券名称',
`description` varchar(500) DEFAULT NULL COMMENT '优惠券描述',
`type` int(11) NOT NULL COMMENT '优惠券类型(10满减券 20折扣券 30免费劵)',
`reduce_price` decimal(10,2) DEFAULT NULL COMMENT '满减券-减免金额',
`discount` int(11) DEFAULT NULL COMMENT '折扣券-折扣率(0-100)',
`min_price` decimal(10,2) DEFAULT NULL COMMENT '最低消费金额',
`apply_range` int(11) DEFAULT 10 COMMENT '适用范围(10全部商品 20指定商品 30指定分类)',
`apply_range_config` text COMMENT '适用范围配置(json格式)',
`start_time` datetime DEFAULT NULL COMMENT '有效期开始时间',
`end_time` datetime DEFAULT NULL COMMENT '有效期结束时间',
`status` int(11) DEFAULT 0 COMMENT '使用状态(0未使用 1已使用 2已过期)',
`use_time` datetime DEFAULT NULL COMMENT '使用时间',
`order_id` bigint(20) DEFAULT NULL COMMENT '使用订单ID',
`order_no` varchar(50) DEFAULT NULL COMMENT '使用订单号',
`obtain_type` int(11) DEFAULT 10 COMMENT '获取方式(10主动领取 20系统发放 30活动赠送)',
`obtain_source` varchar(200) DEFAULT NULL COMMENT '获取来源描述',
`deleted` tinyint(1) DEFAULT 0 COMMENT '是否删除, 0否, 1是',
`tenant_id` int(11) DEFAULT NULL COMMENT '租户id',
`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
PRIMARY KEY (`id`),
KEY `idx_user_id` (`user_id`),
KEY `idx_coupon_id` (`coupon_id`),
KEY `idx_status` (`status`),
KEY `idx_end_time` (`end_time`),
KEY `idx_order_id` (`order_id`),
KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户优惠券';
-- 3. 创建优惠券使用记录表(可选,用于详细统计)
CREATE TABLE `shop_coupon_usage_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
`user_coupon_id` bigint(20) NOT NULL COMMENT '用户优惠券ID',
`user_id` int(11) NOT NULL COMMENT '用户ID',
`order_id` bigint(20) NOT NULL COMMENT '订单ID',
`order_no` varchar(50) NOT NULL COMMENT '订单号',
`order_amount` decimal(10,2) NOT NULL COMMENT '订单金额',
`discount_amount` decimal(10,2) NOT NULL COMMENT '优惠金额',
`final_amount` decimal(10,2) NOT NULL COMMENT '最终金额',
`use_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '使用时间',
`tenant_id` int(11) DEFAULT NULL COMMENT '租户id',
PRIMARY KEY (`id`),
KEY `idx_user_id` (`user_id`),
KEY `idx_order_id` (`order_id`),
KEY `idx_use_time` (`use_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='优惠券使用记录';
-- 4. 插入示例优惠券模板数据
INSERT INTO `shop_coupon` (`name`, `description`, `type`, `reduce_price`, `discount`, `min_price`, `total_count`, `issued_count`, `limit_per_user`, `expire_type`, `expire_day`, `apply_range`, `apply_range_config`, `enabled`, `sort_number`, `status`, `user_id`, `tenant_id`) VALUES
('新用户专享券', '新用户注册即可领取满100减20', 10, 20.00, NULL, 100.00, 1000, 0, 1, 10, 30, 10, NULL, 1, 1, 0, 1, 1),
('满减优惠券', '全场通用满200减50', 10, 50.00, NULL, 200.00, 500, 0, 2, 20, NULL, 10, NULL, 1, 2, 0, 1, 1),
('折扣优惠券', '全场9折优惠券', 20, NULL, 10, 50.00, 300, 0, 1, 10, 15, 10, NULL, 1, 3, 0, 1, 1),
('生日专享券', '生日当天专享满50减30', 10, 30.00, NULL, 50.00, -1, 0, 1, 10, 7, 10, NULL, 1, 4, 0, 1, 1),
('消费返券', '消费满500返100优惠券', 10, 100.00, NULL, 300.00, -1, 0, -1, 10, 60, 10, NULL, 1, 5, 0, 1, 1);
-- 5. 创建索引优化查询性能
CREATE INDEX `idx_shop_coupon_enabled_status` ON `shop_coupon` (`enabled`, `status`);
CREATE INDEX `idx_shop_coupon_expire_type` ON `shop_coupon` (`expire_type`, `start_time`, `end_time`);
CREATE INDEX `idx_shop_user_coupon_user_status` ON `shop_user_coupon` (`user_id`, `status`);
CREATE INDEX `idx_shop_user_coupon_expire` ON `shop_user_coupon` (`status`, `end_time`);