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.WechatNotifyConsumeKey.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 } 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() { return j.wechatVoucherUsed(ctx, orderWechat) } else if req.PlainText.Status.IsExpired() { return j.wechatVoucherExpired(ctx, orderWechat) } else { return fmt.Errorf("未知通知类型:%s", req.PlainText.Status.GetText()) } }) } func (j *VoucherBiz) wechatVoucherUsed(ctx context.Context, orderWechat *bo.OrderWechatBo) error { if orderWechat.Status.IsUse() { return nil } order, err := j.OrderRepo.GetByOrderNo(ctx, orderWechat.OrderNo) if err != nil { return err } if err = j.OrderWechatRepo.Used(ctx, orderWechat.ID); err != nil { return err } if err = j.OrderRepo.Used(ctx, order.ID); err != nil { return err } return nil } func (j *VoucherBiz) wechatVoucherExpired(ctx context.Context, orderWechat *bo.OrderWechatBo) error { if orderWechat.Status.IsExpired() { 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 } if err = j.OrderRepo.Expired(ctx, order.ID); err != nil { return err } return nil }