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 }