修改了新增了题库类型2、3
This commit is contained in:
+201
-91
@@ -48,6 +48,77 @@ func (r *QuestionQaService) AddQuestionQa(req requests.AddQuestionQa) (bool, err
|
||||
return false, errors.New("明细题目错误")
|
||||
}
|
||||
|
||||
// 选择的明细数量 += QuestionQaItem.Quantity
|
||||
itemQuantity := 0
|
||||
for _, item := range req.QuestionQaItem {
|
||||
itemQuantity = itemQuantity + item.Quantity
|
||||
}
|
||||
|
||||
// 题目总数量
|
||||
qaQuantity := req.QaQuantity
|
||||
|
||||
// 常规模式
|
||||
if req.QaType == 1 {
|
||||
// 总数量 需大于 选择的明细数量
|
||||
if qaQuantity > itemQuantity {
|
||||
return false, errors.New("明细题目数量需超过题库所需题目总数量")
|
||||
}
|
||||
}
|
||||
|
||||
// 固定套题模式
|
||||
if req.QaType == 2 {
|
||||
// 计算飞花令题目数量
|
||||
tokenQuestionQuantity := 0
|
||||
for _, content := range req.TokenQuestionContent {
|
||||
tokenQuestionQuantity = tokenQuestionQuantity + content.Quantity
|
||||
}
|
||||
|
||||
// 总数量 = 飞花令题目数量 * 飞花令数量
|
||||
qaQuantity = tokenQuestionQuantity * *req.TokenNum
|
||||
if qaQuantity > itemQuantity {
|
||||
return false, errors.New("明细题目数量需超过题库所需题目总数量")
|
||||
}
|
||||
|
||||
// 单个类型总数量 = 单一题目类型数量 * 飞花令数量
|
||||
for _, content := range req.TokenQuestionContent {
|
||||
for _, item := range req.QuestionQaItem {
|
||||
if content.QuestionType == item.QuestionType {
|
||||
quantity := content.Quantity * *req.TokenNum
|
||||
if quantity > item.Quantity {
|
||||
return false, errors.New(utils.QuestionType(content.QuestionType) + "数量不足")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 多轮固定题型模式
|
||||
if req.QaType == 3 {
|
||||
// 计算飞花令题目数量
|
||||
tokenQuestionQuantity := 0
|
||||
for _, content := range req.TokenQuestionContent {
|
||||
tokenQuestionQuantity = tokenQuestionQuantity + content.Quantity
|
||||
}
|
||||
|
||||
// 总数量 = 飞花令题目数量 * 飞花令数量 * 轮次
|
||||
qaQuantity = tokenQuestionQuantity * *req.TokenNum * *req.RoundNum
|
||||
if qaQuantity > itemQuantity {
|
||||
return false, errors.New("明细题目数量需超过题库所需题目总数量")
|
||||
}
|
||||
|
||||
// 单个类型总数量 = 单一题目类型数量 * 飞花令数量 * 轮次
|
||||
for _, content := range req.TokenQuestionContent {
|
||||
for _, item := range req.QuestionQaItem {
|
||||
if content.QuestionType == item.QuestionType {
|
||||
quantity := content.Quantity * *req.TokenNum * *req.RoundNum
|
||||
if quantity > item.Quantity {
|
||||
return false, errors.New(utils.QuestionType(content.QuestionType) + "数量不足")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
@@ -59,7 +130,7 @@ func (r *QuestionQaService) AddQuestionQa(req requests.AddQuestionQa) (bool, err
|
||||
// 新增题目
|
||||
questionQa := &model.QuestionQa{
|
||||
QaName: req.QaName,
|
||||
QaQuantity: req.QaQuantity,
|
||||
QaQuantity: qaQuantity,
|
||||
QaStatus: 1,
|
||||
QaRuleContent: req.QaRuleContent,
|
||||
QaDisplayType: req.QaDisplayType,
|
||||
@@ -70,6 +141,32 @@ func (r *QuestionQaService) AddQuestionQa(req requests.AddQuestionQa) (bool, err
|
||||
ItemContent: string(questionQaItemContent),
|
||||
}
|
||||
|
||||
// 固定套题模式-飞花令数量
|
||||
if req.QaType == 2 {
|
||||
questionQa.TokenNum = req.TokenNum
|
||||
} else {
|
||||
questionQa.TokenNum = nil
|
||||
}
|
||||
|
||||
// 多轮固定题型模式-飞花令数量、轮次数量
|
||||
if req.QaType == 3 {
|
||||
questionQa.TokenNum = req.TokenNum
|
||||
questionQa.RoundNum = req.RoundNum
|
||||
} else {
|
||||
questionQa.TokenNum = nil
|
||||
questionQa.RoundNum = nil
|
||||
}
|
||||
|
||||
if req.QaType == 2 || req.QaType == 3 {
|
||||
// 处理飞花令题目数量规则
|
||||
tokenQuestionContent, err := json.Marshal(req.TokenQuestionContent)
|
||||
if err != nil {
|
||||
return false, errors.New("飞花令题目数量错误")
|
||||
}
|
||||
|
||||
questionQa.TokenQuestionContent = string(tokenQuestionContent)
|
||||
}
|
||||
|
||||
questionQaDao := dao.QuestionQaDao{}
|
||||
questionQa, err = questionQaDao.AddQuestionQa(tx, questionQa)
|
||||
if err != nil {
|
||||
@@ -78,14 +175,8 @@ func (r *QuestionQaService) AddQuestionQa(req requests.AddQuestionQa) (bool, err
|
||||
}
|
||||
|
||||
// 生成分享链接
|
||||
qaShareId := utils.HashString(fmt.Sprintf("%d", questionQa.QaId))
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
}
|
||||
|
||||
questionQaData := make(map[string]interface{})
|
||||
questionQaData["qa_share_id"] = qaShareId
|
||||
questionQaData["qa_share_id"] = utils.HashString(fmt.Sprintf("%d", questionQa.QaId))
|
||||
err = questionQaDao.EditQuestionQaById(tx, questionQa.QaId, questionQaData)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
@@ -96,35 +187,6 @@ func (r *QuestionQaService) AddQuestionQa(req requests.AddQuestionQa) (bool, err
|
||||
questionDao := dao.QuestionDao{}
|
||||
questionQaItemDao := dao.QuestionQaItemDao{}
|
||||
for _, item := range req.QuestionQaItem {
|
||||
// 验证一级标签
|
||||
firstLabelId, err := strconv.ParseInt(item.FirstLabelId, 10, 64)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
}
|
||||
|
||||
labelDao := dao.LabelDao{}
|
||||
_, err = labelDao.GetLabelFirstById(firstLabelId)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
}
|
||||
|
||||
// 验证二级标签
|
||||
if item.SecondLabelId != "" {
|
||||
secondLabelId, err := strconv.ParseInt(item.SecondLabelId, 10, 64)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
}
|
||||
|
||||
_, err = labelDao.GetLabelFirstById(secondLabelId)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
// 验证数量
|
||||
maps := make(map[string]interface{})
|
||||
maps["question_type"] = item.QuestionType
|
||||
@@ -138,16 +200,6 @@ func (r *QuestionQaService) AddQuestionQa(req requests.AddQuestionQa) (bool, err
|
||||
if item.SecondLabelId != "" {
|
||||
maps["second_label_id"] = item.SecondLabelId
|
||||
}
|
||||
total, err := questionDao.GetQuestionCount(maps)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
}
|
||||
|
||||
if total < int64(item.Quantity) {
|
||||
tx.Rollback()
|
||||
return false, errors.New("选题超出数量")
|
||||
}
|
||||
|
||||
// 获取随机明细题目
|
||||
questions, err := questionDao.GetQuestionListRand(maps, item.Quantity)
|
||||
@@ -156,6 +208,11 @@ func (r *QuestionQaService) AddQuestionQa(req requests.AddQuestionQa) (bool, err
|
||||
return false, err
|
||||
}
|
||||
|
||||
if len(questions) < item.Quantity {
|
||||
tx.Rollback()
|
||||
return false, errors.New("选题超出可选择题目数量")
|
||||
}
|
||||
|
||||
// 新增明细题目
|
||||
for _, question := range questions {
|
||||
questionQaItem := &model.QuestionQaItem{
|
||||
@@ -170,7 +227,7 @@ func (r *QuestionQaService) AddQuestionQa(req requests.AddQuestionQa) (bool, err
|
||||
}
|
||||
}
|
||||
|
||||
// 新增飞花令
|
||||
// 新增飞花令标签数据
|
||||
if req.QaDisplayType == 2 {
|
||||
questionQaTokenDao := dao.QuestionQaTokenDao{}
|
||||
baseTokenDao := dao.BaseTokenDao{}
|
||||
@@ -277,10 +334,10 @@ func (r *QuestionQaService) PutQuestionQa(qaId int64, req requests.PutQuestionQa
|
||||
questionQaData["image"] = image
|
||||
}
|
||||
|
||||
// 处理明细选题规则
|
||||
questionQaItemContent, _ := json.Marshal(req.QuestionQaItem)
|
||||
if string(questionQaItemContent) != questionQa.ItemContent {
|
||||
questionQaData["item_content"] = string(questionQaItemContent)
|
||||
// 处理飞花令题目数量规则
|
||||
tokenQuestionContent, _ := json.Marshal(req.TokenQuestionContent)
|
||||
if string(tokenQuestionContent) != questionQa.TokenQuestionContent {
|
||||
questionQaData["token_question_content"] = string(tokenQuestionContent)
|
||||
}
|
||||
|
||||
// 题目数量
|
||||
@@ -290,6 +347,82 @@ func (r *QuestionQaService) PutQuestionQa(qaId int64, req requests.PutQuestionQa
|
||||
return false, errors.New("您修改了题目数量,无法正常修改该题库,请重新生成")
|
||||
}
|
||||
|
||||
// 选择的明细数量 += QuestionQaItem.Quantity
|
||||
itemQuantity := 0
|
||||
for _, item := range req.QuestionQaItem {
|
||||
itemQuantity = itemQuantity + item.Quantity
|
||||
}
|
||||
|
||||
// 题目总数量
|
||||
qaQuantity := req.QaQuantity
|
||||
|
||||
// 常规模式
|
||||
if req.QaType == 1 {
|
||||
// 总数量 需大于 选择的明细数量
|
||||
if qaQuantity > itemQuantity {
|
||||
tx.Rollback()
|
||||
return false, errors.New("明细题目数量需超过题库所需题目总数量")
|
||||
}
|
||||
}
|
||||
|
||||
// 固定套题模式
|
||||
if req.QaType == 2 {
|
||||
// 计算飞花令题目数量
|
||||
tokenQuestionQuantity := 0
|
||||
for _, content := range req.TokenQuestionContent {
|
||||
tokenQuestionQuantity = tokenQuestionQuantity + content.Quantity
|
||||
}
|
||||
|
||||
// 总数量 = 飞花令题目数量 * 飞花令数量
|
||||
qaQuantity = tokenQuestionQuantity * *req.TokenNum
|
||||
if qaQuantity > itemQuantity {
|
||||
tx.Rollback()
|
||||
return false, errors.New("明细题目数量需超过题库所需题目总数量")
|
||||
}
|
||||
|
||||
// 单个类型总数量 = 单一题目类型数量 * 飞花令数量
|
||||
for _, content := range req.TokenQuestionContent {
|
||||
for _, item := range req.QuestionQaItem {
|
||||
if content.QuestionType == item.QuestionType {
|
||||
quantity := content.Quantity * *req.TokenNum
|
||||
if quantity > item.Quantity {
|
||||
tx.Rollback()
|
||||
return false, errors.New(utils.QuestionType(content.QuestionType) + "数量不足")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 多轮固定题型模式
|
||||
if req.QaType == 3 {
|
||||
// 计算飞花令题目数量
|
||||
tokenQuestionQuantity := 0
|
||||
for _, content := range req.TokenQuestionContent {
|
||||
tokenQuestionQuantity = tokenQuestionQuantity + content.Quantity
|
||||
}
|
||||
|
||||
// 总数量 = 飞花令题目数量 * 飞花令数量 * 轮次
|
||||
qaQuantity = tokenQuestionQuantity * *req.TokenNum * *req.RoundNum
|
||||
if qaQuantity > itemQuantity {
|
||||
tx.Rollback()
|
||||
return false, errors.New("明细题目数量需超过题库所需题目总数量")
|
||||
}
|
||||
|
||||
// 单个类型总数量 = 单一题目类型数量 * 飞花令数量 * 轮次
|
||||
for _, content := range req.TokenQuestionContent {
|
||||
for _, item := range req.QuestionQaItem {
|
||||
if content.QuestionType == item.QuestionType {
|
||||
quantity := content.Quantity * *req.TokenNum * *req.RoundNum
|
||||
if quantity > item.Quantity {
|
||||
tx.Rollback()
|
||||
return false, errors.New(utils.QuestionType(content.QuestionType) + "数量不足")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
questionQaData["qa_quantity"] = req.QaQuantity
|
||||
}
|
||||
|
||||
@@ -304,35 +437,6 @@ func (r *QuestionQaService) PutQuestionQa(qaId int64, req requests.PutQuestionQa
|
||||
}
|
||||
|
||||
for _, item := range req.QuestionQaItem {
|
||||
// 验证一级标签
|
||||
firstLabelId, err := strconv.ParseInt(item.FirstLabelId, 10, 64)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
}
|
||||
|
||||
labelDao := dao.LabelDao{}
|
||||
_, err = labelDao.GetLabelFirstById(firstLabelId)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
}
|
||||
|
||||
// 验证二级标签
|
||||
if item.SecondLabelId != "" {
|
||||
secondLabelId, err := strconv.ParseInt(item.SecondLabelId, 10, 64)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
}
|
||||
|
||||
_, err = labelDao.GetLabelFirstById(secondLabelId)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
// 验证数量
|
||||
maps := make(map[string]interface{})
|
||||
maps["question_type"] = item.QuestionType
|
||||
@@ -346,16 +450,6 @@ func (r *QuestionQaService) PutQuestionQa(qaId int64, req requests.PutQuestionQa
|
||||
if item.SecondLabelId != "" {
|
||||
maps["second_label_id"] = item.SecondLabelId
|
||||
}
|
||||
total, err := questionDao.GetQuestionCount(maps)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
}
|
||||
|
||||
if total < int64(item.Quantity) {
|
||||
tx.Rollback()
|
||||
return false, errors.New("选题超出数量")
|
||||
}
|
||||
|
||||
// 获取随机明细题目
|
||||
questions, err := questionDao.GetQuestionListRand(maps, item.Quantity)
|
||||
@@ -364,6 +458,11 @@ func (r *QuestionQaService) PutQuestionQa(qaId int64, req requests.PutQuestionQa
|
||||
return false, err
|
||||
}
|
||||
|
||||
if len(questions) < item.Quantity {
|
||||
tx.Rollback()
|
||||
return false, errors.New("选题超出现有题目数量")
|
||||
}
|
||||
|
||||
for _, question := range questions {
|
||||
questionQaItem := &model.QuestionQaItem{
|
||||
QaId: questionQa.QaId,
|
||||
@@ -376,6 +475,17 @@ func (r *QuestionQaService) PutQuestionQa(qaId int64, req requests.PutQuestionQa
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 处理明细选题规则
|
||||
questionQaItemContent, err := json.Marshal(req.QuestionQaItem)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
}
|
||||
|
||||
if string(questionQaItemContent) != questionQa.ItemContent {
|
||||
questionQaData["item_content"] = string(questionQaItemContent)
|
||||
}
|
||||
}
|
||||
|
||||
if len(questionQaData) > 0 {
|
||||
|
||||
Reference in New Issue
Block a user