新增了 检测题目明细重复问题

This commit is contained in:
2024-11-21 16:07:47 +08:00
parent f36b3173af
commit e42952441b
2 changed files with 60 additions and 11 deletions
+59 -1
View File
@@ -61,6 +61,16 @@ func (r *QuestionQaService) AddQuestionQa(req requests.AddQuestionQa) (bool, err
return false, err
}
// 检测题目明细重复问题
var items []*requests.QuestionQaItem
for _, v := range req.QuestionQaItem {
if r.CheckQuestionQaItemRepeat(items, v) {
items = append(items, v)
} else {
return false, errors.New(utils.QuestionType(v.QuestionType) + "选题存在冲突")
}
}
// 开始事务
tx := global.Db.Begin()
defer func() {
@@ -265,7 +275,7 @@ func (r *QuestionQaService) AddQuestionQa(req requests.AddQuestionQa) (bool, err
}
}
//tx.Commit()
tx.Commit()
return true, nil
}
@@ -374,6 +384,16 @@ func (r *QuestionQaService) PutQuestionQa(qaId int64, req requests.PutQuestionQa
// 处理题库明细-需重新生成时才会检测明细
if req.Action == 2 {
// 检测题目明细重复问题
var items []*requests.QuestionQaItem
for _, v := range req.QuestionQaItem {
if r.CheckQuestionQaItemRepeat(items, v) {
items = append(items, v)
} else {
return false, errors.New(utils.QuestionType(v.QuestionType) + ":选题存在冲突")
}
}
// 删除所有明细
questionQaItemDao := dao.QuestionQaItemDao{}
err := questionQaItemDao.DeleteQuestionQaItemByQaId(tx, questionQa.QaId)
@@ -1265,3 +1285,41 @@ func (r *QuestionQaService) GetShareQuestionQaForTypeThree(questionQa *model.Que
return g, nil
}
// CheckQuestionQaItemRepeat 检测题目明细重复
func (r *QuestionQaService) CheckQuestionQaItemRepeat(items []*requests.QuestionQaItem, newItem *requests.QuestionQaItem) bool {
for _, item := range items {
// 检查必填项
if item.QuestionType == newItem.QuestionType &&
item.FirstLabelId == newItem.FirstLabelId {
// 如果所有字段相同(包括选填项),不能添加
if item.SecondLabelId == newItem.SecondLabelId &&
((item.Difficulty == nil && newItem.Difficulty == nil) ||
(item.Difficulty != nil && newItem.Difficulty != nil && *item.Difficulty == *newItem.Difficulty)) {
return false
}
// 如果新增项选填项为空,但已有项选填项不为空,则冲突
if newItem.SecondLabelId == "" && item.SecondLabelId != "" {
return false
}
// 如果新增项选填项不为空,但已有项选填项为空,则冲突
if newItem.SecondLabelId != "" && item.SecondLabelId == "" {
return false
}
// 如果新增项选填项为空,但已有项选填项不为空,则冲突
if newItem.Difficulty == nil && item.Difficulty != nil {
return false
}
// 如果新增项选填项不为空,但已有项选填项为空,则冲突
if newItem.Difficulty != nil && item.Difficulty == nil {
return false
}
}
}
return true
}