285 lines
6.3 KiB
Go
285 lines
6.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
|
|
}
|
|
|
|
// 处理返回值
|
|
GetQuestionQaPageResponses := 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"] = GetQuestionQaPageResponses
|
|
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.Item {
|
|
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.Item {
|
|
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
|
|
}
|
|
|
|
// 业务处理
|
|
questionQaService := service.QuestionQaService{}
|
|
getQuestionQaResponses, err := questionQaService.GetQuestionQa(qaId)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
responses.OkWithData(getQuestionQaResponses, c)
|
|
}
|