use school_news;
-- 用户收藏表
DROP TABLE IF EXISTS `tb_user_collection`;
CREATE TABLE `tb_user_collection` (
  `id` VARCHAR(50) NOT NULL COMMENT '收藏ID',
  `user_id` VARCHAR(50) NOT NULL COMMENT '用户ID',
  `collection_type` INT(4) NOT NULL COMMENT '收藏类型（1资源 2课程）',
  `collection_id` VARCHAR(50) NOT NULL COMMENT '收藏对象ID',
  `create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '收藏时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_collection` (`user_id`, `collection_type`, `collection_id`),
  KEY `idx_user` (`user_id`),
  KEY `idx_collection` (`collection_type`, `collection_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='用户收藏表';

-- 成就表
DROP TABLE IF EXISTS `tb_achievement`;
CREATE TABLE `tb_achievement` (
  `id` VARCHAR(50) NOT NULL COMMENT '成就ID',
  `achievement_id` VARCHAR(50) NOT NULL COMMENT '成就唯一标识',
  `name` VARCHAR(100) NOT NULL COMMENT '成就名称',
  `description` VARCHAR(255) DEFAULT NULL COMMENT '成就描述',
  `icon` VARCHAR(255) DEFAULT NULL COMMENT '成就图标',
  `type` INT(4) DEFAULT 1 COMMENT '成就类型（1勋章 2等级）',
  `level` INT(4) DEFAULT 1 COMMENT '成就等级',
  `condition_type` INT(4) DEFAULT 1 COMMENT '获取条件类型（1学习时长 2资源数量 3课程数量 4连续学习天数）',
  `condition_value` INT(11) DEFAULT 0 COMMENT '条件值',
  `points` INT(11) DEFAULT 0 COMMENT '获得积分',
  `order_num` INT(4) DEFAULT 0 COMMENT '排序号',
  `creator` VARCHAR(50) DEFAULT NULL COMMENT '创建者',
  `updater` VARCHAR(50) DEFAULT NULL COMMENT '更新者',
  `create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `delete_time` TIMESTAMP NULL DEFAULT NULL COMMENT '删除时间',
  `deleted` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否删除',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_achievement_id` (`achievement_id`),
  KEY `idx_type` (`type`),
  KEY `idx_level` (`level`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='成就表';

-- 用户成就关联表
DROP TABLE IF EXISTS `tb_user_achievement`;
CREATE TABLE `tb_user_achievement` (
  `id` VARCHAR(50) NOT NULL COMMENT '关联ID',
  `user_id` VARCHAR(50) NOT NULL COMMENT '用户ID',
  `achievement_id` VARCHAR(50) NOT NULL COMMENT '成就ID',
  `obtain_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '获得时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_achievement` (`user_id`, `achievement_id`),
  KEY `idx_user` (`user_id`),
  KEY `idx_achievement` (`achievement_id`),
  KEY `idx_obtain_time` (`obtain_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='用户成就表';

-- 用户积分表
DROP TABLE IF EXISTS `tb_user_points`;
CREATE TABLE `tb_user_points` (
  `id` VARCHAR(50) NOT NULL COMMENT '积分ID',
  `user_id` VARCHAR(50) NOT NULL COMMENT '用户ID',
  `total_points` INT(11) DEFAULT 0 COMMENT '总积分',
  `current_points` INT(11) DEFAULT 0 COMMENT '当前积分',
  `level` INT(4) DEFAULT 1 COMMENT '用户等级',
  `create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user` (`user_id`),
  KEY `idx_level` (`level`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='用户积分表';

-- 积分记录表
DROP TABLE IF EXISTS `tb_points_record`;
CREATE TABLE `tb_points_record` (
  `id` VARCHAR(50) NOT NULL COMMENT '记录ID',
  `user_id` VARCHAR(50) NOT NULL COMMENT '用户ID',
  `points` INT(11) NOT NULL COMMENT '积分变动',
  `type` INT(4) DEFAULT 1 COMMENT '类型（1获得 2消费）',
  `source_type` INT(4) DEFAULT 1 COMMENT '来源类型（1学习资源 2学习课程 3完成任务 4获得成就）',
  `source_id` VARCHAR(50) DEFAULT NULL COMMENT '来源ID',
  `description` VARCHAR(255) DEFAULT NULL COMMENT '说明',
  `create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`),
  KEY `idx_user` (`user_id`),
  KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='积分记录表';

-- 用户浏览记录表
DROP TABLE IF EXISTS `tb_user_browse_record`;
CREATE TABLE `tb_user_browse_record` (
  `id` VARCHAR(50) NOT NULL COMMENT '记录ID',
  `user_id` VARCHAR(50) NOT NULL COMMENT '用户ID',
  `browse_type` INT(4) NOT NULL COMMENT '浏览类型（1资源 2课程）',
  `browse_id` VARCHAR(50) NOT NULL COMMENT '浏览对象ID',
  `browse_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '浏览时间',
  `ip_address` VARCHAR(45) DEFAULT NULL COMMENT 'IP地址',
  PRIMARY KEY (`id`),
  KEY `idx_user` (`user_id`),
  KEY `idx_browse` (`browse_type`, `browse_id`),
  KEY `idx_time` (`browse_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='用户浏览记录表';

