324 lines
7.3 KiB
Go
324 lines
7.3 KiB
Go
package controller
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"knowledge/api/dao"
|
|
"knowledge/api/dto"
|
|
"knowledge/api/requests"
|
|
"knowledge/api/responses"
|
|
"knowledge/api/service"
|
|
"knowledge/global"
|
|
"knowledge/utils"
|
|
"strconv"
|
|
)
|
|
|
|
type QuestionQa struct{}
|
|
|
|
// GetQuestionQaPage 获取问答题库列表-分页
|
|
func (r *QuestionQa) GetQuestionQaPage(c *gin.Context) {
|
|
questionQaRequest := requests.QuestionQaRequest{}
|
|
req := questionQaRequest.GetQuestionQaPage
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 参数验证
|
|
if err := global.Validate.Struct(req); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
|
|
if req.Page == 0 {
|
|
req.Page = 1
|
|
}
|
|
|
|
if req.PageSize == 0 {
|
|
req.PageSize = 20
|
|
}
|
|
|
|
questionQaDao := dao.QuestionQaDao{}
|
|
questionQa, total, err := questionQaDao.GetQuestionQaPageSearch(req, req.Page, req.PageSize)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 处理返回值
|
|
g := dto.GetQuestionQaListDto(questionQa)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
result := make(map[string]interface{})
|
|
result["page"] = req.Page
|
|
result["page_size"] = req.PageSize
|
|
result["total"] = total
|
|
result["data"] = g
|
|
responses.OkWithData(result, c)
|
|
}
|
|
|
|
// AddQuestionQa 新增问答题库
|
|
func (r *QuestionQa) AddQuestionQa(c *gin.Context) {
|
|
questionQaRequest := requests.QuestionQaRequest{}
|
|
req := questionQaRequest.AddQuestionQa
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 参数验证
|
|
if err := global.Validate.Struct(req); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
|
|
// 参数验证
|
|
for _, item := range req.QuestionQaItem {
|
|
if err := global.Validate.Struct(item); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
}
|
|
|
|
if req.BaseTokenItem != nil && req.QaDisplayType == 2 {
|
|
for _, item := range req.BaseTokenItem {
|
|
if err := global.Validate.Struct(item); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// 业务处理
|
|
questionQaService := service.QuestionQaService{}
|
|
_, err := questionQaService.AddQuestionQa(req)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
responses.Ok(c)
|
|
}
|
|
|
|
// PutQuestionQa 修改问答题库
|
|
func (r *QuestionQa) PutQuestionQa(c *gin.Context) {
|
|
questionQaRequest := requests.QuestionQaRequest{}
|
|
req := questionQaRequest.PutQuestionQa
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 参数验证
|
|
if err := global.Validate.Struct(req); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
|
|
// 参数验证
|
|
for _, item := range req.QuestionQaItem {
|
|
if err := global.Validate.Struct(item); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
}
|
|
|
|
if req.BaseTokenItem != nil && req.QaDisplayType == 2 {
|
|
for _, item := range req.BaseTokenItem {
|
|
if err := global.Validate.Struct(item); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
id := c.Param("qa_id")
|
|
if id == "" {
|
|
responses.FailWithMessage("缺少参数", c)
|
|
return
|
|
}
|
|
|
|
// 将 id 转换为 int64 类型
|
|
qaId, err := strconv.ParseInt(id, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
// 业务处理
|
|
questionQaService := service.QuestionQaService{}
|
|
_, err = questionQaService.PutQuestionQa(qaId, req)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
responses.Ok(c)
|
|
}
|
|
|
|
// PutQuestionQaPassword 修改问答题库密码
|
|
func (r *QuestionQa) PutQuestionQaPassword(c *gin.Context) {
|
|
questionQaRequest := requests.QuestionQaRequest{}
|
|
req := questionQaRequest.PutQuestionQaPassword
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 参数验证
|
|
if err := global.Validate.Struct(req); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
|
|
id := c.Param("qa_id")
|
|
if id == "" {
|
|
responses.FailWithMessage("缺少参数", c)
|
|
return
|
|
}
|
|
|
|
// 将 id 转换为 int64 类型
|
|
qaId, err := strconv.ParseInt(id, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
// 业务处理
|
|
questionQaService := service.QuestionQaService{}
|
|
_, err = questionQaService.PutQuestionQaPassword(qaId, req)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
responses.Ok(c)
|
|
}
|
|
|
|
// PutQuestionQaExpire 修改问答题库有效期
|
|
func (r *QuestionQa) PutQuestionQaExpire(c *gin.Context) {
|
|
questionQaRequest := requests.QuestionQaRequest{}
|
|
req := questionQaRequest.PutQuestionQaExpire
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 参数验证
|
|
if err := global.Validate.Struct(req); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
|
|
id := c.Param("qa_id")
|
|
if id == "" {
|
|
responses.FailWithMessage("缺少参数", c)
|
|
return
|
|
}
|
|
|
|
// 将 id 转换为 int64 类型
|
|
qaId, err := strconv.ParseInt(id, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
// 业务处理
|
|
questionQaService := service.QuestionQaService{}
|
|
_, err = questionQaService.PutQuestionQaExpire(qaId, req)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
responses.Ok(c)
|
|
}
|
|
|
|
// PutQuestionQaRule 修改问答题库规则解释
|
|
func (r *QuestionQa) PutQuestionQaRule(c *gin.Context) {
|
|
questionQaRequest := requests.QuestionQaRequest{}
|
|
req := questionQaRequest.PutQuestionQaRule
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 参数验证
|
|
if err := global.Validate.Struct(req); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
|
|
id := c.Param("qa_id")
|
|
if id == "" {
|
|
responses.FailWithMessage("缺少参数", c)
|
|
return
|
|
}
|
|
|
|
// 将 id 转换为 int64 类型
|
|
qaId, err := strconv.ParseInt(id, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
// 业务处理
|
|
questionQaService := service.QuestionQaService{}
|
|
_, err = questionQaService.PutQuestionQaRule(qaId, req)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
responses.Ok(c)
|
|
}
|
|
|
|
// GetQuestionQa 获取问答题库详情
|
|
func (r *QuestionQa) GetQuestionQa(c *gin.Context) {
|
|
id := c.Param("qa_id")
|
|
if id == "" {
|
|
responses.FailWithMessage("缺少参数", c)
|
|
return
|
|
}
|
|
|
|
// 将 id 转换为 int64 类型
|
|
qaId, err := strconv.ParseInt(id, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
// 获取问答题库详情数据
|
|
questionQaDao := dao.QuestionQaDao{}
|
|
questionQa, err := questionQaDao.GetQuestionQaById(qaId)
|
|
if err != nil {
|
|
responses.FailWithMessage("题库不存在", c)
|
|
return
|
|
}
|
|
|
|
// 处理返回值
|
|
g := dto.GetQuestionQaDto(questionQa)
|
|
|
|
// 加载明细选题规则
|
|
g = g.LoadItemContent(questionQa.ItemContent)
|
|
|
|
// 获取题库飞花令数据
|
|
if questionQa.QaDisplayType == 2 {
|
|
questionQaTokenDao := dao.QuestionQaTokenDao{}
|
|
maps := make(map[string]interface{})
|
|
maps["qa_id"] = questionQa.QaId
|
|
questionQaTokens, err := questionQaTokenDao.GetQuestionQaTokenPreloadList(maps)
|
|
if err != nil {
|
|
responses.FailWithMessage("题库不存在", c)
|
|
return
|
|
}
|
|
|
|
// 加载飞花令数据
|
|
g = g.LoadQuestionQaToken(questionQaTokens)
|
|
}
|
|
|
|
responses.OkWithData(g, c)
|
|
}
|