566 lines
13 KiB
Go
566 lines
13 KiB
Go
package controller
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"hepa-calc-admin-api/api/dao"
|
|
"hepa-calc-admin-api/api/dto"
|
|
"hepa-calc-admin-api/api/model"
|
|
"hepa-calc-admin-api/api/requests"
|
|
"hepa-calc-admin-api/api/responses"
|
|
"hepa-calc-admin-api/api/service"
|
|
"hepa-calc-admin-api/global"
|
|
"hepa-calc-admin-api/utils"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type Question struct{}
|
|
|
|
// GetQuestionPage 获取问题列表-分页
|
|
func (b *Question) GetQuestionPage(c *gin.Context) {
|
|
questionRequest := requests.QuestionRequest{}
|
|
req := questionRequest.GetQuestionPage
|
|
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
|
|
}
|
|
|
|
// 获取数据
|
|
questionDao := dao.QuestionDao{}
|
|
questions, total, err := questionDao.GetQuestionPageSearch(req, req.Page, req.PageSize)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 处理返回值
|
|
g := dto.GetQuestionListDto(questions)
|
|
|
|
result := make(map[string]interface{})
|
|
result["page"] = req.Page
|
|
result["page_size"] = req.PageSize
|
|
result["total"] = total
|
|
result["data"] = g
|
|
responses.OkWithData(result, c)
|
|
}
|
|
|
|
// GetQuestionList 获取问题列表
|
|
func (b *Question) GetQuestionList(c *gin.Context) {
|
|
questionRequest := requests.QuestionRequest{}
|
|
req := questionRequest.GetQuestionList
|
|
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
|
|
}
|
|
|
|
// 获取数据
|
|
questionDao := dao.QuestionDao{}
|
|
questions, err := questionDao.GetQuestionListSearch(req)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 处理返回值
|
|
g := dto.GetQuestionListDto(questions)
|
|
|
|
responses.OkWithData(g, c)
|
|
}
|
|
|
|
// GetQuestion 获取问题详情
|
|
func (b *Question) GetQuestion(c *gin.Context) {
|
|
id := c.Param("question_id")
|
|
if id == "" {
|
|
responses.FailWithMessage("缺少参数", c)
|
|
return
|
|
}
|
|
|
|
// 将 id 转换为 int64 类型
|
|
questionId, err := strconv.ParseInt(id, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
// 获取问题数据
|
|
questionDao := dao.QuestionDao{}
|
|
question, err := questionDao.GetQuestionPreloadById(questionId)
|
|
if err != nil {
|
|
responses.FailWithMessage("分类异常", c)
|
|
return
|
|
}
|
|
|
|
// 处理返回值
|
|
g := dto.GetQuestionDto(question)
|
|
|
|
// 获取关联分类数据
|
|
questionService := service.QuestionService{}
|
|
g.BaseClass, err = questionService.GetQuestionBaseClass(question.QuestionId)
|
|
|
|
responses.OkWithData(g, c)
|
|
}
|
|
|
|
// PutQuestion 修改问题
|
|
func (b *Question) PutQuestion(c *gin.Context) {
|
|
questionRequest := requests.QuestionRequest{}
|
|
req := questionRequest.PutQuestion
|
|
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.DiscountPrice != nil {
|
|
if req.DiscountEndTime == nil {
|
|
responses.FailWithMessage("请填写优惠过期时间", c)
|
|
return
|
|
}
|
|
|
|
if *req.Price < *req.DiscountPrice {
|
|
responses.FailWithMessage("优惠价格不可超过原价", c)
|
|
return
|
|
}
|
|
}
|
|
|
|
id := c.Param("question_id")
|
|
if id == "" {
|
|
responses.FailWithMessage("缺少参数", c)
|
|
return
|
|
}
|
|
|
|
// 将 id 转换为 int64 类型
|
|
questionId, err := strconv.ParseInt(id, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
// 获取问题数据
|
|
questionDao := dao.QuestionDao{}
|
|
question, err := questionDao.GetQuestionById(questionId)
|
|
if err != nil {
|
|
responses.FailWithMessage("问题异常", c)
|
|
return
|
|
}
|
|
|
|
// 修改值
|
|
questionData := make(map[string]interface{})
|
|
|
|
// 标题
|
|
if req.QuestionTitle != question.QuestionTitle {
|
|
questionData["question_title"] = req.QuestionTitle
|
|
}
|
|
|
|
// 副标题
|
|
if req.QuestionSubtitle != question.QuestionSubtitle {
|
|
questionData["question_subtitle"] = req.QuestionSubtitle
|
|
}
|
|
|
|
// 唯一标识
|
|
if req.QuestionIden != question.QuestionIden {
|
|
questionData["question_iden"] = req.QuestionIden
|
|
}
|
|
|
|
// 问题状态
|
|
if req.QuestionStatus != question.QuestionStatus {
|
|
questionData["question_status"] = req.QuestionStatus
|
|
}
|
|
|
|
// 是否隐藏
|
|
if *req.IsHide != question.IsHide {
|
|
questionData["is_hide"] = req.IsHide
|
|
}
|
|
|
|
// 是否推荐
|
|
if *req.IsRecommend != question.IsRecommend {
|
|
questionData["is_recommend"] = req.IsRecommend
|
|
}
|
|
|
|
// 点击次数
|
|
if *req.ClickCount != question.ClickCount {
|
|
questionData["click_count"] = req.ClickCount
|
|
}
|
|
|
|
// 提交次数
|
|
if *req.SubmitCount != question.SubmitCount {
|
|
questionData["submit_count"] = req.SubmitCount
|
|
}
|
|
|
|
// 支付次数
|
|
if *req.PayCount != question.PayCount {
|
|
questionData["pay_count"] = req.PayCount
|
|
}
|
|
|
|
// 价格
|
|
if *req.Price != question.Price {
|
|
questionData["price"] = req.Price
|
|
}
|
|
|
|
// 优惠价格
|
|
if req.DiscountPrice != nil {
|
|
if req.DiscountEndTime == nil {
|
|
responses.FailWithMessage("请填写优惠过期时间", c)
|
|
return
|
|
}
|
|
if question.DiscountPrice == nil {
|
|
questionData["discount_price"] = req.DiscountPrice
|
|
} else {
|
|
if *req.DiscountPrice != *question.DiscountPrice {
|
|
questionData["discount_price"] = req.DiscountPrice
|
|
}
|
|
}
|
|
} else {
|
|
if question.DiscountPrice != nil {
|
|
questionData["discount_price"] = nil
|
|
}
|
|
}
|
|
|
|
// 优惠截止时间
|
|
if req.DiscountEndTime != nil {
|
|
// 获取本地时区
|
|
location, err := time.LoadLocation("Local")
|
|
if err != nil {
|
|
responses.FailWithMessage("优惠截止时间错误", c)
|
|
return
|
|
}
|
|
|
|
discountEndTime, err := time.ParseInLocation("2006-01-02 15:04:05", *req.DiscountEndTime, location)
|
|
if err != nil {
|
|
responses.FailWithMessage("优惠截止时间错误", c)
|
|
return
|
|
}
|
|
|
|
if question.DiscountEndTime == nil {
|
|
questionData["discount_end_time"] = discountEndTime
|
|
} else {
|
|
if *question.DiscountEndTime != discountEndTime {
|
|
questionData["discount_end_time"] = discountEndTime
|
|
}
|
|
}
|
|
} else {
|
|
if question.DiscountEndTime != nil {
|
|
questionData["discount_end_time"] = nil
|
|
}
|
|
}
|
|
|
|
// 问题介绍
|
|
if req.QuestionBrief != question.QuestionBrief {
|
|
questionData["question_brief"] = req.QuestionBrief
|
|
}
|
|
|
|
// 问题解释/科普
|
|
if req.QuestionExplain != question.QuestionExplain {
|
|
questionData["question_explain"] = req.QuestionExplain
|
|
}
|
|
|
|
// 开始事务
|
|
tx := global.Db.Begin()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
|
|
if len(questionData) > 0 {
|
|
err = questionDao.EditQuestionById(tx, question.QuestionId, questionData)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
responses.FailWithMessage("操作失败", c)
|
|
return
|
|
}
|
|
}
|
|
|
|
// 删除问题关联分类
|
|
questionClassDao := dao.QuestionClassDao{}
|
|
err = questionClassDao.DeleteQuestionClassByQuestionId(tx, questionId)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
responses.FailWithMessage("操作失败", c)
|
|
return
|
|
}
|
|
|
|
// 新增问题关联分类
|
|
for _, s := range req.ClassId {
|
|
// 将 id 转换为 int64 类型
|
|
classId, err := strconv.ParseInt(*s, 10, 64)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
questionClass := &model.QuestionClass{
|
|
QuestionId: questionId,
|
|
ClassId: classId,
|
|
}
|
|
|
|
questionClass, err = questionClassDao.AddQuestionClass(tx, questionClass)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
}
|
|
|
|
tx.Commit()
|
|
responses.Ok(c)
|
|
}
|
|
|
|
// AddQuestion 新增问题
|
|
func (b *Question) AddQuestion(c *gin.Context) {
|
|
questionRequest := requests.QuestionRequest{}
|
|
req := questionRequest.AddQuestion
|
|
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.DiscountPrice != nil {
|
|
if *req.Price < *req.DiscountPrice {
|
|
responses.FailWithMessage("优惠价格不可超过原价", c)
|
|
return
|
|
}
|
|
|
|
if req.DiscountEndTime == nil {
|
|
responses.FailWithMessage("请填写优惠过期时间", c)
|
|
return
|
|
}
|
|
}
|
|
|
|
// 开始事务
|
|
tx := global.Db.Begin()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
|
|
question := &model.Question{
|
|
QuestionTitle: req.QuestionTitle,
|
|
QuestionSubtitle: req.QuestionSubtitle,
|
|
QuestionIden: req.QuestionIden,
|
|
QuestionStatus: req.QuestionStatus,
|
|
IsHide: *req.IsHide,
|
|
IsRecommend: *req.IsRecommend,
|
|
ClickCount: *req.ClickCount,
|
|
SubmitCount: *req.SubmitCount,
|
|
PayCount: *req.PayCount,
|
|
Price: *req.Price,
|
|
DiscountPrice: req.DiscountPrice,
|
|
QuestionBrief: req.QuestionBrief,
|
|
QuestionExplain: req.QuestionExplain,
|
|
}
|
|
|
|
// 处理优惠截止时间
|
|
if req.DiscountEndTime != nil {
|
|
// 获取本地时区
|
|
location, err := time.LoadLocation("Local")
|
|
if err != nil {
|
|
tx.Rollback()
|
|
responses.FailWithMessage("优惠截止时间错误", c)
|
|
return
|
|
}
|
|
|
|
discountEndTime, err := time.ParseInLocation("2006-01-02 15:04:05", *req.DiscountEndTime, location)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
responses.FailWithMessage("优惠截止时间错误", c)
|
|
return
|
|
}
|
|
|
|
question.DiscountEndTime = &discountEndTime
|
|
}
|
|
|
|
questionDao := dao.QuestionDao{}
|
|
question, err := questionDao.AddQuestion(tx, question)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
responses.FailWithMessage("操作失败", c)
|
|
return
|
|
}
|
|
|
|
// 新增问题关联分类
|
|
questionClassDao := dao.QuestionClassDao{}
|
|
for _, s := range req.ClassId {
|
|
// 将 id 转换为 int64 类型
|
|
classId, err := strconv.ParseInt(*s, 10, 64)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
questionClass := &model.QuestionClass{
|
|
QuestionId: question.QuestionId,
|
|
ClassId: classId,
|
|
}
|
|
|
|
questionClass, err = questionClassDao.AddQuestionClass(tx, questionClass)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
}
|
|
|
|
tx.Commit()
|
|
responses.Ok(c)
|
|
}
|
|
|
|
// PutQuestionStatus 操作问题发布状态
|
|
func (b *Question) PutQuestionStatus(c *gin.Context) {
|
|
questionRequest := requests.QuestionRequest{}
|
|
req := questionRequest.PutQuestionStatus
|
|
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
|
|
}
|
|
|
|
id := c.Param("question_id")
|
|
if id == "" {
|
|
responses.FailWithMessage("缺少参数", c)
|
|
return
|
|
}
|
|
|
|
// 将 id 转换为 int64 类型
|
|
questionId, err := strconv.ParseInt(id, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
// 获取基础分类数据
|
|
questionDao := dao.QuestionDao{}
|
|
question, err := questionDao.GetQuestionById(questionId)
|
|
if err != nil {
|
|
responses.FailWithMessage("题目异常", c)
|
|
return
|
|
}
|
|
|
|
// 检测状态
|
|
if question.QuestionStatus == req.QuestionStatus {
|
|
responses.Ok(c)
|
|
return
|
|
}
|
|
|
|
// 开始事务
|
|
tx := global.Db.Begin()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
|
|
questionData := make(map[string]interface{})
|
|
questionData["question_status"] = req.QuestionStatus
|
|
err = questionDao.EditQuestionById(tx, questionId, questionData)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
responses.FailWithMessage("操作失败", c)
|
|
return
|
|
}
|
|
|
|
tx.Commit()
|
|
responses.Ok(c)
|
|
}
|
|
|
|
// PutQuestionHideStatus 操作问题隐藏状态
|
|
func (b *Question) PutQuestionHideStatus(c *gin.Context) {
|
|
questionRequest := requests.QuestionRequest{}
|
|
req := questionRequest.PutQuestionHideStatus
|
|
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
|
|
}
|
|
|
|
id := c.Param("question_id")
|
|
if id == "" {
|
|
responses.FailWithMessage("缺少参数", c)
|
|
return
|
|
}
|
|
|
|
// 将 id 转换为 int64 类型
|
|
questionId, err := strconv.ParseInt(id, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
// 获取基础分类数据
|
|
questionDao := dao.QuestionDao{}
|
|
question, err := questionDao.GetQuestionById(questionId)
|
|
if err != nil {
|
|
responses.FailWithMessage("题目异常", c)
|
|
return
|
|
}
|
|
|
|
// 检测状态
|
|
if question.IsHide == req.IsHide {
|
|
responses.Ok(c)
|
|
return
|
|
}
|
|
|
|
// 开始事务
|
|
tx := global.Db.Begin()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
|
|
questionData := make(map[string]interface{})
|
|
questionData["is_hide"] = req.IsHide
|
|
err = questionDao.EditQuestionById(tx, questionId, questionData)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
responses.FailWithMessage("操作失败", c)
|
|
return
|
|
}
|
|
|
|
tx.Commit()
|
|
responses.Ok(c)
|
|
}
|