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:
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
|
||||
}
|
||||
Reference in New Issue
Block a user