修改优惠卷发放数量问题

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{}
@ -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)
if err != nil {
@ -211,81 +180,111 @@ func (r *CouponService) ReceiveCoupon(req requestsV1.ReceiveCoupon) (g *dtoV1.Re
return nil, errors.New("发放失败")
}
// 添加弹窗表
if coupon.IsPopup == 1 {
popupModel := &model.Popup{
UserId: user.UserId,
AppType: 1,
ClientType: 1,
PopupType: 2,
PopupTitle: "新人红包福利",
PopupContent: "",
for i := 0; i < req.Quantity; i++ {
// 添加用户优惠卷表
UserCouponModel := &model.UserCoupon{
UserId: user.UserId,
PatientId: userPatient.PatientId,
CouponId: couponId,
CouponUseDate: nil,
}
popupContent := make(map[string]interface{})
popupContent["user_coupon_id"] = fmt.Sprintf("%d", userCoupon.UserCouponId)
popupContent["coupon_price"] = coupon.CouponPrice
popupContent["application_scope"] = coupon.ApplicationScope
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
// 有效类型1:绝对时效xxx-xxx时间段有效 2:相对时效 n天内有效
if coupon.ValidType == 1 {
UserCouponModel.ValidStartTime = time.Time(coupon.ValidStartTime)
UserCouponModel.ValidEndTime = time.Time(coupon.ValidEndTime)
}
popupContentData, err := json.Marshal(popupContent)
if err != nil {
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("发放失败")
}
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{}
popup, _ := popupDao.AddPopup(tx, popupModel)
if popup == nil {
tx.Rollback()
return nil, errors.New("发放失败")
}
}
popupContent := make(map[string]interface{})
popupContent["user_coupon_id"] = fmt.Sprintf("%d", userCoupon.UserCouponId)
popupContent["coupon_price"] = coupon.CouponPrice
popupContent["application_scope"] = coupon.ApplicationScope
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
// 增加优惠卷过期队列
// 计算当天的结束时间
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("内部错误")
}
popupContentData, err := json.Marshal(popupContent)
if err != nil {
tx.Rollback()
return nil, errors.New("发放失败")
}
defer rabbitMQ.Close()
popupModel.PopupContent = string(popupContentData)
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("优惠卷通知发送失败")
popupDao := dao.PopupDao{}
popup, _ := popupDao.AddPopup(tx, popupModel)
if popup == nil {
tx.Rollback()
return nil, errors.New("发放失败")
}
}
}()
}
// 增加优惠卷过期队列
// 计算当天的结束时间
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()