| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- -- 任务模块数据库创建脚本
- -- 创建时间:根据数据库设计文档自动生成
- -- 1. 任务分类表
- CREATE TABLE IF NOT EXISTS `task_categories` (
- `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
- `name` varchar(100) NOT NULL COMMENT '分类名称',
- `code` varchar(50) NOT NULL COMMENT '分类编码(唯一)',
- `description` varchar(255) DEFAULT NULL COMMENT '分类描述',
- `sort_order` int(11) NOT NULL DEFAULT 0 COMMENT '排序顺序',
- `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
- `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
- PRIMARY KEY (`id`),
- UNIQUE KEY `idx_code` (`code`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='任务分类表';
- -- 2. 任务定义表
- CREATE TABLE IF NOT EXISTS `task_tasks` (
- `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
- `category_id` int(11) NOT NULL COMMENT '任务分类ID,外键关联task_categories表',
- `name` varchar(100) NOT NULL COMMENT '任务名称',
- `description` varchar(500) DEFAULT NULL COMMENT '任务描述',
- `type` varchar(20) NOT NULL COMMENT '任务类型(daily, weekly, achievement, event, tutorial, team)',
- `prerequisite_tasks` json DEFAULT NULL COMMENT '前置任务ID(JSON格式)',
- `level_required` int(11) NOT NULL DEFAULT 0 COMMENT '所需等级',
- `time_limit` int(11) DEFAULT NULL COMMENT '时间限制(秒,NULL表示无限制)',
- `max_completions` int(11) NOT NULL DEFAULT 1 COMMENT '最大完成次数(用于限制任务可完成的次数)',
- `reset_type` varchar(20) NOT NULL DEFAULT 'none' COMMENT '重置类型(none, daily, weekly, monthly)',
- `display_params` json DEFAULT NULL COMMENT '显示参数(JSON格式,存储与任务显示相关的参数)',
- `sort_order` int(11) NOT NULL DEFAULT 0 COMMENT '排序权重(数值越大越靠前)',
- `is_active` tinyint(1) NOT NULL DEFAULT 1 COMMENT '是否激活(0:否, 1:是)',
- `start_time` timestamp NULL DEFAULT NULL COMMENT '开始时间(NULL表示立即开始)',
- `end_time` timestamp NULL DEFAULT NULL COMMENT '结束时间(NULL表示永不结束)',
- `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
- `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
- PRIMARY KEY (`id`),
- KEY `idx_category` (`category_id`),
- KEY `idx_type` (`type`),
- KEY `idx_active_time` (`is_active`, `start_time`, `end_time`),
- KEY `idx_sort` (`sort_order`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='任务定义表';
- -- 3. 任务奖励定义表
- CREATE TABLE IF NOT EXISTS `task_rewards` (
- `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
- `task_id` int(11) NOT NULL COMMENT '任务ID,外键关联task_tasks表',
- `reward_type` varchar(50) NOT NULL COMMENT '奖励类型(item, currency, experience, feature_unlock等)',
- `reward_param1` varchar(100) NOT NULL COMMENT '奖励参数1(如物品类型、货币类型等)',
- `reward_param2` varchar(100) NOT NULL COMMENT '奖励参数2(如物品ID、货币ID等)',
- `quantity` int(11) NOT NULL DEFAULT 1 COMMENT '奖励数量',
- `extra_data` json DEFAULT NULL COMMENT '额外数据(JSON格式)',
- `sort_order` int(11) NOT NULL DEFAULT 0 COMMENT '排序权重(数值越大越靠前)',
- `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
- `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
- PRIMARY KEY (`id`),
- KEY `idx_task_id` (`task_id`),
- KEY `idx_reward_type` (`reward_type`),
- KEY `idx_reward_params` (`reward_param1`, `reward_param2`),
- KEY `idx_sort` (`sort_order`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='任务奖励定义表';
- -- 4. 任务接取消耗表
- CREATE TABLE IF NOT EXISTS `task_costs` (
- `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
- `task_id` int(11) NOT NULL COMMENT '任务ID,外键关联task_tasks表',
- `cost_type` varchar(50) NOT NULL COMMENT '消耗类型(currency, item, energy, ticket等)',
- `cost_param1` varchar(100) NOT NULL COMMENT '消耗参数1(如货币类型、物品类型等)',
- `cost_param2` varchar(100) NOT NULL COMMENT '消耗参数2(如货币ID、物品ID等)',
- `quantity` int(11) NOT NULL DEFAULT 1 COMMENT '消耗数量',
- `extra_data` json DEFAULT NULL COMMENT '额外数据(JSON格式)',
- `sort_order` int(11) NOT NULL DEFAULT 0 COMMENT '排序权重(数值越大越靠前)',
- `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
- `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
- PRIMARY KEY (`id`),
- KEY `idx_task_id` (`task_id`),
- KEY `idx_cost_type` (`cost_type`),
- KEY `idx_cost_params` (`cost_param1`, `cost_param2`),
- KEY `idx_sort` (`sort_order`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='任务接取消耗表';
- -- 5. 任务条件表
- CREATE TABLE IF NOT EXISTS `task_conditions` (
- `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
- `code` varchar(50) NOT NULL COMMENT '条件代码(唯一),如login, plant, harvest',
- `name` varchar(100) NOT NULL COMMENT '条件名称,如登录游戏, 种植作物, 收获作物',
- `description` varchar(255) DEFAULT NULL COMMENT '条件描述',
- `param_schema` json DEFAULT NULL COMMENT '参数模式,定义此条件需要的参数及其类型',
- `handler_class` varchar(255) DEFAULT NULL COMMENT '处理此条件的类名',
- `is_active` tinyint(1) NOT NULL DEFAULT 1 COMMENT '是否激活(0:否, 1:是)',
- `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
- `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
- PRIMARY KEY (`id`),
- UNIQUE KEY `idx_code` (`code`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='任务条件表';
- -- 6. 任务达成条件表
- CREATE TABLE IF NOT EXISTS `task_achievement_conditions` (
- `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
- `task_id` int(11) NOT NULL COMMENT '任务ID,外键关联task_tasks表',
- `condition_id` int(11) NOT NULL COMMENT '条件ID,外键关联task_conditions表',
- `condition_type` varchar(20) NOT NULL DEFAULT 'progress' COMMENT '条件类型(prerequisite=前置条件,progress=进度条件)',
- `target_value` int(11) NOT NULL DEFAULT 1 COMMENT '目标值,如需要完成的次数',
- `params` json DEFAULT NULL COMMENT '条件参数,如特定物品ID、特定作物ID等',
- `operator` varchar(10) NOT NULL DEFAULT '=' COMMENT '运算符,如=, >=, <=',
- `sort_order` int(11) NOT NULL DEFAULT 0 COMMENT '排序顺序,用于多条件任务',
- `is_required` tinyint(1) NOT NULL DEFAULT 1 COMMENT '是否必须满足此条件(0:否, 1:是)',
- `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
- `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
- PRIMARY KEY (`id`),
- KEY `idx_task_id` (`task_id`),
- KEY `idx_condition_id` (`condition_id`),
- KEY `idx_task_condition` (`task_id`, `condition_id`),
- KEY `idx_condition_type` (`condition_type`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='任务达成条件表';
- -- 7. 用户任务关联表
- CREATE TABLE IF NOT EXISTS `task_user_tasks` (
- `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
- `user_id` int(11) NOT NULL COMMENT '用户ID',
- `task_id` int(11) NOT NULL COMMENT '任务ID,外键关联task_tasks表',
- `status` tinyint(1) NOT NULL DEFAULT 0 COMMENT '状态(0:未接取, 1:进行中, 2:已完成, 3:已领取奖励, 4:已失败, 5:已过期)',
- `progress` int(11) NOT NULL DEFAULT 0 COMMENT '当前进度',
- `completed_at` timestamp NULL DEFAULT NULL COMMENT '完成时间',
- `rewarded_at` timestamp NULL DEFAULT NULL COMMENT '奖励发放时间',
- `expire_at` timestamp NULL DEFAULT NULL COMMENT '过期时间',
- `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
- `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
- PRIMARY KEY (`id`),
- UNIQUE KEY `idx_user_task` (`user_id`, `task_id`),
- KEY `idx_user_status` (`user_id`, `status`),
- KEY `idx_task` (`task_id`),
- KEY `idx_expire` (`expire_at`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户任务关联表';
- -- 8. 用户任务进度表
- CREATE TABLE IF NOT EXISTS `task_user_progress` (
- `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
- `user_id` int(11) NOT NULL COMMENT '用户ID',
- `task_id` int(11) NOT NULL COMMENT '任务ID,外键关联task_tasks表',
- `achievement_condition_id` int(11) NOT NULL COMMENT '达成条件ID,外键关联task_achievement_conditions表',
- `current_value` int(11) NOT NULL DEFAULT 0 COMMENT '当前值',
- `last_update_time` timestamp NULL DEFAULT NULL COMMENT '最后更新时间',
- `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
- `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
- PRIMARY KEY (`id`),
- UNIQUE KEY `idx_user_task_condition` (`user_id`, `task_id`, `achievement_condition_id`),
- KEY `idx_user_task` (`user_id`, `task_id`),
- KEY `idx_achievement_condition` (`achievement_condition_id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户任务进度表';
- -- 9. 任务完成日志表
- CREATE TABLE IF NOT EXISTS `task_completion_logs` (
- `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
- `user_id` int(11) NOT NULL COMMENT '用户ID',
- `task_id` int(11) NOT NULL COMMENT '任务ID,外键关联task_tasks表',
- `completed_at` timestamp NOT NULL COMMENT '完成时间',
- `time_spent` int(11) DEFAULT NULL COMMENT '完成耗时(秒)',
- `ip_address` varchar(45) DEFAULT NULL COMMENT 'IP地址',
- `device_info` varchar(255) DEFAULT NULL COMMENT '设备信息',
- `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
- PRIMARY KEY (`id`),
- KEY `idx_user` (`user_id`),
- KEY `idx_task` (`task_id`),
- KEY `idx_completed_at` (`completed_at`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='任务完成日志表';
- -- 10. 任务奖励发放日志表
- CREATE TABLE IF NOT EXISTS `task_reward_logs` (
- `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
- `user_id` int(11) NOT NULL COMMENT '用户ID',
- `task_id` int(11) NOT NULL COMMENT '任务ID,外键关联task_tasks表',
- `user_task_id` int(11) NOT NULL COMMENT '用户任务ID,外键关联task_user_tasks表',
- `rewards` json NOT NULL COMMENT '奖励内容(JSON格式)',
- `rewarded_at` timestamp NOT NULL COMMENT '奖励发放时间',
- `ip_address` varchar(45) DEFAULT NULL COMMENT 'IP地址',
- `device_info` varchar(255) DEFAULT NULL COMMENT '设备信息',
- `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
- PRIMARY KEY (`id`),
- KEY `idx_user` (`user_id`),
- KEY `idx_task` (`task_id`),
- KEY `idx_user_task` (`user_task_id`),
- KEY `idx_rewarded_at` (`rewarded_at`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='任务奖励发放日志表';
- -- 11. 任务消耗日志表
- CREATE TABLE IF NOT EXISTS `task_cost_logs` (
- `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
- `user_id` int(11) NOT NULL COMMENT '用户ID',
- `task_id` int(11) NOT NULL COMMENT '任务ID,外键关联task_tasks表',
- `cost_type` varchar(50) NOT NULL COMMENT '消耗类型(currency, item, energy, ticket等)',
- `cost_param1` varchar(100) NOT NULL COMMENT '消耗参数1(如货币类型、物品类型等)',
- `cost_param2` varchar(100) NOT NULL COMMENT '消耗参数2(如货币ID、物品ID等)',
- `quantity` int(11) NOT NULL DEFAULT 1 COMMENT '消耗数量',
- `cost_at` timestamp NOT NULL COMMENT '消耗时间',
- `ip_address` varchar(45) DEFAULT NULL COMMENT 'IP地址',
- `device_info` varchar(255) DEFAULT NULL COMMENT '设备信息',
- `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
- PRIMARY KEY (`id`),
- KEY `idx_user` (`user_id`),
- KEY `idx_task` (`task_id`),
- KEY `idx_cost_type` (`cost_type`),
- KEY `idx_cost_at` (`cost_at`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='任务消耗日志表';
- -- 12. 任务重置日志表
- CREATE TABLE IF NOT EXISTS `task_reset_logs` (
- `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
- `reset_type` varchar(20) NOT NULL COMMENT '重置类型(daily, weekly, monthly)',
- `reset_time` timestamp NOT NULL COMMENT '重置时间',
- `affected_tasks` json DEFAULT NULL COMMENT '受影响的任务ID列表(JSON格式)',
- `affected_count` int(11) NOT NULL DEFAULT 0 COMMENT '受影响的任务数量',
- `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
- PRIMARY KEY (`id`),
- KEY `idx_reset_type` (`reset_type`),
- KEY `idx_reset_time` (`reset_time`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='任务重置日志表';
- -- 添加外键约束(如果需要)
- -- ALTER TABLE `task_tasks` ADD CONSTRAINT `fk_task_category` FOREIGN KEY (`category_id`) REFERENCES `task_categories` (`id`) ON DELETE CASCADE;
- -- ALTER TABLE `task_rewards` ADD CONSTRAINT `fk_reward_task` FOREIGN KEY (`task_id`) REFERENCES `task_tasks` (`id`) ON DELETE CASCADE;
- -- ALTER TABLE `task_costs` ADD CONSTRAINT `fk_cost_task` FOREIGN KEY (`task_id`) REFERENCES `task_tasks` (`id`) ON DELETE CASCADE;
- -- ALTER TABLE `task_achievement_conditions` ADD CONSTRAINT `fk_achievement_task` FOREIGN KEY (`task_id`) REFERENCES `task_tasks` (`id`) ON DELETE CASCADE;
- -- ALTER TABLE `task_achievement_conditions` ADD CONSTRAINT `fk_achievement_condition` FOREIGN KEY (`condition_id`) REFERENCES `task_conditions` (`id`) ON DELETE CASCADE;
- -- ALTER TABLE `task_user_tasks` ADD CONSTRAINT `fk_user_task` FOREIGN KEY (`task_id`) REFERENCES `task_tasks` (`id`) ON DELETE CASCADE;
- -- ALTER TABLE `task_user_progress` ADD CONSTRAINT `fk_progress_task` FOREIGN KEY (`task_id`) REFERENCES `task_tasks` (`id`) ON DELETE CASCADE;
- -- ALTER TABLE `task_user_progress` ADD CONSTRAINT `fk_progress_condition` FOREIGN KEY (`achievement_condition_id`) REFERENCES `task_achievement_conditions` (`id`) ON DELETE CASCADE;
|