-- ============================================
-- 压力测试数据生成脚本
-- 用于生成大量测试数据以进行性能测试
-- ============================================

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- 清理测试数据（可选，谨慎使用）
-- DELETE FROM wallet_ledger WHERE user_id > 1000;
-- DELETE FROM rental_orders WHERE id > 100;
-- DELETE FROM rental_listings WHERE id > 100;
-- DELETE FROM game_accounts WHERE id > 100;
-- DELETE FROM users WHERE id > 1000;

-- ============================================
-- 1. 批量生成用户数据（10000个用户）
-- ============================================
DROP PROCEDURE IF EXISTS generate_users;
DELIMITER $$
CREATE PROCEDURE generate_users(IN batch_size INT)
BEGIN
    DECLARE i INT DEFAULT 1;
    DECLARE phone_num VARCHAR(32);
    DECLARE nick VARCHAR(64);

    WHILE i <= batch_size DO
        SET phone_num = CONCAT('138', LPAD(i, 8, '0'));
        SET nick = CONCAT('测试用户', i);

        INSERT INTO users (
            phone, nickname, realname_status, risk_status,
            credit_score, status, created_at
        ) VALUES (
            phone_num, nick,
            CASE WHEN i % 10 = 0 THEN 'unverified' ELSE 'verified' END,
            CASE WHEN i % 100 = 0 THEN 'frozen' ELSE 'normal' END,
            80 + (i % 20),
            'active',
            DATE_SUB(NOW(), INTERVAL (i % 365) DAY)
        ) ON DUPLICATE KEY UPDATE id=id;

        -- 为已实名用户创建实名记录
        IF i % 10 != 0 THEN
            INSERT INTO user_realname (
                user_id, provider, status, masked_name, masked_id_no, verified_at
            ) VALUES (
                LAST_INSERT_ID(), 'mock', 'success',
                CONCAT('张*', CHAR(65 + (i % 26))),
                CONCAT('3301**********', LPAD(i % 10000, 4, '0')),
                DATE_SUB(NOW(), INTERVAL (i % 300) DAY)
            ) ON DUPLICATE KEY UPDATE user_id=user_id;
        END IF;

        -- 为用户创建钱包
        INSERT INTO wallet_accounts (user_id, available_balance, frozen_balance, status)
        VALUES (
            LAST_INSERT_ID(),
            (i % 10) * 100.00,
            (i % 5) * 50.00,
            'active'
        ) ON DUPLICATE KEY UPDATE user_id=user_id;

        SET i = i + 1;

        -- 每1000条提交一次
        IF i % 1000 = 0 THEN
            COMMIT;
        END IF;
    END WHILE;

    COMMIT;
END$$
DELIMITER ;

-- ============================================
-- 2. 批量生成游戏账号和商品（50000个商品）
-- ============================================
DROP PROCEDURE IF EXISTS generate_listings;
DELIMITER $$
CREATE PROCEDURE generate_listings(IN batch_size INT)
BEGIN
    DECLARE i INT DEFAULT 1;
    DECLARE user_id_val BIGINT;
    DECLARE account_id_val BIGINT;
    DECLARE price_val DECIMAL(12,2);
    DECLARE deposit_val DECIMAL(12,2);

    WHILE i <= batch_size DO
        -- 随机选择一个用户作为号主（已实名用户）
        SELECT id INTO user_id_val FROM users
        WHERE realname_status = 'verified'
        ORDER BY RAND() LIMIT 1;

        -- 创建游戏账号
        INSERT INTO game_accounts (
            owner_id, game_name, server_region, login_platform,
            title, description, rank_level, haf_coin_amount, status
        ) VALUES (
            user_id_val,
            'delta_force',
            CASE (i % 4)
                WHEN 0 THEN '亚服'
                WHEN 1 THEN '美服'
                WHEN 2 THEN '欧服'
                ELSE '国服'
            END,
            CASE (i % 3)
                WHEN 0 THEN 'Steam'
                WHEN 1 THEN 'Epic'
                ELSE 'WeGame'
            END,
            CONCAT('账号', i, ' 高分段'),
            CONCAT('这是一个测试账号，编号', i),
            CASE (i % 5)
                WHEN 0 THEN '青铜'
                WHEN 1 THEN '白银'
                WHEN 2 THEN '黄金'
                WHEN 3 THEN '铂金'
                ELSE '钻石'
            END,
            (i % 100) * 10000,
            'active'
        );

        SET account_id_val = LAST_INSERT_ID();

        -- 创建租号商品
        SET price_val = 5.00 + (i % 20) * 0.5;
        SET deposit_val = 100.00 + (i % 10) * 50.00;

        INSERT INTO rental_listings (
            account_id, owner_id, price, deposit_amount,
            in_transaction, status, review_status, published_at
        ) VALUES (
            account_id_val,
            user_id_val,
            price_val,
            deposit_val,
            CASE WHEN i % 10 = 0 THEN 1 ELSE 0 END,
            CASE
                WHEN i % 20 = 0 THEN 'offline'
                WHEN i % 15 = 0 THEN 'draft'
                ELSE 'active'
            END,
            CASE
                WHEN i % 15 = 0 THEN 'pending'
                WHEN i % 30 = 0 THEN 'rejected'
                ELSE 'approved'
            END,
            DATE_SUB(NOW(), INTERVAL (i % 90) DAY)
        );

        SET i = i + 1;

        IF i % 1000 = 0 THEN
            COMMIT;
        END IF;
    END WHILE;

    COMMIT;
