增加了队列消费端处理
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
package consumer
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/rabbitmq/amqp091-go"
|
||||
"hepa-calc-api/api/dao"
|
||||
"hepa-calc-api/extend/rabbitMq"
|
||||
"hepa-calc-api/global"
|
||||
"hepa-calc-api/utils"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type UserCouponExpireData struct {
|
||||
UserCouponId string `json:"user_coupon_id"`
|
||||
}
|
||||
|
||||
// UserCouponExpire 用户优惠卷过期
|
||||
func UserCouponExpire(msg amqp091.Delivery) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
utils.LogJsonInfo("consumer.UserCouponExpire:", r)
|
||||
_ = msg.Reject(false)
|
||||
}
|
||||
}()
|
||||
|
||||
// 记录日志
|
||||
utils.LogJsonInfo("consumer.UserCouponExpire:", string(msg.Body))
|
||||
if msg.Body == nil {
|
||||
_ = msg.Ack(false)
|
||||
return
|
||||
}
|
||||
|
||||
// 解析JSON字符串到结构体
|
||||
var data UserCouponExpireData
|
||||
err := json.Unmarshal(msg.Body, &data)
|
||||
if err != nil {
|
||||
_ = msg.Ack(false)
|
||||
return
|
||||
}
|
||||
|
||||
userCouponId, err := strconv.ParseInt(data.UserCouponId, 10, 64)
|
||||
if err != nil {
|
||||
_ = msg.Ack(false)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取优惠卷数据
|
||||
userCouponDao := dao.UserCouponDao{}
|
||||
userCoupon, err := userCouponDao.GetUserCouponById(userCouponId)
|
||||
if err != nil || userCoupon == nil {
|
||||
utils.LogJsonInfo("consumer.UserCouponExpire:", "无优惠卷数据")
|
||||
_ = msg.Reject(false)
|
||||
return
|
||||
}
|
||||
|
||||
// 检测优惠卷是否被使用
|
||||
if userCoupon.UserCouponStatus == 1 {
|
||||
_ = msg.Reject(false)
|
||||
return
|
||||
}
|
||||
|
||||
// 检测优惠卷是否已执行过期处理
|
||||
if userCoupon.UserCouponStatus == 3 {
|
||||
_ = msg.Reject(false)
|
||||
return
|
||||
}
|
||||
|
||||
// 检测优惠卷过期时间
|
||||
now := time.Now()
|
||||
validEndTime := time.Time(userCoupon.ValidEndTime)
|
||||
diffTime := validEndTime.Sub(now)
|
||||
if diffTime >= 60*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: diffTime,
|
||||
}
|
||||
err = p.PublishWithDelay()
|
||||
if err != nil {
|
||||
utils.LogJsonErr("consumer.UserCouponExpire:", err.Error())
|
||||
// 重回队列
|
||||
_ = msg.Reject(true)
|
||||
return
|
||||
}
|
||||
|
||||
_ = msg.Ack(false)
|
||||
return
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
utils.LogJsonErr("consumer.UserCouponExpire:", r)
|
||||
_ = msg.Reject(false)
|
||||
}
|
||||
}()
|
||||
|
||||
// 修改用户优惠卷表
|
||||
userCouponData := make(map[string]interface{})
|
||||
userCouponData["user_coupon_status"] = 3
|
||||
err = userCouponDao.EditUserCouponById(tx, userCouponId, userCouponData)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
utils.LogJsonErr("consumer.UserCouponExpire:", err.Error())
|
||||
_ = msg.Reject(false)
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
err = msg.Ack(false)
|
||||
}
|
||||
@@ -93,6 +93,15 @@ func (r *CallBack) WxPaySingle(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 增加题目支付次数
|
||||
questionDao := dao.QuestionDao{}
|
||||
err = questionDao.Inc(tx, orderSingle.QuestionId, "pay_count", 1)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
responses.FailWithMessage("内部错误", c)
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
c.JSON(http.StatusOK, gin.H{"code": "SUCCESS", "message": "OK"})
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -106,3 +106,12 @@ func (r *CouponDao) GetCoupon(maps interface{}) (m *model.Coupon, err error) {
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetCouponListByValidTime 获取列表-今天开始时间/过期时间
|
||||
func (r *CouponDao) GetCouponListByValidTime(maps interface{}, startTime, endTime string) (m []*model.Coupon, err error) {
|
||||
err = global.Db.Where(maps).Where("valid_end_time BETWEEN ? AND ?", startTime, endTime).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
@@ -200,3 +200,12 @@ func (r *UserCouponDao) GetUserCouponPageSearch(req requests.GetUserCouponPage,
|
||||
}
|
||||
return m, totalRecords, nil
|
||||
}
|
||||
|
||||
// GetUserCouponListByValidTime 获取列表-今天开始时间/过期时间
|
||||
func (r *UserCouponDao) GetUserCouponListByValidTime(maps interface{}, startTime, endTime string) (m []*model.UserCoupon, err error) {
|
||||
err = global.Db.Where(maps).Where("valid_end_time BETWEEN ? AND ?", startTime, endTime).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
@@ -153,7 +153,6 @@ func privateRouter(r *gin.Engine, api controller.Api) {
|
||||
// 增加问题提交次数(提交个人信息进行了算算的人次)
|
||||
clickGroup.PUT("/submit/:question_id", api.Question.PutQuestionSubmitCount)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 用户
|
||||
@@ -246,5 +245,4 @@ func privateRouter(r *gin.Engine, api controller.Api) {
|
||||
// 获取会员配置数据
|
||||
memberGroup.GET("", api.SystemMember.GetSystemMember)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -297,7 +297,7 @@ func (r *OrderMemberService) GetJsapiPrepay(m *model.OrderMember) (prepay *jsapi
|
||||
|
||||
jsapiRequest := weChat.JsapiRequest{
|
||||
AppId: config.C.Wechat.AppId,
|
||||
MchId: config.C.Wechat.Pay1659662936.MchId,
|
||||
MchId: config.C.Wechat.Pay1281030301.MchId,
|
||||
Description: "肝病算一算",
|
||||
OutTradeNo: m.OrderNo,
|
||||
NotifyUrl: config.C.Wechat.RefundNotifyDomain + config.C.Wechat.RefundNotifyUrl,
|
||||
@@ -331,7 +331,7 @@ func (r *OrderMemberService) GetAppPrepay(m *model.OrderMember) (prepay *app.Pre
|
||||
|
||||
appRequest := weChat.AppRequest{
|
||||
AppId: config.C.Wechat.AppId,
|
||||
MchId: config.C.Wechat.Pay1659662936.MchId,
|
||||
MchId: config.C.Wechat.Pay1281030301.MchId,
|
||||
Description: "肝病算一算",
|
||||
OutTradeNo: m.OrderNo,
|
||||
NotifyUrl: config.C.Wechat.RefundNotifyDomain + config.C.Wechat.RefundNotifyUrl,
|
||||
|
||||
@@ -293,7 +293,7 @@ func (r *OrderSingleService) GetJsapiPrepay(m *model.OrderSingle) (prepay *jsapi
|
||||
|
||||
jsapiRequest := weChat.JsapiRequest{
|
||||
AppId: config.C.Wechat.AppId,
|
||||
MchId: config.C.Wechat.Pay1659662936.MchId,
|
||||
MchId: config.C.Wechat.Pay1281030301.MchId,
|
||||
Description: "肝病算一算",
|
||||
OutTradeNo: m.OrderNo,
|
||||
NotifyUrl: config.C.Wechat.RefundNotifyDomain + config.C.Wechat.RefundNotifyUrl,
|
||||
@@ -327,7 +327,7 @@ func (r *OrderSingleService) GetAppPrepay(m *model.OrderSingle) (prepay *app.Pre
|
||||
|
||||
appRequest := weChat.AppRequest{
|
||||
AppId: config.C.Wechat.AppId,
|
||||
MchId: config.C.Wechat.Pay1659662936.MchId,
|
||||
MchId: config.C.Wechat.Pay1281030301.MchId,
|
||||
Description: "肝病算一算",
|
||||
OutTradeNo: m.OrderNo,
|
||||
NotifyUrl: config.C.Wechat.RefundNotifyDomain + config.C.Wechat.RefundNotifyUrl,
|
||||
|
||||
@@ -117,7 +117,7 @@ func (r *UserCouponService) ReturnUserCoupon(tx *gorm.DB, userCouponId int64) bo
|
||||
return true
|
||||
}
|
||||
|
||||
// GetUserUsableQuestionCoupon 获取患者可使用优惠卷-单项
|
||||
// GetUserUsableQuestionCoupon 获取用户可使用优惠卷-单项
|
||||
func (r *UserCouponService) GetUserUsableQuestionCoupon(userId, questionId int64, amountTotal float64) (g []*dto.UserCouponDto, err error) {
|
||||
// 获取用户数据
|
||||
userDao := dao.UserDao{}
|
||||
@@ -162,7 +162,7 @@ func (r *UserCouponService) GetUserUsableQuestionCoupon(userId, questionId int64
|
||||
return g, nil
|
||||
}
|
||||
|
||||
// GetUserUsableMemberCoupon 获取患者可使用优惠卷-会员
|
||||
// GetUserUsableMemberCoupon 获取用户可使用优惠卷-会员
|
||||
func (r *UserCouponService) GetUserUsableMemberCoupon(userId, systemMemberId int64, amountTotal float64) (g []*dto.UserCouponDto, err error) {
|
||||
// 获取用户数据
|
||||
userDao := dao.UserDao{}
|
||||
|
||||
Reference in New Issue
Block a user