feat(diary): 添加 15 个 SeaORM 实体和数据库迁移 (Phase B1)

实体:
- journal_entry: 日记核心表 (心情/天气/标签/版本)
- journal_element: 日记元素 (文字/图片/贴纸/手写/胶带)
- handwriting_stroke: 手写笔画 (独立大字段表)
- school_class: 班级 (6位码/过期控制)
- class_member: 班级成员 (复合PK)
- topic_assignment: 主题布置
- comment: 老师点评
- sticker_pack + sticker: 贴纸包和贴纸
- template: 日记模板
- achievement + user_achievement: 成就系统
- parent_child_binding: 家长-孩子绑定 (PIPL)
- teacher_profile: 老师档案
- user_settings: 用户设置

迁移 (000170-000184):
- 15 个建表迁移 + 索引 + RLS 策略 + 种子数据
- 所有表含 tenant_id 多租户隔离
- 软删除 + 乐观锁版本号
- 外键级联删除
- 暖记权限注册到基座 permissions 表

验证: cargo check 通过, 425 个测试全通过
This commit is contained in:
iven
2026-05-31 22:29:56 +08:00
parent a99d565e2e
commit 3d9896a676
32 changed files with 2316 additions and 2 deletions

View File

@@ -0,0 +1,50 @@
// 成就定义 — 可解锁的成就徽章
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "achievements")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub tenant_id: Uuid,
/// 成就编码(唯一标识,如 "first_diary", "streak_7"
pub code: String,
/// 成就名称
pub name: String,
/// 成就描述
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
/// 图标 URL 或 emoji
#[serde(skip_serializing_if = "Option::is_none")]
pub icon: Option<String>,
/// 分类writing/social/collection/special
pub category: String,
/// 解锁条件 (JSONB: { type, threshold, ... })
#[serde(skip_serializing_if = "Option::is_none")]
pub condition: Option<serde_json::Value>,
/// 排序权重
pub sort_order: i32,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
pub created_by: Uuid,
pub updated_by: Uuid,
#[serde(skip_serializing_if = "Option::is_none")]
pub deleted_at: Option<DateTimeUtc>,
pub version: i32,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::user_achievement::Entity")]
UserAchievement,
}
impl Related<super::user_achievement::Entity> for Entity {
fn to() -> RelationDef {
Relation::UserAchievement.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -0,0 +1,49 @@
// 班级成员 — 复合主键 (class_id + user_id)
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "class_members")]
pub struct Model {
/// 班级 ID复合主键之一
#[sea_orm(primary_key, auto_increment = false)]
pub class_id: Uuid,
/// 用户 ID复合主键之一
#[sea_orm(primary_key, auto_increment = false)]
pub user_id: Uuid,
pub tenant_id: Uuid,
/// 成员角色student/teacher
pub role: String,
/// 班级内昵称
#[serde(skip_serializing_if = "Option::is_none")]
pub nickname: Option<String>,
/// 加入时间
pub joined_at: DateTimeUtc,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
pub created_by: Uuid,
pub updated_by: Uuid,
#[serde(skip_serializing_if = "Option::is_none")]
pub deleted_at: Option<DateTimeUtc>,
pub version: i32,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::school_class::Entity",
from = "Column::ClassId",
to = "super::school_class::Column::Id",
on_delete = "Cascade"
)]
SchoolClass,
}
impl Related<super::school_class::Entity> for Entity {
fn to() -> RelationDef {
Relation::SchoolClass.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -0,0 +1,44 @@
// 老师点评 — 老师对学生日记的评语
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "comments")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub tenant_id: Uuid,
/// 被点评的日记
pub journal_id: Uuid,
/// 点评者老师ID
pub author_id: Uuid,
/// 评语内容
pub content: String,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
pub created_by: Uuid,
pub updated_by: Uuid,
#[serde(skip_serializing_if = "Option::is_none")]
pub deleted_at: Option<DateTimeUtc>,
pub version: i32,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::journal_entry::Entity",
from = "Column::JournalId",
to = "super::journal_entry::Column::Id",
on_delete = "Cascade"
)]
JournalEntry,
}
impl Related<super::journal_entry::Entity> for Entity {
fn to() -> RelationDef {
Relation::JournalEntry.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -0,0 +1,79 @@
// 手写笔画 — 独立表,大字段隔离,延迟加载
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
/// 画笔类型
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BrushType {
/// 钢笔(压感)
Pen,
/// 铅笔(纹理)
Pencil,
/// 马克笔(半透明)
Marker,
/// 橡皮擦
Eraser,
}
impl std::fmt::Display for BrushType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BrushType::Pen => write!(f, "pen"),
BrushType::Pencil => write!(f, "pencil"),
BrushType::Marker => write!(f, "marker"),
BrushType::Eraser => write!(f, "eraser"),
}
}
}
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "handwriting_strokes")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub tenant_id: Uuid,
/// 所属日记元素
pub element_id: Uuid,
/// 点坐标序列 (JSONB: [{x, y}, ...])
pub points: serde_json::Value,
/// 压力值序列 (JSONB: [0.0-1.0, ...])
#[serde(skip_serializing_if = "Option::is_none")]
pub pressures: Option<serde_json::Value>,
/// 时间戳序列 (JSONB: [ms, ...])
#[serde(skip_serializing_if = "Option::is_none")]
pub timestamps: Option<serde_json::Value>,
/// 笔画颜色 (hex)
pub color: String,
/// 笔画基础宽度
pub stroke_width: f64,
/// 画笔类型
pub brush_type: String,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
pub created_by: Uuid,
pub updated_by: Uuid,
#[serde(skip_serializing_if = "Option::is_none")]
pub deleted_at: Option<DateTimeUtc>,
pub version: i32,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::journal_element::Entity",
from = "Column::ElementId",
to = "super::journal_element::Column::Id",
on_delete = "Cascade"
)]
JournalElement,
}
impl Related<super::journal_element::Entity> for Entity {
fn to() -> RelationDef {
Relation::JournalElement.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -0,0 +1,93 @@
// 日记元素 — 手账页面中的文字/图片/贴纸/手写引用/胶带等元素
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
/// 元素类型枚举
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ElementType {
/// 文本框
Text,
/// 图片
Image,
/// 贴纸
Sticker,
/// 手写引用(指向 handwriting_stroke
HandwritingRef,
/// 和纸胶带
Tape,
}
impl std::fmt::Display for ElementType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ElementType::Text => write!(f, "text"),
ElementType::Image => write!(f, "image"),
ElementType::Sticker => write!(f, "sticker"),
ElementType::HandwritingRef => write!(f, "handwriting_ref"),
ElementType::Tape => write!(f, "tape"),
}
}
}
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "journal_elements")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub tenant_id: Uuid,
/// 所属日记
pub journal_id: Uuid,
/// 元素类型
pub element_type: String,
/// X 坐标位置
pub position_x: f64,
/// Y 坐标位置
pub position_y: f64,
/// 宽度
pub width: f64,
/// 高度
pub height: f64,
/// 旋转角度(度)
pub rotation: f64,
/// 层叠顺序
pub z_index: i32,
/// 元素内容JSON: 文本内容/图片URL/贴纸ID等
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<serde_json::Value>,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
pub created_by: Uuid,
pub updated_by: Uuid,
#[serde(skip_serializing_if = "Option::is_none")]
pub deleted_at: Option<DateTimeUtc>,
pub version: i32,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::journal_entry::Entity",
from = "Column::JournalId",
to = "super::journal_entry::Column::Id",
on_delete = "Cascade"
)]
JournalEntry,
#[sea_orm(has_many = "super::handwriting_stroke::Entity")]
HandwritingStroke,
}
impl Related<super::journal_entry::Entity> for Entity {
fn to() -> RelationDef {
Relation::JournalEntry.def()
}
}
impl Related<super::handwriting_stroke::Entity> for Entity {
fn to() -> RelationDef {
Relation::HandwritingStroke.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -0,0 +1,70 @@
// 日记条目 — 暖记核心实体
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "journal_entries")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub tenant_id: Uuid,
/// 日记作者
pub author_id: Uuid,
/// 所属班级(可选,独立用户可为空)
#[serde(skip_serializing_if = "Option::is_none")]
pub class_id: Option<Uuid>,
/// 日记标题
pub title: String,
/// 日记日期
pub date: chrono::NaiveDate,
/// 心情
pub mood: String,
/// 天气
pub weather: String,
/// 标签JSON 数组)
#[serde(skip_serializing_if = "Option::is_none")]
pub tags: Option<serde_json::Value>,
/// 是否私密
pub is_private: bool,
/// 是否分享到班级
pub shared_to_class: bool,
/// 关联的主题布置(老师布置的主题)
#[serde(skip_serializing_if = "Option::is_none")]
pub assigned_topic_id: Option<Uuid>,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
pub created_by: Uuid,
pub updated_by: Uuid,
#[serde(skip_serializing_if = "Option::is_none")]
pub deleted_at: Option<DateTimeUtc>,
/// 乐观锁版本号(同步冲突检测核心)
pub version: i32,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::journal_element::Entity")]
JournalElement,
#[sea_orm(
belongs_to = "super::school_class::Entity",
from = "Column::ClassId",
to = "super::school_class::Column::Id",
on_delete = "Cascade"
)]
SchoolClass,
}
impl Related<super::journal_element::Entity> for Entity {
fn to() -> RelationDef {
Relation::JournalElement.def()
}
}
impl Related<super::school_class::Entity> for Entity {
fn to() -> RelationDef {
Relation::SchoolClass.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -1,2 +1,17 @@
// erp-diary SeaORM 实体占位
// 后续 Phase B1 会定义完整的 ~15 个实体
// erp-diary SeaORM 实体定义
pub mod journal_entry;
pub mod journal_element;
pub mod handwriting_stroke;
pub mod school_class;
pub mod class_member;
pub mod topic_assignment;
pub mod comment;
pub mod sticker_pack;
pub mod sticker;
pub mod template;
pub mod achievement;
pub mod user_achievement;
pub mod parent_child_binding;
pub mod teacher_profile;
pub mod user_settings;

View File

@@ -0,0 +1,35 @@
// 家长-孩子绑定 — PIPL 合规:未满 14 岁必须家长授权
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "parent_child_bindings")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub tenant_id: Uuid,
/// 家长用户 ID
pub parent_id: Uuid,
/// 孩子用户 ID
pub child_id: Uuid,
/// 验证方式qr/sms/manual
pub verification_method: String,
/// 验证通过时间
#[serde(skip_serializing_if = "Option::is_none")]
pub verified_at: Option<DateTimeUtc>,
/// 绑定状态pending/verified/revoked
pub status: String,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
pub created_by: Uuid,
pub updated_by: Uuid,
#[serde(skip_serializing_if = "Option::is_none")]
pub deleted_at: Option<DateTimeUtc>,
pub version: i32,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -0,0 +1,65 @@
// 班级 — 老师创建,学生通过班级码加入
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "school_classes")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub tenant_id: Uuid,
/// 班级名称
pub name: String,
/// 学校名称
#[serde(skip_serializing_if = "Option::is_none")]
pub school_name: Option<String>,
/// 创建者老师ID
pub teacher_id: Uuid,
/// 6 位班级码字母数字混合62^6 ≈ 568 亿种组合)
pub class_code: String,
/// 成员数量(缓存字段)
pub member_count: i32,
/// 是否激活
pub is_active: bool,
/// 班级码过期时间(学期结束自动失效)
#[serde(skip_serializing_if = "Option::is_none")]
pub expires_at: Option<DateTimeUtc>,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
pub created_by: Uuid,
pub updated_by: Uuid,
#[serde(skip_serializing_if = "Option::is_none")]
pub deleted_at: Option<DateTimeUtc>,
pub version: i32,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::class_member::Entity")]
ClassMember,
#[sea_orm(has_many = "super::topic_assignment::Entity")]
TopicAssignment,
#[sea_orm(has_many = "super::journal_entry::Entity")]
JournalEntry,
}
impl Related<super::class_member::Entity> for Entity {
fn to() -> RelationDef {
Relation::ClassMember.def()
}
}
impl Related<super::topic_assignment::Entity> for Entity {
fn to() -> RelationDef {
Relation::TopicAssignment.def()
}
}
impl Related<super::journal_entry::Entity> for Entity {
fn to() -> RelationDef {
Relation::JournalEntry.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -0,0 +1,50 @@
// 贴纸 — 单个贴纸素材
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "stickers")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub tenant_id: Uuid,
/// 所属贴纸包
pub pack_id: Uuid,
/// 贴纸名称
pub name: String,
/// 图片 URL
pub image_url: String,
/// 分类
#[serde(skip_serializing_if = "Option::is_none")]
pub category: Option<String>,
/// 标签 (JSON 数组)
#[serde(skip_serializing_if = "Option::is_none")]
pub tags: Option<serde_json::Value>,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
pub created_by: Uuid,
pub updated_by: Uuid,
#[serde(skip_serializing_if = "Option::is_none")]
pub deleted_at: Option<DateTimeUtc>,
pub version: i32,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::sticker_pack::Entity",
from = "Column::PackId",
to = "super::sticker_pack::Column::Id",
on_delete = "Cascade"
)]
StickerPack,
}
impl Related<super::sticker_pack::Entity> for Entity {
fn to() -> RelationDef {
Relation::StickerPack.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -0,0 +1,48 @@
// 贴纸包 — 一组贴纸的集合
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "sticker_packs")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub tenant_id: Uuid,
/// 贴纸包名称
pub name: String,
/// 描述
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
/// 缩略图 URL
#[serde(skip_serializing_if = "Option::is_none")]
pub thumbnail_url: Option<String>,
/// 是否免费
pub is_free: bool,
/// 价格积分0 = 免费)
pub price: i32,
/// 分类
#[serde(skip_serializing_if = "Option::is_none")]
pub category: Option<String>,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
pub created_by: Uuid,
pub updated_by: Uuid,
#[serde(skip_serializing_if = "Option::is_none")]
pub deleted_at: Option<DateTimeUtc>,
pub version: i32,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::sticker::Entity")]
Sticker,
}
impl Related<super::sticker::Entity> for Entity {
fn to() -> RelationDef {
Relation::Sticker.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -0,0 +1,35 @@
// 老师档案 — 老师的扩展信息
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "teacher_profiles")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub tenant_id: Uuid,
/// 关联的用户 ID
pub user_id: Uuid,
/// 学校名称
#[serde(skip_serializing_if = "Option::is_none")]
pub school_name: Option<String>,
/// 任教科目 (JSON 数组: ["语文", "数学"])
#[serde(skip_serializing_if = "Option::is_none")]
pub subjects: Option<serde_json::Value>,
/// 个人简介
#[serde(skip_serializing_if = "Option::is_none")]
pub bio: Option<String>,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
pub created_by: Uuid,
pub updated_by: Uuid,
#[serde(skip_serializing_if = "Option::is_none")]
pub deleted_at: Option<DateTimeUtc>,
pub version: i32,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -0,0 +1,37 @@
// 模板 — 日记模板(布局+预设元素)
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "templates")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub tenant_id: Uuid,
/// 模板名称
pub name: String,
/// 缩略图 URL
#[serde(skip_serializing_if = "Option::is_none")]
pub thumbnail_url: Option<String>,
/// 布局数据 (JSONB: 元素定义数组)
#[serde(skip_serializing_if = "Option::is_none")]
pub layout_data: Option<serde_json::Value>,
/// 分类
#[serde(skip_serializing_if = "Option::is_none")]
pub category: Option<String>,
/// 是否官方模板
pub is_official: bool,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
pub created_by: Uuid,
pub updated_by: Uuid,
#[serde(skip_serializing_if = "Option::is_none")]
pub deleted_at: Option<DateTimeUtc>,
pub version: i32,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -0,0 +1,52 @@
// 主题布置 — 老师发布日记主题,学生提交对应日记
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "topic_assignments")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub tenant_id: Uuid,
/// 所属班级
pub class_id: Uuid,
/// 布置主题的老师
pub teacher_id: Uuid,
/// 主题标题
pub title: String,
/// 主题描述/要求
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
/// 截止日期
#[serde(skip_serializing_if = "Option::is_none")]
pub due_date: Option<chrono::NaiveDate>,
/// 是否激活
pub is_active: bool,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
pub created_by: Uuid,
pub updated_by: Uuid,
#[serde(skip_serializing_if = "Option::is_none")]
pub deleted_at: Option<DateTimeUtc>,
pub version: i32,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::school_class::Entity",
from = "Column::ClassId",
to = "super::school_class::Column::Id",
on_delete = "Cascade"
)]
SchoolClass,
}
impl Related<super::school_class::Entity> for Entity {
fn to() -> RelationDef {
Relation::SchoolClass.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -0,0 +1,44 @@
// 用户成就 — 复合主键 (user_id + achievement_id)
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "user_achievements")]
pub struct Model {
/// 用户 ID复合主键之一
#[sea_orm(primary_key, auto_increment = false)]
pub user_id: Uuid,
/// 成就 ID复合主键之一
#[sea_orm(primary_key, auto_increment = false)]
pub achievement_id: Uuid,
pub tenant_id: Uuid,
/// 解锁时间
pub unlocked_at: DateTimeUtc,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
pub created_by: Uuid,
pub updated_by: Uuid,
#[serde(skip_serializing_if = "Option::is_none")]
pub deleted_at: Option<DateTimeUtc>,
pub version: i32,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::achievement::Entity",
from = "Column::AchievementId",
to = "super::achievement::Column::Id",
on_delete = "Cascade"
)]
Achievement,
}
impl Related<super::achievement::Entity> for Entity {
fn to() -> RelationDef {
Relation::Achievement.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -0,0 +1,28 @@
// 用户设置 — 个性化配置
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "user_settings")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub tenant_id: Uuid,
/// 关联的用户 ID
pub user_id: Uuid,
/// 设置数据 (JSONB: { theme, fontSize, defaultBrush, ... })
pub settings: serde_json::Value,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
pub created_by: Uuid,
pub updated_by: Uuid,
#[serde(skip_serializing_if = "Option::is_none")]
pub deleted_at: Option<DateTimeUtc>,
pub version: i32,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -55,6 +55,21 @@ mod m20260504_000106_create_api_clients;
mod m20260513_000144_enforce_version_optimistic_lock;
mod m20260518_000149_fix_admin_permissions;
mod m20260529_000169_supplement_rls_for_new_tables;
mod m20260531_000170_create_journal_entries;
mod m20260531_000171_create_journal_elements;
mod m20260531_000172_create_handwriting_strokes;
mod m20260531_000173_create_school_classes;
mod m20260531_000174_create_class_members;
mod m20260531_000175_create_topic_assignments;
mod m20260531_000176_create_comments;
mod m20260531_000177_create_sticker_packs;
mod m20260531_000178_create_templates;
mod m20260531_000179_create_achievements;
mod m20260531_000180_create_parent_child_bindings;
mod m20260531_000181_create_teacher_profiles;
mod m20260531_000182_create_user_settings;
mod m20260531_000183_diary_indexes_and_fts;
mod m20260531_000184_diary_seed_data;
pub struct Migrator;
@@ -115,6 +130,22 @@ impl MigratorTrait for Migrator {
Box::new(m20260513_000144_enforce_version_optimistic_lock::Migration),
Box::new(m20260518_000149_fix_admin_permissions::Migration),
Box::new(m20260529_000169_supplement_rls_for_new_tables::Migration),
// --- 暖记 (Warm Notes) 迁移 ---
Box::new(m20260531_000170_create_journal_entries::Migration),
Box::new(m20260531_000171_create_journal_elements::Migration),
Box::new(m20260531_000172_create_handwriting_strokes::Migration),
Box::new(m20260531_000173_create_school_classes::Migration),
Box::new(m20260531_000174_create_class_members::Migration),
Box::new(m20260531_000175_create_topic_assignments::Migration),
Box::new(m20260531_000176_create_comments::Migration),
Box::new(m20260531_000177_create_sticker_packs::Migration),
Box::new(m20260531_000178_create_templates::Migration),
Box::new(m20260531_000179_create_achievements::Migration),
Box::new(m20260531_000180_create_parent_child_bindings::Migration),
Box::new(m20260531_000181_create_teacher_profiles::Migration),
Box::new(m20260531_000182_create_user_settings::Migration),
Box::new(m20260531_000183_diary_indexes_and_fts::Migration),
Box::new(m20260531_000184_diary_seed_data::Migration),
]
}
}

