-- 虚拟货币和礼物系统数据库表

-- 1. 用户虚拟货币余额表（扩展eb_user表的字段）
-- 检查并添加virtual_balance字段
SET @col_exists = 0;
SELECT COUNT(*) INTO @col_exists 
FROM information_schema.COLUMNS 
WHERE TABLE_SCHEMA = DATABASE() 
AND TABLE_NAME = 'eb_user' 
AND COLUMN_NAME = 'virtual_balance';

SET @sql = IF(@col_exists = 0, 
    'ALTER TABLE eb_user ADD COLUMN virtual_balance DECIMAL(10,2) DEFAULT 0.00 COMMENT ''虚拟货币余额''', 
    'SELECT ''Column virtual_balance already exists'' AS message');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 2. 虚拟货币充值记录表
CREATE TABLE IF NOT EXISTS eb_virtual_currency_recharge (
    id INT PRIMARY KEY AUTO_INCREMENT COMMENT '充值记录ID',
    user_id INT NOT NULL COMMENT '用户ID',
    order_no VARCHAR(50) NOT NULL UNIQUE COMMENT '订单号',
    amount DECIMAL(10,2) NOT NULL COMMENT '充值金额（人民币）',
    virtual_amount DECIMAL(10,2) NOT NULL COMMENT '获得的虚拟货币数量',
    payment_method VARCHAR(20) NOT NULL COMMENT '支付方式：alipay/wechat/balance',
    payment_status TINYINT DEFAULT 0 COMMENT '支付状态：0-待支付，1-已支付，2-已取消',
    transaction_id VARCHAR(100) COMMENT '第三方支付交易号',
    create_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    pay_time DATETIME COMMENT '支付时间',
    INDEX idx_user_id (user_id),
    INDEX idx_order_no (order_no),
    INDEX idx_create_time (create_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='虚拟货币充值记录表';

-- 3. 虚拟货币消费记录表
CREATE TABLE IF NOT EXISTS eb_virtual_currency_transaction (
    id INT PRIMARY KEY AUTO_INCREMENT COMMENT '交易记录ID',
    user_id INT NOT NULL COMMENT '用户ID',
    transaction_type VARCHAR(20) NOT NULL COMMENT '交易类型：recharge-充值，gift-送礼，refund-退款',
    amount DECIMAL(10,2) NOT NULL COMMENT '交易金额（正数为收入，负数为支出）',
    balance_after DECIMAL(10,2) NOT NULL COMMENT '交易后余额',
    related_id INT COMMENT '关联ID（礼物记录ID、充值记录ID等）',
    description VARCHAR(200) COMMENT '交易描述',
    create_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    INDEX idx_user_id (user_id),
    INDEX idx_transaction_type (transaction_type),
    INDEX idx_create_time (create_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='虚拟货币交易记录表';

-- 4. 礼物配置表
CREATE TABLE IF NOT EXISTS eb_gift_config (
    id INT PRIMARY KEY AUTO_INCREMENT COMMENT '礼物ID',
    name VARCHAR(50) NOT NULL COMMENT '礼物名称',
    icon VARCHAR(200) NOT NULL COMMENT '礼物图标URL',
    price DECIMAL(10,2) NOT NULL COMMENT '礼物价格（虚拟货币）',
    animation VARCHAR(200) COMMENT '礼物动画效果',
    sort_order INT DEFAULT 0 COMMENT '排序',
    is_enabled TINYINT DEFAULT 1 COMMENT '是否启用：0-禁用，1-启用',
    create_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    INDEX idx_sort_order (sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='礼物配置表';

-- 5. 礼物赠送记录表
CREATE TABLE IF NOT EXISTS eb_gift_record (
    id INT PRIMARY KEY AUTO_INCREMENT COMMENT '礼物记录ID',
    sender_id INT NOT NULL COMMENT '送礼者ID',
    receiver_id INT NOT NULL COMMENT '接收者ID（主播ID）',
    room_id INT COMMENT '直播间ID',
    gift_id INT NOT NULL COMMENT '礼物ID',
    gift_name VARCHAR(50) NOT NULL COMMENT '礼物名称',
    gift_icon VARCHAR(200) COMMENT '礼物图标',
    gift_price DECIMAL(10,2) NOT NULL COMMENT '礼物价格',
    quantity INT DEFAULT 1 COMMENT '数量',
    total_price DECIMAL(10,2) NOT NULL COMMENT '总价格',
    is_anonymous TINYINT DEFAULT 0 COMMENT '是否匿名：0-否，1-是',
    create_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    INDEX idx_sender_id (sender_id),
    INDEX idx_receiver_id (receiver_id),
    INDEX idx_room_id (room_id),
    INDEX idx_create_time (create_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='礼物赠送记录表';

-- 6. 充值套餐配置表
CREATE TABLE IF NOT EXISTS eb_recharge_package (
    id INT PRIMARY KEY AUTO_INCREMENT COMMENT '套餐ID',
    amount DECIMAL(10,2) NOT NULL COMMENT '充值金额（人民币）',
    virtual_amount DECIMAL(10,2) NOT NULL COMMENT '获得的虚拟货币',
    bonus_amount DECIMAL(10,2) DEFAULT 0 COMMENT '赠送的虚拟货币',
    title VARCHAR(50) COMMENT '套餐标题',
    description VARCHAR(200) COMMENT '套餐描述',
    is_hot TINYINT DEFAULT 0 COMMENT '是否热门推荐',
    sort_order INT DEFAULT 0 COMMENT '排序',
    is_enabled TINYINT DEFAULT 1 COMMENT '是否启用',
    create_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    INDEX idx_sort_order (sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='充值套餐配置表';

-- 插入默认充值套餐
INSERT INTO eb_recharge_package (amount, virtual_amount, bonus_amount, title, description, is_hot, sort_order) VALUES
(6, 60, 0, '6元', '获得60虚拟币', 0, 1),
(30, 300, 30, '30元', '获得330虚拟币（赠送30）', 0, 2),
(68, 680, 88, '68元', '获得768虚拟币（赠送88）', 1, 3),
(128, 1280, 200, '128元', '获得1480虚拟币（赠送200）', 1, 4),
(328, 3280, 680, '328元', '获得3960虚拟币（赠送680）', 0, 5),
(648, 6480, 1520, '648元', '获得8000虚拟币（赠送1520）', 0, 6);

-- 插入默认礼物配置
INSERT INTO eb_gift_config (name, icon, price, sort_order) VALUES
('玫瑰', 'https://example.com/gifts/rose.png', 1, 1),
('巧克力', 'https://example.com/gifts/chocolate.png', 5, 2),
('棒棒糖', 'https://example.com/gifts/lollipop.png', 10, 3),
('冰淇淋', 'https://example.com/gifts/icecream.png', 20, 4),
('蛋糕', 'https://example.com/gifts/cake.png', 50, 5),
('香水', 'https://example.com/gifts/perfume.png', 100, 6),
('口红', 'https://example.com/gifts/lipstick.png', 200, 7),
('钻戒', 'https://example.com/gifts/ring.png', 520, 8),
('跑车', 'https://example.com/gifts/car.png', 1314, 9),
('城堡', 'https://example.com/gifts/castle.png', 5200, 10);

-- 查看表结构
SHOW TABLES LIKE 'eb_%gift%';
SHOW TABLES LIKE 'eb_%virtual%';
SHOW TABLES LIKE 'eb_%recharge%';
