-- 创建中奖公告配置表
-- 用于首页展示假中奖公告，营造热闹氛围

-- 创建中奖公告配置表
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'prize_announcements')
BEGIN
    CREATE TABLE prize_announcements (
        id INT IDENTITY(1,1) NOT NULL,
        user_avatar NVARCHAR(500) NULL,           -- 用户头像URL
        user_name NVARCHAR(50) NOT NULL,          -- 用户名称
        prize_level NVARCHAR(20) NOT NULL,        -- 奖品等级（如：无上、传说、史诗、稀有）
        prize_name NVARCHAR(100) NOT NULL,        -- 奖品名称
        sort INT NOT NULL DEFAULT 0,              -- 排序值，越小越靠前
        is_enabled BIT NOT NULL DEFAULT 1,        -- 是否启用
        created_at DATETIME2 NOT NULL DEFAULT GETDATE(),
        updated_at DATETIME2 NOT NULL DEFAULT GETDATE(),
        CONSTRAINT pk_prize_announcements PRIMARY KEY (id)
    );
    
    -- 创建索引
    CREATE INDEX ix_prize_announcements_is_enabled ON prize_announcements(is_enabled);
    CREATE INDEX ix_prize_announcements_sort ON prize_announcements(sort);
    
    PRINT N'创建中奖公告配置表 prize_announcements 成功';
END
ELSE
BEGIN
    PRINT N'中奖公告配置表 prize_announcements 已存在';
END
GO

