# 抽奖系统数据库初始化SQL

## 1. 抽奖活动表
```sql
CREATE TABLE lottery_activity (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    activity_code VARCHAR(50) UNIQUE NOT NULL COMMENT '活动编码',
    activity_name VARCHAR(100) NOT NULL COMMENT '活动名称',
    activity_desc VARCHAR(500) COMMENT '活动描述',
    start_time DATETIME NOT NULL COMMENT '开始时间',
    end_time DATETIME NOT NULL COMMENT '结束时间',
    status TINYINT DEFAULT 0 COMMENT '状态：0-未开始，1-进行中，2-已结束，3-已关闭',
    total_stock INT DEFAULT 0 COMMENT '总奖品库存',
    consumed_stock INT DEFAULT 0 COMMENT '已消耗库存',
    draw_count_limit INT DEFAULT 0 COMMENT '每人抽奖次数限制（0为不限制）',
    draw_count_cycle VARCHAR(20) COMMENT '次数限制周期：DAILY-每天，TOTAL-总计',
    need_ticket TINYINT DEFAULT 0 COMMENT '是否需要抽奖券：0-否，1-是',
    ticket_cost INT DEFAULT 1 COMMENT '每次消耗抽奖券数量',
    sort_order INT DEFAULT 0 COMMENT '排序',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_status_time (status, start_time, end_time),
    INDEX idx_code (activity_code)
) COMMENT '抽奖活动表';
```

## 2. 奖品配置表
```sql
CREATE TABLE lottery_prize (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    activity_id BIGINT NOT NULL COMMENT '活动ID',
    prize_code VARCHAR(50) NOT NULL COMMENT '奖品编码',
    prize_name VARCHAR(100) NOT NULL COMMENT '奖品名称',
    prize_type TINYINT NOT NULL COMMENT '奖品类型：1-实物，2-虚拟币，3-优惠券，4-积分，5-谢谢参与',
    prize_value DECIMAL(10,2) COMMENT '奖品价值/数量',
    prize_image VARCHAR(200) COMMENT '奖品图片',
    prize_level TINYINT COMMENT '奖品等级：1-特等奖，2-一等奖，3-二等奖...',
    total_stock INT NOT NULL COMMENT '总库存',
    remaining_stock INT NOT NULL COMMENT '剩余库存',
    daily_stock INT DEFAULT 0 COMMENT '每日库存限制（0为不限制）',
    probability DECIMAL(10,6) NOT NULL COMMENT '中奖概率（0-1之间）',
    sort_order INT DEFAULT 0 COMMENT '排序（九宫格位置）',
    is_display TINYINT DEFAULT 1 COMMENT '是否展示：0-否，1-是',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_activity (activity_id),
    INDEX idx_code (prize_code),
    UNIQUE KEY uk_activity_code (activity_id, prize_code)
) COMMENT '奖品配置表';
```

## 3. 中奖记录表
```sql
CREATE TABLE lottery_record (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    record_no VARCHAR(50) UNIQUE NOT NULL COMMENT '记录编号',
    user_id BIGINT NOT NULL COMMENT '用户ID',
    activity_id BIGINT NOT NULL COMMENT '活动ID',
    prize_id BIGINT COMMENT '奖品ID（未中奖为NULL）',
    prize_name VARCHAR(100) COMMENT '奖品名称快照',
    prize_type TINYINT COMMENT '奖品类型快照',
    is_win TINYINT NOT NULL COMMENT '是否中奖：0-未中奖，1-中奖',
    draw_time DATETIME NOT NULL COMMENT '抽奖时间',
    prize_status TINYINT DEFAULT 0 COMMENT '奖品状态：0-待发放，1-已发放，2-发放失败',
    deliver_time DATETIME COMMENT '发放时间',
    remark VARCHAR(500) COMMENT '备注',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_user_activity (user_id, activity_id),
    INDEX idx_draw_time (draw_time),
    INDEX idx_activity_win (activity_id, is_win),
    INDEX idx_record_no (record_no)
) COMMENT '中奖记录表';
```

## 4. 抽奖券表
```sql
CREATE TABLE lottery_ticket (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    user_id BIGINT NOT NULL COMMENT '用户ID',
    ticket_source VARCHAR(50) COMMENT '来源：SYSTEM-系统发放，ACTIVITY-活动赠送，PURCHASE-购买',
    source_id VARCHAR(50) COMMENT '来源ID',
    total_count INT NOT NULL COMMENT '总数量',
    used_count INT DEFAULT 0 COMMENT '已使用数量',
    remaining_count INT NOT NULL COMMENT '剩余数量',
    expire_time DATETIME COMMENT '过期时间',
    status TINYINT DEFAULT 1 COMMENT '状态：0-已失效，1-正常',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_user_status (user_id, status),
    INDEX idx_expire (expire_time)
) COMMENT '抽奖券表';
```

## 5. 抽奖券使用记录表
```sql
CREATE TABLE lottery_ticket_record (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    user_id BIGINT NOT NULL COMMENT '用户ID',
    ticket_id BIGINT NOT NULL COMMENT '抽奖券ID',
    change_count INT NOT NULL COMMENT '变动数量（正数增加，负数减少）',
    change_type TINYINT NOT NULL COMMENT '变动类型：1-发放，2-消耗，3-过期，4-退回',
    lottery_record_id BIGINT COMMENT '关联的抽奖记录ID',
    remark VARCHAR(200) COMMENT '备注',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_user (user_id),
    INDEX idx_ticket (ticket_id),
    INDEX idx_lottery_record (lottery_record_id)
) COMMENT '抽奖券使用记录表';
```

## 6. 用户抽奖次数统计表
```sql
CREATE TABLE lottery_user_count (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    user_id BIGINT NOT NULL COMMENT '用户ID',
    activity_id BIGINT NOT NULL COMMENT '活动ID',
    draw_date DATE NOT NULL COMMENT '统计日期',
    draw_count INT DEFAULT 0 COMMENT '抽奖次数',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_user_activity_date (user_id, activity_id, draw_date),
    INDEX idx_user_activity (user_id, activity_id)
) COMMENT '用户抽奖次数统计表';
```