END$$
DELIMITER ;

-- ============================================
-- 3. 批量生成订单数据（30000个订单）
-- ============================================
DROP PROCEDURE IF EXISTS generate_orders;
DELIMITER $$
CREATE PROCEDURE generate_orders(IN batch_size INT)
BEGIN
    DECLARE i INT DEFAULT 1;
    DECLARE listing_id_val BIGINT;
    DECLARE account_id_val BIGINT;
    DECLARE owner_id_val BIGINT;
    DECLARE renter_id_val BIGINT;
    DECLARE order_no_val VARCHAR(64);
    DECLARE rent_amount_val DECIMAL(12,2);
    DECLARE deposit_val DECIMAL(12,2);

    WHILE i <= batch_size DO
        -- 随机选择一个上架的商品
        SELECT rl.id, rl.account_id, rl.owner_id, rl.price, rl.deposit_amount
        INTO listing_id_val, account_id_val, owner_id_val, rent_amount_val, deposit_val
        FROM rental_listings rl
        WHERE rl.status = 'active' AND rl.review_status = 'approved'
        ORDER BY RAND() LIMIT 1;

        -- 随机选择一个租客（不能是号主本人）
        SELECT id INTO renter_id_val FROM users
        WHERE id != owner_id_val AND realname_status = 'verified'
        ORDER BY RAND() LIMIT 1;

        SET order_no_val = CONCAT('ORD', DATE_FORMAT(NOW(), '%Y%m%d'), LPAD(i, 8, '0'));
        SET rent_amount_val = rent_amount_val * 24; -- 24小时租金

        INSERT INTO rental_orders (
            order_no, listing_id, account_id, owner_id, renter_id,
            estimated_duration_hours, rent_amount, owner_rent_amount,
            deposit_amount, platform_fee, status, handoff_status,
            settlement_status, rented_at, created_at
        ) VALUES (
            order_no_val,
            listing_id_val,
            account_id_val,
            owner_id_val,
            renter_id_val,
            24,
            rent_amount_val,
            rent_amount_val * 0.95, -- 号主实得95%
            deposit_val,
            rent_amount_val * 0.05, -- 平台5%手续费
            CASE (i % 10)
                WHEN 0 THEN 'pending_payment'
                WHEN 1 THEN 'cancelled'
                WHEN 2 THEN 'closed'
                ELSE 'completed'
            END,
            CASE (i % 10)
                WHEN 0 THEN 'none'
                WHEN 1 THEN 'none'
                WHEN 2 THEN 'owner_delivered'
                ELSE 'owner_received'
            END,
            CASE (i % 10)
                WHEN 0 THEN 'unsettled'
                WHEN 1 THEN 'unsettled'
                ELSE 'settled'
            END,
            DATE_SUB(NOW(), INTERVAL (i % 60) DAY),
            DATE_SUB(NOW(), INTERVAL (i % 60) DAY)
        );

        SET i = i + 1;

        IF i % 1000 = 0 THEN
            COMMIT;
        END IF;
    END WHILE;

    COMMIT;
END$$
DELIMITER ;

