package crontab import ( "fmt" "hepa-calc-api/api/dao" "hepa-calc-api/api/model" "hepa-calc-api/extend/rabbitMq" "hepa-calc-api/utils" "time" ) // UserCouponExpire 用户优惠卷过期 func UserCouponExpire() { // 获取今日过期优惠卷 userCoupons := getUserExecCoupon() if len(userCoupons) == 0 { return } for _, userCoupon := range userCoupons { // 计算过期时间 validEndTime := time.Time(*userCoupon.ValidEndTime) delay := validEndTime.Sub(time.Now()) if delay < 5*time.Second { delay = 5 * time.Second } // 添加处理优惠卷过期队列 data := make(map[string]interface{}) data["user_coupon_id"] = fmt.Sprintf("%d", userCoupon.UserCouponId) p := rabbitMq.PublishS{ QueueName: "user.coupon.expired.delay.queue", ExchangeName: "amqp.delay.direct", RoutingKey: "UserCouponExpired", Message: data, Delay: delay, } err := p.PublishWithDelay() if err != nil { utils.LogJsonErr("添加处理用户优惠卷过期队列失败:", err.Error()) return } } } // 获取今日过期优惠卷 func getUserExecCoupon() (coupon []*model.UserCoupon) { now := time.Now() // 今天开始时间 year, month, day := now.Date() location := now.Location() startTime := time.Date(year, month, day, 00, 00, 00, 0, location).Format("2006-01-02 15:04:05") // 今天结束时间 endTime := time.Date(year, month, day, 23, 59, 59, 0, location).Format("2006-01-02 15:04:05") maps := make(map[string]interface{}) maps["user_coupon_status"] = 0 userCouponDao := dao.UserCouponDao{} userCoupons, err := userCouponDao.GetUserCouponListByValidTime(maps, startTime, endTime) if err != nil { utils.LogJsonErr("系统优惠卷过期:", err.Error()) return nil } return userCoupons }