init.sql 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. -- 浠艾福数据库初始化脚本
  2. -- 创建数据库
  3. CREATE DATABASE IF NOT EXISTS xzyj DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
  4. USE xzyj;
  5. -- 家庭表
  6. CREATE TABLE IF NOT EXISTS families (
  7. id BIGINT AUTO_INCREMENT PRIMARY KEY,
  8. name VARCHAR(100),
  9. invite_code VARCHAR(8) UNIQUE,
  10. created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  11. updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  12. INDEX idx_invite_code (invite_code)
  13. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  14. -- 用户表
  15. CREATE TABLE IF NOT EXISTS users (
  16. id BIGINT AUTO_INCREMENT PRIMARY KEY,
  17. openid VARCHAR(64) NOT NULL,
  18. unionid VARCHAR(64),
  19. family_id BIGINT NOT NULL,
  20. role ENUM('parent', 'child') NOT NULL,
  21. nickname VARCHAR(50),
  22. avatar VARCHAR(255),
  23. password VARCHAR(128),
  24. created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  25. updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  26. INDEX idx_openid (openid),
  27. INDEX idx_family_id (family_id)
  28. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  29. -- 孩子详情表
  30. CREATE TABLE IF NOT EXISTS children (
  31. id BIGINT AUTO_INCREMENT PRIMARY KEY,
  32. user_id BIGINT NOT NULL,
  33. family_id BIGINT NOT NULL,
  34. nickname VARCHAR(50) NOT NULL,
  35. age TINYINT NOT NULL,
  36. penalty_enabled TINYINT DEFAULT 1 COMMENT '是否开启扣分:0否1是',
  37. theme VARCHAR(32) DEFAULT 'default',
  38. total_points INT DEFAULT 0,
  39. streak_days INT DEFAULT 0,
  40. last_task_date DATE,
  41. focus_max_daily TINYINT DEFAULT 3,
  42. focus_remaining TINYINT DEFAULT 3,
  43. focus_reset_date DATE DEFAULT (CURRENT_DATE),
  44. created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  45. updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  46. INDEX idx_user_id (user_id),
  47. INDEX idx_family_id (family_id)
  48. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  49. -- 任务表
  50. CREATE TABLE IF NOT EXISTS tasks (
  51. id BIGINT AUTO_INCREMENT PRIMARY KEY,
  52. family_id BIGINT NOT NULL,
  53. creator_id BIGINT NOT NULL,
  54. child_id BIGINT NOT NULL,
  55. title VARCHAR(200) NOT NULL,
  56. description VARCHAR(500),
  57. points TINYINT DEFAULT 2 COMMENT '任务积分1-10',
  58. deadline DATETIME NOT NULL,
  59. repeat_type ENUM('none', 'daily', 'weekly') DEFAULT 'none',
  60. category VARCHAR(32),
  61. need_review TINYINT DEFAULT 0 COMMENT '是否需要审核',
  62. review_by_category TINYINT DEFAULT 0 COMMENT '是否按分类审核',
  63. status ENUM('pending', 'completed', 'overdue', 'cancelled') DEFAULT 'pending',
  64. completed_at DATETIME,
  65. created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  66. updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  67. INDEX idx_child_id_deadline (child_id, deadline),
  68. INDEX idx_status (status),
  69. INDEX idx_category (category)
  70. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  71. -- 积分流水表
  72. CREATE TABLE IF NOT EXISTS points_log (
  73. id BIGINT AUTO_INCREMENT PRIMARY KEY,
  74. child_id BIGINT NOT NULL,
  75. task_id BIGINT,
  76. amount INT NOT NULL COMMENT '积分数量正负',
  77. type ENUM('earn', 'spend', 'bonus', 'penalty', 'adjust', 'reset', 'refund') NOT NULL,
  78. description VARCHAR(255),
  79. created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  80. INDEX idx_child_id_created (child_id, created_at),
  81. INDEX idx_type (type)
  82. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  83. -- 积分清零记录表
  84. CREATE TABLE IF NOT EXISTS points_reset_log (
  85. id BIGINT AUTO_INCREMENT PRIMARY KEY,
  86. child_id BIGINT NOT NULL,
  87. balance_before INT NOT NULL,
  88. balance_after INT DEFAULT 0,
  89. reset_type ENUM('monthly', 'manual') NOT NULL,
  90. created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  91. INDEX idx_child_id (child_id)
  92. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  93. -- 奖励表
  94. CREATE TABLE IF NOT EXISTS rewards (
  95. id BIGINT AUTO_INCREMENT PRIMARY KEY,
  96. family_id BIGINT NOT NULL,
  97. child_id BIGINT NOT NULL,
  98. title VARCHAR(100) NOT NULL,
  99. points_required INT NOT NULL,
  100. category VARCHAR(32),
  101. status ENUM('available', 'pending', 'exchanged', 'rejected') DEFAULT 'available',
  102. exchange_approved TINYINT COMMENT '是否审批通过',
  103. exchanged_at DATETIME,
  104. is_template TINYINT DEFAULT 0 COMMENT '是否为预设模板',
  105. created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  106. updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  107. INDEX idx_child_id_status (child_id, status),
  108. INDEX idx_is_template (is_template)
  109. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  110. -- 奖励分类表
  111. CREATE TABLE IF NOT EXISTS reward_categories (
  112. id BIGINT AUTO_INCREMENT PRIMARY KEY,
  113. family_id BIGINT NOT NULL,
  114. name VARCHAR(32) NOT NULL,
  115. icon VARCHAR(64),
  116. sort_order INT DEFAULT 0,
  117. created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  118. INDEX idx_family_id (family_id)
  119. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  120. -- 预设奖励模板数据
  121. INSERT INTO rewards (family_id, child_id, title, points_required, category, is_template) VALUES
  122. (0, 0, '看动画片30分钟', 30, '精神类', 1),
  123. (0, 0, '玩平板游戏30分钟', 40, '特权类', 1),
  124. (0, 0, '去游乐场', 100, '物质类', 1),
  125. (0, 0, '买玩具', 150, '物质类', 1),
  126. (0, 0, '吃冰淇淋', 20, '物质类', 1),
  127. (0, 0, '选择晚餐', 25, '特权类', 1);
  128. -- 默认奖励分类
  129. INSERT INTO reward_categories (family_id, name, icon, sort_order) VALUES
  130. (0, '物质类', 'gift', 1),
  131. (0, '精神类', 'star', 2),
  132. (0, '特权类', 'crown', 3);
  133. -- 敏感词表
  134. CREATE TABLE IF NOT EXISTS sensitive_words (
  135. id BIGINT AUTO_INCREMENT PRIMARY KEY,
  136. word VARCHAR(50) NOT NULL,
  137. category VARCHAR(32) NOT NULL,
  138. created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  139. INDEX idx_word (word)
  140. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  141. -- 任务审核表
  142. CREATE TABLE IF NOT EXISTS task_reviews (
  143. id BIGINT AUTO_INCREMENT PRIMARY KEY,
  144. task_id BIGINT NOT NULL,
  145. child_id BIGINT NOT NULL,
  146. photo_url VARCHAR(255),
  147. voice_note_url VARCHAR(255),
  148. ai_result ENUM('pass', 'review', 'pending'),
  149. ai_confidence DECIMAL(3,2),
  150. ai_suggestion VARCHAR(255),
  151. ai_threshold ENUM('strict', 'normal', 'loose') DEFAULT 'normal',
  152. parent_result ENUM('pass', 'reject'),
  153. parent_override TINYINT DEFAULT 0,
  154. review_type ENUM('ai', 'manual') NOT NULL,
  155. created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  156. reviewed_at DATETIME,
  157. INDEX idx_task_id (task_id),
  158. INDEX idx_ai_result (ai_result)
  159. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  160. -- 勋章定义表
  161. CREATE TABLE IF NOT EXISTS badge_definitions (
  162. id BIGINT AUTO_INCREMENT PRIMARY KEY,
  163. badge_type VARCHAR(32) NOT NULL,
  164. name VARCHAR(50) NOT NULL,
  165. description VARCHAR(200),
  166. icon VARCHAR(64),
  167. condition_type VARCHAR(32) NOT NULL,
  168. condition_value INT NOT NULL,
  169. reward_points INT DEFAULT 0,
  170. INDEX idx_badge_type (badge_type)
  171. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  172. -- 勋章表
  173. CREATE TABLE IF NOT EXISTS badges (
  174. id BIGINT AUTO_INCREMENT PRIMARY KEY,
  175. child_id BIGINT NOT NULL,
  176. badge_type VARCHAR(32) NOT NULL,
  177. unlocked_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  178. INDEX idx_child_id_type (child_id, badge_type)
  179. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  180. -- 打卡里程碑表
  181. CREATE TABLE IF NOT EXISTS streak_milestones (
  182. id BIGINT AUTO_INCREMENT PRIMARY KEY,
  183. days INT NOT NULL,
  184. reward_points INT DEFAULT 0,
  185. created_at DATETIME DEFAULT CURRENT_TIMESTAMP
  186. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  187. -- 插入打卡里程碑数据
  188. INSERT INTO streak_milestones (days, reward_points) VALUES
  189. (3, 5),
  190. (7, 10),
  191. (30, 30),
  192. (100, 100);
  193. -- 专注训练记录表
  194. CREATE TABLE IF NOT EXISTS focus_sessions (
  195. id BIGINT AUTO_INCREMENT PRIMARY KEY,
  196. child_id BIGINT NOT NULL,
  197. mode ENUM('simple', 'pomodoro') NOT NULL,
  198. duration TINYINT NOT NULL,
  199. stars TINYINT COMMENT '评分1-5',
  200. points_earned INT,
  201. voice_note_url VARCHAR(255),
  202. started_at DATETIME NOT NULL,
  203. completed_at DATETIME,
  204. INDEX idx_child_id_completed (child_id, completed_at)
  205. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  206. -- 专注训练配置表
  207. CREATE TABLE IF NOT EXISTS focus_config (
  208. id BIGINT AUTO_INCREMENT PRIMARY KEY,
  209. family_id BIGINT,
  210. max_daily_count TINYINT DEFAULT 3,
  211. allow_exit TINYINT DEFAULT 1 COMMENT '是否允许退出0否1是',
  212. created_at DATETIME DEFAULT CURRENT_TIMESTAMP
  213. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  214. -- DAN测评表
  215. CREATE TABLE IF NOT EXISTS dan_assessments (
  216. id BIGINT AUTO_INCREMENT PRIMARY KEY,
  217. child_id BIGINT NOT NULL,
  218. assessment_date DATE NOT NULL,
  219. details JSON,
  220. source ENUM('api', 'manual') DEFAULT 'manual',
  221. photo_url VARCHAR(255),
  222. created_at DATETIME DEFAULT CURRENT_TIMESTAMP
  223. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  224. -- 任务模板表
  225. CREATE TABLE IF NOT EXISTS task_templates (
  226. id BIGINT AUTO_INCREMENT PRIMARY KEY,
  227. title VARCHAR(200) NOT NULL,
  228. points TINYINT DEFAULT 2,
  229. duration_minutes TINYINT,
  230. category VARCHAR(32),
  231. is_default TINYINT DEFAULT 1,
  232. family_id BIGINT,
  233. created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  234. INDEX idx_is_default (is_default)
  235. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;