-- =============================================
-- 权益等级表创建脚本
-- 用于福利与任务管理模块
-- =============================================

-- 创建权益等级表
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[equity_levels]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[equity_levels] (
        [id] INT IDENTITY(1,1) NOT NULL,
        [level] INT NOT NULL,
        [title] NVARCHAR(100) NOT NULL,
        [required_points] INT NOT NULL DEFAULT 0,
        [created_at] DATETIME2 DEFAULT GETDATE(),
        [updated_at] DATETIME2 DEFAULT GETDATE(),
        [deleted_at] DATETIME2 NULL,
        CONSTRAINT [pk_equity_levels] PRIMARY KEY CLUSTERED ([id] ASC)
    );

    -- 添加表注释
    EXEC sp_addextendedproperty 
        @name = N'MS_Description', 
        @value = N'权益等级表，存储用户权益等级配置', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_levels';

    -- 添加列注释
    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'主键ID', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_levels', 
        @level2type = N'COLUMN', @level2name = N'id';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'等级', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_levels', 
        @level2type = N'COLUMN', @level2name = N'level';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'等级名称', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_levels', 
        @level2type = N'COLUMN', @level2name = N'title';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'所需欧气值', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_levels', 
        @level2type = N'COLUMN', @level2name = N'required_points';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'创建时间', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_levels', 
        @level2type = N'COLUMN', @level2name = N'created_at';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'更新时间', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_levels', 
        @level2type = N'COLUMN', @level2name = N'updated_at';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'删除时间（软删除）', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_levels', 
        @level2type = N'COLUMN', @level2name = N'deleted_at';

    -- 创建索引
    CREATE INDEX [ix_equity_levels_level] ON [dbo].[equity_levels] ([level]);
    CREATE INDEX [ix_equity_levels_deleted_at] ON [dbo].[equity_levels] ([deleted_at]);

    PRINT '权益等级表 equity_levels 创建成功';
END
ELSE
BEGIN
    PRINT '权益等级表 equity_levels 已存在';
END
GO

-- 创建权益等级奖品表
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[equity_level_prizes]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[equity_level_prizes] (
        [id] INT IDENTITY(1,1) NOT NULL,
        [qy_level_id] INT NOT NULL,
        [qy_level] INT NOT NULL,
        [type] TINYINT NOT NULL DEFAULT 2,
        [title] NVARCHAR(255) NULL,
        [coupon_id] INT NULL,
        [z_num] INT NULL DEFAULT 0,
        [man_price] DECIMAL(10,2) NULL DEFAULT 0,
        [jian_price] DECIMAL(10,2) NULL DEFAULT 0,
        [effective_day] INT NULL DEFAULT 0,
        [jiang_price] DECIMAL(10,2) NULL DEFAULT 0,
        [money] DECIMAL(10,2) NULL DEFAULT 0,
        [sc_money] DECIMAL(10,2) NULL DEFAULT 0,
        [probability] DECIMAL(5,2) NULL DEFAULT 0,
        [imgurl] NVARCHAR(500) NULL,
        [prize_code] NVARCHAR(100) NULL,
        [shang_id] INT NULL,
        [sort] INT NULL DEFAULT 0,
        [created_at] DATETIME2 DEFAULT GETDATE(),
        [updated_at] DATETIME2 DEFAULT GETDATE(),
        [deleted_at] DATETIME2 NULL,
        CONSTRAINT [pk_equity_level_prizes] PRIMARY KEY CLUSTERED ([id] ASC),
        CONSTRAINT [fk_equity_level_prizes_equity_levels] FOREIGN KEY ([qy_level_id]) 
            REFERENCES [dbo].[equity_levels] ([id]) ON DELETE CASCADE
    );

    -- 添加表注释
    EXEC sp_addextendedproperty 
        @name = N'MS_Description', 
        @value = N'权益等级奖品表，存储各等级对应的奖品配置', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_level_prizes';

    -- 添加列注释
    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'主键ID', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_level_prizes', 
        @level2type = N'COLUMN', @level2name = N'id';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'权益等级ID', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_level_prizes', 
        @level2type = N'COLUMN', @level2name = N'qy_level_id';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'权益等级', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_level_prizes', 
        @level2type = N'COLUMN', @level2name = N'qy_level';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'奖品类型：1-优惠券 2-实物奖品', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_level_prizes', 
        @level2type = N'COLUMN', @level2name = N'type';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'奖品标题', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_level_prizes', 
        @level2type = N'COLUMN', @level2name = N'title';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'优惠券ID', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_level_prizes', 
        @level2type = N'COLUMN', @level2name = N'coupon_id';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'优惠券数量', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_level_prizes', 
        @level2type = N'COLUMN', @level2name = N'z_num';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'满减门槛', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_level_prizes', 
        @level2type = N'COLUMN', @level2name = N'man_price';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'减免金额', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_level_prizes', 
        @level2type = N'COLUMN', @level2name = N'jian_price';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'有效天数', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_level_prizes', 
        @level2type = N'COLUMN', @level2name = N'effective_day';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'奖品价值', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_level_prizes', 
        @level2type = N'COLUMN', @level2name = N'jiang_price';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'兑换价格', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_level_prizes', 
        @level2type = N'COLUMN', @level2name = N'money';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'市场参考价', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_level_prizes', 
        @level2type = N'COLUMN', @level2name = N'sc_money';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'中奖概率（0-100）', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_level_prizes', 
        @level2type = N'COLUMN', @level2name = N'probability';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'奖品图片URL', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_level_prizes', 
        @level2type = N'COLUMN', @level2name = N'imgurl';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'奖品编码', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_level_prizes', 
        @level2type = N'COLUMN', @level2name = N'prize_code';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'奖品等级ID', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_level_prizes', 
        @level2type = N'COLUMN', @level2name = N'shang_id';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'排序', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_level_prizes', 
        @level2type = N'COLUMN', @level2name = N'sort';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'创建时间', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_level_prizes', 
        @level2type = N'COLUMN', @level2name = N'created_at';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'更新时间', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_level_prizes', 
        @level2type = N'COLUMN', @level2name = N'updated_at';

    EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'删除时间（软删除）', 
        @level0type = N'SCHEMA', @level0name = N'dbo', 
        @level1type = N'TABLE', @level1name = N'equity_level_prizes', 
        @level2type = N'COLUMN', @level2name = N'deleted_at';

    -- 创建索引
    CREATE INDEX [ix_equity_level_prizes_qy_level_id] ON [dbo].[equity_level_prizes] ([qy_level_id]);
    CREATE INDEX [ix_equity_level_prizes_type] ON [dbo].[equity_level_prizes] ([type]);
    CREATE INDEX [ix_equity_level_prizes_deleted_at] ON [dbo].[equity_level_prizes] ([deleted_at]);
    CREATE INDEX [ix_equity_level_prizes_sort] ON [dbo].[equity_level_prizes] ([sort]);

    PRINT '权益等级奖品表 equity_level_prizes 创建成功';
END
ELSE
BEGIN
    PRINT '权益等级奖品表 equity_level_prizes 已存在';
END
GO

PRINT '权益等级表创建脚本执行完成';
GO
