202 lines
4.7 KiB
Go
202 lines
4.7 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"
|
|
)
|
|
|
|
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
|
|
}
|
|
|
|
userId := c.GetInt64("UserId")
|
|
req.UserId = userId
|
|
|
|
// 获取数据
|
|
userCouponDao := dao.UserCouponDao{}
|
|
userCoupon, total, err := userCouponDao.GetUserCouponPageSearch(req, req.Page, req.PageSize)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 处理返回值
|
|
g := dto.GetUserCouponListDto(userCoupon)
|
|
|
|
result := make(map[string]interface{})
|
|
result["page"] = req.Page
|
|
result["page_size"] = req.PageSize
|
|
result["total"] = total
|
|
result["data"] = g
|
|
responses.OkWithData(result, c)
|
|
}
|
|
|
|
// GetUserCouponUnnotified 获取还未弹窗的优惠卷
|
|
func (b *UserCoupon) GetUserCouponUnnotified(c *gin.Context) {
|
|
userId := c.GetInt64("UserId")
|
|
|
|
// 获取数据
|
|
userCouponDao := dao.UserCouponDao{}
|
|
maps := make(map[string]interface{})
|
|
maps["user_id"] = userId
|
|
maps["user_coupon_status"] = 0
|
|
maps["is_windows"] = 0
|
|
userCoupon, _ := userCouponDao.GetUserCoupon(maps)
|
|
if userCoupon == nil {
|
|
responses.OkWithData(nil, c)
|
|
return
|
|
}
|
|
|
|
// 获取优惠卷数据
|
|
couponDao := dao.CouponDao{}
|
|
coupon, _ := couponDao.GetCouponById(userCoupon.CouponId)
|
|
if coupon == nil {
|
|
responses.OkWithData(nil, c)
|
|
return
|
|
}
|
|
|
|
// 开始事务
|
|
tx := global.Db.Begin()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
|
|
maps = make(map[string]interface{})
|
|
maps["is_windows"] = 1
|
|
_ = userCouponDao.EditUserCouponById(tx, userCoupon.UserCouponId, maps)
|
|
|
|
tx.Commit()
|
|
|
|
// 处理返回值
|
|
g := dto.GetUserCouponDto(userCoupon)
|
|
|
|
g.LoadCoupon(coupon)
|
|
|
|
responses.OkWithData(g, c)
|
|
}
|
|
|
|
// GetUserUsableQuestionCoupon 获取患者可使用优惠卷-单项
|
|
func (b *UserCoupon) GetUserUsableQuestionCoupon(c *gin.Context) {
|
|
userCouponRequest := requests.UserCouponRequest{}
|
|
req := userCouponRequest.GetUserUsableQuestionCoupon
|
|
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
|
|
}
|
|
|
|
userId := c.GetInt64("UserId")
|
|
|
|
// 将 id 转换为 int64 类型
|
|
questionId, err := strconv.ParseInt(req.QuestionId, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
// 获取患者可使用优惠卷-单项
|
|
userCouponService := service.UserCouponService{}
|
|
userCoupon, _ := userCouponService.GetUserUsableQuestionCoupon(userId, questionId, req.AmountTotal)
|
|
|
|
responses.OkWithData(userCoupon, c)
|
|
}
|
|
|
|
// GetUserUsableMemberCoupon 获取患者可使用优惠卷-会员
|
|
func (b *UserCoupon) GetUserUsableMemberCoupon(c *gin.Context) {
|
|
userCouponRequest := requests.UserCouponRequest{}
|
|
req := userCouponRequest.GetUserUsableMemberCoupon
|
|
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
|
|
}
|
|
|
|
userId := c.GetInt64("UserId")
|
|
|
|
// 将 id 转换为 int64 类型
|
|
systemMemberId, err := strconv.ParseInt(req.SystemMemberId, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
// 获取患者可使用优惠卷-会员
|
|
userCouponService := service.UserCouponService{}
|
|
userCoupon, _ := userCouponService.GetUserUsableMemberCoupon(userId, systemMemberId, req.AmountTotal)
|
|
|
|
responses.OkWithData(userCoupon, c)
|
|
}
|
|
|
|
// PutUserCouponUnnotified 修改还未弹窗的优惠卷
|
|
func (b *UserCoupon) PutUserCouponUnnotified(c *gin.Context) {
|
|
userId := c.GetInt64("UserId")
|
|
|
|
// 获取数据
|
|
userCouponDao := dao.UserCouponDao{}
|
|
maps := make(map[string]interface{})
|
|
maps["user_id"] = userId
|
|
userCoupons, _ := userCouponDao.GetUserCouponList(maps)
|
|
if len(userCoupons) <= 0 {
|
|
responses.OkWithData(nil, c)
|
|
return
|
|
}
|
|
|
|
// 开始事务
|
|
tx := global.Db.Begin()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
|
|
for _, coupon := range userCoupons {
|
|
maps = make(map[string]interface{})
|
|
maps["is_windows"] = 1
|
|
_ = userCouponDao.EditUserCouponById(tx, coupon.UserCouponId, maps)
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
responses.Ok(c)
|
|
}
|