修改了新增了题库类型2、3
This commit is contained in:
parent
e44601c30f
commit
b61ede1a03
@ -91,6 +91,39 @@ func (r *QuestionQa) AddQuestionQa(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// 判断题库类型
|
||||
if req.QaType == 2 || req.QaType == 3 {
|
||||
if req.QaDisplayType == 1 {
|
||||
responses.FailWithMessage("展示类型错误,该题库下只允许选择飞花令类型", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 判断飞花令题目数量规则
|
||||
if req.TokenQuestionContent == nil {
|
||||
responses.FailWithMessage("请填入飞花令题目数量规则", c)
|
||||
return
|
||||
}
|
||||
|
||||
for _, item := range req.TokenQuestionContent {
|
||||
if err := global.Validate.Struct(item); err != nil {
|
||||
responses.FailWithMessage(utils.Translate(err), c)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if req.TokenNum == nil {
|
||||
responses.FailWithMessage("请填入飞花令数量", c)
|
||||
return
|
||||
}
|
||||
|
||||
if req.QaType == 3 {
|
||||
if req.RoundNum == nil {
|
||||
responses.FailWithMessage("请填入轮次数量", c)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 业务处理
|
||||
questionQaService := service.QuestionQaService{}
|
||||
_, err := questionQaService.AddQuestionQa(req)
|
||||
|
||||
@ -11,8 +11,11 @@ import (
|
||||
type QuestionQaDto struct {
|
||||
QaId string `json:"qa_id"` // 主键id
|
||||
QaName string `json:"qa_name"` // 名称
|
||||
QaType int `json:"qa_type"` // 题库类型(1:常规模式 2:固定套题模式 3:多轮固定题型模式)
|
||||
QaQuantity int `json:"qa_quantity"` // 题目数量
|
||||
QaStatus int `json:"qa_status"` // 状态(1:正常 2:无效)
|
||||
TokenNum *int `json:"token_num"` // 飞花令数量(当题库类型为2、3时存在)
|
||||
RoundNum *int `json:"round_num"` // 轮次数量(当题库类型为3时存在)
|
||||
QaRuleContent string `json:"qa_rule_content"` // 规则解释
|
||||
QaDisplayType int `json:"qa_display_type"` // 展示类型(1:常规 2:飞花令)
|
||||
QaExpireTime *model.LocalTime `json:"qa_expire_time"` // 过期时间
|
||||
@ -20,8 +23,9 @@ type QuestionQaDto struct {
|
||||
QaPassword string `json:"qa_password"` // 分享密码
|
||||
OpenNumber int `json:"open_number"` // 打开的次数
|
||||
Image string `json:"image"` // 背景图
|
||||
ItemContent []ItemContentDto `json:"item_content"` // 明细选题规则(json)
|
||||
ItemContent []ItemContentDto `json:"item_content"` // 明细选题(json)
|
||||
QuestionQaToken []*QuestionQaTokenDto `json:"base_token_item"` // 飞花令明细 展示类型为飞花令时存在
|
||||
TokenQuestionContent []*TokenQuestionContentDto `json:"token_question_content"` // 飞花令题目数量规则(当题库类型为2、3时存在。2表示飞花令后固定题目数量,3表示飞花令后单个类型题目数量)
|
||||
CreatedAt model.LocalTime `json:"created_at"` // 创建时间
|
||||
UpdatedAt model.LocalTime `json:"updated_at"` // 更新时间
|
||||
}
|
||||
@ -40,6 +44,15 @@ type ItemContentDto struct {
|
||||
Quantity int `json:"quantity"` // 数量
|
||||
}
|
||||
|
||||
// TokenQuestionContentDto 问答题库-飞花令题目数量规则
|
||||
type TokenQuestionContentDto struct {
|
||||
QuestionType int `json:"question_type"` // 题目类型(1:单选 2:多选 3:问答 4:判断)
|
||||
FirstLabelId string `json:"first_label_id"` // 一级标签id
|
||||
SecondLabelId string `json:"second_label_id"` // 二级标签id
|
||||
Difficulty int `json:"difficulty"` // 难度(0:未知 1:低 2:中 3:高)
|
||||
Quantity int `json:"quantity"` // 数量
|
||||
}
|
||||
|
||||
// GetQuestionQaListDto 问答题库列表
|
||||
func GetQuestionQaListDto(m []*model.QuestionQa) []*QuestionQaDto {
|
||||
// 处理返回值
|
||||
@ -50,6 +63,7 @@ func GetQuestionQaListDto(m []*model.QuestionQa) []*QuestionQaDto {
|
||||
response := &QuestionQaDto{
|
||||
QaId: fmt.Sprintf("%d", v.QaId),
|
||||
QaName: v.QaName,
|
||||
QaType: v.QaType,
|
||||
QaQuantity: v.QaQuantity,
|
||||
QaStatus: v.QaStatus,
|
||||
QaRuleContent: v.QaRuleContent,
|
||||
@ -63,6 +77,16 @@ func GetQuestionQaListDto(m []*model.QuestionQa) []*QuestionQaDto {
|
||||
UpdatedAt: v.UpdatedAt,
|
||||
}
|
||||
|
||||
// 加载飞花令数量(当题库类型为2、3时存在)
|
||||
if v.TokenNum != nil {
|
||||
response = response.LoadTokenNum(v.TokenNum)
|
||||
}
|
||||
|
||||
// 加载轮次数量(当题库类型为3时存在)
|
||||
if v.RoundNum != nil {
|
||||
response = response.LoadRoundNum(v.RoundNum)
|
||||
}
|
||||
|
||||
// 将转换后的结构体添加到新切片中
|
||||
responses[i] = response
|
||||
}
|
||||
@ -110,3 +134,32 @@ func (r *QuestionQaDto) LoadQuestionQaToken(m []*model.QuestionQaToken) *Questio
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// LoadTokenNum 加载飞花令数量(当题库类型为2、3时存在)
|
||||
func (r *QuestionQaDto) LoadTokenNum(s *int) *QuestionQaDto {
|
||||
if s != nil {
|
||||
r.TokenNum = s
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// LoadRoundNum 加载轮次数量(当题库类型为3时存在)
|
||||
func (r *QuestionQaDto) LoadRoundNum(s *int) *QuestionQaDto {
|
||||
if s != nil {
|
||||
r.RoundNum = s
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// LoadQaQuestionQuantityContent 加载飞花令题目数量规则(当题库类型为2、3时存在。2表示飞花令后固定题目数量,3表示飞花令后单个类型题目数量)
|
||||
func (r *QuestionQaDto) LoadQaQuestionQuantityContent(s string) *QuestionQaDto {
|
||||
if s != "" {
|
||||
var tokenQuestionContentDto []*TokenQuestionContentDto
|
||||
|
||||
err := json.Unmarshal([]byte(s), &tokenQuestionContentDto)
|
||||
if err == nil {
|
||||
r.TokenQuestionContent = tokenQuestionContentDto
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
@ -10,8 +10,11 @@ import (
|
||||
type QuestionQa struct {
|
||||
QaId int64 `gorm:"column:qa_id;type:bigint(19);primary_key;comment:主键id" json:"qa_id"`
|
||||
QaName string `gorm:"column:qa_name;type:varchar(255);comment:名称" json:"qa_name"`
|
||||
QaType int `gorm:"column:qa_type;type:tinyint(1);default:1;comment:题库类型(1:常规模式 2:固定套题模式 3:多轮固定题型模式)" json:"qa_type"`
|
||||
QaQuantity int `gorm:"column:qa_quantity;type:int(5);default:0;comment:题目数量" json:"qa_quantity"`
|
||||
QaStatus int `gorm:"column:qa_status;type:int(1);default:1;comment:状态(1:正常 2:过期)" json:"qa_status"`
|
||||
TokenNum *int `gorm:"column:token_num;type:int(1);comment:飞花令数量(当题库类型为2、3时存在)" json:"token_num"`
|
||||
RoundNum *int `gorm:"column:round_num;type:int(11);comment:轮次数量(当题库类型为3时存在)" json:"round_num"`
|
||||
QaRuleContent string `gorm:"column:qa_rule_content;type:text;comment:规则解释" json:"qa_rule_content"`
|
||||
QaDisplayType int `gorm:"column:qa_display_type;type:tinyint(1);default:1;comment:展示类型(1:常规 2:飞花令)" json:"qa_display_type"`
|
||||
QaExpireTime LocalTime `gorm:"column:qa_expire_time;type:datetime;comment:过期时间" json:"qa_expire_time"`
|
||||
@ -19,7 +22,8 @@ type QuestionQa struct {
|
||||
QaPassword string `gorm:"column:qa_password;type:varchar(255);comment:分享密码" json:"qa_password"`
|
||||
OpenNumber int `gorm:"column:open_number;type:int(1);default:0;comment:打开的次数" json:"open_number"`
|
||||
Image string `gorm:"column:image;type:varchar(255);comment:背景图" json:"image"`
|
||||
ItemContent string `gorm:"column:item_content;type:text;comment:明细选题规则(json)" json:"item_content"`
|
||||
ItemContent string `gorm:"column:item_content;type:text;comment:明细选题(json)" json:"item_content"`
|
||||
TokenQuestionContent string `gorm:"column:token_question_content;type:text;comment:飞花令题目数量规则(当题库类型为2、3时存在。2表示飞花令后固定题目数量,3表示飞花令后单个类型题目数量)" json:"token_question_content"`
|
||||
Model
|
||||
}
|
||||
|
||||
|
||||
@ -15,6 +15,7 @@ type GetQuestionQaPage struct {
|
||||
PageSize int `json:"page_size" form:"page_size" label:"每页个数"`
|
||||
QaId string `json:"qa_id" form:"qa_id" label:"主键id"`
|
||||
QaName string `json:"qa_name" form:"qa_name" label:"名称"`
|
||||
QaType int `json:"qa_type" form:"qa_type" label:"题库类型" validate:"required,oneof=1 2 3"` // (1:常规模式 2:固定套题模式 3:多轮固定题型模式)
|
||||
QaStatus *int `json:"qa_status" form:"qa_status" label:"状态"` // (1:正常 2:过期)
|
||||
QaRuleContent string `json:"qa_rule_content" form:"qa_rule_content" label:"规则解释"`
|
||||
QaDisplayType *int `json:"qa_display_type" form:"qa_display_type" label:"展示类型"` // (1:常规 2:飞花令)
|
||||
@ -34,14 +35,18 @@ type GetQuestionQaPageOrder struct {
|
||||
// AddQuestionQa 新增问答题库
|
||||
type AddQuestionQa struct {
|
||||
QaName string `json:"qa_name" form:"qa_name" label:"名称" validate:"required"`
|
||||
QaType int `json:"qa_type" form:"qa_type" label:"题库类型" validate:"required,oneof=1 2 3"` // (1:常规模式 2:固定套题模式 3:多轮固定题型模式)
|
||||
QaRuleContent string `json:"qa_rule_content" form:"qa_rule_content" label:"规则解释" validate:"required"`
|
||||
QaQuantity int `json:"qa_quantity" form:"qa_quantity" label:"题目数量" validate:"required,number,min=1"`
|
||||
TokenNum *int `json:"token_num" form:"token_num" label:"飞花令数量" validate:"omitempty,number,min=1"` // (当题库类型为2、3时存在)
|
||||
RoundNum *int `json:"round_num" form:"round_num" label:"轮次数量" validate:"omitempty,number,min=1"` // (当题库类型为3时存在)
|
||||
QaDisplayType int `json:"qa_display_type" form:"qa_display_type" label:"展示类型" validate:"required,oneof=1 2"` // (1:常规 2:飞花令)
|
||||
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 []QuestionQaItem `json:"question_qa_item" form:"question_qa_item" label:"题目明细" validate:"required"`
|
||||
QuestionQaItem []*QuestionQaItem `json:"question_qa_item" form:"question_qa_item" label:"题目明细" validate:"required"`
|
||||
BaseTokenItem []AddQuestionQaBaseTokenItem `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表示飞花令后单个类型题目数量)
|
||||
}
|
||||
|
||||
// QuestionQaItem 新增问答题库-题目明细
|
||||
@ -53,6 +58,12 @@ type QuestionQaItem struct {
|
||||
Quantity int `json:"quantity" form:"quantity" validate:"required,number,min=1" label:"数量"`
|
||||
}
|
||||
|
||||
// TokenQuestionContent 新增问答题库-飞花令题目数量规则
|
||||
type TokenQuestionContent struct {
|
||||
QuestionType int `json:"question_type" form:"question_type" validate:"required,number,oneof=1 2 3 4" label:"题目类型"` // 题目类型(1:单选 2:多选 3:问答 4:判断)
|
||||
Quantity int `json:"quantity" form:"quantity" validate:"number,min=0" label:"数量"`
|
||||
}
|
||||
|
||||
// AddQuestionQaBaseTokenItem 新增问答题库-飞花令明细
|
||||
type AddQuestionQaBaseTokenItem struct {
|
||||
TokenId string `json:"token_id" form:"token_id" label:"飞花令明细id" validate:"required"`
|
||||
@ -61,15 +72,19 @@ type AddQuestionQaBaseTokenItem struct {
|
||||
// PutQuestionQa 修改问答题库
|
||||
type PutQuestionQa struct {
|
||||
QaName string `json:"qa_name" form:"qa_name" label:"名称" validate:"required"`
|
||||
QaType int `json:"qa_type" form:"qa_type" label:"题库类型" validate:"required,oneof=1 2 3"` // (1:常规模式 2:固定套题模式 3:多轮固定题型模式)
|
||||
QaRuleContent string `json:"qa_rule_content" form:"qa_rule_content" label:"规则解释" validate:"required"`
|
||||
QaQuantity int `json:"qa_quantity" form:"qa_quantity" label:"题目数量" validate:"required,number,min=1"`
|
||||
TokenNum *int `json:"token_num" form:"token_num" label:"飞花令数量" validate:"omitempty,number,min=1"` // (当题库类型为2、3时存在)
|
||||
RoundNum *int `json:"round_num" form:"round_num" label:"轮次数量" validate:"omitempty,number,min=1"` // (当题库类型为3时存在)
|
||||
QaDisplayType int `json:"qa_display_type" form:"qa_display_type" label:"展示类型" validate:"required,oneof=1 2"` // (1:常规 2:飞花令)
|
||||
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 []*PutQuestionQaItem `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表示飞花令后单个类型题目数量)
|
||||
}
|
||||
|
||||
// PutQuestionQaItem 修改问答题库-明细
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -3,7 +3,6 @@ package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -38,10 +37,8 @@ func GetCardAge(cardNum string) (int, error) {
|
||||
|
||||
// CheckCardNum 检测身份证号
|
||||
func CheckCardNum(cardNum string) (bool, error) {
|
||||
fmt.Println(cardNum)
|
||||
regex := `^(?:1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5])\d{4}(?:1[89]|20)\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}[\dxX]$`
|
||||
match, err := regexp.MatchString(regex, cardNum)
|
||||
fmt.Println(match)
|
||||
if !match || err != nil {
|
||||
return false, errors.New("身份证号错误")
|
||||
}
|
||||
|
||||
@ -1,3 +1,16 @@
|
||||
package utils
|
||||
|
||||
// int转字符串
|
||||
// QuestionType int转字符串
|
||||
func QuestionType(t int) string {
|
||||
if t == 1 {
|
||||
return "单选题"
|
||||
} else if t == 2 {
|
||||
return "多选题"
|
||||
} else if t == 2 {
|
||||
return "问答题"
|
||||
} else if t == 2 {
|
||||
return "判断题"
|
||||
} else {
|
||||
return "未知"
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user