View File

@@ -0,0 +1,114 @@
// 日记条目表
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(JournalEntries::Table)
.if_not_exists()
.col(ColumnDef::new(JournalEntries::Id).uuid().not_null().primary_key())
.col(ColumnDef::new(JournalEntries::TenantId).uuid().not_null())
.col(ColumnDef::new(JournalEntries::AuthorId).uuid().not_null())
.col(ColumnDef::new(JournalEntries::ClassId).uuid().null())
.col(ColumnDef::new(JournalEntries::Title).string().not_null())
.col(ColumnDef::new(JournalEntries::Date).date().not_null())
.col(ColumnDef::new(JournalEntries::Mood).string().not_null().default("calm"))
.col(ColumnDef::new(JournalEntries::Weather).string().not_null().default("sunny"))
.col(ColumnDef::new(JournalEntries::Tags).json_binary().null())
.col(ColumnDef::new(JournalEntries::IsPrivate).boolean().not_null().default(true))
.col(ColumnDef::new(JournalEntries::SharedToClass).boolean().not_null().default(false))
.col(ColumnDef::new(JournalEntries::AssignedTopicId).uuid().null())
.col(
ColumnDef::new(JournalEntries::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(JournalEntries::UpdatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(ColumnDef::new(JournalEntries::CreatedBy).uuid().not_null())
.col(ColumnDef::new(JournalEntries::UpdatedBy).uuid().not_null())
.col(ColumnDef::new(JournalEntries::DeletedAt).timestamp_with_time_zone().null())
.col(ColumnDef::new(JournalEntries::Version).integer().not_null().default(1))
.to_owned(),
)
.await?;
// 租户 + 作者索引
manager
.create_index(
Index::create()
.name("idx_journal_entries_tenant_author")
.table(JournalEntries::Table)
.col(JournalEntries::TenantId)
.col(JournalEntries::AuthorId)
.to_owned(),
)
.await?;
// 日期索引(日历查询)
manager
.create_index(
Index::create()
.name("idx_journal_entries_tenant_date")
.table(JournalEntries::Table)
.col(JournalEntries::TenantId)
.col(JournalEntries::Date)
.to_owned(),
)
.await?;
// 班级索引(班级日记墙)
manager
.create_index(
Index::create()
.name("idx_journal_entries_class")
.table(JournalEntries::Table)
.col(JournalEntries::ClassId)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(JournalEntries::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
pub enum JournalEntries {
Table,
Id,
TenantId,
AuthorId,
ClassId,
Title,
Date,
Mood,
Weather,
Tags,
IsPrivate,
SharedToClass,
AssignedTopicId,
CreatedAt,
UpdatedAt,
CreatedBy,
UpdatedBy,
DeletedAt,
Version,
}

View File

@@ -0,0 +1,101 @@
// 日记元素表
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(JournalElements::Table)
.if_not_exists()
.col(ColumnDef::new(JournalElements::Id).uuid().not_null().primary_key())
.col(ColumnDef::new(JournalElements::TenantId).uuid().not_null())
.col(ColumnDef::new(JournalElements::JournalId).uuid().not_null())
.col(ColumnDef::new(JournalElements::ElementType).string().not_null())
.col(ColumnDef::new(JournalElements::PositionX).double().not_null().default(0.0))
.col(ColumnDef::new(JournalElements::PositionY).double().not_null().default(0.0))
.col(ColumnDef::new(JournalElements::Width).double().not_null().default(100.0))
.col(ColumnDef::new(JournalElements::Height).double().not_null().default(100.0))
.col(ColumnDef::new(JournalElements::Rotation).double().not_null().default(0.0))
.col(ColumnDef::new(JournalElements::ZIndex).integer().not_null().default(0))
.col(ColumnDef::new(JournalElements::Content).json_binary().null())
.col(
ColumnDef::new(JournalElements::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(JournalElements::UpdatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(ColumnDef::new(JournalElements::CreatedBy).uuid().not_null())
.col(ColumnDef::new(JournalElements::UpdatedBy).uuid().not_null())
.col(ColumnDef::new(JournalElements::DeletedAt).timestamp_with_time_zone().null())
.col(ColumnDef::new(JournalElements::Version).integer().not_null().default(1))
.to_owned(),
)
.await?;
// 日记 + 层级索引(加载日记时按 z_index 排序)
manager
.create_index(
Index::create()
.name("idx_journal_elements_journal_zindex")
.table(JournalElements::Table)
.col(JournalElements::JournalId)
.col(JournalElements::ZIndex)
.to_owned(),
)
.await?;
// 外键约束
manager
.create_foreign_key(
ForeignKey::create()
.name("fk_journal_elements_journal")
.from(JournalElements::Table, JournalElements::JournalId)
.to(super::m20260531_000170_create_journal_entries::JournalEntries::Table, super::m20260531_000170_create_journal_entries::JournalEntries::Id)
.on_delete(ForeignKeyAction::Cascade)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(JournalElements::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
pub enum JournalElements {
Table,
Id,
TenantId,
JournalId,
ElementType,
PositionX,
PositionY,
Width,
Height,
Rotation,
ZIndex,
Content,
CreatedAt,
UpdatedAt,
CreatedBy,
UpdatedBy,
DeletedAt,
Version,
}

View File

@@ -0,0 +1,96 @@
// 手写笔画表 — 独立表,大字段延迟加载
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(HandwritingStrokes::Table)
.if_not_exists()
.col(ColumnDef::new(HandwritingStrokes::Id).uuid().not_null().primary_key())
.col(ColumnDef::new(HandwritingStrokes::TenantId).uuid().not_null())
.col(ColumnDef::new(HandwritingStrokes::ElementId).uuid().not_null())
.col(ColumnDef::new(HandwritingStrokes::Points).json_binary().not_null())
.col(ColumnDef::new(HandwritingStrokes::Pressures).json_binary().null())
.col(ColumnDef::new(HandwritingStrokes::Timestamps).json_binary().null())
.col(ColumnDef::new(HandwritingStrokes::Color).string().not_null().default("#2D2420"))
.col(ColumnDef::new(HandwritingStrokes::StrokeWidth).double().not_null().default(3.0))
.col(ColumnDef::new(HandwritingStrokes::BrushType).string().not_null().default("pen"))
.col(
ColumnDef::new(HandwritingStrokes::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(HandwritingStrokes::UpdatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(ColumnDef::new(HandwritingStrokes::CreatedBy).uuid().not_null())
.col(ColumnDef::new(HandwritingStrokes::UpdatedBy).uuid().not_null())
.col(ColumnDef::new(HandwritingStrokes::DeletedAt).timestamp_with_time_zone().null())
.col(ColumnDef::new(HandwritingStrokes::Version).integer().not_null().default(1))
.to_owned(),
)
.await?;
// 元素索引(加载元素笔画时使用)
manager
.create_index(
Index::create()
.name("idx_handwriting_strokes_element")
.table(HandwritingStrokes::Table)
.col(HandwritingStrokes::ElementId)
.to_owned(),
)
.await?;
// 外键约束
manager
.create_foreign_key(
ForeignKey::create()
.name("fk_handwriting_strokes_element")
.from(HandwritingStrokes::Table, HandwritingStrokes::ElementId)
.to(super::m20260531_000171_create_journal_elements::JournalElements::Table, super::m20260531_000171_create_journal_elements::JournalElements::Id)
.on_delete(ForeignKeyAction::Cascade)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(HandwritingStrokes::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum HandwritingStrokes {
Table,
Id,
TenantId,
ElementId,
Points,
Pressures,
Timestamps,
Color,
StrokeWidth,
BrushType,
CreatedAt,
UpdatedAt,
CreatedBy,
UpdatedBy,
DeletedAt,
Version,
}

View File

@@ -0,0 +1,91 @@
// 班级表
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(SchoolClasses::Table)
.if_not_exists()
.col(ColumnDef::new(SchoolClasses::Id).uuid().not_null().primary_key())
.col(ColumnDef::new(SchoolClasses::TenantId).uuid().not_null())
.col(ColumnDef::new(SchoolClasses::Name).string().not_null())
.col(ColumnDef::new(SchoolClasses::SchoolName).string().null())
.col(ColumnDef::new(SchoolClasses::TeacherId).uuid().not_null())
.col(ColumnDef::new(SchoolClasses::ClassCode).string().not_null())
.col(ColumnDef::new(SchoolClasses::MemberCount).integer().not_null().default(0))
.col(ColumnDef::new(SchoolClasses::IsActive).boolean().not_null().default(true))
.col(ColumnDef::new(SchoolClasses::ExpiresAt).timestamp_with_time_zone().null())
.col(
ColumnDef::new(SchoolClasses::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(SchoolClasses::UpdatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(ColumnDef::new(SchoolClasses::CreatedBy).uuid().not_null())
.col(ColumnDef::new(SchoolClasses::UpdatedBy).uuid().not_null())
.col(ColumnDef::new(SchoolClasses::DeletedAt).timestamp_with_time_zone().null())
.col(ColumnDef::new(SchoolClasses::Version).integer().not_null().default(1))
.to_owned(),
)
.await?;
// 班级码唯一索引(软删除安全)
manager.get_connection().execute(sea_orm::Statement::from_string(
sea_orm::DatabaseBackend::Postgres,
"CREATE UNIQUE INDEX idx_school_classes_code ON school_classes (class_code) WHERE deleted_at IS NULL".to_string(),
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
// 租户 + 老师索引
manager
.create_index(
Index::create()
.name("idx_school_classes_tenant_teacher")
.table(SchoolClasses::Table)
.col(SchoolClasses::TenantId)
.col(SchoolClasses::TeacherId)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(SchoolClasses::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
pub enum SchoolClasses {
Table,
Id,
TenantId,
Name,
SchoolName,
TeacherId,
ClassCode,
MemberCount,
IsActive,
ExpiresAt,
CreatedAt,
UpdatedAt,
CreatedBy,
UpdatedBy,
DeletedAt,
Version,
}

View File

@@ -0,0 +1,100 @@
// 班级成员表 — 复合主键
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(ClassMembers::Table)
.if_not_exists()
.col(ColumnDef::new(ClassMembers::ClassId).uuid().not_null())
.col(ColumnDef::new(ClassMembers::UserId).uuid().not_null())
.primary_key(
Index::create()
.col(ClassMembers::ClassId)
.col(ClassMembers::UserId),
)
.col(ColumnDef::new(ClassMembers::TenantId).uuid().not_null())
.col(ColumnDef::new(ClassMembers::Role).string().not_null().default("student"))
.col(ColumnDef::new(ClassMembers::Nickname).string().null())
.col(
ColumnDef::new(ClassMembers::JoinedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(ClassMembers::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(ClassMembers::UpdatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(ColumnDef::new(ClassMembers::CreatedBy).uuid().not_null())
.col(ColumnDef::new(ClassMembers::UpdatedBy).uuid().not_null())
.col(ColumnDef::new(ClassMembers::DeletedAt).timestamp_with_time_zone().null())
.col(ColumnDef::new(ClassMembers::Version).integer().not_null().default(1))
.to_owned(),
)
.await?;
// 班级成员索引
manager
.create_index(
Index::create()
.name("idx_class_members_class")
.table(ClassMembers::Table)
.col(ClassMembers::ClassId)
.to_owned(),
)
.await?;
// 外键约束
manager
.create_foreign_key(
ForeignKey::create()
.name("fk_class_members_class")
.from(ClassMembers::Table, ClassMembers::ClassId)
.to(super::m20260531_000173_create_school_classes::SchoolClasses::Table, super::m20260531_000173_create_school_classes::SchoolClasses::Id)
.on_delete(ForeignKeyAction::Cascade)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(ClassMembers::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum ClassMembers {
Table,
ClassId,
UserId,
TenantId,
Role,
Nickname,
JoinedAt,
CreatedAt,
UpdatedAt,
CreatedBy,
UpdatedBy,
DeletedAt,
Version,
}

View File

@@ -0,0 +1,95 @@
// 主题布置表
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(TopicAssignments::Table)
.if_not_exists()
.col(ColumnDef::new(TopicAssignments::Id).uuid().not_null().primary_key())
.col(ColumnDef::new(TopicAssignments::TenantId).uuid().not_null())
.col(ColumnDef::new(TopicAssignments::ClassId).uuid().not_null())
.col(ColumnDef::new(TopicAssignments::TeacherId).uuid().not_null())
.col(ColumnDef::new(TopicAssignments::Title).string().not_null())
.col(ColumnDef::new(TopicAssignments::Description).text().null())
.col(ColumnDef::new(TopicAssignments::DueDate).date().null())
.col(ColumnDef::new(TopicAssignments::IsActive).boolean().not_null().default(true))
.col(
ColumnDef::new(TopicAssignments::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(TopicAssignments::UpdatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(ColumnDef::new(TopicAssignments::CreatedBy).uuid().not_null())
.col(ColumnDef::new(TopicAssignments::UpdatedBy).uuid().not_null())
.col(ColumnDef::new(TopicAssignments::DeletedAt).timestamp_with_time_zone().null())
.col(ColumnDef::new(TopicAssignments::Version).integer().not_null().default(1))
.to_owned(),
)
.await?;
// 班级 + 激活状态索引
manager
.create_index(
Index::create()
.name("idx_topic_assignments_class_active")
.table(TopicAssignments::Table)
.col(TopicAssignments::ClassId)
.col(TopicAssignments::IsActive)
.to_owned(),
)
.await?;
// 外键约束
manager
.create_foreign_key(
ForeignKey::create()
.name("fk_topic_assignments_class")
.from(TopicAssignments::Table, TopicAssignments::ClassId)
.to(super::m20260531_000173_create_school_classes::SchoolClasses::Table, super::m20260531_000173_create_school_classes::SchoolClasses::Id)
.on_delete(ForeignKeyAction::Cascade)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(TopicAssignments::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum TopicAssignments {
Table,
Id,
TenantId,
ClassId,
TeacherId,
Title,
Description,
DueDate,
IsActive,
CreatedAt,
UpdatedAt,
CreatedBy,
UpdatedBy,
DeletedAt,
Version,
}

View File

@@ -0,0 +1,88 @@
// 老师点评表
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Comments::Table)
.if_not_exists()
.col(ColumnDef::new(Comments::Id).uuid().not_null().primary_key())
.col(ColumnDef::new(Comments::TenantId).uuid().not_null())
.col(ColumnDef::new(Comments::JournalId).uuid().not_null())
.col(ColumnDef::new(Comments::AuthorId).uuid().not_null())
.col(ColumnDef::new(Comments::Content).text().not_null())
.col(
ColumnDef::new(Comments::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(Comments::UpdatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(ColumnDef::new(Comments::CreatedBy).uuid().not_null())
.col(ColumnDef::new(Comments::UpdatedBy).uuid().not_null())
.col(ColumnDef::new(Comments::DeletedAt).timestamp_with_time_zone().null())
.col(ColumnDef::new(Comments::Version).integer().not_null().default(1))
.to_owned(),
)
.await?;
// 日记索引(加载日记的所有评语)
manager
.create_index(
Index::create()
.name("idx_comments_journal")
.table(Comments::Table)
.col(Comments::JournalId)
.to_owned(),
)
.await?;
// 外键约束
manager
.create_foreign_key(
ForeignKey::create()
.name("fk_comments_journal")
.from(Comments::Table, Comments::JournalId)
.to(super::m20260531_000170_create_journal_entries::JournalEntries::Table, super::m20260531_000170_create_journal_entries::JournalEntries::Id)
.on_delete(ForeignKeyAction::Cascade)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Comments::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum Comments {
Table,
Id,
TenantId,
JournalId,
AuthorId,
Content,
CreatedAt,
UpdatedAt,
CreatedBy,
UpdatedBy,
DeletedAt,
Version,
}

View File

@@ -0,0 +1,145 @@
// 贴纸包 + 贴纸表
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// 贴纸包表
manager
.create_table(
Table::create()
.table(StickerPacks::Table)
.if_not_exists()
.col(ColumnDef::new(StickerPacks::Id).uuid().not_null().primary_key())
.col(ColumnDef::new(StickerPacks::TenantId).uuid().not_null())
.col(ColumnDef::new(StickerPacks::Name).string().not_null())
.col(ColumnDef::new(StickerPacks::Description).text().null())
.col(ColumnDef::new(StickerPacks::ThumbnailUrl).string().null())
.col(ColumnDef::new(StickerPacks::IsFree).boolean().not_null().default(true))
.col(ColumnDef::new(StickerPacks::Price).integer().not_null().default(0))
.col(ColumnDef::new(StickerPacks::Category).string().null())
.col(
ColumnDef::new(StickerPacks::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(StickerPacks::UpdatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(ColumnDef::new(StickerPacks::CreatedBy).uuid().not_null())
.col(ColumnDef::new(StickerPacks::UpdatedBy).uuid().not_null())
.col(ColumnDef::new(StickerPacks::DeletedAt).timestamp_with_time_zone().null())
.col(ColumnDef::new(StickerPacks::Version).integer().not_null().default(1))
.to_owned(),
)
.await?;
// 贴纸表
manager
.create_table(
Table::create()
.table(Stickers::Table)
.if_not_exists()
.col(ColumnDef::new(Stickers::Id).uuid().not_null().primary_key())
.col(ColumnDef::new(Stickers::TenantId).uuid().not_null())
.col(ColumnDef::new(Stickers::PackId).uuid().not_null())
.col(ColumnDef::new(Stickers::Name).string().not_null())
.col(ColumnDef::new(Stickers::ImageUrl).string().not_null())
.col(ColumnDef::new(Stickers::Category).string().null())
.col(ColumnDef::new(Stickers::Tags).json_binary().null())
.col(
ColumnDef::new(Stickers::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(Stickers::UpdatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(ColumnDef::new(Stickers::CreatedBy).uuid().not_null())
.col(ColumnDef::new(Stickers::UpdatedBy).uuid().not_null())
.col(ColumnDef::new(Stickers::DeletedAt).timestamp_with_time_zone().null())
.col(ColumnDef::new(Stickers::Version).integer().not_null().default(1))
.to_owned(),
)
.await?;
// 贴纸包索引
manager
.create_index(
Index::create()
.name("idx_stickers_pack")
.table(Stickers::Table)
.col(Stickers::PackId)
.to_owned(),
)
.await?;
// 外键约束
manager
.create_foreign_key(
ForeignKey::create()
.name("fk_stickers_pack")
.from(Stickers::Table, Stickers::PackId)
.to(StickerPacks::Table, StickerPacks::Id)
.on_delete(ForeignKeyAction::Cascade)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager.drop_table(Table::drop().table(Stickers::Table).to_owned()).await?;
manager.drop_table(Table::drop().table(StickerPacks::Table).to_owned()).await
}
}
#[derive(DeriveIden)]
enum StickerPacks {
Table,
Id,
TenantId,
Name,
Description,
ThumbnailUrl,
IsFree,
Price,
Category,
CreatedAt,
UpdatedAt,
CreatedBy,
UpdatedBy,
DeletedAt,
Version,
}
#[derive(DeriveIden)]
enum Stickers {
Table,
Id,
TenantId,
PackId,
Name,
ImageUrl,
Category,
Tags,
CreatedAt,
UpdatedAt,
CreatedBy,
UpdatedBy,
DeletedAt,
Version,
}

View File

@@ -0,0 +1,80 @@
// 模板表
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Templates::Table)
.if_not_exists()
.col(ColumnDef::new(Templates::Id).uuid().not_null().primary_key())
.col(ColumnDef::new(Templates::TenantId).uuid().not_null())
.col(ColumnDef::new(Templates::Name).string().not_null())
.col(ColumnDef::new(Templates::ThumbnailUrl).string().null())
.col(ColumnDef::new(Templates::LayoutData).json_binary().null())
.col(ColumnDef::new(Templates::Category).string().null())
.col(ColumnDef::new(Templates::IsOfficial).boolean().not_null().default(false))
.col(
ColumnDef::new(Templates::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(Templates::UpdatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(ColumnDef::new(Templates::CreatedBy).uuid().not_null())
.col(ColumnDef::new(Templates::UpdatedBy).uuid().not_null())
.col(ColumnDef::new(Templates::DeletedAt).timestamp_with_time_zone().null())
.col(ColumnDef::new(Templates::Version).integer().not_null().default(1))
.to_owned(),
)
.await?;
// 分类索引
manager
.create_index(
Index::create()
.name("idx_templates_category")
.table(Templates::Table)
.col(Templates::Category)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Templates::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum Templates {
Table,
Id,
TenantId,
Name,
ThumbnailUrl,
LayoutData,
Category,
IsOfficial,
CreatedAt,
UpdatedAt,
CreatedBy,
UpdatedBy,
DeletedAt,
Version,
}

View File

@@ -0,0 +1,157 @@
// 成就定义 + 用户成就表
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// 成就定义表
manager
.create_table(
Table::create()
.table(Achievements::Table)
.if_not_exists()
.col(ColumnDef::new(Achievements::Id).uuid().not_null().primary_key())
.col(ColumnDef::new(Achievements::TenantId).uuid().not_null())
.col(ColumnDef::new(Achievements::Code).string().not_null())
.col(ColumnDef::new(Achievements::Name).string().not_null())
.col(ColumnDef::new(Achievements::Description).text().null())
.col(ColumnDef::new(Achievements::Icon).string().null())
.col(ColumnDef::new(Achievements::Category).string().not_null().default("writing"))
.col(ColumnDef::new(Achievements::Condition).json_binary().null())
.col(ColumnDef::new(Achievements::SortOrder).integer().not_null().default(0))
.col(
ColumnDef::new(Achievements::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(Achievements::UpdatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(ColumnDef::new(Achievements::CreatedBy).uuid().not_null())
.col(ColumnDef::new(Achievements::UpdatedBy).uuid().not_null())
.col(ColumnDef::new(Achievements::DeletedAt).timestamp_with_time_zone().null())
.col(ColumnDef::new(Achievements::Version).integer().not_null().default(1))
.to_owned(),
)
.await?;
// 成就编码唯一索引(软删除安全)
manager.get_connection().execute(sea_orm::Statement::from_string(
sea_orm::DatabaseBackend::Postgres,
"CREATE UNIQUE INDEX idx_achievements_tenant_code ON achievements (tenant_id, code) WHERE deleted_at IS NULL".to_string(),
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
// 用户成就表(复合主键)
manager
.create_table(
Table::create()
.table(UserAchievements::Table)
.if_not_exists()
.col(ColumnDef::new(UserAchievements::UserId).uuid().not_null())
.col(ColumnDef::new(UserAchievements::AchievementId).uuid().not_null())
.primary_key(
Index::create()
.col(UserAchievements::UserId)
.col(UserAchievements::AchievementId),
)
.col(ColumnDef::new(UserAchievements::TenantId).uuid().not_null())
.col(
ColumnDef::new(UserAchievements::UnlockedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(UserAchievements::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(UserAchievements::UpdatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(ColumnDef::new(UserAchievements::CreatedBy).uuid().not_null())
.col(ColumnDef::new(UserAchievements::UpdatedBy).uuid().not_null())
.col(ColumnDef::new(UserAchievements::DeletedAt).timestamp_with_time_zone().null())
.col(ColumnDef::new(UserAchievements::Version).integer().not_null().default(1))
.to_owned(),
)
.await?;
// 用户成就索引
manager
.create_index(
Index::create()
.name("idx_user_achievements_user")
.table(UserAchievements::Table)
.col(UserAchievements::UserId)
.to_owned(),
)
.await?;
// 外键约束
manager
.create_foreign_key(
ForeignKey::create()
.name("fk_user_achievements_achievement")
.from(UserAchievements::Table, UserAchievements::AchievementId)
.to(Achievements::Table, Achievements::Id)
.on_delete(ForeignKeyAction::Cascade)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager.drop_table(Table::drop().table(UserAchievements::Table).to_owned()).await?;
manager.drop_table(Table::drop().table(Achievements::Table).to_owned()).await
}
}
#[derive(DeriveIden)]
enum Achievements {
Table,
Id,
TenantId,
Code,
Name,
Description,
Icon,
Category,
Condition,
SortOrder,
CreatedAt,
UpdatedAt,
CreatedBy,
UpdatedBy,
DeletedAt,
Version,
}
#[derive(DeriveIden)]
enum UserAchievements {
Table,
UserId,
AchievementId,
TenantId,
UnlockedAt,
CreatedAt,
UpdatedAt,
CreatedBy,
UpdatedBy,
DeletedAt,
Version,
}

View File

@@ -0,0 +1,97 @@
// 家长-孩子绑定表
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(ParentChildBindings::Table)
.if_not_exists()
.col(ColumnDef::new(ParentChildBindings::Id).uuid().not_null().primary_key())
.col(ColumnDef::new(ParentChildBindings::TenantId).uuid().not_null())
.col(ColumnDef::new(ParentChildBindings::ParentId).uuid().not_null())
.col(ColumnDef::new(ParentChildBindings::ChildId).uuid().not_null())
.col(ColumnDef::new(ParentChildBindings::VerificationMethod).string().not_null().default("manual"))
.col(ColumnDef::new(ParentChildBindings::VerifiedAt).timestamp_with_time_zone().null())
.col(ColumnDef::new(ParentChildBindings::Status).string().not_null().default("pending"))
.col(
ColumnDef::new(ParentChildBindings::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(ParentChildBindings::UpdatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(ColumnDef::new(ParentChildBindings::CreatedBy).uuid().not_null())
.col(ColumnDef::new(ParentChildBindings::UpdatedBy).uuid().not_null())
.col(ColumnDef::new(ParentChildBindings::DeletedAt).timestamp_with_time_zone().null())
.col(ColumnDef::new(ParentChildBindings::Version).integer().not_null().default(1))
.to_owned(),
)
.await?;
// 家长索引
manager
.create_index(
Index::create()
.name("idx_parent_child_parent")
.table(ParentChildBindings::Table)
.col(ParentChildBindings::ParentId)
.to_owned(),
)
.await?;
// 孩子索引
manager
.create_index(
Index::create()
.name("idx_parent_child_child")
.table(ParentChildBindings::Table)
.col(ParentChildBindings::ChildId)
.to_owned(),
)
.await?;
// 家长-孩子唯一约束(软删除安全)
manager.get_connection().execute(sea_orm::Statement::from_string(
sea_orm::DatabaseBackend::Postgres,
"CREATE UNIQUE INDEX idx_parent_child_unique ON parent_child_bindings (parent_id, child_id) WHERE deleted_at IS NULL".to_string(),
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(ParentChildBindings::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum ParentChildBindings {
Table,
Id,
TenantId,
ParentId,
ChildId,
VerificationMethod,
VerifiedAt,
Status,
CreatedAt,
UpdatedAt,
CreatedBy,
UpdatedBy,
DeletedAt,
Version,
}

View File

@@ -0,0 +1,73 @@
// 老师档案表
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(TeacherProfiles::Table)
.if_not_exists()
.col(ColumnDef::new(TeacherProfiles::Id).uuid().not_null().primary_key())
.col(ColumnDef::new(TeacherProfiles::TenantId).uuid().not_null())
.col(ColumnDef::new(TeacherProfiles::UserId).uuid().not_null())
.col(ColumnDef::new(TeacherProfiles::SchoolName).string().null())
.col(ColumnDef::new(TeacherProfiles::Subjects).json_binary().null())
.col(ColumnDef::new(TeacherProfiles::Bio).text().null())
.col(
ColumnDef::new(TeacherProfiles::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(TeacherProfiles::UpdatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(ColumnDef::new(TeacherProfiles::CreatedBy).uuid().not_null())
.col(ColumnDef::new(TeacherProfiles::UpdatedBy).uuid().not_null())
.col(ColumnDef::new(TeacherProfiles::DeletedAt).timestamp_with_time_zone().null())
.col(ColumnDef::new(TeacherProfiles::Version).integer().not_null().default(1))
.to_owned(),
)
.await?;
// 用户唯一索引(软删除安全,一个用户一个老师档案)
manager.get_connection().execute(sea_orm::Statement::from_string(
sea_orm::DatabaseBackend::Postgres,
"CREATE UNIQUE INDEX idx_teacher_profiles_user ON teacher_profiles (user_id) WHERE deleted_at IS NULL".to_string(),
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(TeacherProfiles::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum TeacherProfiles {
Table,
Id,
TenantId,
UserId,
SchoolName,
Subjects,
Bio,
CreatedAt,
UpdatedAt,
CreatedBy,
UpdatedBy,
DeletedAt,
Version,
}

View File

@@ -0,0 +1,69 @@
// 用户设置表
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(UserSettings::Table)
.if_not_exists()
.col(ColumnDef::new(UserSettings::Id).uuid().not_null().primary_key())
.col(ColumnDef::new(UserSettings::TenantId).uuid().not_null())
.col(ColumnDef::new(UserSettings::UserId).uuid().not_null())
.col(ColumnDef::new(UserSettings::Settings).json_binary().not_null())
.col(
ColumnDef::new(UserSettings::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(UserSettings::UpdatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(ColumnDef::new(UserSettings::CreatedBy).uuid().not_null())
.col(ColumnDef::new(UserSettings::UpdatedBy).uuid().not_null())
.col(ColumnDef::new(UserSettings::DeletedAt).timestamp_with_time_zone().null())
.col(ColumnDef::new(UserSettings::Version).integer().not_null().default(1))
.to_owned(),
)
.await?;
// 用户唯一索引(软删除安全,一个用户一条设置)
manager.get_connection().execute(sea_orm::Statement::from_string(
sea_orm::DatabaseBackend::Postgres,
"CREATE UNIQUE INDEX idx_user_settings_user ON user_settings (user_id) WHERE deleted_at IS NULL".to_string(),
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(UserSettings::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum UserSettings {
Table,
Id,
TenantId,
UserId,
Settings,
CreatedAt,
UpdatedAt,
CreatedBy,
UpdatedBy,
DeletedAt,
Version,
}

View File

@@ -0,0 +1,84 @@
// 暖记补充索引 + RLS 策略
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// 为暖记所有表启用 RLS行级安全
let diary_tables = [
"journal_entries",
"journal_elements",
"handwriting_strokes",
"school_classes",
"class_members",
"topic_assignments",
"comments",
"sticker_packs",
"stickers",
"templates",
"achievements",
"user_achievements",
"parent_child_bindings",
"teacher_profiles",
"user_settings",
];
for table in &diary_tables {
// 启用 RLS
manager.get_connection().execute(sea_orm::Statement::from_string(
sea_orm::DatabaseBackend::Postgres,
format!("ALTER TABLE {table} ENABLE ROW LEVEL SECURITY"),
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
// 创建 RLS 策略tenant_id 隔离
manager.get_connection().execute(sea_orm::Statement::from_string(
sea_orm::DatabaseBackend::Postgres,
format!(
"CREATE POLICY tenant_isolation ON {table} USING (tenant_id = current_setting('app.current_tenant')::uuid)"
),
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
// 允许超级用户绕过 RLS迁移和管理用
manager.get_connection().execute(sea_orm::Statement::from_string(
sea_orm::DatabaseBackend::Postgres,
format!("ALTER TABLE {table} FORCE ROW LEVEL SECURITY"),
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
}
// 日记全文搜索索引(标题 + 标签)
manager.get_connection().execute(sea_orm::Statement::from_string(
sea_orm::DatabaseBackend::Postgres,
"CREATE INDEX idx_journal_entries_title_trgm ON journal_entries USING gin (title gin_trgm_ops)".to_string(),
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
// 心情索引(统计查询)
manager
.create_index(
Index::create()
.name("idx_journal_entries_mood")
.table(JournalEntries::Table)
.col(JournalEntries::Mood)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// 移除全文搜索索引
manager.get_connection().execute(sea_orm::Statement::from_string(
sea_orm::DatabaseBackend::Postgres,
"DROP INDEX IF EXISTS idx_journal_entries_title_trgm".to_string(),
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
// RLS 策略会在表删除时自动移除
Ok(())
}
}
use super::m20260531_000170_create_journal_entries::JournalEntries;

View File

@@ -0,0 +1,99 @@
// 暖记种子数据 — 默认成就 + 基础贴纸包 + 暖记权限
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let conn = manager.get_connection();
// 默认租户和系统用户 UUID内嵌 SQL避免类型转换问题
let tid = "'00000000-0000-0000-0000-000000000000'::uuid";
// 插入默认成就
let achievements = [
("first_diary", "初出茅庐", "写下第一篇日记", "📝", "writing", r#"{"type":"diary_count","threshold":1}"#, 10),
("diary_10", "小有成就", "累计写 10 篇日记", "✏️", "writing", r#"{"type":"diary_count","threshold":10}"#, 20),
("diary_50", "笔下生花", "累计写 50 篇日记", "🌸", "writing", r#"{"type":"diary_count","threshold":50}"#, 30),
("diary_100", "日记达人", "累计写 100 篇日记", "🏆", "writing", r#"{"type":"diary_count","threshold":100}"#, 40),
("streak_3", "三日不间断", "连续 3 天写日记", "🔥", "writing", r#"{"type":"streak","threshold":3}"#, 50),
("streak_7", "一周坚持", "连续 7 天写日记", "", "writing", r#"{"type":"streak","threshold":7}"#, 60),
("streak_30", "月度冠军", "连续 30 天写日记", "👑", "writing", r#"{"type":"streak","threshold":30}"#, 70),
("first_share", "分享快乐", "第一次分享日记到班级", "💝", "social", r#"{"type":"share_count","threshold":1}"#, 80),
("comment_received", "获得鼓励", "第一次收到老师点评", "💌", "social", r#"{"type":"comment_received","threshold":1}"#, 90),
("all_moods", "情绪彩虹", "使用过所有 5 种心情", "🌈", "collection", r#"{"type":"mood_variety","threshold":5}"#, 100),
];
for (code, name, desc, icon, category, condition, sort) in &achievements {
let sql = format!(
r#"INSERT INTO achievements (id, tenant_id, code, name, description, icon, category, condition, sort_order, created_at, updated_at, created_by, updated_by, version)
VALUES (gen_random_uuid(), {tid}, '{code}', '{name}', '{desc}', '{icon}', '{category}', '{condition}'::jsonb, {sort}, now(), now(), {tid}, {tid}, 1)"#,
);
conn.execute(sea_orm::Statement::from_string(
sea_orm::DatabaseBackend::Postgres,
sql,
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
}
// 插入基础贴纸包
conn.execute(sea_orm::Statement::from_string(
sea_orm::DatabaseBackend::Postgres,
format!(
r#"INSERT INTO sticker_packs (id, tenant_id, name, description, is_free, price, category, created_at, updated_at, created_by, updated_by, version)
VALUES (gen_random_uuid(), {tid}, '基础贴纸', '暖记默认贴纸包,包含常用表情和装饰', true, 0, 'basic', now(), now(), {tid}, {tid}, 1)"#,
),
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
// 插入暖记权限到 permissions 表 (resource + action 模式)
let diary_permissions = [
("diary.journal.create", "创建日记", "journal", "create", "允许创建日记条目"),
("diary.journal.read", "查看日记", "journal", "read", "允许查看日记条目"),
("diary.journal.update", "编辑日记", "journal", "update", "允许编辑日记条目"),
("diary.journal.delete", "删除日记", "journal", "delete", "允许删除日记条目"),
("diary.class.manage", "管理班级", "class", "manage", "允许创建和管理班级"),
("diary.topic.assign", "布置主题", "topic", "assign", "允许老师布置日记主题"),
("diary.comment.write", "写评语", "comment", "write", "允许老师点评日记"),
("diary.parent.bind", "家长绑定", "parent", "bind", "允许家长绑定孩子账号"),
];
for (code, name, resource, action, desc) in &diary_permissions {
let sql = format!(
r#"INSERT INTO permissions (id, tenant_id, code, name, resource, action, description, created_at, updated_at, created_by, updated_by, version)
VALUES (gen_random_uuid(), {tid}, '{code}', '{name}', '{resource}', '{action}', '{desc}', now(), now(), {tid}, {tid}, 1)
ON CONFLICT (tenant_id, code) WHERE deleted_at IS NULL DO NOTHING"#,
);
conn.execute(sea_orm::Statement::from_string(
sea_orm::DatabaseBackend::Postgres,
sql,
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
}
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let conn = manager.get_connection();
let tid_str = "WHERE tenant_id = '00000000-0000-0000-0000-000000000000'";
conn.execute(sea_orm::Statement::from_string(
sea_orm::DatabaseBackend::Postgres,
format!("DELETE FROM achievements {tid_str}"),
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
conn.execute(sea_orm::Statement::from_string(
sea_orm::DatabaseBackend::Postgres,
format!("DELETE FROM sticker_packs {tid_str}"),
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
conn.execute(sea_orm::Statement::from_string(
sea_orm::DatabaseBackend::Postgres,
format!("DELETE FROM permissions WHERE code LIKE 'diary.%' AND tenant_id = '00000000-0000-0000-0000-000000000000'"),
)).await.map_err(|e| DbErr::Custom(e.to_string()))?;
Ok(())
}
}