refactor: 后端仓库层更名为数据访问层(internal/repository → internal/dal)
「仓库层」是 repository 的直译,中文里与「代码仓库 / git 仓库」同词, 而这一层做的事就是数据访问。名字改成它实际在做的事。 改名口径(纯机械替换,无逻辑改动): - 包:internal/repository → internal/dal(package repository → package dal) - 类型:XxxRepo → XxxDAO(TaskRecordDAO / SpecialistDAO / PositionDAO …) - 变量:xxxRepo → xxxDAO - import 路径、包限定符、日志前缀 [repository] → [dal] 同步 - 注释里的「仓库层」→「数据访问层」;core.go 包注释补上 DAL/DAO 全称 命名规范补登(AR09 是命名问题的最高依据,改了名就得回去登记): - AR09 §3.1 术语表新增「数据访问层 dal / DAO」一行 - AR09 §5.6 缩写表新增 DAO / dal —— 原文是「只有下表内的缩写允许使用」, 不登记就是自己破自己的规矩 - PROJECT_STATE.md 新增 D27 记录本次更名决策 验证:全部在 db 副本上做,生产库 data/eai_agentplatform.db 未触碰。 - 等价性对照:拿 HEAD 源码 + 仅改名 造出第二棵树,两棵树各自起 httptest 服务跑同一份探针(60 个 GET + 13 个写/回读,覆盖专员/技能/应用/ 任务/交付物/项目/岗位/考试/知识/积分/管理端只读等),逐端点比对响应体: 73 项里 52 项字节完全一致、21 项仅运行期时间戳不同、内容差异 0。 - 探针非空:往改名后的树注入「SpecialistDAO.List 限 3 条」变异, /api/specialists 立刻被抓出 —— 证明上面那个 0 不是没测到。 - 暂存区自洽:把索引整个导出成源码树,go build / go vet / go test ./... 全绿。 - gofmt:因 import 排序变化而错位的 19 个文件已修;另 2 个文件(skill_definition.go、 seed.go)的格式问题是工作区里别人的在制品带来的,未替其改动。 未纳入本次提交:工作区里正在进行中的「文生语音技能 + 技能展示色/交互卡」 (tts_handlers.go、text_to_speech/manifest.go、skillCatalog.js 等), 以及 router.go / skill_definition.go / seed.go 三个文件里属于该在制品的改动 —— 这三个文件只把「改名那一版」放进索引,工作区原样保留。 Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
package dal
|
||||
|
||||
import (
|
||||
"eai_agentplatform/backend/internal/model"
|
||||
)
|
||||
|
||||
// Course 课程仓库。
|
||||
type CourseDAO struct{ *QueryBuilder }
|
||||
|
||||
// List 获取课程列表(按条件过滤)。
|
||||
//
|
||||
// status 的三档含义与产品列表一致,调用方直接透传 query 参数即可:
|
||||
// - ""(默认):仅 active —— 员工浏览视角
|
||||
// - "all":管理员维护视角,不按状态过滤
|
||||
// - 其他:按该状态精确过滤
|
||||
func (r CourseDAO) List(category, status string) []model.Course {
|
||||
q := r.Type(&model.Course{})
|
||||
if category != "" {
|
||||
q = q.Where("category = ?", category)
|
||||
}
|
||||
switch status {
|
||||
case "":
|
||||
q = q.Where("status = ?", "active")
|
||||
case "all":
|
||||
default:
|
||||
q = q.Where("status = ?", status)
|
||||
}
|
||||
var items []model.Course
|
||||
if q.Order("id ASC").Find(&items) {
|
||||
return items
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetByID 按 ID 获取。
|
||||
func (r CourseDAO) GetByID(id uint) (model.Course, bool) {
|
||||
var c model.Course
|
||||
if r.Type(&c).Where("id = ?", id).First(&c) {
|
||||
return c, true
|
||||
}
|
||||
return model.Course{}, false
|
||||
}
|
||||
|
||||
// GetByCode 按编号获取。
|
||||
func (r CourseDAO) GetByCode(code string) (model.Course, bool) {
|
||||
var c model.Course
|
||||
if r.Type(&c).Where("code = ?", code).First(&c) {
|
||||
return c, true
|
||||
}
|
||||
return model.Course{}, false
|
||||
}
|
||||
|
||||
// Insert 创建课程。
|
||||
func (r CourseDAO) Insert(c *model.Course) bool {
|
||||
return r.QueryBuilder.Insert(c)
|
||||
}
|
||||
|
||||
// Update 更新课程。
|
||||
func (r CourseDAO) Update(c *model.Course) bool {
|
||||
return r.Save(c)
|
||||
}
|
||||
|
||||
// Delete 软删除。
|
||||
func (r CourseDAO) Delete(id uint) bool {
|
||||
return r.Type(&model.Course{}).Where("id = ?", id).Updates(map[string]any{"status": "inactive"})
|
||||
}
|
||||
|
||||
// CountByCode 按编号统计(唯一性检查)。
|
||||
func (r CourseDAO) CountByCode(code string, excludeID *uint) int64 {
|
||||
var c int64
|
||||
q := r.Inner().Model(&model.Course{}).Where("code = ?", code)
|
||||
if excludeID != nil {
|
||||
q = q.Where("id <> ?", *excludeID)
|
||||
}
|
||||
q.Count(&c)
|
||||
return c
|
||||
}
|
||||
|
||||
// NamesByIDs 批量解析课程名称(id → name),用于列表页回填关联名称。
|
||||
// 未命中的 ID 不会出现在返回的 map 中,调用方需自行兜底。
|
||||
func (r CourseDAO) NamesByIDs(ids []uint) map[uint]string {
|
||||
names := map[uint]string{}
|
||||
if len(ids) == 0 {
|
||||
return names
|
||||
}
|
||||
var items []model.Course
|
||||
if r.Type(&items).Where("id IN ?", ids).Find(&items) {
|
||||
for _, c := range items {
|
||||
names[c.ID] = c.Name
|
||||
}
|
||||
}
|
||||
return names
|
||||
}
|
||||
Reference in New Issue
Block a user