voucher/internal/biz/wechat_notify_consume.go

86 lines
2.3 KiB
Go

package biz
import (
"context"
"fmt"
"github.com/go-kratos/kratos/v2/log"
"voucher/internal/biz/bo"
"voucher/internal/biz/vo"
"voucher/internal/pkg/lock"
)
func (j *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(j.rdb.Rdb, c.TTL).Lock(ctx, c.Key, func(ctx context.Context) error {
if req.PlainText.Status.IsSended() {
log.Warnf("券状态可用,忽略不处理,couponId:%s,stockId:%s,status:%s",
req.PlainText.CouponID, req.PlainText.StockID, req.PlainText.Status.GetText())
return nil
}
//req.PlainText.StockCreatorMchid = "1676203838"
//req.PlainText.StockID = "20215869"
//req.PlainText.CouponID = "96059179220"
orderWechat, err := j.OrderWechatRepo.GetByMSCId(ctx, req.PlainText.StockCreatorMchid, req.PlainText.StockID, req.PlainText.CouponID)
if err != nil {
return err
}
if req.PlainText.Status.IsUsed() {
err = j.wechatVoucherUsed(ctx, orderWechat)
} else if req.PlainText.Status.IsExpired() {
err = j.wechatVoucherExpired(ctx, orderWechat)
} else {
err = fmt.Errorf("未知通知类型:%s", req.PlainText.Status.GetText())
}
if err != nil {
return err
}
return j.PushNotifyMQ(ctx, orderWechat.OrderNo, orderWechat.OutRequestNo)
})
}
func (v *VoucherBiz) wechatVoucherUsed(ctx context.Context, orderWechat *bo.OrderWechatBo) error {
if orderWechat.Status.IsUse() {
log.Warnf("券状态已是已使用,忽略不处理,orderNo:%s", orderWechat.OrderNo)
return nil
}
order, err := v.OrderRepo.GetByOrderNo(ctx, orderWechat.OrderNo)
if err != nil {
return err
}
if err = v.OrderWechatRepo.Used(ctx, orderWechat.ID); err != nil {
return err
}
return v.OrderRepo.Used(ctx, order.ID)
}
func (j *VoucherBiz) wechatVoucherExpired(ctx context.Context, orderWechat *bo.OrderWechatBo) error {
if orderWechat.Status.IsExpired() {
log.Warnf("券状态已是已过期,忽略不处理,orderNo:%s", orderWechat.OrderNo)
return nil
}
order, err := j.OrderRepo.GetByOrderNo(ctx, orderWechat.OrderNo)
if err != nil {
return err
}
if err = j.OrderWechatRepo.Expired(ctx, orderWechat.ID); err != nil {
return err
}
return j.OrderRepo.Expired(ctx, order.ID)
}