-- 添加表注释
IF EXISTS (SELECT * FROM sys.tables WHERE name = 'prize_announcements')
BEGIN
    -- 检查是否已存在表注释
    IF NOT EXISTS (
        SELECT * FROM sys.extended_properties 
        WHERE major_id = OBJECT_ID('prize_announcements') 
        AND minor_id = 0 
        AND name = 'MS_Description'
    )
    BEGIN
        EXEC sp_addextendedproperty 
            @name = N'MS_Description', 
            @value = N'中奖公告配置表，用于首页展示假中奖公告，营造热闹氛围', 
            @level0type = N'SCHEMA', @level0name = N'dbo',
            @level1type = N'TABLE', @level1name = N'prize_announcements';
    END

    -- 添加列注释
    IF NOT EXISTS (
        SELECT * FROM sys.extended_properties 
        WHERE major_id = OBJECT_ID('prize_announcements') 
        AND minor_id = (SELECT column_id FROM sys.columns WHERE object_id = OBJECT_ID('prize_announcements') AND name = 'id')
        AND name = 'MS_Description'
    )
    BEGIN
        EXEC sp_addextendedproperty 
            @name = N'MS_Description', 
            @value = N'主键ID', 
            @level0type = N'SCHEMA', @level0name = N'dbo',
            @level1type = N'TABLE', @level1name = N'prize_announcements',
            @level2type = N'COLUMN', @level2name = N'id';
    END
    
    IF NOT EXISTS (
        SELECT * FROM sys.extended_properties 
        WHERE major_id = OBJECT_ID('prize_announcements') 
        AND minor_id = (SELECT column_id FROM sys.columns WHERE object_id = OBJECT_ID('prize_announcements') AND name = 'user_avatar')
        AND name = 'MS_Description'
    )
    BEGIN
        EXEC sp_addextendedproperty 
            @name = N'MS_Description', 
            @value = N'用户头像URL', 
            @level0type = N'SCHEMA', @level0name = N'dbo',
            @level1type = N'TABLE', @level1name = N'prize_announcements',
            @level2type = N'COLUMN', @level2name = N'user_avatar';
    END
    
    IF NOT EXISTS (
        SELECT * FROM sys.extended_properties 
        WHERE major_id = OBJECT_ID('prize_announcements') 
        AND minor_id = (SELECT column_id FROM sys.columns WHERE object_id = OBJECT_ID('prize_announcements') AND name = 'user_name')
        AND name = 'MS_Description'
    )
    BEGIN
        EXEC sp_addextendedproperty 
            @name = N'MS_Description', 
            @value = N'用户名称', 
            @level0type = N'SCHEMA', @level0name = N'dbo',
            @level1type = N'TABLE', @level1name = N'prize_announcements',
            @level2type = N'COLUMN', @level2name = N'user_name';
    END
    
    IF NOT EXISTS (
        SELECT * FROM sys.extended_properties 
        WHERE major_id = OBJECT_ID('prize_announcements') 
        AND minor_id = (SELECT column_id FROM sys.columns WHERE object_id = OBJECT_ID('prize_announcements') AND name = 'prize_level')
        AND name = 'MS_Description'
    )
    BEGIN
        EXEC sp_addextendedproperty 
            @name = N'MS_Description', 
            @value = N'奖品等级（如：无上、传说、史诗、稀有）', 
            @level0type = N'SCHEMA', @level0name = N'dbo',
            @level1type = N'TABLE', @level1name = N'prize_announcements',
            @level2type = N'COLUMN', @level2name = N'prize_level';
    END
    
    IF NOT EXISTS (
        SELECT * FROM sys.extended_properties 
        WHERE major_id = OBJECT_ID('prize_announcements') 
        AND minor_id = (SELECT column_id FROM sys.columns WHERE object_id = OBJECT_ID('prize_announcements') AND name = 'prize_name')
        AND name = 'MS_Description'
    )
    BEGIN
        EXEC sp_addextendedproperty 
            @name = N'MS_Description', 
            @value = N'奖品名称', 
            @level0type = N'SCHEMA', @level0name = N'dbo',
            @level1type = N'TABLE', @level1name = N'prize_announcements',
            @level2type = N'COLUMN', @level2name = N'prize_name';
    END
    
    IF NOT EXISTS (
        SELECT * FROM sys.extended_properties 
        WHERE major_id = OBJECT_ID('prize_announcements') 
        AND minor_id = (SELECT column_id FROM sys.columns WHERE object_id = OBJECT_ID('prize_announcements') AND name = 'sort')
        AND name = 'MS_Description'
    )
    BEGIN
        EXEC sp_addextendedproperty 
            @name = N'MS_Description', 
            @value = N'排序值，越小越靠前', 
            @level0type = N'SCHEMA', @level0name = N'dbo',
            @level1type = N'TABLE', @level1name = N'prize_announcements',
            @level2type = N'COLUMN', @level2name = N'sort';
    END
    
    IF NOT EXISTS (
        SELECT * FROM sys.extended_properties 
        WHERE major_id = OBJECT_ID('prize_announcements') 
        AND minor_id = (SELECT column_id FROM sys.columns WHERE object_id = OBJECT_ID('prize_announcements') AND name = 'is_enabled')
        AND name = 'MS_Description'
    )
    BEGIN
        EXEC sp_addextendedproperty 
            @name = N'MS_Description', 
            @value = N'是否启用 1-启用 0-禁用', 
            @level0type = N'SCHEMA', @level0name = N'dbo',
            @level1type = N'TABLE', @level1name = N'prize_announcements',
            @level2type = N'COLUMN', @level2name = N'is_enabled';
    END
    
    IF NOT EXISTS (
        SELECT * FROM sys.extended_properties 
        WHERE major_id = OBJECT_ID('prize_announcements') 
        AND minor_id = (SELECT column_id FROM sys.columns WHERE object_id = OBJECT_ID('prize_announcements') AND name = 'created_at')
        AND name = 'MS_Description'
    )
    BEGIN
        EXEC sp_addextendedproperty 
            @name = N'MS_Description', 
            @value = N'创建时间', 
            @level0type = N'SCHEMA', @level0name = N'dbo',
            @level1type = N'TABLE', @level1name = N'prize_announcements',
            @level2type = N'COLUMN', @level2name = N'created_at';
    END
    
    IF NOT EXISTS (
        SELECT * FROM sys.extended_properties 
        WHERE major_id = OBJECT_ID('prize_announcements') 
        AND minor_id = (SELECT column_id FROM sys.columns WHERE object_id = OBJECT_ID('prize_announcements') AND name = 'updated_at')
        AND name = 'MS_Description'
    )
    BEGIN
        EXEC sp_addextendedproperty 
            @name = N'MS_Description', 
            @value = N'更新时间', 
            @level0type = N'SCHEMA', @level0name = N'dbo',
            @level1type = N'TABLE', @level1name = N'prize_announcements',
            @level2type = N'COLUMN', @level2name = N'updated_at';
    END
END
GO

PRINT N'中奖公告配置表迁移脚本执行完成';
GO
