voucher/internal/biz/wechat_notify_consume.go

77 lines
1.8 KiB
Go

package biz
import (
"context"
"fmt"
"github.com/go-kratos/kratos/v2/log"
errPb "voucher/api/err"
"voucher/internal/biz/bo"
"voucher/internal/biz/vo"
"voucher/internal/pkg/lock"
)
func (v *VoucherBiz) WechatNotifyConsumer(ctx context.Context, tag string, req *bo.WechatVoucherNotifyBo) error {
c := vo.WechatNotifyConsumeLockKey.BuildCache([]string{tag, req.PlainText.StockID, req.PlainText.CouponID})
return lock.NewMutex(v.rdb.Rdb, c.TTL).Lock(ctx, c.Key, func(ctx context.Context) error {
order, err := v.OrderRepo.GetByMBV(ctx, req.PlainText.StockCreatorMchid, req.PlainText.StockID, req.PlainText.CouponID)
if err != nil {
return err
}
if req.PlainText.Status.IsUsed() {
if err = v.used(ctx, order); err != nil {
return err
}
} else if req.PlainText.Status.IsExpired() {
if err = v.expired(ctx, order); err != nil {
return err
}
} else {
return fmt.Errorf("未知通知类型:%s", req.PlainText.Status.GetText())
}
if order.Type.IsCmb() {
if orderNotify, err2 := v.Cmb.Notify(ctx, order); err2 != nil {
if !errPb.IsNeedRetryNotify(err2) {
return err2
}
// 第一次通知失败重试入队
// 状态回调接口失败需要支持重试 重试间隔为1分钟、2分钟、12分钟、60分钟、360分钟
return v.PushNotifyRetryDelayMQ(ctx, 60, orderNotify.ID)
}
}
return nil
})
}
func (v *VoucherBiz) used(ctx context.Context, order *bo.OrderBo) error {
if order.Status.IsUse() {
log.Warnf("券状态已是已使用,忽略不处理,orderNo:%s", order.OrderNo)
return nil
}
return v.OrderRepo.Used(ctx, order.ID)
}
func (j *VoucherBiz) expired(ctx context.Context, order *bo.OrderBo) error {
if order.Status.IsExpired() {
log.Warnf("券状态已是已过期,忽略不处理,orderNo:%s", order.OrderNo)
return nil
}
return j.OrderRepo.Expired(ctx, order.ID)
}