新增了 检测题目明细重复问题
This commit is contained in:
parent
f36b3173af
commit
e42952441b
@ -91,22 +91,13 @@ type PutQuestionQa struct {
|
||||
QaExpireTime string `json:"qa_expire_time" form:"qa_expire_time" label:"过期时间" validate:"required"` // 注意:这里假设LocalTime转换为字符串格式处理
|
||||
QaPassword string `json:"qa_password" form:"qa_password" label:"分享密码" validate:"required"`
|
||||
Image string `json:"image" form:"image" label:"背景图" validate:"required"`
|
||||
QuestionQaItem []*PutQuestionQaItem `json:"question_qa_item" form:"question_qa_item" label:"题目明细" validate:"required"`
|
||||
QuestionQaItem []*QuestionQaItem `json:"question_qa_item" form:"question_qa_item" label:"题目明细" validate:"required"`
|
||||
Action int `json:"action" form:"action" label:"动作" validate:"required,oneof=1 2"` // 1:正常修改 2:重新生成题库
|
||||
BaseTokenItem []PutQuestionQaBaseTokenItem `json:"base_token_item" form:"base_token_item" label:"飞花令明细"` // 展示类型为飞花令时存在
|
||||
TokenQuestionContent []TokenQuestionContent `json:"token_question_content" form:"token_question_content" label:"飞花令题目数量规则"` // -json(当题库类型为2、3时存在。2表示飞花令后固定题目数量,3表示飞花令后单个类型题目数量)
|
||||
QuestionQaTimer []QuestionQaTimer `json:"question_qa_timer" form:"question_qa_timer" label:"计时设置"` //
|
||||
}
|
||||
|
||||
// PutQuestionQaItem 修改问答题库-明细
|
||||
type PutQuestionQaItem struct {
|
||||
QuestionType int `json:"question_type" form:"question_type" validate:"required,number,oneof=1 2 3 4" label:"题目类型"` // 题目类型(1:单选 2:多选 3:问答 4:判断)
|
||||
FirstLabelId string `json:"first_label_id" form:"first_label_id" validate:"required" label:"一级标签id"`
|
||||
SecondLabelId string `json:"second_label_id" form:"second_label_id" label:"二级标签id"`
|
||||
Difficulty *int `json:"difficulty" form:"difficulty" validate:"omitempty,number,oneof=1 2 3" label:"难度"`
|
||||
Quantity int `json:"quantity" form:"quantity" validate:"required,number,min=1" label:"数量"`
|
||||
}
|
||||
|
||||
// PutQuestionQaBaseTokenItem 修改问答题库-飞花令明细
|
||||
type PutQuestionQaBaseTokenItem struct {
|
||||
TokenId string `json:"token_id" form:"token_id" label:"飞花令明细id" validate:"required"`
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user