feat(diary): 数据层 + 班级系统 (Phase F1 + B3)
Flutter 数据层 (Phase F1): - journal_entry.dart: 日记数据模型 (Mood/Weather/tags/version) - journal_element.dart: 元素模型 (text/image/sticker/handwriting_ref/tape) - school_class.dart: 班级模型 - user_settings.dart: 用户设置 (主题/画笔/字号) - isar_database.dart: Isar 初始化 - api_client.dart: Dio + JWT注入 + 离线感知 + 401处理 - journal_repository.dart: 抽象接口 + InMemory实现 (乐观锁) - sync_engine.dart: WiFi同步 + 操作队列 + 重试(5次) + 快照持久化 Rust 班级系统 (Phase B3): - class_service.rs: 创建班级(6位码) + 加入班级 + 成员管理 - topic_service.rs: 老师布置主题 + 主题列表 - comment_service.rs: 老师点评 + 评语列表 - class_handler.rs: 5个API端点 + 权限守卫 - topic_handler.rs: 2个API端点 - comment_handler.rs: 2个API端点 - dto.rs: 新增5个DTO (ClassMemberResp/CreateTopicReq/TopicResp/CreateCommentReq/CommentResp) - 6条新路由注册 验证: cargo check 通过, 433测试全绿, flutter analyze 1 warning
This commit is contained in:
300
crates/erp-diary/src/service/class_service.rs
Normal file
300
crates/erp-diary/src/service/class_service.rs
Normal file
@@ -0,0 +1,300 @@
|
||||
// 班级服务 — 创建班级、加入班级、班级查询
|
||||
|
||||
use chrono::{Months, Utc};
|
||||
use sea_orm::{
|
||||
ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, PaginatorTrait, QueryFilter,
|
||||
Set,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::dto::{ClassMemberResp, ClassResp};
|
||||
use crate::entity::{class_member, school_class};
|
||||
use crate::error::{DiaryError, DiaryResult};
|
||||
use erp_core::events::{DomainEvent, EventBus};
|
||||
|
||||
/// 班级服务 — 6 位码生成、过期控制、成员管理
|
||||
pub struct ClassService;
|
||||
|
||||
impl ClassService {
|
||||
/// 创建班级(老师)
|
||||
///
|
||||
/// 生成 6 位随机班级码,设置过期时间(6 个月后),
|
||||
/// 自动将老师加入 class_members。
|
||||
pub async fn create_class(
|
||||
tenant_id: Uuid,
|
||||
teacher_id: Uuid,
|
||||
name: String,
|
||||
school_name: Option<String>,
|
||||
db: &DatabaseConnection,
|
||||
event_bus: &EventBus,
|
||||
) -> DiaryResult<ClassResp> {
|
||||
let now = Utc::now();
|
||||
let id = Uuid::now_v7();
|
||||
|
||||
// 生成唯一班级码(最多重试 10 次)
|
||||
let class_code = Self::generate_unique_code(db).await?;
|
||||
|
||||
// 过期时间:6 个月后
|
||||
let expires_at = now.checked_add_months(Months::new(6));
|
||||
|
||||
// 创建班级记录
|
||||
let class_model = school_class::ActiveModel {
|
||||
id: Set(id),
|
||||
tenant_id: Set(tenant_id),
|
||||
name: Set(name),
|
||||
school_name: Set(school_name),
|
||||
teacher_id: Set(teacher_id),
|
||||
class_code: Set(class_code.clone()),
|
||||
member_count: Set(1), // 老师自动计入
|
||||
is_active: Set(true),
|
||||
expires_at: Set(expires_at),
|
||||
created_at: Set(now),
|
||||
updated_at: Set(now),
|
||||
created_by: Set(teacher_id),
|
||||
updated_by: Set(teacher_id),
|
||||
deleted_at: Set(None),
|
||||
version: Set(1),
|
||||
};
|
||||
let inserted_class = class_model.insert(db).await?;
|
||||
|
||||
// 自动将老师加入成员表
|
||||
let member_model = class_member::ActiveModel {
|
||||
class_id: Set(id),
|
||||
user_id: Set(teacher_id),
|
||||
tenant_id: Set(tenant_id),
|
||||
role: Set("teacher".to_string()),
|
||||
nickname: Set(None),
|
||||
joined_at: Set(now),
|
||||
created_at: Set(now),
|
||||
updated_at: Set(now),
|
||||
created_by: Set(teacher_id),
|
||||
updated_by: Set(teacher_id),
|
||||
deleted_at: Set(None),
|
||||
version: Set(1),
|
||||
};
|
||||
member_model.insert(db).await?;
|
||||
|
||||
// 发布 ClassCreated 事件
|
||||
event_bus
|
||||
.publish(
|
||||
DomainEvent::new(
|
||||
"diary.class.created",
|
||||
tenant_id,
|
||||
serde_json::json!({
|
||||
"class_id": id,
|
||||
"teacher_id": teacher_id,
|
||||
}),
|
||||
),
|
||||
db,
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(class_model_to_resp(inserted_class))
|
||||
}
|
||||
|
||||
/// 加入班级(学生通过班级码)
|
||||
///
|
||||
/// 验证班级码有效性和过期状态,检查是否已是成员,
|
||||
/// 创建 class_member 记录并更新 member_count。
|
||||
pub async fn join_class(
|
||||
tenant_id: Uuid,
|
||||
user_id: Uuid,
|
||||
class_code: String,
|
||||
nickname: Option<String>,
|
||||
db: &DatabaseConnection,
|
||||
event_bus: &EventBus,
|
||||
) -> DiaryResult<ClassResp> {
|
||||
let now = Utc::now();
|
||||
|
||||
// 1. 查找班级码对应的班级
|
||||
let class_model = school_class::Entity::find()
|
||||
.filter(school_class::Column::ClassCode.eq(&class_code))
|
||||
.filter(school_class::Column::TenantId.eq(tenant_id))
|
||||
.filter(school_class::Column::DeletedAt.is_null())
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or(DiaryError::InvalidClassCode)?;
|
||||
|
||||
// 2. 检查班级是否激活
|
||||
if !class_model.is_active {
|
||||
return Err(DiaryError::BadRequest("班级已停用".to_string()));
|
||||
}
|
||||
|
||||
// 3. 检查是否过期
|
||||
if let Some(expires) = class_model.expires_at {
|
||||
if now > expires {
|
||||
return Err(DiaryError::ClassCodeExpired);
|
||||
}
|
||||
}
|
||||
|
||||
let class_id = class_model.id;
|
||||
|
||||
// 4. 检查是否已是成员
|
||||
let existing = class_member::Entity::find()
|
||||
.filter(class_member::Column::ClassId.eq(class_id))
|
||||
.filter(class_member::Column::UserId.eq(user_id))
|
||||
.filter(class_member::Column::DeletedAt.is_null())
|
||||
.one(db)
|
||||
.await?;
|
||||
|
||||
if existing.is_some() {
|
||||
return Err(DiaryError::BadRequest("已是班级成员".to_string()));
|
||||
}
|
||||
|
||||
// 5. 创建成员记录
|
||||
let member_model = class_member::ActiveModel {
|
||||
class_id: Set(class_id),
|
||||
user_id: Set(user_id),
|
||||
tenant_id: Set(tenant_id),
|
||||
role: Set("student".to_string()),
|
||||
nickname: Set(nickname),
|
||||
joined_at: Set(now),
|
||||
created_at: Set(now),
|
||||
updated_at: Set(now),
|
||||
created_by: Set(user_id),
|
||||
updated_by: Set(user_id),
|
||||
deleted_at: Set(None),
|
||||
version: Set(1),
|
||||
};
|
||||
member_model.insert(db).await?;
|
||||
|
||||
// 6. 更新 member_count
|
||||
let mut active_class: school_class::ActiveModel = class_model.into();
|
||||
let new_count = active_class.member_count.unwrap() + 1;
|
||||
active_class.member_count = Set(new_count);
|
||||
active_class.updated_at = Set(now);
|
||||
active_class.version = Set(active_class.version.unwrap() + 1);
|
||||
let updated_class = active_class.update(db).await?;
|
||||
|
||||
// 7. 发布 StudentJoinedClass 事件
|
||||
event_bus
|
||||
.publish(
|
||||
DomainEvent::new(
|
||||
"diary.class.student_joined",
|
||||
tenant_id,
|
||||
serde_json::json!({
|
||||
"class_id": class_id,
|
||||
"student_id": user_id,
|
||||
}),
|
||||
),
|
||||
db,
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(class_model_to_resp(updated_class))
|
||||
}
|
||||
|
||||
/// 获取班级详情
|
||||
pub async fn get_class(
|
||||
tenant_id: Uuid,
|
||||
class_id: Uuid,
|
||||
db: &DatabaseConnection,
|
||||
) -> DiaryResult<ClassResp> {
|
||||
let model = school_class::Entity::find()
|
||||
.filter(school_class::Column::Id.eq(class_id))
|
||||
.filter(school_class::Column::TenantId.eq(tenant_id))
|
||||
.filter(school_class::Column::DeletedAt.is_null())
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or_else(|| DiaryError::NotFound(format!("班级 {} 不存在", class_id)))?;
|
||||
|
||||
Ok(class_model_to_resp(model))
|
||||
}
|
||||
|
||||
/// 获取班级成员列表
|
||||
pub async fn list_members(
|
||||
tenant_id: Uuid,
|
||||
class_id: Uuid,
|
||||
db: &DatabaseConnection,
|
||||
) -> DiaryResult<Vec<ClassMemberResp>> {
|
||||
let members = class_member::Entity::find()
|
||||
.filter(class_member::Column::ClassId.eq(class_id))
|
||||
.filter(class_member::Column::TenantId.eq(tenant_id))
|
||||
.filter(class_member::Column::DeletedAt.is_null())
|
||||
.all(db)
|
||||
.await?;
|
||||
|
||||
Ok(members.into_iter().map(member_model_to_resp).collect())
|
||||
}
|
||||
|
||||
/// 获取我加入的班级列表
|
||||
pub async fn my_classes(
|
||||
tenant_id: Uuid,
|
||||
user_id: Uuid,
|
||||
db: &DatabaseConnection,
|
||||
) -> DiaryResult<Vec<ClassResp>> {
|
||||
// 先查用户所在的班级 ID
|
||||
let memberships = class_member::Entity::find()
|
||||
.filter(class_member::Column::UserId.eq(user_id))
|
||||
.filter(class_member::Column::TenantId.eq(tenant_id))
|
||||
.filter(class_member::Column::DeletedAt.is_null())
|
||||
.all(db)
|
||||
.await?;
|
||||
|
||||
let class_ids: Vec<Uuid> = memberships.iter().map(|m| m.class_id).collect();
|
||||
|
||||
if class_ids.is_empty() {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
let classes = school_class::Entity::find()
|
||||
.filter(school_class::Column::Id.is_in(class_ids))
|
||||
.filter(school_class::Column::TenantId.eq(tenant_id))
|
||||
.filter(school_class::Column::DeletedAt.is_null())
|
||||
.filter(school_class::Column::IsActive.eq(true))
|
||||
.all(db)
|
||||
.await?;
|
||||
|
||||
Ok(classes.into_iter().map(class_model_to_resp).collect())
|
||||
}
|
||||
|
||||
/// 生成唯一班级码(重试最多 10 次)
|
||||
async fn generate_unique_code(db: &DatabaseConnection) -> DiaryResult<String> {
|
||||
for _ in 0..10 {
|
||||
let code = generate_class_code();
|
||||
let exists = school_class::Entity::find()
|
||||
.filter(school_class::Column::ClassCode.eq(&code))
|
||||
.one(db)
|
||||
.await?
|
||||
.is_some();
|
||||
|
||||
if !exists {
|
||||
return Ok(code);
|
||||
}
|
||||
}
|
||||
Err(DiaryError::Internal("无法生成唯一班级码".to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
/// 生成 6 位班级码(UUID 前 6 位字符)
|
||||
fn generate_class_code() -> String {
|
||||
Uuid::new_v4()
|
||||
.to_string()
|
||||
.replace("-", "")
|
||||
.chars()
|
||||
.take(6)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// school_class::Model -> ClassResp
|
||||
fn class_model_to_resp(model: school_class::Model) -> ClassResp {
|
||||
ClassResp {
|
||||
id: model.id,
|
||||
name: model.name,
|
||||
school_name: model.school_name,
|
||||
teacher_id: model.teacher_id,
|
||||
class_code: model.class_code,
|
||||
member_count: model.member_count,
|
||||
is_active: model.is_active,
|
||||
}
|
||||
}
|
||||
|
||||
/// class_member::Model -> ClassMemberResp
|
||||
fn member_model_to_resp(model: class_member::Model) -> ClassMemberResp {
|
||||
ClassMemberResp {
|
||||
user_id: model.user_id,
|
||||
role: model.role,
|
||||
nickname: model.nickname,
|
||||
joined_at: model.joined_at,
|
||||
}
|
||||
}
|
||||
134
crates/erp-diary/src/service/comment_service.rs
Normal file
134
crates/erp-diary/src/service/comment_service.rs
Normal file
@@ -0,0 +1,134 @@
|
||||
// 评语服务 — 老师点评学生日记
|
||||
|
||||
use chrono::Utc;
|
||||
use sea_orm::{
|
||||
ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter, QueryOrder, Set,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::dto::CommentResp;
|
||||
use crate::entity::{comment, journal_entry};
|
||||
use crate::error::{DiaryError, DiaryResult};
|
||||
use erp_core::events::{DomainEvent, EventBus};
|
||||
|
||||
/// 评语服务 — 老师对学生日记的点评
|
||||
pub struct CommentService;
|
||||
|
||||
impl CommentService {
|
||||
/// 添加评语(老师点评学生日记)
|
||||
///
|
||||
/// 验证日记存在,执行基础内容安全检查,
|
||||
/// 创建评论记录,发布 CommentCreated 事件。
|
||||
pub async fn create_comment(
|
||||
tenant_id: Uuid,
|
||||
author_id: Uuid,
|
||||
journal_id: Uuid,
|
||||
content: String,
|
||||
db: &DatabaseConnection,
|
||||
event_bus: &EventBus,
|
||||
) -> DiaryResult<CommentResp> {
|
||||
// 1. 验证日记存在
|
||||
let journal = journal_entry::Entity::find()
|
||||
.filter(journal_entry::Column::Id.eq(journal_id))
|
||||
.filter(journal_entry::Column::TenantId.eq(tenant_id))
|
||||
.filter(journal_entry::Column::DeletedAt.is_null())
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or_else(|| DiaryError::NotFound(format!("日记 {} 不存在", journal_id)))?;
|
||||
|
||||
// 2. 简单内容安全检查(基础敏感词过滤)
|
||||
if contains_sensitive_words(&content) {
|
||||
return Err(DiaryError::ContentSafetyViolation);
|
||||
}
|
||||
|
||||
let now = Utc::now();
|
||||
let id = Uuid::now_v7();
|
||||
|
||||
// 3. 创建评论记录
|
||||
let model = comment::ActiveModel {
|
||||
id: Set(id),
|
||||
tenant_id: Set(tenant_id),
|
||||
journal_id: Set(journal_id),
|
||||
author_id: Set(author_id),
|
||||
content: Set(content),
|
||||
created_at: Set(now),
|
||||
updated_at: Set(now),
|
||||
created_by: Set(author_id),
|
||||
updated_by: Set(author_id),
|
||||
deleted_at: Set(None),
|
||||
version: Set(1),
|
||||
};
|
||||
let inserted = model.insert(db).await?;
|
||||
|
||||
// 4. 发布 CommentCreated 事件
|
||||
event_bus
|
||||
.publish(
|
||||
DomainEvent::new(
|
||||
"diary.comment.created",
|
||||
tenant_id,
|
||||
serde_json::json!({
|
||||
"comment_id": id,
|
||||
"journal_id": journal_id,
|
||||
"teacher_id": author_id,
|
||||
"student_id": journal.author_id,
|
||||
}),
|
||||
),
|
||||
db,
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(comment_model_to_resp(inserted))
|
||||
}
|
||||
|
||||
/// 获取日记的评语列表
|
||||
///
|
||||
/// 按创建时间正序返回日记下所有未删除的评语。
|
||||
pub async fn list_comments(
|
||||
tenant_id: Uuid,
|
||||
journal_id: Uuid,
|
||||
db: &DatabaseConnection,
|
||||
) -> DiaryResult<Vec<CommentResp>> {
|
||||
let comments = comment::Entity::find()
|
||||
.filter(comment::Column::JournalId.eq(journal_id))
|
||||
.filter(comment::Column::TenantId.eq(tenant_id))
|
||||
.filter(comment::Column::DeletedAt.is_null())
|
||||
.order_by_asc(comment::Column::CreatedAt)
|
||||
.all(db)
|
||||
.await?;
|
||||
|
||||
Ok(comments.into_iter().map(comment_model_to_resp).collect())
|
||||
}
|
||||
}
|
||||
|
||||
/// comment::Model -> CommentResp
|
||||
fn comment_model_to_resp(model: comment::Model) -> CommentResp {
|
||||
CommentResp {
|
||||
id: model.id,
|
||||
journal_id: model.journal_id,
|
||||
author_id: model.author_id,
|
||||
content: model.content,
|
||||
created_at: model.created_at,
|
||||
}
|
||||
}
|
||||
|
||||
/// 基础敏感词检查
|
||||
///
|
||||
/// Phase 1 使用简单字符串匹配,后续迭代替换为完整词库。
|
||||
fn contains_sensitive_words(content: &str) -> bool {
|
||||
const SENSITIVE_WORDS: &[&str] = &[
|
||||
// 占位 — Phase 1 仅检查是否为空或过短
|
||||
// 完整词库将在后续迭代中添加
|
||||
];
|
||||
|
||||
if content.trim().is_empty() {
|
||||
return true;
|
||||
}
|
||||
|
||||
for word in SENSITIVE_WORDS {
|
||||
if content.contains(word) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
@@ -2,3 +2,6 @@
|
||||
|
||||
pub mod journal_service;
|
||||
pub mod sync_service;
|
||||
pub mod class_service;
|
||||
pub mod topic_service;
|
||||
pub mod comment_service;
|
||||
|
||||
116
crates/erp-diary/src/service/topic_service.rs
Normal file
116
crates/erp-diary/src/service/topic_service.rs
Normal file
@@ -0,0 +1,116 @@
|
||||
// 主题布置服务 — 老师发布日记主题
|
||||
|
||||
use chrono::Utc;
|
||||
use sea_orm::{
|
||||
ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter,
|
||||
QueryOrder, Set,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::dto::{CreateTopicReq, TopicResp};
|
||||
use crate::entity::topic_assignment;
|
||||
use crate::error::{DiaryError, DiaryResult};
|
||||
use erp_core::events::{DomainEvent, EventBus};
|
||||
|
||||
/// 主题布置服务 — 老师发布日记主题,学生提交对应日记
|
||||
pub struct TopicService;
|
||||
|
||||
impl TopicService {
|
||||
/// 布置主题(老师)
|
||||
///
|
||||
/// 创建主题布置记录,验证老师是班级成员,
|
||||
/// 发布 TopicAssigned 事件。
|
||||
pub async fn assign_topic(
|
||||
tenant_id: Uuid,
|
||||
teacher_id: Uuid,
|
||||
class_id: Uuid,
|
||||
req: &CreateTopicReq,
|
||||
db: &DatabaseConnection,
|
||||
event_bus: &EventBus,
|
||||
) -> DiaryResult<TopicResp> {
|
||||
// 验证班级存在
|
||||
let class = crate::entity::school_class::Entity::find()
|
||||
.filter(crate::entity::school_class::Column::Id.eq(class_id))
|
||||
.filter(crate::entity::school_class::Column::TenantId.eq(tenant_id))
|
||||
.filter(crate::entity::school_class::Column::DeletedAt.is_null())
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or_else(|| DiaryError::NotFound(format!("班级 {} 不存在", class_id)))?;
|
||||
|
||||
// 验证请求者是班级老师
|
||||
if class.teacher_id != teacher_id {
|
||||
return Err(DiaryError::Forbidden);
|
||||
}
|
||||
|
||||
let now = Utc::now();
|
||||
let id = Uuid::now_v7();
|
||||
|
||||
let model = topic_assignment::ActiveModel {
|
||||
id: Set(id),
|
||||
tenant_id: Set(tenant_id),
|
||||
class_id: Set(class_id),
|
||||
teacher_id: Set(teacher_id),
|
||||
title: Set(req.title.clone()),
|
||||
description: Set(req.description.clone()),
|
||||
due_date: Set(req.due_date),
|
||||
is_active: Set(true),
|
||||
created_at: Set(now),
|
||||
updated_at: Set(now),
|
||||
created_by: Set(teacher_id),
|
||||
updated_by: Set(teacher_id),
|
||||
deleted_at: Set(None),
|
||||
version: Set(1),
|
||||
};
|
||||
let inserted = model.insert(db).await?;
|
||||
|
||||
// 发布 TopicAssigned 事件
|
||||
event_bus
|
||||
.publish(
|
||||
DomainEvent::new(
|
||||
"diary.topic.assigned",
|
||||
tenant_id,
|
||||
serde_json::json!({
|
||||
"topic_id": id,
|
||||
"class_id": class_id,
|
||||
"teacher_id": teacher_id,
|
||||
}),
|
||||
),
|
||||
db,
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(topic_model_to_resp(inserted))
|
||||
}
|
||||
|
||||
/// 获取班级的主题列表
|
||||
///
|
||||
/// 按创建时间倒序返回班级下所有激活的主题。
|
||||
pub async fn list_topics(
|
||||
tenant_id: Uuid,
|
||||
class_id: Uuid,
|
||||
db: &DatabaseConnection,
|
||||
) -> DiaryResult<Vec<TopicResp>> {
|
||||
let topics = topic_assignment::Entity::find()
|
||||
.filter(topic_assignment::Column::ClassId.eq(class_id))
|
||||
.filter(topic_assignment::Column::TenantId.eq(tenant_id))
|
||||
.filter(topic_assignment::Column::DeletedAt.is_null())
|
||||
.order_by_desc(topic_assignment::Column::CreatedAt)
|
||||
.all(db)
|
||||
.await?;
|
||||
|
||||
Ok(topics.into_iter().map(topic_model_to_resp).collect())
|
||||
}
|
||||
}
|
||||
|
||||
/// topic_assignment::Model -> TopicResp
|
||||
fn topic_model_to_resp(model: topic_assignment::Model) -> TopicResp {
|
||||
TopicResp {
|
||||
id: model.id,
|
||||
class_id: model.class_id,
|
||||
teacher_id: model.teacher_id,
|
||||
title: model.title,
|
||||
description: model.description,
|
||||
due_date: model.due_date,
|
||||
is_active: model.is_active,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user