feat(skill): optimize loadCandidateSkills to use batch retrieval for skill definitions

This commit is contained in:
mlogclub
2026-04-17 17:27:33 +08:00
parent c396a4a5e3
commit 81056a61a1
2 changed files with 18 additions and 12 deletions
+3 -12
View File
@@ -25,21 +25,12 @@ func (l *candidateLoader) loadCandidateSkills(aiAgent *models.AIAgent) []models.
return nil
}
skillIDs := utils.SplitInt64s(aiAgent.SkillIDs)
if len(skillIDs) == 0 {
return nil
}
skills := repositories.SkillDefinitionRepository.GetByIDs(sqls.DB(), skillIDs)
ret := make([]models.SkillDefinition, 0, len(skillIDs))
for _, id := range skillIDs {
// TODO 这里批量查询一下,批量查询返回数据的顺序需要保证和skillIDs一致
skill := l.getSkillByID(id)
if skill == nil || skill.Status != enums.StatusOk {
continue
if skill, ok := skills[id]; ok && skill.Status == enums.StatusOk {
ret = append(ret, skill)
}
ret = append(ret, *skill)
}
return ret
}
func (l *candidateLoader) getSkillByID(id int64) *models.SkillDefinition {
return repositories.SkillDefinitionRepository.Get(sqls.DB(), id)
}
@@ -103,3 +103,18 @@ func (r *skillDefinitionRepository) Delete(db *gorm.DB, id int64) {
func (r *skillDefinitionRepository) GetByCode(db *gorm.DB, code string) *models.SkillDefinition {
return r.FindOne(db, sqls.NewCnd().Where("code = ?", code))
}
func (r *skillDefinitionRepository) GetByIDs(db *gorm.DB, ids []int64) map[int64]models.SkillDefinition {
if len(ids) == 0 {
return nil
}
list := r.Find(db, sqls.NewCnd().Where("id IN (?)", ids))
if len(list) == 0 {
return nil
}
result := make(map[int64]models.SkillDefinition, len(list))
for _, item := range list {
result[item.ID] = item
}
return result
}