-- =====================================================
-- 测试邀请码生成功能
-- 执行时间: 2025-12-12
-- =====================================================

USE blade;

-- =====================================================
-- 1. 插入测试数据
-- =====================================================

-- 为评委生成测试邀请码（使用现有字段）
INSERT INTO martial_judge_invite (
    id, competition_id, judge_id, invite_code, role,
    venue_id, projects, expire_time, is_used,
    status, create_time
) VALUES (
    1001, 1, 1, 'TEST01', 'judge',
    1, '["女子组长拳","男子组陈氏太极拳"]',
    DATE_ADD(NOW(), INTERVAL 30 DAY), 0,
    1, NOW()
);

-- 为裁判长生成邀请码
INSERT INTO martial_judge_invite (
    id, competition_id, judge_id, invite_code, role,
    venue_id, projects, expire_time, is_used,
    status, create_time
) VALUES (
    1002, 1, 2, 'ADMIN1', 'chief_judge',
    NULL, NULL,
    DATE_ADD(NOW(), INTERVAL 30 DAY), 0,
    1, NOW()
);

-- =====================================================
-- 2. 查询测试
-- =====================================================

-- 查询某个评委的有效邀请码
SELECT
    ji.id,
    ji.invite_code,
    ji.role,
    ji.expire_time,
    ji.is_used,
    ji.status,
    j.name AS judge_name,
    c.competition_name
FROM martial_judge_invite ji
LEFT JOIN martial_judge j ON ji.judge_id = j.id
LEFT JOIN martial_competition c ON ji.competition_id = c.id
WHERE ji.competition_id = 1
  AND ji.judge_id = 1
  AND ji.status = 1
  AND ji.is_deleted = 0
  AND ji.expire_time > NOW()
ORDER BY ji.create_time DESC
LIMIT 1;

-- 查询所有有效邀请码
SELECT
    ji.id,
    ji.invite_code,
    ji.role,
    j.name AS judge_name,
    ji.expire_time,
    ji.is_used,
    CASE
        WHEN ji.is_used = 1 THEN '已使用'
        WHEN ji.expire_time < NOW() THEN '已过期'
        WHEN ji.status = 0 THEN '已禁用'
        ELSE '待使用'
    END AS status_text
FROM martial_judge_invite ji
LEFT JOIN martial_judge j ON ji.judge_id = j.id
WHERE ji.competition_id = 1
  AND ji.is_deleted = 0
ORDER BY ji.create_time DESC;

-- =====================================================
-- 3. 统计查询
-- =====================================================

-- 统计某个赛事的邀请码状态
SELECT
    COUNT(*) AS total,
    SUM(CASE WHEN is_used = 0 AND status = 1 AND expire_time > NOW() THEN 1 ELSE 0 END) AS available,
    SUM(CASE WHEN is_used = 1 THEN 1 ELSE 0 END) AS used,
    SUM(CASE WHEN expire_time <= NOW() THEN 1 ELSE 0 END) AS expired,
    SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) AS disabled
FROM martial_judge_invite
WHERE competition_id = 1
  AND is_deleted = 0;

-- =====================================================
-- 4. 验证邀请码唯一性
-- =====================================================

-- 检查邀请码是否重复
SELECT
    invite_code,
    COUNT(*) AS count
FROM martial_judge_invite
WHERE is_deleted = 0
GROUP BY invite_code
HAVING COUNT(*) > 1;

-- =====================================================
-- 5. 清理测试数据（可选）
-- =====================================================

-- 删除测试数据
-- DELETE FROM martial_judge_invite WHERE id IN (1001, 1002);

SELECT 'Test data inserted successfully!' AS status;
