修改优惠卷发放数量问题

This commit is contained in:
wucongxing8150 2024-06-07 16:29:06 +08:00
parent d6e28d292e
commit 7af51c49ce

View File

@ -148,12 +148,6 @@ func (r *CouponService) ReceiveCoupon(req requestsV1.ReceiveCoupon) (g *dtoV1.Re
} }
} }
// 检测优惠卷过期时间
now = now.Add(time.Minute * 10)
if coupon.ValidType == 1 {
}
// 检测用户是否已领取该优惠卷 // 检测用户是否已领取该优惠卷
userCouponDao := dao.UserCouponDao{} userCouponDao := dao.UserCouponDao{}
@ -179,31 +173,6 @@ func (r *CouponService) ReceiveCoupon(req requestsV1.ReceiveCoupon) (g *dtoV1.Re
} }
}() }()
// 添加用户优惠卷表
UserCouponModel := &model.UserCoupon{
UserId: user.UserId,
PatientId: userPatient.PatientId,
CouponId: couponId,
CouponUseDate: nil,
}
// 有效类型1:绝对时效xxx-xxx时间段有效 2:相对时效 n天内有效
if coupon.ValidType == 1 {
UserCouponModel.ValidStartTime = time.Time(coupon.ValidStartTime)
UserCouponModel.ValidEndTime = time.Time(coupon.ValidEndTime)
}
if coupon.ValidType == 2 {
UserCouponModel.ValidStartTime = now
UserCouponModel.ValidEndTime = now.AddDate(0, 0, coupon.ValidDays)
}
userCoupon, _ := userCouponDao.AddUserCoupon(tx, UserCouponModel)
if userCoupon == nil {
tx.Rollback()
return nil, errors.New("发放失败")
}
// 增加优惠卷发放数量 // 增加优惠卷发放数量
err = couponDao.Inc(tx, couponId, "coupon_take_count", req.Quantity) err = couponDao.Inc(tx, couponId, "coupon_take_count", req.Quantity)
if err != nil { if err != nil {
@ -211,81 +180,111 @@ func (r *CouponService) ReceiveCoupon(req requestsV1.ReceiveCoupon) (g *dtoV1.Re
return nil, errors.New("发放失败") return nil, errors.New("发放失败")
} }
// 添加弹窗表 for i := 0; i < req.Quantity; i++ {
if coupon.IsPopup == 1 { // 添加用户优惠卷表
popupModel := &model.Popup{ UserCouponModel := &model.UserCoupon{
UserId: user.UserId, UserId: user.UserId,
AppType: 1, PatientId: userPatient.PatientId,
ClientType: 1, CouponId: couponId,
PopupType: 2, CouponUseDate: nil,
PopupTitle: "新人红包福利",
PopupContent: "",
} }
popupContent := make(map[string]interface{}) // 有效类型1:绝对时效xxx-xxx时间段有效 2:相对时效 n天内有效
popupContent["user_coupon_id"] = fmt.Sprintf("%d", userCoupon.UserCouponId) if coupon.ValidType == 1 {
popupContent["coupon_price"] = coupon.CouponPrice UserCouponModel.ValidStartTime = time.Time(coupon.ValidStartTime)
popupContent["application_scope"] = coupon.ApplicationScope UserCouponModel.ValidEndTime = time.Time(coupon.ValidEndTime)
popupContent["inquiry_type"] = coupon.InquiryType }
popupContent["valid_type"] = coupon.ValidType
popupContent["valid_days"] = coupon.ValidDays
popupContent["valid_start_time"] = coupon.ValidStartTime
popupContent["valid_end_time"] = coupon.ValidEndTime
popupContentData, err := json.Marshal(popupContent) if coupon.ValidType == 2 {
if err != nil { UserCouponModel.ValidStartTime = now
UserCouponModel.ValidEndTime = now.AddDate(0, 0, coupon.ValidDays)
}
userCoupon, _ := userCouponDao.AddUserCoupon(tx, UserCouponModel)
if userCoupon == nil {
tx.Rollback() tx.Rollback()
return nil, errors.New("发放失败") return nil, errors.New("发放失败")
} }
popupModel.PopupContent = string(popupContentData) // 同类型优惠卷只添加一次弹窗
if i == 0 {
// 添加弹窗表
if coupon.IsPopup == 1 {
popupModel := &model.Popup{
UserId: user.UserId,
AppType: 1,
ClientType: 1,
PopupType: 2,
PopupTitle: "新人红包福利",
PopupContent: "",
}
popupDao := dao.PopupDao{} popupContent := make(map[string]interface{})
popup, _ := popupDao.AddPopup(tx, popupModel) popupContent["user_coupon_id"] = fmt.Sprintf("%d", userCoupon.UserCouponId)
if popup == nil { popupContent["coupon_price"] = coupon.CouponPrice
tx.Rollback() popupContent["application_scope"] = coupon.ApplicationScope
return nil, errors.New("发放失败") popupContent["inquiry_type"] = coupon.InquiryType
} popupContent["valid_type"] = coupon.ValidType
} popupContent["valid_days"] = coupon.ValidDays
popupContent["valid_start_time"] = coupon.ValidStartTime
popupContent["valid_end_time"] = coupon.ValidEndTime
// 增加优惠卷过期队列 popupContentData, err := json.Marshal(popupContent)
// 计算当天的结束时间 if err != nil {
year, month, day := now.Date() tx.Rollback()
location := now.Location() return nil, errors.New("发放失败")
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() popupModel.PopupContent = string(popupContentData)
data := make(map[string]interface{}) popupDao := dao.PopupDao{}
data["user_coupon_id"] = fmt.Sprintf("%d", userCoupon.UserCouponId) popup, _ := popupDao.AddPopup(tx, popupModel)
if popup == nil {
delay := userCoupon.ValidEndTime.Sub(time.Now()) tx.Rollback()
return nil, errors.New("发放失败")
if delay < 10 { }
delay = 10 * time.Second
}
err = rabbitMQ.PublishWithDelay("user.coupon.expired.delay.queue", "amqp.delay.direct", "UserCouponExpired", data, delay)
if err != nil {
tx.Rollback()
return nil, err
}
go func() {
// 发送通知
res, _ := service.PatientDistributeCoupon(coupon.CouponName, user.UserId)
if !res {
utils.LogJsonError("优惠卷通知发送失败")
} }
}() }
// 增加优惠卷过期队列
// 计算当天的结束时间
year, month, day := now.Date()
location := now.Location()
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)
delay := userCoupon.ValidEndTime.Sub(time.Now())
if delay < 10 {
delay = 10 * time.Second
}
err = rabbitMQ.PublishWithDelay("user.coupon.expired.delay.queue", "amqp.delay.direct", "UserCouponExpired", data, delay)
if err != nil {
tx.Rollback()
return nil, err
}
go func() {
// 发送通知
res, _ := service.PatientDistributeCoupon(coupon.CouponName, user.UserId)
if !res {
utils.LogJsonError("优惠卷通知发送失败")
}
}()
}
} }
tx.Commit() tx.Commit()