From 7af51c49ce70de4d0a28d1a806e4b46c7e260035 Mon Sep 17 00:00:00 2001 From: wucongxing8150 <815046773@qq.com> Date: Fri, 7 Jun 2024 16:29:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BC=98=E6=83=A0=E5=8D=B7?= =?UTF-8?q?=E5=8F=91=E6=94=BE=E6=95=B0=E9=87=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/service/v1/Coupon.go | 187 +++++++++++++++++++-------------------- 1 file changed, 93 insertions(+), 94 deletions(-) diff --git a/api/service/v1/Coupon.go b/api/service/v1/Coupon.go index 3792086..7ed62c3 100644 --- a/api/service/v1/Coupon.go +++ b/api/service/v1/Coupon.go @@ -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()