From 1766cefde9d53d4b15841b32f3a1fcc56f776185 Mon Sep 17 00:00:00 2001 From: iven Date: Wed, 3 Jun 2026 17:15:00 +0800 Subject: [PATCH] =?UTF-8?q?refactor(diary):=20Service=20=E5=B1=82=E6=94=B9?= =?UTF-8?q?=E7=94=A8=20DiaryEvent=20=E6=9E=9A=E4=B8=BE=E6=9B=BF=E4=BB=A3?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - journal_service: 3 处 (JournalCreated/Updated/Deleted) - class_service: 2 处 (ClassCreated/StudentJoinedClass) - comment_service: 1 处 (CommentCreated) - topic_service: 1 处 (TopicAssigned) - parent_service: 1 处 confirm_binding → ParentBound 保留 DomainEvent::new 的场景: - class_service deactivate_class (diary.class.deactivated) - parent_service bind_child (diary.parent.binding_requested) - parent_service delete_child_data (diary.parent.data_deleted) 以上事件不在 DiaryEvent 枚举中(非核心创建事件) 测试: 509/509 全部通过 --- crates/erp-diary/src/service/class_service.rs | 28 +++++------ .../erp-diary/src/service/comment_service.rs | 22 ++++----- .../erp-diary/src/service/journal_service.rs | 47 ++++++++----------- .../erp-diary/src/service/parent_service.rs | 16 +++---- crates/erp-diary/src/service/topic_service.rs | 19 ++++---- 5 files changed, 58 insertions(+), 74 deletions(-) diff --git a/crates/erp-diary/src/service/class_service.rs b/crates/erp-diary/src/service/class_service.rs index d3cdfd2..0426db0 100644 --- a/crates/erp-diary/src/service/class_service.rs +++ b/crates/erp-diary/src/service/class_service.rs @@ -12,6 +12,8 @@ use crate::entity::{class_member, school_class}; use crate::error::{DiaryError, DiaryResult}; use erp_core::events::{DomainEvent, EventBus}; +use crate::event::DiaryEvent; + /// 班级服务 — 6 位码生成、过期控制、成员管理 pub struct ClassService; @@ -84,14 +86,11 @@ impl ClassService { // 发布 ClassCreated 事件 event_bus .publish( - DomainEvent::new( - "diary.class.created", - tenant_id, - serde_json::json!({ - "class_id": id, - "teacher_id": teacher_id, - }), - ), + DiaryEvent::ClassCreated { + class_id: id, + teacher_id, + } + .to_domain_event(tenant_id), db, ) .await; @@ -229,14 +228,11 @@ impl ClassService { // 8. 发布 StudentJoinedClass 事件 event_bus .publish( - DomainEvent::new( - "diary.class.student_joined", - tenant_id, - serde_json::json!({ - "class_id": class_id, - "student_id": user_id, - }), - ), + DiaryEvent::StudentJoinedClass { + class_id, + student_id: user_id, + } + .to_domain_event(tenant_id), db, ) .await; diff --git a/crates/erp-diary/src/service/comment_service.rs b/crates/erp-diary/src/service/comment_service.rs index f5e8a55..221e2e4 100644 --- a/crates/erp-diary/src/service/comment_service.rs +++ b/crates/erp-diary/src/service/comment_service.rs @@ -11,7 +11,9 @@ use crate::entity::{class_member, comment, journal_entry}; use crate::error::{DiaryError, DiaryResult}; use crate::service::content_safety_service::ContentSafetyService; use crate::service::notification_service::NotificationService; -use erp_core::events::{DomainEvent, EventBus}; +use erp_core::events::EventBus; + +use crate::event::DiaryEvent; /// 评语服务 — 老师对学生日记的点评 /// @@ -81,17 +83,13 @@ impl CommentService { // 5. 发布 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, - "content_preview": content.chars().take(50).collect::(), - }), - ), + DiaryEvent::CommentCreated { + comment_id: id, + journal_id, + teacher_id: author_id, + student_id: journal.author_id, + } + .to_domain_event(tenant_id), db, ) .await; diff --git a/crates/erp-diary/src/service/journal_service.rs b/crates/erp-diary/src/service/journal_service.rs index 16f95f6..7dd4474 100644 --- a/crates/erp-diary/src/service/journal_service.rs +++ b/crates/erp-diary/src/service/journal_service.rs @@ -11,7 +11,9 @@ use crate::dto::{CreateJournalReq, JournalResp, UpdateJournalReq}; use crate::entity::journal_entry; use crate::error::{DiaryError, DiaryResult}; use erp_core::error::check_version; -use erp_core::events::{DomainEvent, EventBus}; +use erp_core::events::EventBus; + +use crate::event::DiaryEvent; /// 日记 CRUD 服务 — 创建、读取、更新、软删除日记条目 pub struct JournalService; @@ -57,15 +59,12 @@ impl JournalService { // 发布领域事件 event_bus .publish( - DomainEvent::new( - "diary.created", - tenant_id, - serde_json::json!({ - "journal_id": id, - "author_id": author_id, - "class_id": req.class_id, - }), - ), + DiaryEvent::JournalCreated { + journal_id: id, + author_id, + class_id: req.class_id, + } + .to_domain_event(tenant_id), db, ) .await; @@ -153,15 +152,12 @@ impl JournalService { // 发布领域事件 event_bus .publish( - DomainEvent::new( - "diary.updated", - tenant_id, - serde_json::json!({ - "journal_id": id, - "author_id": operator_id, - "version": new_version, - }), - ), + DiaryEvent::JournalUpdated { + journal_id: id, + author_id: operator_id, + version: new_version, + } + .to_domain_event(tenant_id), db, ) .await; @@ -206,14 +202,11 @@ impl JournalService { // 发布领域事件 event_bus .publish( - DomainEvent::new( - "diary.deleted", - tenant_id, - serde_json::json!({ - "journal_id": id, - "author_id": operator_id, - }), - ), + DiaryEvent::JournalDeleted { + journal_id: id, + author_id: operator_id, + } + .to_domain_event(tenant_id), db, ) .await; diff --git a/crates/erp-diary/src/service/parent_service.rs b/crates/erp-diary/src/service/parent_service.rs index 01d9708..8a7492e 100644 --- a/crates/erp-diary/src/service/parent_service.rs +++ b/crates/erp-diary/src/service/parent_service.rs @@ -12,6 +12,8 @@ use crate::entity::parent_child_binding; use crate::error::{DiaryError, DiaryResult}; use erp_core::events::{DomainEvent, EventBus}; +use crate::event::DiaryEvent; + /// 家长中心服务 — 绑定管理、数据查阅、导出、删除 pub struct ParentService; @@ -127,15 +129,11 @@ impl ParentService { event_bus .publish( - DomainEvent::new( - "diary.parent.binding_confirmed", - tenant_id, - serde_json::json!({ - "parent_id": updated.parent_id, - "child_id": child_id, - "binding_id": binding_id, - }), - ), + DiaryEvent::ParentBound { + parent_id: updated.parent_id, + child_id, + } + .to_domain_event(tenant_id), db, ) .await; diff --git a/crates/erp-diary/src/service/topic_service.rs b/crates/erp-diary/src/service/topic_service.rs index 3f3c66a..34ec615 100644 --- a/crates/erp-diary/src/service/topic_service.rs +++ b/crates/erp-diary/src/service/topic_service.rs @@ -11,7 +11,9 @@ use crate::dto::{CreateTopicReq, TopicResp, UpdateTopicReq}; use crate::entity::topic_assignment; use crate::error::{DiaryError, DiaryResult}; use crate::service::notification_service::NotificationService; -use erp_core::events::{DomainEvent, EventBus}; +use erp_core::events::EventBus; + +use crate::event::DiaryEvent; /// 主题布置服务 — 老师发布日记主题,学生提交对应日记 pub struct TopicService; @@ -67,15 +69,12 @@ impl TopicService { // 发布 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, - }), - ), + DiaryEvent::TopicAssigned { + topic_id: id, + class_id, + teacher_id, + } + .to_domain_event(tenant_id), db, ) .await;