110 lines
2.4 KiB
Go
110 lines
2.4 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/requests"
|
||
"hepa-calc-admin-api/api/responses"
|
||
"hepa-calc-admin-api/global"
|
||
"hepa-calc-admin-api/utils"
|
||
"strconv"
|
||
)
|
||
|
||
type UserCoupon struct{}
|
||
|
||
// GetUserCouponPage 获取用户优惠卷列表-分页
|
||
func (b *UserCoupon) GetUserCouponPage(c *gin.Context) {
|
||
userCouponRequest := requests.UserCouponRequest{}
|
||
req := userCouponRequest.GetUserCouponPage
|
||
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
|
||
}
|
||
|
||
if req.Page == 0 {
|
||
req.Page = 1
|
||
}
|
||
|
||
if req.PageSize == 0 {
|
||
req.PageSize = 20
|
||
}
|
||
|
||
// 获取数据
|
||
userCouponDao := dao.UserCouponDao{}
|
||
userCoupons, total, err := userCouponDao.GetUserCouponPageSearch(req, req.Page, req.PageSize)
|
||
if err != nil {
|
||
responses.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
|
||
// 处理返回值
|
||
g := dto.GetUserCouponListDto(userCoupons)
|
||
|
||
result := make(map[string]interface{})
|
||
result["page"] = req.Page
|
||
result["page_size"] = req.PageSize
|
||
result["total"] = total
|
||
result["data"] = g
|
||
responses.OkWithData(result, c)
|
||
}
|
||
|
||
// DeleteUserCoupon 删除用户优惠卷
|
||
func (b *UserCoupon) DeleteUserCoupon(c *gin.Context) {
|
||
id := c.Param("user_coupon_id")
|
||
if id == "" {
|
||
responses.FailWithMessage("缺少参数", c)
|
||
return
|
||
}
|
||
|
||
// 将 id 转换为 int64 类型
|
||
userCouponId, err := strconv.ParseInt(id, 10, 64)
|
||
if err != nil {
|
||
responses.Fail(c)
|
||
return
|
||
}
|
||
|
||
// 获取优惠卷数据
|
||
userCouponDao := dao.UserCouponDao{}
|
||
userCoupon, err := userCouponDao.GetUserCouponById(userCouponId)
|
||
if err != nil {
|
||
responses.FailWithMessage("优惠卷异常", c)
|
||
return
|
||
}
|
||
|
||
// 检测优惠卷状态(0:未使用 1:已使用 3:已过期)
|
||
if userCoupon.UserCouponStatus == 1 {
|
||
responses.FailWithMessage("优惠卷已使用,无需删除", c)
|
||
return
|
||
}
|
||
|
||
if userCoupon.UserCouponStatus == 3 {
|
||
responses.FailWithMessage("优惠卷已过期,无需删除", c)
|
||
return
|
||
}
|
||
|
||
// 开始事务
|
||
tx := global.Db.Begin()
|
||
defer func() {
|
||
if r := recover(); r != nil {
|
||
tx.Rollback()
|
||
}
|
||
}()
|
||
|
||
err = userCouponDao.DeleteUserCouponById(tx, userCouponId)
|
||
if err != nil {
|
||
tx.Rollback()
|
||
responses.FailWithMessage("操作失败", c)
|
||
return
|
||
}
|
||
|
||
tx.Commit()
|
||
responses.Ok(c)
|
||
}
|