From 09bf8b90fccfb15990146f42cd482903d99e5cbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AD=90=E9=93=AD?= Date: Fri, 7 Mar 2025 17:57:03 +0800 Subject: [PATCH] cmb --- internal/biz/cmb/consume.go | 4 ++++ internal/biz/consume.go | 31 +++++++++++++------------ internal/biz/vo/cmb.go | 1 + internal/biz/vo/order_wechant_status.go | 3 ++- internal/biz/wechat_notify_consume.go | 30 +++++++++++------------- 5 files changed, 37 insertions(+), 32 deletions(-) diff --git a/internal/biz/cmb/consume.go b/internal/biz/cmb/consume.go index 71ba886..7928064 100644 --- a/internal/biz/cmb/consume.go +++ b/internal/biz/cmb/consume.go @@ -206,6 +206,10 @@ func (v *Cmb) bizContent(_ context.Context, orderWechat *bo.OrderWechatBo) (stri func (v *Cmb) NotifyConsume(ctx context.Context, order *bo.OrderBo, orderOutRequestNo string) error { + if !order.Channel.IsWeChat() { + return fmt.Errorf("暂不支持订单%s渠道%s回调通知处理", order.OrderNo, order.Channel.GetText()) + } + orderWechat, err := v.orderWechat(ctx, order, orderOutRequestNo) if err != nil { return err diff --git a/internal/biz/consume.go b/internal/biz/consume.go index 2d5b7e5..0145ebb 100644 --- a/internal/biz/consume.go +++ b/internal/biz/consume.go @@ -24,21 +24,6 @@ func (v *VoucherBiz) PushOrderMQ(ctx context.Context, orderNo string) error { return nil } -func (v *VoucherBiz) PushNotifyMQ(ctx context.Context, orderNo, outRequestNo string) error { - - eventMap := v.bc.RocketMQ.EventMap["notify"] - sendOption := []mq.SendOption{ - mq.WithSendShardingKeysOption(fmt.Sprintf("%s_%s", orderNo, outRequestNo)), - mq.WithOpenTelemetryOption(trace.SpanFromContext(ctx).SpanContext().TraceID().String()), - } - - if err := v.MqSendMixRepo.SendSync(ctx, eventMap.Topic, []byte("{}"), sendOption...); err != nil { - return fmt.Errorf("notify,消费队列投递失败[%v]", err) - } - - return nil -} - func (v *VoucherBiz) OrderConsume(ctx context.Context, orderNo string) (err error) { err = lock.NewMutex(v.rdb.Rdb, time.Second*30).Lock(ctx, fmt.Sprintf("order_consume_%s", orderNo), func(ctx context.Context) error { @@ -54,6 +39,7 @@ func (v *VoucherBiz) OrderConsume(ctx context.Context, orderNo string) (err erro if err != nil { return err } + return v.PushNotifyMQ(ctx, orderNo, outRequestNo) } @@ -64,6 +50,21 @@ func (v *VoucherBiz) OrderConsume(ctx context.Context, orderNo string) (err erro return } +func (v *VoucherBiz) PushNotifyMQ(ctx context.Context, orderNo, outRequestNo string) error { + + eventMap := v.bc.RocketMQ.EventMap["notify"] + sendOption := []mq.SendOption{ + mq.WithSendShardingKeysOption(fmt.Sprintf("%s_%s", orderNo, outRequestNo)), + mq.WithOpenTelemetryOption(trace.SpanFromContext(ctx).SpanContext().TraceID().String()), + } + + if err := v.MqSendMixRepo.SendSync(ctx, eventMap.Topic, []byte("{}"), sendOption...); err != nil { + return fmt.Errorf("notify,消费队列投递失败[%v]", err) + } + + return nil +} + func (v *VoucherBiz) NotifyConsume(ctx context.Context, orderNo, orderOutRequestNo string) (err error) { err = lock.NewMutex(v.rdb.Rdb, time.Second*30).Lock(ctx, fmt.Sprintf("notify_consume_%s", orderNo), func(ctx context.Context) error { diff --git a/internal/biz/vo/cmb.go b/internal/biz/vo/cmb.go index 08e492b..eede1d2 100644 --- a/internal/biz/vo/cmb.go +++ b/internal/biz/vo/cmb.go @@ -18,6 +18,7 @@ type CmbStatus string const ( CmbStatusSuccess CmbStatus = "0" CmbStatusUse CmbStatus = "1" + CmbStatusExpired CmbStatus = "2" // 券过期-待确认是否通知 ) func (s CmbStatus) GetValue() string { diff --git a/internal/biz/vo/order_wechant_status.go b/internal/biz/vo/order_wechant_status.go index 68654be..dd751be 100644 --- a/internal/biz/vo/order_wechant_status.go +++ b/internal/biz/vo/order_wechant_status.go @@ -37,7 +37,7 @@ func (s OrderWechatStatus) IsExpired() bool { } func (s OrderWechatStatus) CanNotify() bool { - return s.IsSuccess() || s.IsUse() + return s.IsSuccess() || s.IsUse() || s.IsExpired() } var OrderWechatStatusMap = map[OrderWechatStatus]string{ @@ -51,6 +51,7 @@ var OrderWechatStatusMap = map[OrderWechatStatus]string{ var OrderStatusMapCmbStatus = map[OrderWechatStatus]CmbStatus{ OrderWechatStatusSuccess: CmbStatusSuccess, OrderWechatStatusUse: CmbStatusUse, + OrderWechatStatusExpired: CmbStatusExpired, } func (s OrderWechatStatus) GetCmbStatusText() (CmbStatus, error) { diff --git a/internal/biz/wechat_notify_consume.go b/internal/biz/wechat_notify_consume.go index 39886ef..ee3a436 100644 --- a/internal/biz/wechat_notify_consume.go +++ b/internal/biz/wechat_notify_consume.go @@ -26,34 +26,36 @@ func (j *VoucherBiz) WechatNotifyConsumer(ctx context.Context, tag string, req * } if req.PlainText.Status.IsUsed() { - return j.wechatVoucherUsed(ctx, orderWechat) + err = j.wechatVoucherUsed(ctx, orderWechat) } else if req.PlainText.Status.IsExpired() { - return j.wechatVoucherExpired(ctx, orderWechat) + err = j.wechatVoucherExpired(ctx, orderWechat) } else { - return fmt.Errorf("未知通知类型:%s", req.PlainText.Status.GetText()) + err = fmt.Errorf("未知通知类型:%s", req.PlainText.Status.GetText()) } + + if err != nil { + return err + } + + return j.PushNotifyMQ(ctx, orderWechat.OrderNo, orderWechat.OutRequestNo) }) } -func (j *VoucherBiz) wechatVoucherUsed(ctx context.Context, orderWechat *bo.OrderWechatBo) error { +func (v *VoucherBiz) wechatVoucherUsed(ctx context.Context, orderWechat *bo.OrderWechatBo) error { if orderWechat.Status.IsUse() { return nil } - order, err := j.OrderRepo.GetByOrderNo(ctx, orderWechat.OrderNo) + order, err := v.OrderRepo.GetByOrderNo(ctx, orderWechat.OrderNo) if err != nil { return err } - if err = j.OrderWechatRepo.Used(ctx, orderWechat.ID); err != nil { + if err = v.OrderWechatRepo.Used(ctx, orderWechat.ID); err != nil { return err } - if err = j.OrderRepo.Used(ctx, order.ID); err != nil { - return err - } - - return nil + return v.OrderRepo.Used(ctx, order.ID) } func (j *VoucherBiz) wechatVoucherExpired(ctx context.Context, orderWechat *bo.OrderWechatBo) error { @@ -70,9 +72,5 @@ func (j *VoucherBiz) wechatVoucherExpired(ctx context.Context, orderWechat *bo.O return err } - if err = j.OrderRepo.Expired(ctx, order.ID); err != nil { - return err - } - - return nil + return j.OrderRepo.Expired(ctx, order.ID) }