修改了领取优惠卷的入参格式,
This commit is contained in:
parent
f5694cb227
commit
75ed34e791
@ -7,11 +7,6 @@ type CouponRequest struct {
|
||||
// ReceiveCoupon 领取优惠卷
|
||||
type ReceiveCoupon struct {
|
||||
Mobile string `json:"mobile" form:"mobile" validate:"required,Mobile" label:"手机号"`
|
||||
ReceiveCouponCoupon []*ReceiveCouponCoupon `json:"coupon" form:"coupon" validate:"required" label:"优惠卷"` // 领取优惠卷,优惠卷部分入参数据
|
||||
}
|
||||
|
||||
// ReceiveCouponCoupon 领取优惠卷,优惠卷部分入参数据
|
||||
type ReceiveCouponCoupon struct {
|
||||
CouponId string `json:"coupon_id" form:"coupon_id" validate:"required" label:"优惠卷id"`
|
||||
Quantity int `json:"quantity" form:"quantity" validate:"required" label:"优惠卷数量"`
|
||||
}
|
||||
|
||||
@ -50,51 +50,26 @@ func (r *CouponService) ReceiveCoupon(req requestsV1.ReceiveCoupon) (g *dtoV1.Re
|
||||
return g, err
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
// 建立队列连接
|
||||
rabbitMQ, err := rabbitMq.NewRabbitMQClient()
|
||||
// 将 id 转换为 int64 类型
|
||||
couponId, err := strconv.ParseInt(req.CouponId, 10, 64)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return g, errors.New("内部错误")
|
||||
}
|
||||
|
||||
defer rabbitMQ.Close()
|
||||
|
||||
// 获取优惠卷数据
|
||||
couponDao := dao.CouponDao{}
|
||||
userCouponDao := dao.UserCouponDao{}
|
||||
popupDao := dao.PopupDao{}
|
||||
for _, v := range req.ReceiveCouponCoupon {
|
||||
// 将 id 转换为 int64 类型
|
||||
couponId, err := strconv.ParseInt(v.CouponId, 10, 64)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return g, errors.New("内部错误")
|
||||
}
|
||||
|
||||
coupon, _ := couponDao.GetCouponById(couponId)
|
||||
if coupon == nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.New("非法优惠卷")
|
||||
}
|
||||
|
||||
// 检测优惠卷状态
|
||||
if coupon.CouponStatus != 1 {
|
||||
tx.Rollback()
|
||||
return nil, errors.New("存在错误优惠卷")
|
||||
}
|
||||
|
||||
// 检测发放优惠卷数量
|
||||
if v.Quantity <= 0 {
|
||||
tx.Rollback()
|
||||
|
||||
if req.Quantity <= 0 {
|
||||
g = &dtoV1.ReceiveCouponDto{
|
||||
Status: 0,
|
||||
Message: "优惠卷数量错误",
|
||||
@ -106,8 +81,6 @@ func (r *CouponService) ReceiveCoupon(req requestsV1.ReceiveCoupon) (g *dtoV1.Re
|
||||
// 检测优惠卷剩余数量
|
||||
remainingQuantity := coupon.CouponCount - coupon.CouponTakeCount
|
||||
if remainingQuantity <= 0 {
|
||||
tx.Rollback()
|
||||
|
||||
g = &dtoV1.ReceiveCouponDto{
|
||||
Status: 0,
|
||||
Message: "优惠卷数量不足,领取失败",
|
||||
@ -116,9 +89,7 @@ func (r *CouponService) ReceiveCoupon(req requestsV1.ReceiveCoupon) (g *dtoV1.Re
|
||||
return g, nil
|
||||
}
|
||||
|
||||
if remainingQuantity < v.Quantity {
|
||||
tx.Rollback()
|
||||
|
||||
if remainingQuantity < req.Quantity {
|
||||
g = &dtoV1.ReceiveCouponDto{
|
||||
Status: 0,
|
||||
Message: "优惠卷数量不足,领取失败",
|
||||
@ -135,8 +106,6 @@ func (r *CouponService) ReceiveCoupon(req requestsV1.ReceiveCoupon) (g *dtoV1.Re
|
||||
// 1:绝对时效
|
||||
validStartTime := time.Time(coupon.ValidStartTime)
|
||||
if now.Before(validStartTime) {
|
||||
tx.Rollback()
|
||||
|
||||
g = &dtoV1.ReceiveCouponDto{
|
||||
Status: 0,
|
||||
Message: "优惠卷还未启用",
|
||||
@ -147,8 +116,6 @@ func (r *CouponService) ReceiveCoupon(req requestsV1.ReceiveCoupon) (g *dtoV1.Re
|
||||
|
||||
validEndTime := time.Time(coupon.ValidEndTime)
|
||||
if now.After(validEndTime) {
|
||||
tx.Rollback()
|
||||
|
||||
g = &dtoV1.ReceiveCouponDto{
|
||||
Status: 0,
|
||||
Message: "优惠卷已过期",
|
||||
@ -159,13 +126,28 @@ func (r *CouponService) ReceiveCoupon(req requestsV1.ReceiveCoupon) (g *dtoV1.Re
|
||||
}
|
||||
|
||||
// 检测用户是否已领取该优惠卷
|
||||
maps := make(map[string]interface{})
|
||||
userCouponDao := dao.UserCouponDao{}
|
||||
|
||||
maps = make(map[string]interface{})
|
||||
maps["user_id"] = user.UserId
|
||||
maps["coupon_id"] = coupon.CouponId
|
||||
userCoupon, _ := userCouponDao.GetUserCoupon(maps)
|
||||
if userCoupon != nil {
|
||||
continue
|
||||
g = &dtoV1.ReceiveCouponDto{
|
||||
Status: 0,
|
||||
Message: "重复领取",
|
||||
Data: nil,
|
||||
}
|
||||
return g, nil
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
// 添加用户优惠卷表
|
||||
UserCouponModel := &model.UserCoupon{
|
||||
@ -193,7 +175,7 @@ func (r *CouponService) ReceiveCoupon(req requestsV1.ReceiveCoupon) (g *dtoV1.Re
|
||||
}
|
||||
|
||||
// 增加优惠卷发放数量
|
||||
err = couponDao.Inc(tx, couponId, "coupon_take_count", v.Quantity)
|
||||
err = couponDao.Inc(tx, couponId, "coupon_take_count", req.Quantity)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.New("发放失败")
|
||||
@ -228,6 +210,7 @@ func (r *CouponService) ReceiveCoupon(req requestsV1.ReceiveCoupon) (g *dtoV1.Re
|
||||
|
||||
popupModel.PopupContent = string(popupContentData)
|
||||
|
||||
popupDao := dao.PopupDao{}
|
||||
popup, _ := popupDao.AddPopup(tx, popupModel)
|
||||
if popup == nil {
|
||||
tx.Rollback()
|
||||
@ -242,6 +225,15 @@ func (r *CouponService) ReceiveCoupon(req requestsV1.ReceiveCoupon) (g *dtoV1.Re
|
||||
endOfDay := time.Date(year, month, day, 23, 59, 59, 0, location)
|
||||
if userCoupon.ValidEndTime.Before(endOfDay) {
|
||||
// 需添加队列
|
||||
// 建立队列连接
|
||||
rabbitMQ, err := rabbitMq.NewRabbitMQClient()
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return g, errors.New("内部错误")
|
||||
}
|
||||
|
||||
defer rabbitMQ.Close()
|
||||
|
||||
data := make(map[string]interface{})
|
||||
data["user_coupon_id"] = fmt.Sprintf("%d", userCoupon.UserCouponId)
|
||||
|
||||
@ -265,7 +257,6 @@ func (r *CouponService) ReceiveCoupon(req requestsV1.ReceiveCoupon) (g *dtoV1.Re
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user