-- 创建 goods_king_ranks 表
-- 领主赏记录表，记录领主占领和挑战信息

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='goods_king_ranks' AND xtype='U')
BEGIN
    CREATE TABLE goods_king_ranks (
        id INT IDENTITY(1,1) NOT NULL,
        user_id INT NOT NULL,
        goods_id INT NOT NULL,
        count INT NOT NULL DEFAULT 0,
        z_nums INT NOT NULL DEFAULT 0,
        money DECIMAL(10,2) NOT NULL DEFAULT 0,
        order_list_id INT NOT NULL DEFAULT 0,
        addtime INT NOT NULL DEFAULT 0,
        end_time INT NOT NULL DEFAULT 0,
        CONSTRAINT pk_goods_king_ranks PRIMARY KEY CLUSTERED (id)
    );
    
    -- 创建索引
    CREATE INDEX ix_goods_king_ranks_user_id ON goods_king_ranks(user_id);
    CREATE INDEX ix_goods_king_ranks_goods_id ON goods_king_ranks(goods_id);
    CREATE INDEX ix_goods_king_ranks_user_goods ON goods_king_ranks(user_id, goods_id);
    
    -- 添加表注释
    EXEC sys.sp_addextendedproperty 
        @name=N'MS_Description', 
        @value=N'领主赏记录表，记录领主占领和挑战信息', 
        @level0type=N'SCHEMA', @level0name=N'dbo', 
        @level1type=N'TABLE', @level1name=N'goods_king_ranks';
    
    -- 添加列注释
    EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键ID', @level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'goods_king_ranks', @level2type=N'COLUMN', @level2name=N'id';
    EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户ID（领主）', @level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'goods_king_ranks', @level2type=N'COLUMN', @level2name=N'user_id';
    EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'商品ID', @level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'goods_king_ranks', @level2type=N'COLUMN', @level2name=N'goods_id';
    EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'占领时的抽奖次数', @level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'goods_king_ranks', @level2type=N'COLUMN', @level2name=N'count';
    EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'被挑战次数', @level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'goods_king_ranks', @level2type=N'COLUMN', @level2name=N'z_nums';
    EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'领主收益金额', @level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'goods_king_ranks', @level2type=N'COLUMN', @level2name=N'money';
    EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'关联的中奖记录ID', @level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'goods_king_ranks', @level2type=N'COLUMN', @level2name=N'order_list_id';
    EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'成为领主的时间戳', @level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'goods_king_ranks', @level2type=N'COLUMN', @level2name=N'addtime';
    EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'结束领主的时间戳', @level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'goods_king_ranks', @level2type=N'COLUMN', @level2name=N'end_time';
    
    PRINT '表 goods_king_ranks 创建成功';
END
ELSE
BEGIN
    PRINT '表 goods_king_ranks 已存在';
END
GO
