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 { //req.PlainText.StockCreatorMchid = "1676203838" //req.PlainText.StockID = "20215869" //req.PlainText.CouponID = "96059179220" 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 } orderWechat, err := j.OrderWechatRepo.GetByMSCId(ctx, req.PlainText.StockCreatorMchid, req.PlainText.StockID, req.PlainText.CouponID) if err != nil { return err } order, err := j.OrderRepo.GetByOrderNo(ctx, orderWechat.OrderNo) if err != nil { return fmt.Errorf("根据订单号%s获取订单信息失败:%s", orderWechat.OrderNo, err.Error()) } if req.PlainText.Status.IsUsed() { if err = j.wechatVoucherUsed(ctx, order, orderWechat); err != nil { return err } } else if req.PlainText.Status.IsExpired() { if err = j.wechatVoucherExpired(ctx, order, orderWechat); err != nil { return err } } else { return fmt.Errorf("未知通知类型:%s", req.PlainText.Status.GetText()) } return j.PushNotifyMQ(ctx, orderWechat.OrderNo, orderWechat.OutRequestNo) }) } func (v *VoucherBiz) wechatVoucherUsed(ctx context.Context, order *bo.OrderBo, orderWechat *bo.OrderWechatBo) error { if orderWechat.Status.IsUse() { log.Warnf("券状态已是已使用,忽略不处理,orderNo:%s", orderWechat.OrderNo) return nil } 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, order *bo.OrderBo, orderWechat *bo.OrderWechatBo) error { if orderWechat.Status.IsExpired() { log.Warnf("券状态已是已过期,忽略不处理,orderNo:%s", orderWechat.OrderNo) return nil } if err := j.OrderWechatRepo.Expired(ctx, orderWechat.ID); err != nil { return err } return j.OrderRepo.Expired(ctx, order.ID) }