增加了队列消费端处理
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package crontab
|
||||
|
||||
import (
|
||||
"hepa-calc-api/api/dao"
|
||||
"hepa-calc-api/api/model"
|
||||
"hepa-calc-api/utils"
|
||||
"time"
|
||||
)
|
||||
|
||||
// SystemCouponExpire 系统优惠卷过期
|
||||
func SystemCouponExpire() {
|
||||
// 获取今日过期优惠卷
|
||||
coupons := getSystemExecCoupon()
|
||||
if len(coupons) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, coupon := range coupons {
|
||||
// 计算过期时间
|
||||
validEndTime := time.Time(coupon.ValidEndTime)
|
||||
|
||||
diffTime := validEndTime.Sub(time.Now())
|
||||
if diffTime < 5*time.Second {
|
||||
diffTime = 5 * time.Second
|
||||
}
|
||||
|
||||
// 添加处理优惠卷过期队列
|
||||
}
|
||||
}
|
||||
|
||||
// 获取今日过期优惠卷
|
||||
func getSystemExecCoupon() (coupon []*model.Coupon) {
|
||||
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["coupon_status"] = 1
|
||||
maps["valid_type"] = 1
|
||||
|
||||
couponDao := dao.CouponDao{}
|
||||
coupons, err := couponDao.GetCouponListByValidTime(maps, startTime, endTime)
|
||||
if err != nil {
|
||||
utils.LogJsonErr("系统优惠卷过期:", err.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
return coupons
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
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
|
||||
}
|
||||
|
||||
fmt.Println(userCoupon.UserCouponId)
|
||||
now := time.Now().Format("2006-01-02 15:04:05")
|
||||
fmt.Println("添加队列成功" + now)
|
||||
}
|
||||
}
|
||||
|
||||
// 获取今日过期优惠卷
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user