-- =============================================
-- 成就系统完整建表SQL
-- 作者: yslg
-- 日期: 2025-10-24
-- 版本: 1.0.0
-- 说明: 包含成就系统的所有数据表及初始数据
-- =============================================

-- =============================================
-- 1. 成就定义表 (tb_achievement)
-- =============================================

DROP TABLE IF EXISTS `tb_achievement`;

CREATE TABLE `tb_achievement` (
    `id` VARCHAR(32) NOT NULL COMMENT '主键ID',
    `achievement_id` VARCHAR(32) NOT NULL COMMENT '成就唯一标识',
    `name` VARCHAR(100) NOT NULL COMMENT '成就名称',
    `description` VARCHAR(500) DEFAULT NULL COMMENT '成就描述',
    `icon` VARCHAR(255) DEFAULT NULL COMMENT '成就图标URL',
    `type` INT DEFAULT 1 COMMENT '成就类型：1-勋章 2-等级',
    `level` INT DEFAULT 1 COMMENT '成就等级：1-铜牌 2-银牌 3-金牌 4-钻石',
    `condition_type` INT NOT NULL COMMENT '触发条件类型：1-学习时长 2-资源数量 3-课程数量 4-连续登录 5-收藏数量 6-任务数量 7-积分数量 8-评论数量 9-章节数量 10-累计登录',
    `condition_value` INT NOT NULL COMMENT '条件值（达成所需的数值）',
    `points` INT DEFAULT 0 COMMENT '获得积分奖励',
    `order_num` INT DEFAULT 0 COMMENT '排序号',
    `creator` VARCHAR(32) DEFAULT NULL COMMENT '创建者ID',
    `updater` VARCHAR(32) DEFAULT NULL COMMENT '更新者ID',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `update_time` DATETIME DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `delete_time` DATETIME DEFAULT NULL COMMENT '删除时间',
    `deleted` TINYINT(1) DEFAULT 0 COMMENT '是否删除：0-否 1-是',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_achievement_id` (`achievement_id`),
    UNIQUE KEY `uk_name` (`name`, `deleted`),
    KEY `idx_type` (`type`),
    KEY `idx_level` (`level`),
    KEY `idx_condition_type` (`condition_type`),
    KEY `idx_deleted` (`deleted`),
    KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='成就定义表';

-- =============================================
-- 2. 用户成就表 (tb_user_achievement)
-- =============================================

DROP TABLE IF EXISTS `tb_user_achievement`;

CREATE TABLE `tb_user_achievement` (
    `id` VARCHAR(32) NOT NULL COMMENT '主键ID',
    `user_id` VARCHAR(32) NOT NULL COMMENT '用户ID',
    `achievement_id` VARCHAR(32) NOT NULL COMMENT '成就ID',
    `obtain_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '获得时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_user_achievement` (`user_id`, `achievement_id`),
    KEY `idx_user_id` (`user_id`),
    KEY `idx_achievement_id` (`achievement_id`),
    KEY `idx_obtain_time` (`obtain_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='用户成就表';

-- =============================================
-- 3. 用户成就进度表 (tb_user_achievement_progress)
-- =============================================

DROP TABLE IF EXISTS `tb_user_achievement_progress`;

CREATE TABLE `tb_user_achievement_progress` (
    `id` VARCHAR(32) NOT NULL COMMENT '主键ID',
    `user_id` VARCHAR(32) NOT NULL COMMENT '用户ID',
    `achievement_id` VARCHAR(32) NOT NULL COMMENT '成就ID',
    `current_value` INT DEFAULT 0 COMMENT '当前进度值',
    `target_value` INT DEFAULT 0 COMMENT '目标进度值',
    `progress_percentage` INT DEFAULT 0 COMMENT '进度百分比（0-100）',
    `completed` TINYINT(1) DEFAULT 0 COMMENT '是否已完成：0-否 1-是',
    `last_update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后更新时间',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_user_achievement_progress` (`user_id`, `achievement_id`),
    KEY `idx_user_id` (`user_id`),
    KEY `idx_achievement_id` (`achievement_id`),
    KEY `idx_completed` (`completed`),
    KEY `idx_last_update_time` (`last_update_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='用户成就进度表';

-- =============================================
-- 初始化成就数据
-- =============================================

-- 清空已有数据（可选）
-- DELETE FROM `tb_achievement`;

-- =============================================
-- 学习时长类成就
-- =============================================

-- -- =============================================
-- -- 课程完成类成就
-- -- =============================================

-- INSERT INTO `tb_achievement` (`id`, `achievement_id`, `name`, `description`, `type`, `level`, `condition_type`, `condition_value`, `points`, `order_num`, `deleted`) VALUES
-- ('ACH005', 'course_complete_bronze', '课程新手', '完成1门课程', 1, 1, 3, 1, 10, 5, 0),
-- ('ACH006', 'course_complete_silver', '课程能手', '完成5门课程', 1, 2, 3, 5, 50, 6, 0),
-- ('ACH007', 'course_complete_gold', '课程专家', '完成20门课程', 1, 3, 3, 20, 200, 7, 0),
-- ('ACH008', 'course_complete_diamond', '课程大师', '完成50门课程', 1, 4, 3, 50, 500, 8, 0);

-- -- =============================================
-- -- 资源浏览类成就
-- -- =============================================

-- INSERT INTO `tb_achievement` (`id`, `achievement_id`, `name`, `description`, `type`, `level`, `condition_type`, `condition_value`, `points`, `order_num`, `deleted`) VALUES
-- ('ACH009', 'resource_view_bronze', '好奇宝宝', '浏览10个资源', 1, 1, 2, 10, 5, 9, 0),
-- ('ACH010', 'resource_view_silver', '探索者', '浏览50个资源', 1, 2, 2, 50, 25, 10, 0),
-- ('ACH011', 'resource_view_gold', '资源猎人', '浏览200个资源', 1, 3, 2, 200, 100, 11, 0),
-- ('ACH012', 'resource_view_diamond', '知识探险家', '浏览500个资源', 1, 4, 2, 500, 250, 12, 0);

-- -- =============================================
-- -- 连续登录类成就
-- -- =============================================

-- INSERT INTO `tb_achievement` (`id`, `achievement_id`, `name`, `description`, `type`, `level`, `condition_type`, `condition_value`, `points`, `order_num`, `deleted`) VALUES
-- ('ACH013', 'login_streak_bronze', '每日一练', '连续登录7天', 1, 1, 4, 7, 20, 13, 0),
-- ('ACH014', 'login_streak_silver', '坚持不懈', '连续登录30天', 1, 2, 4, 30, 100, 14, 0),
-- ('ACH015', 'login_streak_gold', '毅力之王', '连续登录100天', 1, 3, 4, 100, 500, 15, 0),
-- ('ACH016', 'login_streak_diamond', '永不放弃', '连续登录365天', 1, 4, 4, 365, 2000, 16, 0);

-- -- =============================================
-- -- 任务完成类成就
-- -- =============================================

-- INSERT INTO `tb_achievement` (`id`, `achievement_id`, `name`, `description`, `type`, `level`, `condition_type`, `condition_value`, `points`, `order_num`, `deleted`) VALUES
-- ('ACH017', 'task_complete_bronze', '任务新兵', '完成5个任务', 1, 1, 6, 5, 15, 17, 0),
-- ('ACH018', 'task_complete_silver', '任务达人', '完成20个任务', 1, 2, 6, 20, 60, 18, 0),
-- ('ACH019', 'task_complete_gold', '任务专家', '完成50个任务', 1, 3, 6, 50, 150, 19, 0),
-- ('ACH020', 'task_complete_diamond', '任务大师', '完成100个任务', 1, 4, 6, 100, 300, 20, 0);

-- -- =============================================
-- -- 收藏资源类成就
-- -- =============================================

-- INSERT INTO `tb_achievement` (`id`, `achievement_id`, `name`, `description`, `type`, `level`, `condition_type`, `condition_value`, `points`, `order_num`, `deleted`) VALUES
-- ('ACH021', 'collect_bronze', '收藏家', '收藏10个资源', 1, 1, 5, 10, 10, 21, 0),
-- ('ACH022', 'collect_silver', '资料库', '收藏50个资源', 1, 2, 5, 50, 50, 22, 0),
-- ('ACH023', 'collect_gold', '知识宝库', '收藏100个资源', 1, 3, 5, 100, 100, 23, 0),
-- ('ACH024', 'collect_diamond', '珍宝收藏家', '收藏200个资源', 1, 4, 5, 200, 200, 24, 0);

-- -- =============================================
-- -- 章节完成类成就
-- -- =============================================

-- INSERT INTO `tb_achievement` (`id`, `achievement_id`, `name`, `description`, `type`, `level`, `condition_type`, `condition_value`, `points`, `order_num`, `deleted`) VALUES
-- ('ACH025', 'chapter_complete_bronze', '章节探索者', '完成10个章节', 1, 1, 9, 10, 20, 25, 0),
-- ('ACH026', 'chapter_complete_silver', '章节达人', '完成50个章节', 1, 2, 9, 50, 100, 26, 0),
-- ('ACH027', 'chapter_complete_gold', '章节专家', '完成100个章节', 1, 3, 9, 100, 200, 27, 0);

-- -- =============================================
-- -- 累计登录类成就
-- -- =============================================

-- INSERT INTO `tb_achievement` (`id`, `achievement_id`, `name`, `description`, `type`, `level`, `condition_type`, `condition_value`, `points`, `order_num`, `deleted`) VALUES
-- ('ACH028', 'total_login_bronze', '常客', '累计登录30天', 1, 1, 10, 30, 30, 28, 0),
-- ('ACH029', 'total_login_silver', '老朋友', '累计登录100天', 1, 2, 10, 100, 100, 29, 0),
-- ('ACH030', 'total_login_gold', '忠实用户', '累计登录365天', 1, 3, 10, 365, 500, 30, 0);

-- -- =============================================
-- -- 数据验证查询
-- -- =============================================

-- -- 查询所有成就数量
-- SELECT 
--     '成就总数' AS '统计项', 
--     COUNT(*) AS '数量' 
-- FROM tb_achievement 
-- WHERE deleted = 0

-- UNION ALL

-- -- 按类型统计
-- SELECT 
--     CONCAT('类型', type, '成就') AS '统计项',
--     COUNT(*) AS '数量'
-- FROM tb_achievement 
-- WHERE deleted = 0
-- GROUP BY type

-- UNION ALL

-- -- 按等级统计
-- SELECT 
--     CASE level
--         WHEN 1 THEN '铜牌成就'
--         WHEN 2 THEN '银牌成就'
--         WHEN 3 THEN '金牌成就'
--         WHEN 4 THEN '钻石成就'
--     END AS '统计项',
--     COUNT(*) AS '数量'
-- FROM tb_achievement 
-- WHERE deleted = 0
-- GROUP BY level
-- ORDER BY '统计项';

-- =============================================
-- 常用查询SQL示例
-- =============================================

-- 1. 查询所有启用的成就（按排序号）
-- SELECT * FROM tb_achievement WHERE deleted = 0 ORDER BY order_num;

-- 2. 查询某用户的所有成就
-- SELECT 
--     ua.id,
--     ua.user_id,
--     a.achievement_id,
--     a.name,
--     a.description,
--     a.icon,
--     a.type,
--     a.level,
--     a.points,
--     ua.obtain_time
-- FROM tb_user_achievement ua
-- INNER JOIN tb_achievement a ON ua.achievement_id = a.achievement_id
-- WHERE ua.user_id = 'USER_ID' AND a.deleted = 0
-- ORDER BY ua.obtain_time DESC;

-- 3. 查询某用户的成就进度
-- SELECT 
--     p.id,
--     p.user_id,
--     a.achievement_id,
--     a.name,
--     a.description,
--     p.current_value,
--     p.target_value,
--     p.progress_percentage,
--     p.completed,
--     p.last_update_time
-- FROM tb_user_achievement_progress p
-- INNER JOIN tb_achievement a ON p.achievement_id = a.achievement_id
-- WHERE p.user_id = 'USER_ID' AND a.deleted = 0
-- ORDER BY p.progress_percentage DESC, p.last_update_time DESC;

-- 4. 查询成就排行榜（按用户获得成就数量）
-- SELECT 
--     ua.user_id,
--     u.username,
--     u.nickname,
--     COUNT(*) as achievement_count,
--     SUM(a.points) as total_points,
--     MAX(ua.obtain_time) as latest_obtain_time
-- FROM tb_user_achievement ua
-- INNER JOIN tb_achievement a ON ua.achievement_id = a.achievement_id
-- INNER JOIN tb_sys_user u ON ua.user_id = u.id
-- WHERE a.deleted = 0
-- GROUP BY ua.user_id, u.username, u.nickname
-- ORDER BY achievement_count DESC, total_points DESC, latest_obtain_time DESC
-- LIMIT 10;

-- 5. 查询某成就的最近获得者
-- SELECT 
--     ua.id,
--     ua.user_id,
--     u.username,
--     u.nickname,
--     ua.obtain_time
-- FROM tb_user_achievement ua
-- INNER JOIN tb_sys_user u ON ua.user_id = u.id
-- WHERE ua.achievement_id = 'ACHIEVEMENT_ID'
-- ORDER BY ua.obtain_time DESC
-- LIMIT 10;

-- 6. 查询某用户可以领取的成就（进度已达100%但未获得）
-- SELECT 
--     p.user_id,
--     a.achievement_id,
--     a.name,
--     a.description,
--     a.points,
--     p.current_value,
--     p.target_value
-- FROM tb_user_achievement_progress p
-- INNER JOIN tb_achievement a ON p.achievement_id = a.achievement_id
-- LEFT JOIN tb_user_achievement ua ON p.user_id = ua.user_id AND p.achievement_id = ua.achievement_id
-- WHERE p.user_id = 'USER_ID' 
--   AND p.completed = 1 
--   AND ua.id IS NULL
--   AND a.deleted = 0;

-- =============================================
-- 条件类型说明
-- =============================================
-- 1  - 学习时长（分钟）
-- 2  - 浏览资源数量
-- 3  - 完成课程数量
-- 4  - 连续登录天数
-- 5  - 收藏资源数量
-- 6  - 完成任务数量
-- 7  - 获得积分数量
-- 8  - 发表评论数量
-- 9  - 完成章节数量
-- 10 - 累计登录天数

-- =============================================
-- 成就类型说明
-- =============================================
-- 1 - 勋章
-- 2 - 等级

-- =============================================
-- 成就等级说明
-- =============================================
-- 1 - 铜牌
-- 2 - 银牌
-- 3 - 金牌
-- 4 - 钻石

-- =============================================
-- 建表完成
-- =============================================
SELECT '成就系统表结构创建完成！' AS '状态', NOW() AS '时间';

