-- 创建周榜和月榜中奖记录表
-- 用于修复 BUG cs100_13 和 cs100_14

-- 创建周榜中奖记录表
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'rank_week')
BEGIN
    CREATE TABLE rank_week (
        id INT IDENTITY(1,1) NOT NULL,
        user_id INT NOT NULL DEFAULT 0,
        [rank] INT NOT NULL DEFAULT 0,
        money DECIMAL(10, 2) NOT NULL DEFAULT 0.00,
        week_time NVARCHAR(100) NULL,
        addtime INT NOT NULL DEFAULT 0,
        order_list_id INT NOT NULL DEFAULT 0,
        prize_title NVARCHAR(255) NULL,
        prize_imgurl NVARCHAR(255) NULL,
        CONSTRAINT pk_rank_week PRIMARY KEY (id)
    );
    
    CREATE INDEX ix_rank_week_user_id ON rank_week(user_id);
    
    PRINT N'创建周榜中奖记录表 rank_week 成功';
END
ELSE
BEGIN
    PRINT N'周榜中奖记录表 rank_week 已存在';
END
GO

-- 创建月榜中奖记录表
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'rank_month')
BEGIN
    CREATE TABLE rank_month (
        id INT IDENTITY(1,1) NOT NULL,
        user_id INT NOT NULL DEFAULT 0,
        [rank] INT NOT NULL DEFAULT 0,
        money DECIMAL(10, 2) NOT NULL DEFAULT 0.00,
        month_time NVARCHAR(100) NULL,
        addtime INT NOT NULL DEFAULT 0,
        order_list_id INT NOT NULL DEFAULT 0,
        prize_title NVARCHAR(255) NULL,
        prize_imgurl NVARCHAR(255) NULL,
        CONSTRAINT pk_rank_month PRIMARY KEY (id)
    );
    
    CREATE INDEX ix_rank_month_user_id ON rank_month(user_id);
    
    PRINT N'创建月榜中奖记录表 rank_month 成功';
END
ELSE
BEGIN
    PRINT N'月榜中奖记录表 rank_month 已存在';
END
GO

-- 添加表注释
EXEC sp_addextendedproperty 
    @name = N'MS_Description', 
    @value = N'周榜中奖记录表', 
    @level0type = N'SCHEMA', @level0name = N'dbo',
    @level1type = N'TABLE', @level1name = N'rank_week';

EXEC sp_addextendedproperty 
    @name = N'MS_Description', 
    @value = N'月榜中奖记录表', 
    @level0type = N'SCHEMA', @level0name = N'dbo',
    @level1type = N'TABLE', @level1name = N'rank_month';
GO
