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 return nil
} }
skillIDs := utils.SplitInt64s(aiAgent.SkillIDs) skillIDs := utils.SplitInt64s(aiAgent.SkillIDs)
if len(skillIDs) == 0 { skills := repositories.SkillDefinitionRepository.GetByIDs(sqls.DB(), skillIDs)
return nil
}
ret := make([]models.SkillDefinition, 0, len(skillIDs)) ret := make([]models.SkillDefinition, 0, len(skillIDs))
for _, id := range skillIDs { for _, id := range skillIDs {
// TODO 这里批量查询一下,批量查询返回数据的顺序需要保证和skillIDs一致 if skill, ok := skills[id]; ok && skill.Status == enums.StatusOk {
skill := l.getSkillByID(id) ret = append(ret, skill)
if skill == nil || skill.Status != enums.StatusOk {
continue
} }
ret = append(ret, *skill)
} }
return ret 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 { func (r *skillDefinitionRepository) GetByCode(db *gorm.DB, code string) *models.SkillDefinition {
return r.FindOne(db, sqls.NewCnd().Where("code = ?", code)) 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
}