359 lines
7.9 KiB
Go
359 lines
7.9 KiB
Go
package controller
|
||
|
||
import (
|
||
"github.com/gin-gonic/gin"
|
||
"hepa-calc-api/api/dao"
|
||
"hepa-calc-api/api/dto"
|
||
"hepa-calc-api/api/requests"
|
||
"hepa-calc-api/api/responses"
|
||
"hepa-calc-api/api/service"
|
||
"hepa-calc-api/global"
|
||
"hepa-calc-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{}
|
||
question, total, err := questionDao.GetQuestionPageSearch(req, req.Page, req.PageSize)
|
||
if err != nil {
|
||
responses.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
|
||
// 处理返回值
|
||
g := dto.GetQuestionPageListDto(question)
|
||
|
||
result := make(map[string]interface{})
|
||
result["page"] = req.Page
|
||
result["page_size"] = req.PageSize
|
||
result["total"] = total
|
||
result["data"] = g
|
||
responses.OkWithData(result, c)
|
||
}
|
||
|
||
// GetQuestionHot 获取问题列表-热榜
|
||
func (b *Question) GetQuestionHot(c *gin.Context) {
|
||
// 获取数据
|
||
questionService := service.QuestionService{}
|
||
|
||
// 获取算一算热榜-人气数最高的9个
|
||
hotQuestions, err := questionService.GetHotList()
|
||
if err != nil {
|
||
responses.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
|
||
// 处理返回值
|
||
g := dto.GetHotQuestionListDto(hotQuestions)
|
||
|
||
responses.OkWithData(g, c)
|
||
}
|
||
|
||
// GetQuestion 获取问题详情
|
||
func (r *Question) GetQuestion(c *gin.Context) {
|
||
userId := c.GetInt64("UserId")
|
||
|
||
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
|
||
}
|
||
|
||
questionService := service.QuestionService{}
|
||
|
||
// 处理问题优惠价格
|
||
question.DiscountPrice = questionService.HandleQuestionDiscountPrice(question.DiscountPrice, question.DiscountEndTime)
|
||
|
||
// 检测用户收藏状态
|
||
userCollectionService := service.UserCollectionService{}
|
||
IsCollection := userCollectionService.GetUserCollectionQuestionStatus(userId, questionId)
|
||
|
||
// 获取用户首单价格
|
||
firstTimePrice, err := questionService.GetUserFirstTimeBuyPrice(userId)
|
||
if err != nil {
|
||
responses.FailWithMessage("题目错误", c)
|
||
return
|
||
}
|
||
|
||
// 获取问题被购买数量
|
||
buyCount, _ := questionService.GetQuestionBuyCount(userId, questionId)
|
||
|
||
g := dto.GetQuestionDto(question)
|
||
|
||
// 加载数据-是否收藏
|
||
g.LoadIsCollection(IsCollection)
|
||
|
||
// 加载数据-首次购买价格
|
||
g.LoadFirstTimePrice(firstTimePrice)
|
||
|
||
// 加载数据-问题被购买数量
|
||
g.LoadBuyCount(buyCount)
|
||
|
||
responses.OkWithData(g, c)
|
||
}
|
||
|
||
// GetQuestionUnlockStatus 获取问题解锁状态
|
||
func (r *Question) GetQuestionUnlockStatus(c *gin.Context) {
|
||
// 购买状态(0:否 1:是)
|
||
var status int
|
||
userId := c.GetInt64("UserId")
|
||
|
||
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 != 1 {
|
||
responses.FailWithMessage("题目错误", c)
|
||
return
|
||
}
|
||
|
||
// 获取用户数据
|
||
userDao := dao.UserDao{}
|
||
user, err := userDao.GetUserById(userId)
|
||
if err != nil || user == nil {
|
||
responses.FailWithMessage("用户错误", c)
|
||
return
|
||
}
|
||
|
||
// 检测用户会员
|
||
userService := service.UserService{}
|
||
isMember := userService.CheckUserMember(user)
|
||
if isMember == true {
|
||
// 用户为会员,可直接查看
|
||
status = 1
|
||
responses.OkWithData(status, c)
|
||
return
|
||
}
|
||
|
||
// 获取单项配置
|
||
systemSingleDao := dao.SystemSingleDao{}
|
||
|
||
maps := make(map[string]interface{})
|
||
systemSingle, err := systemSingleDao.GetSystemSingle(maps)
|
||
if err != nil {
|
||
responses.FailWithMessage("内部错误", c)
|
||
return
|
||
}
|
||
|
||
// 获取用户订单
|
||
orderSingleDao := dao.OrderSingleDao{}
|
||
maps = make(map[string]interface{})
|
||
maps["user_id"] = userId
|
||
maps["question_id"] = questionId
|
||
orderSingles, err := orderSingleDao.GetOrderSingleList(maps)
|
||
if err != nil {
|
||
responses.FailWithMessage("用户错误", c)
|
||
return
|
||
}
|
||
|
||
// 是否购买过(0:否 1:是)
|
||
var isBuy int
|
||
for _, single := range orderSingles {
|
||
// 订单状态(1:待支付 2:已完成 3:已取消)
|
||
if single.OrderStatus != 2 {
|
||
continue
|
||
}
|
||
|
||
// 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
|
||
if single.PayStatus != 2 {
|
||
continue
|
||
}
|
||
|
||
// 订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常 7:部分退款)
|
||
if single.RefundStatus != 0 {
|
||
continue
|
||
}
|
||
|
||
// 取消状态(0:否 1:是)
|
||
if single.CancelStatus == 1 {
|
||
continue
|
||
}
|
||
|
||
// 判断是否还在有效期内
|
||
payTime := time.Time(*single.PayTime)
|
||
validTime := payTime.Add(time.Duration(systemSingle.ValidDays) * 24 * time.Hour)
|
||
now := time.Now()
|
||
if validTime.Before(now) {
|
||
continue
|
||
}
|
||
|
||
isBuy = 1
|
||
break
|
||
}
|
||
|
||
if isBuy == 1 {
|
||
status = 1
|
||
responses.OkWithData(status, c)
|
||
return
|
||
}
|
||
|
||
responses.OkWithData(status, c)
|
||
}
|
||
|
||
// PutQuestionClickCount 增加问题点击次数
|
||
func (r *Question) PutQuestionClickCount(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.GetQuestionById(questionId)
|
||
if err != nil {
|
||
responses.FailWithMessage("题目错误", c)
|
||
return
|
||
}
|
||
|
||
if question.QuestionStatus != 1 {
|
||
responses.FailWithMessage("题目错误", c)
|
||
return
|
||
}
|
||
|
||
// 开始事务
|
||
tx := global.Db.Begin()
|
||
defer func() {
|
||
if r := recover(); r != nil {
|
||
tx.Rollback()
|
||
}
|
||
}()
|
||
|
||
err = questionDao.Inc(tx, questionId, "click_count", 1)
|
||
if err != nil {
|
||
tx.Rollback()
|
||
responses.FailWithMessage("内部错误", c)
|
||
return
|
||
}
|
||
|
||
tx.Commit()
|
||
|
||
responses.Ok(c)
|
||
}
|
||
|
||
// PutQuestionSubmitCount 增加问题提交次数
|
||
func (r *Question) PutQuestionSubmitCount(c *gin.Context) {
|
||
userId := c.GetInt64("UserId")
|
||
|
||
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 != 1 {
|
||
responses.FailWithMessage("题目错误", c)
|
||
return
|
||
}
|
||
|
||
// 开始事务
|
||
tx := global.Db.Begin()
|
||
defer func() {
|
||
if r := recover(); r != nil {
|
||
tx.Rollback()
|
||
}
|
||
}()
|
||
|
||
// 增加单项提交次数
|
||
questionService := service.QuestionService{}
|
||
res, err := questionService.AddQuestionSubmitCount(tx, questionId)
|
||
if err != nil || res == false {
|
||
tx.Rollback()
|
||
responses.FailWithMessage("内部错误", c)
|
||
return
|
||
}
|
||
|
||
// 增加用户单项提交次数
|
||
userService := service.UserService{}
|
||
res, err = userService.AddUserSingleSubmitCount(tx, userId)
|
||
if err != nil || res == false {
|
||
tx.Rollback()
|
||
responses.FailWithMessage("内部错误", c)
|
||
return
|
||
}
|
||
|
||
tx.Commit()
|
||
|
||
responses.Ok(c)
|
||
}
|