refactor: 后端仓库层收口(A1:课程/产品/素材)+ 收进工作区既有对象化重构
本提交含两部分。第一部分是本轮工作;第二部分是此前一直留在工作区、
从未提交的对象化重构,与第一部分在文件上互相咬合(internal/repository
整个包都是未跟踪状态,且 api 层已有文件引用它),无法拆成两个可编译的提交。
一、仓库层收口 A1 批(本轮工作)
把 api 层手写的 store.DB 查询收进具名仓库方法,只给真正获益的对象做方法,
不机械包裹全量。本批迁移 22 处裸查询(courses.go 9 / media.go 12 / products.go 1),
新增方法:
- MediaFileRepo.ListByBind / ListForAudit / MarkExtracted
- KnowledgeChunkRepo.CountByMediaFile
- ProductRepo.GetVisibleByID
两条业务口径改由仓库单点持有,避免各处手写漂移:
「只有 approved 素材出现在课程详情」与「已停用产品不在课程详情露出」。
修掉两个真实缺陷:
- ProductRepo.GetByID 缺 Where 条件。此前 GET /api/products/{id} 对任意 id 都返回
第一条产品、对不存在的 id 返回 200,且 PUT /api/products/{id} 会覆盖第一条产品
—— 数据损坏级。全仓扫描确认这是唯一一处同型写法。
- ProductRepo.Delete 写 status="deleted",而 DELETE 处理器文档与回包都声称
"inactive",接口在说谎;管理员用 status=all 拉列表会看到前端不认识的状态。
已对齐为 inactive(与 CourseRepo.Delete 一致)。
删除 8 个零调用且列名不存在的死方法(一调即 SQL 报错):
- media_file 上的 file_path / file_type / approval_status 三列并不存在,
GetByPath / ListByType / UpdateStatus 全废
- knowledge_chunk 上的 space_id 列不存在(模型早已改为 knowledge_space_key),
List / Total / ListBySpaceIDs / DeleteBySpace / SearchByVector 全废
取舍边界:能对当前 schema 跑通的死方法保留,跑不通的删或修。
CourseRepo.List 补齐 status=all 档(此前传给它会当作 status='all' 过滤出空列表)。
该方法此前零调用,现与产品列表语义对齐。
验证:go build ./... 与 go test ./... 全绿;另用真实 HTTP 请求验证 34 项
(课程 17 / 产品 3 / 素材 14),跑在数据库副本与独立 KB_DATA_DIR 上,
含 multipart 真上传 → 审批 → pdftotext 提取 → 分片入库的完整链路。
二、此前未提交的对象化重构(非本轮工作)
- 新增 internal/repository 仓库层、connectors、skills、specialists、xapps、jsonutil,
model/task_record|task_run|task_artifact、api/task_runtime|action_definition|chat_message
- 删除 api/app_definition、connectors、my_app_center、notification、office_skill、
export_docx|pptx|xlsx、official_account_* 等,随 XApp/Skill/Specialist/Connector
可插拔打包方向(AR10/AR11)调整
- 资产目录归位:backend-go/knowledge_source → assets/knowledge/source、
training_materials → assets/training/materials;README 内相对路径同步加深两级;
deploy env 补 ASSET_ROOT_DIR 并改 KNOWLEDGE_SOURCE_DIR / TRAINING_MATERIALS_DIR
- 前端新增 skills/ specialists/ connectors/ xapps/ 目录与对应页面
验证:前端 npm run build 通过(7.26s)。
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -1,33 +1,29 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"eai_agentplatform/backend/internal/middleware"
|
||||
"eai_agentplatform/backend/internal/model"
|
||||
"eai_agentplatform/backend/internal/store"
|
||||
"eai_agentplatform/backend/internal/repository"
|
||||
"eai_agentplatform/backend/internal/web"
|
||||
)
|
||||
|
||||
var (
|
||||
positionRepo repository.PositionRepo
|
||||
courseRepo repository.CourseRepo // productRepo 定义在 products.go
|
||||
)
|
||||
|
||||
func init() {
|
||||
positionRepo = repository.PositionRepo{}
|
||||
courseRepo = repository.CourseRepo{}
|
||||
}
|
||||
|
||||
// ============ 岗位 CRUD(管理员) ============
|
||||
|
||||
// ListPositions GET /api/positions?status= —— 岗位列表
|
||||
func ListPositions(c *gin.Context) {
|
||||
q := store.DB.Model(&model.Position{})
|
||||
switch st := c.Query("status"); st {
|
||||
case "": // 默认仅 active
|
||||
q = q.Where("status = ?", "active")
|
||||
case "all": // 管理员维护全量
|
||||
default:
|
||||
q = q.Where("status = ?", st)
|
||||
}
|
||||
var items []model.Position
|
||||
if err := q.Order("id ASC").Find(&items).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("查询岗位失败"))
|
||||
return
|
||||
}
|
||||
items := positionRepo.List(c.Query("status"))
|
||||
web.OK(c, items)
|
||||
}
|
||||
|
||||
@@ -42,16 +38,14 @@ func CreatePosition(c *gin.Context) {
|
||||
web.Fail(c, web.NewBadRequest("编号、名称为必填"))
|
||||
return
|
||||
}
|
||||
var count int64
|
||||
store.DB.Model(&model.Position{}).Where("code = ?", p.Code).Count(&count)
|
||||
if count > 0 {
|
||||
web.Fail(c, web.NewConflictError("岗位编号已存在"))
|
||||
return
|
||||
}
|
||||
if p.Status == "" {
|
||||
p.Status = "active"
|
||||
}
|
||||
if err := store.DB.Create(&p).Error; err != nil {
|
||||
if positionRepo.CountByName(p.Code, nil) > 0 {
|
||||
web.Fail(c, web.NewConflictError("岗位编号已存在"))
|
||||
return
|
||||
}
|
||||
if !positionRepo.Insert(&p) {
|
||||
web.Fail(c, web.NewBadRequest("创建岗位失败"))
|
||||
return
|
||||
}
|
||||
@@ -64,8 +58,8 @@ func UpdatePosition(c *gin.Context) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var p model.Position
|
||||
if err := store.DB.First(&p, id).Error; err != nil {
|
||||
p, found := positionRepo.GetByID(id)
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("岗位不存在"))
|
||||
return
|
||||
}
|
||||
@@ -82,9 +76,7 @@ func UpdatePosition(c *gin.Context) {
|
||||
req.Status = "active"
|
||||
}
|
||||
if req.Code != "" && req.Code != p.Code {
|
||||
var count int64
|
||||
store.DB.Model(&model.Position{}).Where("code = ? AND id <> ?", req.Code, id).Count(&count)
|
||||
if count > 0 {
|
||||
if positionRepo.CountByName(req.Code, &id) > 0 {
|
||||
web.Fail(c, web.NewConflictError("岗位编号已存在"))
|
||||
return
|
||||
}
|
||||
@@ -93,7 +85,7 @@ func UpdatePosition(c *gin.Context) {
|
||||
p.Name = req.Name
|
||||
p.Description = req.Description
|
||||
p.Status = req.Status
|
||||
if err := store.DB.Save(&p).Error; err != nil {
|
||||
if !positionRepo.Update(&p) {
|
||||
web.Fail(c, web.NewBadRequest("更新岗位失败"))
|
||||
return
|
||||
}
|
||||
@@ -106,12 +98,7 @@ func DeletePosition(c *gin.Context) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var p model.Position
|
||||
if err := store.DB.First(&p, id).Error; err != nil {
|
||||
web.Fail(c, web.NewNotFoundError("岗位不存在"))
|
||||
return
|
||||
}
|
||||
if err := store.DB.Model(&p).Update("status", "inactive").Error; err != nil {
|
||||
if !positionRepo.Delete(id) {
|
||||
web.Fail(c, web.NewBadRequest("停用岗位失败"))
|
||||
return
|
||||
}
|
||||
@@ -136,10 +123,9 @@ func ListPositionKnowledge(c *gin.Context) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var items []model.PositionKnowledge
|
||||
if err := store.DB.Where("position_id = ?", id).Order("id ASC").Find(&items).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("查询岗位知识映射失败"))
|
||||
return
|
||||
items := positionRepo.Knowledge(id)
|
||||
if items == nil {
|
||||
items = []model.PositionKnowledge{}
|
||||
}
|
||||
web.OK(c, items)
|
||||
}
|
||||
@@ -150,8 +136,8 @@ func SavePositionKnowledge(c *gin.Context) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var pos model.Position
|
||||
if err := store.DB.First(&pos, id).Error; err != nil {
|
||||
_, found := positionRepo.GetByID(id)
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("岗位不存在"))
|
||||
return
|
||||
}
|
||||
@@ -190,18 +176,14 @@ func SavePositionKnowledge(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
// 整表覆盖:先删旧,再批量插入
|
||||
if err := store.DB.Where("position_id = ?", id).Delete(&model.PositionKnowledge{}).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("清除旧映射失败"))
|
||||
if !positionRepo.ReplaceKnowledge(id, rows) {
|
||||
web.Fail(c, web.NewBadRequest("保存岗位知识映射失败"))
|
||||
return
|
||||
}
|
||||
if len(rows) > 0 {
|
||||
if err := store.DB.Create(&rows).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("保存岗位知识映射失败"))
|
||||
return
|
||||
}
|
||||
out := positionRepo.Knowledge(id)
|
||||
if out == nil {
|
||||
out = []model.PositionKnowledge{}
|
||||
}
|
||||
var out []model.PositionKnowledge
|
||||
store.DB.Where("position_id = ?", id).Order("id ASC").Find(&out)
|
||||
web.OK(c, out)
|
||||
}
|
||||
|
||||
@@ -213,8 +195,8 @@ func SetUserPosition(c *gin.Context) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var u model.User
|
||||
if err := store.DB.First(&u, id).Error; err != nil {
|
||||
u, found := userRepo.GetByID(id)
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("用户不存在"))
|
||||
return
|
||||
}
|
||||
@@ -225,25 +207,18 @@ func SetUserPosition(c *gin.Context) {
|
||||
web.Fail(c, web.NewBadRequest("请求参数错误"))
|
||||
return
|
||||
}
|
||||
posName := ""
|
||||
if req.PositionID != nil {
|
||||
var pos model.Position
|
||||
if err := store.DB.First(&pos, *req.PositionID).Error; err != nil || pos.Status != "active" {
|
||||
_, found := positionRepo.GetByID(*req.PositionID)
|
||||
if !found {
|
||||
web.Fail(c, web.NewBadRequest("岗位不存在或已停用"))
|
||||
return
|
||||
}
|
||||
posName = pos.Name
|
||||
}
|
||||
u.PositionID = req.PositionID
|
||||
if err := store.DB.Save(&u).Error; err != nil {
|
||||
if !userRepo.Update(&u) {
|
||||
web.Fail(c, web.NewBadRequest("设置用户岗位失败"))
|
||||
return
|
||||
}
|
||||
if req.PositionID != nil {
|
||||
notifyUser(u.ID, "position_set", "岗位已设置",
|
||||
fmt.Sprintf("你的岗位已设置为「%s」,可在「我的岗位清单」查看应学内容", posName),
|
||||
"/exam/my-position")
|
||||
}
|
||||
web.OK(c, gin.H{"id": u.ID, "position_id": u.PositionID})
|
||||
}
|
||||
|
||||
@@ -256,13 +231,13 @@ func MyPosition(c *gin.Context) {
|
||||
web.OK(c, gin.H{"position": nil, "knowledge": []gin.H{}, "count": 0})
|
||||
return
|
||||
}
|
||||
var pos model.Position
|
||||
if err := store.DB.First(&pos, *u.PositionID).Error; err != nil || pos.Status != "active" {
|
||||
pos, found := positionRepo.GetByID(*u.PositionID)
|
||||
if !found || pos.Status != "active" {
|
||||
web.OK(c, gin.H{"position": nil, "knowledge": []gin.H{}, "count": 0})
|
||||
return
|
||||
}
|
||||
var pks []model.PositionKnowledge
|
||||
store.DB.Where("position_id = ?", pos.ID).Order("id ASC").Find(&pks)
|
||||
|
||||
pks := positionRepo.Knowledge(pos.ID)
|
||||
|
||||
// 批量解析课程/产品名称
|
||||
courseIDs := make([]uint, 0, len(pks))
|
||||
@@ -275,22 +250,8 @@ func MyPosition(c *gin.Context) {
|
||||
productIDs = append(productIDs, *pk.ProductID)
|
||||
}
|
||||
}
|
||||
courseName := map[uint]string{}
|
||||
if len(courseIDs) > 0 {
|
||||
var courses []model.Course
|
||||
store.DB.Where("id IN ?", courseIDs).Find(&courses)
|
||||
for _, c := range courses {
|
||||
courseName[c.ID] = c.Name
|
||||
}
|
||||
}
|
||||
productName := map[uint]string{}
|
||||
if len(productIDs) > 0 {
|
||||
var products []model.Product
|
||||
store.DB.Where("id IN ?", productIDs).Find(&products)
|
||||
for _, p := range products {
|
||||
productName[p.ID] = p.Name
|
||||
}
|
||||
}
|
||||
courseName := courseRepo.NamesByIDs(courseIDs)
|
||||
productName := productRepo.NamesByIDs(productIDs)
|
||||
|
||||
list := make([]gin.H, 0, len(pks))
|
||||
for _, pk := range pks {
|
||||
@@ -313,16 +274,6 @@ func MyPosition(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
func nameOrDash(m map[uint]string, id *uint) string {
|
||||
if id == nil {
|
||||
return ""
|
||||
}
|
||||
if n, ok := m[*id]; ok {
|
||||
return n
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// ============ 岗位考试蓝图(管理员) ============
|
||||
|
||||
type blueprintReq struct {
|
||||
@@ -337,10 +288,9 @@ func ListPositionBlueprint(c *gin.Context) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var items []model.PositionExamBlueprint
|
||||
if err := store.DB.Where("position_id = ?", id).Order("id ASC").Find(&items).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("查询岗位考试蓝图失败"))
|
||||
return
|
||||
items := positionRepo.Blueprints(id)
|
||||
if items == nil {
|
||||
items = []model.PositionExamBlueprint{}
|
||||
}
|
||||
web.OK(c, items)
|
||||
}
|
||||
@@ -351,8 +301,8 @@ func SavePositionBlueprint(c *gin.Context) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var pos model.Position
|
||||
if err := store.DB.First(&pos, id).Error; err != nil {
|
||||
_, found := positionRepo.GetByID(id)
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("岗位不存在"))
|
||||
return
|
||||
}
|
||||
@@ -381,17 +331,23 @@ func SavePositionBlueprint(c *gin.Context) {
|
||||
PositionID: id, Domain: it.Domain, Type: it.Type, Count: it.Count,
|
||||
})
|
||||
}
|
||||
if err := store.DB.Where("position_id = ?", id).Delete(&model.PositionExamBlueprint{}).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("清除旧蓝图失败"))
|
||||
if !positionRepo.ReplaceBlueprints(id, rows) {
|
||||
web.Fail(c, web.NewBadRequest("保存岗位考试蓝图失败"))
|
||||
return
|
||||
}
|
||||
if len(rows) > 0 {
|
||||
if err := store.DB.Create(&rows).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("保存岗位考试蓝图失败"))
|
||||
return
|
||||
}
|
||||
out := positionRepo.Blueprints(id)
|
||||
if out == nil {
|
||||
out = []model.PositionExamBlueprint{}
|
||||
}
|
||||
var out []model.PositionExamBlueprint
|
||||
store.DB.Where("position_id = ?", id).Order("id ASC").Find(&out)
|
||||
web.OK(c, out)
|
||||
}
|
||||
|
||||
func nameOrDash(m map[uint]string, id *uint) string {
|
||||
if id == nil {
|
||||
return ""
|
||||
}
|
||||
if n, ok := m[*id]; ok {
|
||||
return n
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user