hepa-calc-api/api/crontab/SystemCouponExpire.go

73 lines
1.7 KiB
Go

package crontab
import (
"fmt"
"hepa-calc-api/api/dao"
"hepa-calc-api/api/model"
"hepa-calc-api/extend/rabbitMq"
"hepa-calc-api/utils"
"time"
)
// SystemCouponExpire 系统优惠卷过期
func SystemCouponExpire() {
// 获取今日过期优惠卷
coupons := getSystemExecCoupon()
if len(coupons) == 0 {
return
}
for _, coupon := range coupons {
// 计算过期时间
validEndTime := time.Time(*coupon.ValidEndTime)
delay := validEndTime.Sub(time.Now())
if delay < 5*time.Second {
delay = 5 * time.Second
}
// 添加处理优惠卷过期队列
data := make(map[string]interface{})
data["coupon_id"] = fmt.Sprintf("%d", coupon.CouponId)
p := rabbitMq.PublishS{
QueueName: "coupon.expired.delay.queue",
ExchangeName: "amqp.delay.direct",
RoutingKey: "CouponExpired",
Message: data,
Delay: delay,
}
err := p.PublishWithDelay()
if err != nil {
utils.LogJsonErr("添加处理优惠卷过期队列失败:", err.Error())
return
}
}
}
// 获取今日过期优惠卷
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
}