-- ============================================
-- 4. 批量生成钱包流水（100000条流水）
-- ============================================
DROP PROCEDURE IF EXISTS generate_wallet_ledger;
DELIMITER $$
CREATE PROCEDURE generate_wallet_ledger(IN batch_size INT)
BEGIN
    DECLARE i INT DEFAULT 1;
    DECLARE user_id_val BIGINT;
    DECLARE order_id_val BIGINT;
    DECLARE ledger_no_val VARCHAR(64);
    DECLARE amount_val DECIMAL(12,2);

    WHILE i <= batch_size DO
        -- 随机选择用户
        SELECT id INTO user_id_val FROM users ORDER BY RAND() LIMIT 1;

        -- 随机关联订单（50%概率）
        IF RAND() > 0.5 THEN
            SELECT id INTO order_id_val FROM rental_orders ORDER BY RAND() LIMIT 1;
        ELSE
            SET order_id_val = NULL;
        END IF;

        SET ledger_no_val = CONCAT('LDG', DATE_FORMAT(NOW(), '%Y%m%d%H%i%s'), LPAD(i, 6, '0'));
        SET amount_val = (i % 500) + RAND() * 100;

        INSERT INTO wallet_ledger (
            ledger_no, user_id, order_id, direction, amount,
            balance_after, balance_type, biz_type, biz_no, remark, created_at
        ) VALUES (
            ledger_no_val,
            user_id_val,
            order_id_val,
            CASE WHEN i % 2 = 0 THEN 'in' ELSE 'out' END,
            amount_val,
            1000.00 + (i % 1000),
            CASE WHEN i % 5 = 0 THEN 'frozen' ELSE 'available' END,
            CASE (i % 6)
                WHEN 0 THEN 'rent_payment'
                WHEN 1 THEN 'deposit_freeze'
                WHEN 2 THEN 'settlement'
                WHEN 3 THEN 'refund'
                WHEN 4 THEN 'recharge'
                ELSE 'withdraw'
            END,
            CONCAT('BIZ', LPAD(i, 10, '0')),
            CONCAT('测试流水', i),
            DATE_SUB(NOW(), INTERVAL (i % 180) DAY)
        );

        SET i = i + 1;

        IF i % 1000 = 0 THEN
            COMMIT;
        END IF;
    END WHILE;

    COMMIT;
END$$
DELIMITER ;

-- ============================================
-- 5. 批量生成聊天消息（50000条消息）
-- ============================================
DROP PROCEDURE IF EXISTS generate_chat_messages;
DELIMITER $$
CREATE PROCEDURE generate_chat_messages(IN batch_size INT)
BEGIN
    DECLARE i INT DEFAULT 1;
    DECLARE conv_id BIGINT;
    DECLARE user_id_val BIGINT;

    WHILE i <= batch_size DO
        -- 随机选择一个会话
        SELECT id INTO conv_id FROM chat_conversations ORDER BY RAND() LIMIT 1;

        IF conv_id IS NOT NULL THEN
            -- 随机选择发送者
            SELECT id INTO user_id_val FROM users ORDER BY RAND() LIMIT 1;

            INSERT INTO chat_messages (
                conversation_id, sender_type, sender_id, sender_role,
                content_type, content, created_at
            ) VALUES (
                conv_id,
                'user',
                user_id_val,
                CASE WHEN i % 2 = 0 THEN 'owner' ELSE 'renter' END,
                'text',
                CONCAT('这是测试消息', i, '，内容随机生成用于压力测试'),
                DATE_SUB(NOW(), INTERVAL (i % 30) DAY)
            );
        END IF;

        SET i = i + 1;

        IF i % 1000 = 0 THEN
            COMMIT;
        END IF;
    END WHILE;

    COMMIT;
END$$
DELIMITER ;

-- ============================================
-- 执行数据生成（根据需要调整数量）
-- ============================================

-- 生成10000个用户
CALL generate_users(10000);

-- 生成50000个商品
CALL generate_listings(50000);

-- 生成30000个订单
CALL generate_orders(30000);

-- 生成100000条钱包流水
CALL generate_wallet_ledger(100000);

-- 为前1000个订单创建会话
INSERT INTO chat_conversations (order_id, type, title, status, last_message_at)
SELECT id, 'order_group', CONCAT('订单', order_no, '群聊'), 'active', created_at
FROM rental_orders
WHERE id <= 1000
ON DUPLICATE KEY UPDATE order_id=order_id;

-- 生成50000条聊天消息
CALL generate_chat_messages(50000);

-- ============================================
-- 查看数据统计
-- ============================================
SELECT '用户数' as item, COUNT(*) as count FROM users
UNION ALL
SELECT '游戏账号数', COUNT(*) FROM game_accounts
UNION ALL
SELECT '商品数', COUNT(*) FROM rental_listings
UNION ALL
SELECT '订单数', COUNT(*) FROM rental_orders
UNION ALL
SELECT '钱包流水数', COUNT(*) FROM wallet_ledger
UNION ALL
SELECT '聊天会话数', COUNT(*) FROM chat_conversations
UNION ALL
SELECT '聊天消息数', COUNT(*) FROM chat_messages;

SET FOREIGN_KEY_CHECKS = 1;
