From 60a74297b309ca77eaf09217c72a791fa0120fd0 Mon Sep 17 00:00:00 2001 From: wucongxing8150 <815046773@qq.com> Date: Thu, 21 Nov 2024 11:25:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E9=80=89=E9=A2=98?= =?UTF-8?q?=E8=A7=84=E5=88=99=E7=9A=84=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/dao/Question.go | 37 +++++++++++---------- api/service/QuestionQa.go | 68 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 85 insertions(+), 20 deletions(-) diff --git a/api/dao/Question.go b/api/dao/Question.go index 6a99a2f..fc919ac 100644 --- a/api/dao/Question.go +++ b/api/dao/Question.go @@ -252,22 +252,21 @@ func (r *QuestionDao) GetQuestionCountSearch(req requests.GetQuestionCount) (tot return totalRecords, nil } -// -//// GetQuestionNotInListRand 获取列表-随机-排除 -//func (r *QuestionDao) GetQuestionNotInListRand(maps interface{}, notQuestionId []int64, limit int) (m []*model.Question, err error) { -// query := global.Db.Model(&model.Question{}) -// query = query.Where(maps) -// -// // 排除选项 -// if len(notQuestionId) > 0 { -// query = query.Where("question_id not in (?)", notQuestionId) -// } -// -// query = query.Limit(limit).Order("rand()") -// -// err = query.Find(&m).Error -// if err != nil { -// return nil, err -// } -// return m, nil -//} +// GetQuestionNotInListRand 获取列表-随机-排除 +func (r *QuestionDao) GetQuestionNotInListRand(maps interface{}, notQuestionId []int64, limit int) (m []*model.Question, err error) { + query := global.Db.Model(&model.Question{}) + query = query.Where(maps) + + // 排除选项 + if len(notQuestionId) > 0 { + query = query.Where("question_id not in (?)", notQuestionId) + } + + query = query.Limit(limit).Order("rand()") + + err = query.Find(&m).Error + if err != nil { + return nil, err + } + return m, nil +} diff --git a/api/service/QuestionQa.go b/api/service/QuestionQa.go index 8c8fa94..efee227 100644 --- a/api/service/QuestionQa.go +++ b/api/service/QuestionQa.go @@ -205,6 +205,13 @@ func (r *QuestionQaService) AddQuestionQa(req requests.AddQuestionQa) (bool, err // 新增问答题库题目列表 questionDao := dao.QuestionDao{} questionQaItemDao := dao.QuestionQaItemDao{} + + // 已入题库题目id + var isInQaQuestionId []int64 + + // 已入题库题目数量 + var isInQaQuestionQuantity int + for _, item := range req.QuestionQaItem { // 验证数量 maps := make(map[string]interface{}) @@ -221,7 +228,7 @@ func (r *QuestionQaService) AddQuestionQa(req requests.AddQuestionQa) (bool, err } // 获取随机明细题目 - questions, err := questionDao.GetQuestionListRand(maps, item.Quantity) + questions, err := questionDao.GetQuestionNotInListRand(maps, isInQaQuestionId, item.Quantity) if err != nil { tx.Rollback() return false, err @@ -243,9 +250,22 @@ func (r *QuestionQaService) AddQuestionQa(req requests.AddQuestionQa) (bool, err tx.Rollback() return false, errors.New("新增失败") } + + // 新增到已入题库题目id + isInQaQuestionId = append(isInQaQuestionId, question.QuestionId) + + // 新增到 + isInQaQuestionQuantity += 1 } } + // 检测题库已增加题目数量-新增 + err = r.CheckAddedQaQuestionQuantity(req, isInQaQuestionQuantity) + if err != nil { + tx.Rollback() + return false, err + } + // 新增计时设置 if req.IsTurnTimer == 1 { questionQaTimerDao := dao.QuestionQaTimerDao{} @@ -786,6 +806,52 @@ func (r *QuestionQaService) CheckAddQaQuestionQuantity(req requests.AddQuestionQ return qaQuantity, nil } +// CheckAddedQaQuestionQuantity 检测题库已增加题目数量-新增 +func (r *QuestionQaService) CheckAddedQaQuestionQuantity(req requests.AddQuestionQa, itemQuantity int) (err error) { + // 题目总数量 + qaQuantity := req.QaQuantity + + // 常规模式 + if req.QaType == 1 { + // 总数量 需大于 选择的明细数量 + if qaQuantity > itemQuantity { + return 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 errors.New("存在重复选题,经计算可选择数量不满足题库所需题目总数量") + } + } + + // 多轮固定题型模式 + 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 errors.New("存在重复选题,经计算可选择数量不满足题库所需题目总数量") + } + } + + return nil +} + // CheckPutQaQuestionQuantity 检测题库题目数量-修改 func (r *QuestionQaService) CheckPutQaQuestionQuantity(req requests.PutQuestionQa) (qaQuantity int, err error) { // 题目总数量