|
|
@@ -19,30 +19,35 @@ public class DatabaseInitializer implements CommandLineRunner {
|
|
|
|
|
|
@Override
|
|
|
public void run(String... args) throws Exception {
|
|
|
- log.info("开始初始化数据�?..");
|
|
|
+ log.info("开始初始化数据库...");
|
|
|
|
|
|
- // 1. 检查并创建数据�? initializeDatabase();
|
|
|
+ // 1. 检查并创建数据库
|
|
|
+ initializeDatabase();
|
|
|
|
|
|
- // 2. 检查并创建�? initializeTables();
|
|
|
+ // 2. 检查并创建表
|
|
|
+ initializeTables();
|
|
|
|
|
|
- // 3. 初始化默认数�? initializeDefaultData();
|
|
|
+ // 3. 初始化默认数据
|
|
|
+ initializeDefaultData();
|
|
|
|
|
|
- log.info("数据库初始化完成�?);
|
|
|
+ log.info("数据库初始化完成!");
|
|
|
}
|
|
|
|
|
|
private void initializeDatabase() {
|
|
|
try {
|
|
|
// 尝试连接数据库,如果不存在则创建
|
|
|
- jdbcTemplate.execute("CREATE DATABASE IF NOT EXISTS zxyj DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
|
|
|
- log.info("数据库检查完�?);
|
|
|
+ jdbcTemplate.execute("CREATE DATABASE IF NOT EXISTS xzyj DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
|
|
|
+ log.info("数据库检查完成");
|
|
|
} catch (Exception e) {
|
|
|
- log.warn("创建数据库失�? {}", e.getMessage());
|
|
|
+ log.warn("创建数据库失败: {}", e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void initializeTables() {
|
|
|
- // 表创建SQL - 使用IF NOT EXISTS确保幂等�? List<String> createTableSQLs = Arrays.asList(
|
|
|
- // 家庭�? "CREATE TABLE IF NOT EXISTS families (" +
|
|
|
+ // 表创建SQL - 使用IF NOT EXISTS确保幂等性
|
|
|
+ List<String> createTableSQLs = Arrays.asList(
|
|
|
+ // 家庭表
|
|
|
+ "CREATE TABLE IF NOT EXISTS families (" +
|
|
|
"id BIGINT AUTO_INCREMENT PRIMARY KEY, " +
|
|
|
"name VARCHAR(100), " +
|
|
|
"invite_code VARCHAR(8) UNIQUE, " +
|
|
|
@@ -51,7 +56,8 @@ public class DatabaseInitializer implements CommandLineRunner {
|
|
|
"INDEX idx_invite_code (invite_code)" +
|
|
|
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
|
|
|
|
|
|
- // 用户�? "CREATE TABLE IF NOT EXISTS users (" +
|
|
|
+ // 用户表
|
|
|
+ "CREATE TABLE IF NOT EXISTS users (" +
|
|
|
"id BIGINT AUTO_INCREMENT PRIMARY KEY, " +
|
|
|
"openid VARCHAR(64) NOT NULL, " +
|
|
|
"unionid VARCHAR(64), " +
|
|
|
@@ -66,7 +72,8 @@ public class DatabaseInitializer implements CommandLineRunner {
|
|
|
"INDEX idx_family_id (family_id)" +
|
|
|
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
|
|
|
|
|
|
- // 孩子详情�? "CREATE TABLE IF NOT EXISTS children (" +
|
|
|
+ // 孩子详情表
|
|
|
+ "CREATE TABLE IF NOT EXISTS children (" +
|
|
|
"id BIGINT AUTO_INCREMENT PRIMARY KEY, " +
|
|
|
"user_id BIGINT NOT NULL, " +
|
|
|
"family_id BIGINT NOT NULL, " +
|
|
|
@@ -87,7 +94,8 @@ public class DatabaseInitializer implements CommandLineRunner {
|
|
|
"INDEX idx_family_id (family_id)" +
|
|
|
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
|
|
|
|
|
|
- // 任务�? "CREATE TABLE IF NOT EXISTS tasks (" +
|
|
|
+ // 任务表
|
|
|
+ "CREATE TABLE IF NOT EXISTS tasks (" +
|
|
|
"id BIGINT AUTO_INCREMENT PRIMARY KEY, " +
|
|
|
"family_id BIGINT NOT NULL, " +
|
|
|
"creator_id BIGINT NOT NULL, " +
|
|
|
@@ -109,7 +117,8 @@ public class DatabaseInitializer implements CommandLineRunner {
|
|
|
"INDEX idx_category (category)" +
|
|
|
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
|
|
|
|
|
|
- // 积分流水�? "CREATE TABLE IF NOT EXISTS points_log (" +
|
|
|
+ // 积分流水表
|
|
|
+ "CREATE TABLE IF NOT EXISTS points_log (" +
|
|
|
"id BIGINT AUTO_INCREMENT PRIMARY KEY, " +
|
|
|
"child_id BIGINT NOT NULL, " +
|
|
|
"task_id BIGINT, " +
|
|
|
@@ -121,7 +130,8 @@ public class DatabaseInitializer implements CommandLineRunner {
|
|
|
"INDEX idx_type (type)" +
|
|
|
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
|
|
|
|
|
|
- // 积分清零记录�? "CREATE TABLE IF NOT EXISTS points_reset_log (" +
|
|
|
+ // 积分清零记录表
|
|
|
+ "CREATE TABLE IF NOT EXISTS points_reset_log (" +
|
|
|
"id BIGINT AUTO_INCREMENT PRIMARY KEY, " +
|
|
|
"child_id BIGINT NOT NULL, " +
|
|
|
"balance_before INT NOT NULL, " +
|
|
|
@@ -131,7 +141,8 @@ public class DatabaseInitializer implements CommandLineRunner {
|
|
|
"INDEX idx_child_id (child_id)" +
|
|
|
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
|
|
|
|
|
|
- // 奖励�? "CREATE TABLE IF NOT EXISTS rewards (" +
|
|
|
+ // 奖励表
|
|
|
+ "CREATE TABLE IF NOT EXISTS rewards (" +
|
|
|
"id BIGINT AUTO_INCREMENT PRIMARY KEY, " +
|
|
|
"family_id BIGINT NOT NULL, " +
|
|
|
"child_id BIGINT NOT NULL, " +
|
|
|
@@ -148,7 +159,8 @@ public class DatabaseInitializer implements CommandLineRunner {
|
|
|
"INDEX idx_is_template (is_template)" +
|
|
|
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
|
|
|
|
|
|
- // 奖励分类�? "CREATE TABLE IF NOT EXISTS reward_categories (" +
|
|
|
+ // 奖励分类表
|
|
|
+ "CREATE TABLE IF NOT EXISTS reward_categories (" +
|
|
|
"id BIGINT AUTO_INCREMENT PRIMARY KEY, " +
|
|
|
"family_id BIGINT NOT NULL, " +
|
|
|
"name VARCHAR(32) NOT NULL, " +
|
|
|
@@ -167,7 +179,8 @@ public class DatabaseInitializer implements CommandLineRunner {
|
|
|
"INDEX idx_word (word)" +
|
|
|
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
|
|
|
|
|
|
- // 任务审核�? "CREATE TABLE IF NOT EXISTS task_reviews (" +
|
|
|
+ // 任务审核表
|
|
|
+ "CREATE TABLE IF NOT EXISTS task_reviews (" +
|
|
|
"id BIGINT AUTO_INCREMENT PRIMARY KEY, " +
|
|
|
"task_id BIGINT NOT NULL, " +
|
|
|
"child_id BIGINT NOT NULL, " +
|
|
|
@@ -186,7 +199,8 @@ public class DatabaseInitializer implements CommandLineRunner {
|
|
|
"INDEX idx_ai_result (ai_result)" +
|
|
|
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
|
|
|
|
|
|
- // 勋章定义�? "CREATE TABLE IF NOT EXISTS badge_definitions (" +
|
|
|
+ // 勋章定义表
|
|
|
+ "CREATE TABLE IF NOT EXISTS badge_definitions (" +
|
|
|
"id BIGINT AUTO_INCREMENT PRIMARY KEY, " +
|
|
|
"badge_type VARCHAR(32) NOT NULL, " +
|
|
|
"name VARCHAR(50) NOT NULL, " +
|
|
|
@@ -198,7 +212,8 @@ public class DatabaseInitializer implements CommandLineRunner {
|
|
|
"INDEX idx_badge_type (badge_type)" +
|
|
|
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
|
|
|
|
|
|
- // 勋章�? "CREATE TABLE IF NOT EXISTS badges (" +
|
|
|
+ // 勋章表
|
|
|
+ "CREATE TABLE IF NOT EXISTS badges (" +
|
|
|
"id BIGINT AUTO_INCREMENT PRIMARY KEY, " +
|
|
|
"child_id BIGINT NOT NULL, " +
|
|
|
"badge_type VARCHAR(32) NOT NULL, " +
|
|
|
@@ -214,7 +229,8 @@ public class DatabaseInitializer implements CommandLineRunner {
|
|
|
"created_at DATETIME DEFAULT CURRENT_TIMESTAMP" +
|
|
|
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
|
|
|
|
|
|
- // 专注训练记录�? "CREATE TABLE IF NOT EXISTS focus_sessions (" +
|
|
|
+ // 专注训练记录表
|
|
|
+ "CREATE TABLE IF NOT EXISTS focus_sessions (" +
|
|
|
"id BIGINT AUTO_INCREMENT PRIMARY KEY, " +
|
|
|
"child_id BIGINT NOT NULL, " +
|
|
|
"mode ENUM('simple','pomodoro') NOT NULL, " +
|
|
|
@@ -227,7 +243,8 @@ public class DatabaseInitializer implements CommandLineRunner {
|
|
|
"INDEX idx_child_id_completed (child_id, completed_at)" +
|
|
|
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
|
|
|
|
|
|
- // 专注训练配置�? "CREATE TABLE IF NOT EXISTS focus_config (" +
|
|
|
+ // 专注训练配置表
|
|
|
+ "CREATE TABLE IF NOT EXISTS focus_config (" +
|
|
|
"id BIGINT AUTO_INCREMENT PRIMARY KEY, " +
|
|
|
"family_id BIGINT, " +
|
|
|
"max_daily_count TINYINT DEFAULT 3, " +
|
|
|
@@ -235,7 +252,8 @@ public class DatabaseInitializer implements CommandLineRunner {
|
|
|
"created_at DATETIME DEFAULT CURRENT_TIMESTAMP" +
|
|
|
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
|
|
|
|
|
|
- // DAN测评�? "CREATE TABLE IF NOT EXISTS dan_assessments (" +
|
|
|
+ // DAN测评表
|
|
|
+ "CREATE TABLE IF NOT EXISTS dan_assessments (" +
|
|
|
"id BIGINT AUTO_INCREMENT PRIMARY KEY, " +
|
|
|
"child_id BIGINT NOT NULL, " +
|
|
|
"dan_level VARCHAR(2) NOT NULL, " +
|
|
|
@@ -247,7 +265,8 @@ public class DatabaseInitializer implements CommandLineRunner {
|
|
|
"INDEX idx_child_id_level (child_id, dan_level)" +
|
|
|
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
|
|
|
|
|
|
- // 任务模板�? "CREATE TABLE IF NOT EXISTS task_templates (" +
|
|
|
+ // 任务模板表
|
|
|
+ "CREATE TABLE IF NOT EXISTS task_templates (" +
|
|
|
"id BIGINT AUTO_INCREMENT PRIMARY KEY, " +
|
|
|
"dan_level VARCHAR(2) NOT NULL, " +
|
|
|
"title VARCHAR(200) NOT NULL, " +
|
|
|
@@ -296,46 +315,49 @@ public class DatabaseInitializer implements CommandLineRunner {
|
|
|
try {
|
|
|
jdbcTemplate.execute(sql);
|
|
|
} catch (Exception e) {
|
|
|
- log.warn("创建表失�? {}", e.getMessage());
|
|
|
+ log.warn("创建表失败: {}", e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- log.info("数据表检查完�?);
|
|
|
+ log.info("数据表检查完成");
|
|
|
}
|
|
|
|
|
|
private void initializeDefaultData() {
|
|
|
- // 初始化预设奖励模�? try {
|
|
|
- // 检查是否已有预设奖�? Integer count = jdbcTemplate.queryForObject(
|
|
|
+ // 初始化预设奖励模板
|
|
|
+ try {
|
|
|
+ // 检查是否已有预设奖励
|
|
|
+ Integer count = jdbcTemplate.queryForObject(
|
|
|
"SELECT COUNT(*) FROM rewards WHERE is_template = 1", Integer.class);
|
|
|
|
|
|
if (count == null || count == 0) {
|
|
|
// 插入预设奖励模板
|
|
|
jdbcTemplate.execute("INSERT INTO rewards (family_id, child_id, title, points_required, category, is_template) VALUES " +
|
|
|
- "(0, 0, '看动画片30分钟', 30, '精神�?, 1), " +
|
|
|
- "(0, 0, '玩平板游�?0分钟', 40, '特权�?, 1), " +
|
|
|
- "(0, 0, '去游乐场', 100, '物质�?, 1), " +
|
|
|
- "(0, 0, '买玩�?, 150, '物质�?, 1), " +
|
|
|
- "(0, 0, '吃冰淇淋', 20, '物质�?, 1), " +
|
|
|
- "(0, 0, '选择晚餐', 25, '特权�?, 1)");
|
|
|
+ "(0, 0, '看动画片30分钟', 30, '精神类', 1), " +
|
|
|
+ "(0, 0, '玩平板游戏30分钟', 40, '特权类', 1), " +
|
|
|
+ "(0, 0, '去游乐场', 100, '物质类', 1), " +
|
|
|
+ "(0, 0, '买玩具', 150, '物质类', 1), " +
|
|
|
+ "(0, 0, '吃冰淇淋', 20, '物质类', 1), " +
|
|
|
+ "(0, 0, '选择晚餐', 25, '特权类', 1)");
|
|
|
log.info("预设奖励模板已初始化");
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- log.warn("初始化预设奖励失�? {}", e.getMessage());
|
|
|
+ log.warn("初始化预设奖励失败: {}", e.getMessage());
|
|
|
}
|
|
|
|
|
|
- // 初始化奖励分�? try {
|
|
|
+ // 初始化奖励分类
|
|
|
+ try {
|
|
|
Integer count = jdbcTemplate.queryForObject(
|
|
|
"SELECT COUNT(*) FROM reward_categories WHERE family_id = 0", Integer.class);
|
|
|
|
|
|
if (count == null || count == 0) {
|
|
|
jdbcTemplate.execute("INSERT INTO reward_categories (family_id, name, icon, sort_order) VALUES " +
|
|
|
- "(0, '物质�?, 'gift', 1), " +
|
|
|
- "(0, '精神�?, 'star', 2), " +
|
|
|
- "(0, '特权�?, 'crown', 3)");
|
|
|
+ "(0, '物质类', 'gift', 1), " +
|
|
|
+ "(0, '精神类', 'star', 2), " +
|
|
|
+ "(0, '特权类', 'crown', 3)");
|
|
|
log.info("奖励分类已初始化");
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- log.warn("初始化奖励分类失�? {}", e.getMessage());
|
|
|
+ log.warn("初始化奖励分类失败: {}", e.getMessage());
|
|
|
}
|
|
|
|
|
|
// 初始化打卡里程碑
|
|
|
@@ -346,27 +368,28 @@ public class DatabaseInitializer implements CommandLineRunner {
|
|
|
if (count == null || count == 0) {
|
|
|
jdbcTemplate.execute("INSERT INTO streak_milestones (days, reward_points) VALUES " +
|
|
|
"(3, 5), (7, 10), (30, 30), (100, 100)");
|
|
|
- log.info("打卡里程碑已初始�?);
|
|
|
+ log.info("打卡里程碑已初始化");
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
log.warn("初始化打卡里程碑失败: {}", e.getMessage());
|
|
|
}
|
|
|
|
|
|
- // 初始化勋章定�? try {
|
|
|
+ // 初始化勋章定义
|
|
|
+ try {
|
|
|
Integer count = jdbcTemplate.queryForObject(
|
|
|
"SELECT COUNT(*) FROM badge_definitions", Integer.class);
|
|
|
|
|
|
if (count == null || count == 0) {
|
|
|
jdbcTemplate.execute("INSERT INTO badge_definitions (badge_type, name, description, icon, condition_type, condition_value, reward_points) VALUES " +
|
|
|
"('first_task', '初次完成任务', '首次完成任意任务', 'star', 'task_count', 1, 0), " +
|
|
|
- "('streak_3', '连续3�?, '连续3天按时完成任�?, 'fire', 'streak_days', 3, 5), " +
|
|
|
- "('streak_7', '连续7�?, '连续7天按时完成任�?, 'fire', 'streak_days', 7, 10), " +
|
|
|
- "('points_50', '积分达人', '一周获�?0积分', 'trophy', 'weekly_points', 50, 0), " +
|
|
|
- "('task_100', '任务达人', '完成100个任�?, 'medal', 'task_count', 100, 20)");
|
|
|
+ "('streak_3', '连续3天', '连续3天按时完成任务', 'fire', 'streak_days', 3, 5), " +
|
|
|
+ "('streak_7', '连续7天', '连续7天按时完成任务', 'fire', 'streak_days', 7, 10), " +
|
|
|
+ "('points_50', '积分达人', '一周获得50积分', 'trophy', 'weekly_points', 50, 0), " +
|
|
|
+ "('task_100', '任务达人', '完成100个任务', 'medal', 'task_count', 100, 20)");
|
|
|
log.info("勋章定义已初始化");
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- log.warn("初始化勋章定义失�? {}", e.getMessage());
|
|
|
+ log.warn("初始化勋章定义失败: {}", e.getMessage());
|
|
|
}
|
|
|
|
|
|
// 初始化默认管理员
|
|
|
@@ -375,15 +398,14 @@ public class DatabaseInitializer implements CommandLineRunner {
|
|
|
"SELECT COUNT(*) FROM admins", Integer.class);
|
|
|
|
|
|
if (count == null || count == 0) {
|
|
|
- // 默认管理�? 手机�?13800138000, 密码 admin123
|
|
|
- String encryptedPassword = org.springframework.util.DigestUtils.md5DigestAsHex("zxyj_admin_salt_admin123".getBytes());
|
|
|
+ // 默认管理员: 手机号 13800138000, 密码 admin123
|
|
|
+ String encryptedPassword = org.springframework.util.DigestUtils.md5DigestAsHex("xzyj_admin_salt_admin123".getBytes());
|
|
|
jdbcTemplate.execute("INSERT INTO admins (username, password, real_name, phone, email, status) VALUES " +
|
|
|
- "('admin', '" + encryptedPassword + "', '系统管理�?, '13800138000', 'admin@zxyj.com', 1)");
|
|
|
- log.info("默认管理员已初始�? 手机�?13800138000, 密码 admin123");
|
|
|
+ "('admin', '" + encryptedPassword + "', '系统管理员', '13800138000', 'admin@xzyj.com', 1)");
|
|
|
+ log.info("默认管理员已初始化: 手机号 13800138000, 密码 admin123");
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
log.warn("初始化默认管理员失败: {}", e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|