后端:
1.阶段 6 CP4/CP5 目录收口与共享边界纯化
- 将 backend 根目录收口为 services、client、gateway、cmd、shared 五个一级目录
- 收拢 bootstrap、inits、infra/kafka、infra/outbox、conv、respond、pkg、middleware,移除根目录旧实现与空目录
- 将 utils 下沉到 services/userauth/internal/auth,将 logic 下沉到 services/schedule/core/planning
- 将迁移期 runtime 桥接实现统一收拢到 services/runtime/{conv,dao,eventsvc,model},删除 shared/legacy 与未再被 import 的旧 service 实现
- 将 gateway/shared/respond 收口为 HTTP/Gin 错误写回适配,shared/respond 仅保留共享错误语义与状态映射
- 将 HTTP IdempotencyMiddleware 与 RateLimitMiddleware 收口到 gateway/middleware
- 将 GormCachePlugin 下沉到 shared/infra/gormcache,将共享 RateLimiter 下沉到 shared/infra/ratelimit,将 agent token budget 下沉到 services/agent/shared
- 删除 InitEino 兼容壳,收缩 cmd/internal/coreinit 仅保留旧组合壳残留域初始化语义
- 更新微服务迁移计划与桌面 checklist,补齐 CP4/CP5 当前切流点、目录终态与验证结果
- 完成 go test ./...、git diff --check 与最终真实 smoke;health、register/login、task/create+get、schedule/today、task-class/list、memory/items、agent chat/meta/timeline/context-stats 全部 200,SSE 合并结果为 CP5_OK 且 [DONE] 只有 1 个
65 lines
2.1 KiB
Go
65 lines
2.1 KiB
Go
package dao
|
||
|
||
import (
|
||
"context"
|
||
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
// RepoManager 聚合所有 DAO,供服务层做跨仓储事务编排。
|
||
type RepoManager struct {
|
||
db *gorm.DB
|
||
Schedule *ScheduleDAO
|
||
Task *TaskDAO
|
||
Course *CourseDAO
|
||
TaskClass *TaskClassDAO
|
||
Agent *AgentDAO
|
||
ActiveSchedule *ActiveScheduleDAO
|
||
ActiveScheduleSession *ActiveScheduleSessionDAO
|
||
}
|
||
|
||
func NewManager(db *gorm.DB) *RepoManager {
|
||
return &RepoManager{
|
||
db: db,
|
||
Schedule: NewScheduleDAO(db),
|
||
Task: NewTaskDAO(db),
|
||
Course: NewCourseDAO(db),
|
||
TaskClass: NewTaskClassDAO(db),
|
||
Agent: NewAgentDAO(db),
|
||
ActiveSchedule: NewActiveScheduleDAO(db),
|
||
ActiveScheduleSession: NewActiveScheduleSessionDAO(db),
|
||
}
|
||
}
|
||
|
||
// WithTx 基于外部事务句柄构造“同事务 RepoManager”。
|
||
//
|
||
// 职责边界:
|
||
// 1. 只做 DAO 依赖重绑定,不开启/提交/回滚事务;
|
||
// 2. 让服务层在一个 tx 内调用多个 DAO 方法;
|
||
// 3. 适用于 outbox 消费处理器这类“基础设施事务 + 业务事务合并”的场景。
|
||
func (m *RepoManager) WithTx(tx *gorm.DB) *RepoManager {
|
||
return &RepoManager{
|
||
db: tx,
|
||
Schedule: m.Schedule.WithTx(tx),
|
||
Task: m.Task.WithTx(tx),
|
||
TaskClass: m.TaskClass.WithTx(tx),
|
||
Course: m.Course.WithTx(tx),
|
||
Agent: m.Agent.WithTx(tx),
|
||
ActiveSchedule: m.ActiveSchedule.WithTx(tx),
|
||
ActiveScheduleSession: m.ActiveScheduleSession.WithTx(tx),
|
||
}
|
||
}
|
||
|
||
// Transaction 开启事务并把“同事务 RepoManager”传给回调。
|
||
//
|
||
// 使用约束:
|
||
// 1. 回调里应只使用 txM 下挂 DAO,避免混入事务外句柄;
|
||
// 2. 回调返回 error 会触发整体回滚;
|
||
// 3. 回调返回 nil 表示提交事务。
|
||
func (m *RepoManager) Transaction(ctx context.Context, fn func(txM *RepoManager) error) error {
|
||
return m.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||
txM := m.WithTx(tx)
|
||
return fn(txM)
|
||
})
|
||
}
|