新增了 取消未支付订单

This commit is contained in:
2024-07-31 17:07:51 +08:00
parent 1e1ce24939
commit 9182bf0c16
13 changed files with 378 additions and 39 deletions
+102
View File
@@ -0,0 +1,102 @@
package consumer
import (
"encoding/json"
"github.com/rabbitmq/amqp091-go"
"hepa-calc-api/api/service"
"hepa-calc-api/extend/weChat"
"hepa-calc-api/global"
"hepa-calc-api/utils"
"strconv"
)
type cancelUnPayOrderData struct {
OrderId string `json:"order_id"`
OrderNo string `json:"order_no"`
UserId string `json:"user_id"`
OrderType int `json:"order_type"` // 订单类型(1:单项 2:会员)
PayChannel int `json:"pay_channel"` // 支付渠道(1:h5支付 2:app支付 3:会员支付)
}
// CancelUnPayOrder 取消未支付订单
func CancelUnPayOrder(msg amqp091.Delivery) {
defer func() {
if r := recover(); r != nil {
utils.LogJsonInfo("consumer.CancelUnPayOrder:", r)
_ = msg.Reject(false)
}
}()
// 记录日志
utils.LogJsonInfo("consumer.CancelUnPayOrder:", string(msg.Body))
if msg.Body == nil {
_ = msg.Ack(false)
return
}
// 解析JSON字符串到结构体
var data cancelUnPayOrderData
err := json.Unmarshal(msg.Body, &data)
if err != nil {
_ = msg.Ack(false)
return
}
orderId, err := strconv.ParseInt(data.OrderId, 10, 64)
if err != nil {
_ = msg.Ack(false)
return
}
userId, err := strconv.ParseInt(data.UserId, 10, 64)
if err != nil {
_ = msg.Ack(false)
return
}
// 开始事务
tx := global.Db.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
utils.LogJsonErr("consumer.CouponExpire:", r)
_ = msg.Reject(false)
}
}()
// 单项订单
if data.OrderType == 1 {
orderSingleService := service.OrderSingleService{}
res, err := orderSingleService.CancelOrderSingle(tx, userId, orderId, 3)
if err != nil {
tx.Rollback()
utils.LogJsonErr("consumer.CouponExpire:", err)
_ = msg.Reject(true)
return
}
if res == false {
tx.Rollback()
utils.LogJsonErr("consumer.CouponExpire:", "取消失败")
_ = msg.Reject(true)
return
}
}
// 关闭支付订单
if data.PayChannel == 1 {
// h5支付
err = weChat.CloseJsapiOrder(data.OrderNo)
utils.LogJsonErr("consumer.CouponExpire:", err)
}
if data.PayChannel == 2 {
// app支付
err = weChat.CloseAppOrder(data.OrderNo)
utils.LogJsonErr("consumer.CouponExpire:", err)
}
tx.Commit()
_ = msg.Ack(false)
}
+123
View File
@@ -0,0 +1,123 @@
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 couponExpireData struct {
CouponId string `json:"coupon_id"`
}
// CouponExpire 优惠卷过期
func CouponExpire(msg amqp091.Delivery) {
defer func() {
if r := recover(); r != nil {
utils.LogJsonInfo("consumer.CouponExpire:", r)
_ = msg.Reject(false)
}
}()
// 记录日志
utils.LogJsonInfo("consumer.CouponExpire:", string(msg.Body))
if msg.Body == nil {
_ = msg.Ack(false)
return
}
// 解析JSON字符串到结构体
var data couponExpireData
err := json.Unmarshal(msg.Body, &data)
if err != nil {
_ = msg.Ack(false)
return
}
couponId, err := strconv.ParseInt(data.CouponId, 10, 64)
if err != nil {
_ = msg.Ack(false)
return
}
// 获取优惠卷数据
couponDao := dao.CouponDao{}
coupon, err := couponDao.GetCouponById(couponId)
if err != nil || coupon == nil {
utils.LogJsonInfo("consumer.CouponExpire:", "无优惠卷数据")
_ = msg.Reject(false)
return
}
// 检测优惠卷状态-状态(1:正常 2:强制失效 3:结束 4:删除)
if coupon.CouponStatus != 1 {
// 正常,无需处理
_ = msg.Ack(false)
return
}
// 检测优惠卷过期类型
if coupon.ValidType == 2 {
// 正常,无需处理
_ = msg.Ack(false)
return
}
// 检测优惠卷过期时间
now := time.Now()
validEndTime := time.Time(coupon.ValidEndTime)
diffTime := validEndTime.Sub(now)
if diffTime >= 60*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: diffTime,
}
err = p.PublishWithDelay()
if err != nil {
utils.LogJsonErr("consumer.CouponExpire:", 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.CouponExpire:", r)
_ = msg.Reject(false)
}
}()
// 修改用户优惠卷表
couponData := make(map[string]interface{})
couponData["coupon_status"] = 3
err = couponDao.EditCouponById(tx, couponId, couponData)
if err != nil {
tx.Rollback()
utils.LogJsonErr("consumer.CouponExpire:", err.Error())
_ = msg.Reject(false)
return
}
tx.Commit()
_ = msg.Ack(false)
}
+3 -3
View File
@@ -12,7 +12,7 @@ import (
"time"
)
type UserCouponExpireData struct {
type userCouponExpireData struct {
UserCouponId string `json:"user_coupon_id"`
}
@@ -33,7 +33,7 @@ func UserCouponExpire(msg amqp091.Delivery) {
}
// 解析JSON字符串到结构体
var data UserCouponExpireData
var data userCouponExpireData
err := json.Unmarshal(msg.Body, &data)
if err != nil {
_ = msg.Ack(false)
@@ -117,5 +117,5 @@ func UserCouponExpire(msg amqp091.Delivery) {
tx.Commit()
err = msg.Ack(false)
_ = msg.Ack(false)
}
+5
View File
@@ -0,0 +1,5 @@
package consumer
func checkHandleNumber(key string) {
}