diff --git a/api/service/coupon.go b/api/service/coupon.go index ef614fd..2f4cbc6 100644 --- a/api/service/coupon.go +++ b/api/service/coupon.go @@ -271,6 +271,41 @@ func (r *CouponService) AddSystemCoupon(req requests.AddSystemCoupon) (bool, err } // 增加优惠卷过期队列 + if coupon.ValidType == 1 { + validEndTime := time.Time(*coupon.ValidStartTime) + + // 计算当天的结束时间 + now := time.Now() + year, month, day := now.Date() + location := now.Location() + endOfDay := time.Date(year, month, day, 23, 59, 59, 0, location) + if validEndTime.Before(endOfDay) { + // 需添加队列 + // 建立队列连接 + rabbitMQ, err := rabbitMq.NewRabbitMQClient() + if err != nil { + tx.Rollback() + return false, errors.New("发放失败") + } + + defer rabbitMQ.Close() + + data := make(map[string]interface{}) + data["coupon_id"] = fmt.Sprintf("%d", coupon.CouponId) + + delay := validEndTime.Sub(time.Now()) + + if delay < 10 { + delay = 10 * time.Second + } + + err = rabbitMQ.PublishWithDelay("coupon.expired.delay.queue", "amqp.delay.direct", "CouponExpired", data, delay) + if err != nil { + tx.Rollback() + return false, err + } + } + } tx.Commit() return true, nil @@ -447,6 +482,8 @@ func (r *CouponService) PutSystemCoupon(couponId int64, req requests.PutSystemCo return false, errors.New(err.Error()) } + // 新增队列 + tx.Commit() return true, nil } @@ -526,7 +563,6 @@ func (r *CouponService) GrantSystemCoupon(couponId, adminUserId int64, req reque // 检测优惠卷过期时间 now := time.Now() - now = now.Add(time.Minute * 10) if coupon.ValidType == 1 { // 1:绝对时效 validStartTime := time.Time(*coupon.ValidStartTime) @@ -535,7 +571,7 @@ func (r *CouponService) GrantSystemCoupon(couponId, adminUserId int64, req reque } validEndTime := time.Time(*coupon.ValidEndTime) - if now.After(validEndTime) { + if now.Add(time.Minute * 10).After(validEndTime) { return false, errors.New("优惠卷即将过期,不支持发放") } }