130 lines
3.2 KiB
Go
130 lines
3.2 KiB
Go
package controller
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"knowledge/api/dao"
|
|
"knowledge/api/dto"
|
|
"knowledge/api/requests"
|
|
"knowledge/api/responses"
|
|
"knowledge/global"
|
|
"knowledge/utils"
|
|
"sort"
|
|
"time"
|
|
)
|
|
|
|
type Share struct{}
|
|
|
|
// GetShare 获取分享数据
|
|
func (r *Share) GetShare(c *gin.Context) {
|
|
shareRequest := requests.ShareRequest{}
|
|
req := shareRequest.GetShare
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 参数验证
|
|
if err := global.Validate.Struct(req); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
|
|
// 业务处理
|
|
questionQaDao := dao.QuestionQaDao{}
|
|
questionQaItemDao := dao.QuestionQaItemDao{}
|
|
questionDao := dao.QuestionDao{}
|
|
|
|
// 获取题库数据
|
|
maps := make(map[string]interface{})
|
|
maps["qa_share_id"] = req.QaShareId
|
|
questionQa, err := questionQaDao.GetQuestionQa(maps)
|
|
if err != nil || questionQa == nil {
|
|
responses.FailWithMessage("题库不存在", c)
|
|
return
|
|
}
|
|
|
|
if questionQa.QaStatus == 2 {
|
|
responses.FailWithMessage("题库已失效", c)
|
|
return
|
|
}
|
|
|
|
if questionQa.QaPassword != req.QaPassword {
|
|
responses.FailWithMessage("密码错误", c)
|
|
return
|
|
}
|
|
|
|
// 检测过期时间
|
|
now := time.Now()
|
|
qaExpireTime := time.Time(questionQa.QaExpireTime)
|
|
if qaExpireTime.Before(now) {
|
|
responses.FailWithMessage("题库已失效", c)
|
|
return
|
|
}
|
|
|
|
g := &dto.ShareDto{}
|
|
|
|
// 加载题库数据
|
|
g.QuestionQa = dto.GetQuestionQaDto(questionQa)
|
|
|
|
// 题目数据-必备选中
|
|
maps = make(map[string]interface{})
|
|
maps["qa_id"] = questionQa.QaId
|
|
maps["is_must_select"] = 1
|
|
questionQaItems, err := questionQaItemDao.GetQuestionQaItemList(maps)
|
|
if err == nil && len(questionQaItems) > 0 {
|
|
shareQuestionDtos := make([]*dto.ShareQuestionDto, len(questionQaItems))
|
|
|
|
for i, item := range questionQaItems {
|
|
// 获取题目数据
|
|
question, err := questionDao.GetQuestionById(item.QuestionId)
|
|
if err != nil {
|
|
responses.FailWithMessage("题目错误", c)
|
|
return
|
|
}
|
|
|
|
shareQuestionDto := dto.GetShareQuestionDto(question)
|
|
|
|
shareQuestionDto.IsMustSelect = item.IsMustSelect
|
|
|
|
// 将转换后的结构体添加到新切片中
|
|
shareQuestionDtos[i] = shareQuestionDto
|
|
}
|
|
|
|
g.Question = shareQuestionDtos
|
|
}
|
|
|
|
// 题目数据-剩余随机数量
|
|
remainingQuantity := questionQa.QaQuantity - len(questionQaItems)
|
|
if remainingQuantity > 0 {
|
|
// 随机获取剩余题目
|
|
maps = make(map[string]interface{})
|
|
maps["qa_id"] = questionQa.QaId
|
|
maps["is_must_select"] = 0
|
|
questionQaItems, err = questionQaItemDao.GetQuestionQaItemListRand(maps, remainingQuantity)
|
|
if err == nil && len(questionQaItems) > 0 {
|
|
for _, item := range questionQaItems {
|
|
// 获取题目数据
|
|
question, err := questionDao.GetQuestionById(item.QuestionId)
|
|
if err != nil {
|
|
responses.FailWithMessage("题目错误", c)
|
|
return
|
|
}
|
|
|
|
shareQuestionDto := dto.GetShareQuestionDto(question)
|
|
|
|
shareQuestionDto.IsMustSelect = item.IsMustSelect
|
|
|
|
// 将转换后的结构体添加到新切片中
|
|
g.Question = append(g.Question, shareQuestionDto)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 按照难度重新排序题目顺序
|
|
sort.SliceStable(g.Question, func(i, j int) bool {
|
|
return g.Question[i].Difficulty < g.Question[j].Difficulty
|
|
})
|
|
|
|
responses.OkWithData(g, c)
|
|
}
|