69 lines
1.9 KiB
Go
69 lines
1.9 KiB
Go
package biz
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"go.opentelemetry.io/otel/trace"
|
||
errPb "voucher/api/err"
|
||
"voucher/internal/biz/bo"
|
||
"voucher/internal/biz/vo"
|
||
"voucher/internal/pkg/lock"
|
||
"voucher/internal/pkg/mq"
|
||
)
|
||
|
||
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) error {
|
||
|
||
var (
|
||
err error
|
||
orderNotify *bo.OrderNotifyBo
|
||
cache = vo.NotifyConsume.BuildCache([]string{orderNo})
|
||
)
|
||
|
||
err = lock.NewMutex(v.rdb.Rdb, cache.TTL).Lock(ctx, cache.Key, func(ctx context.Context) error {
|
||
|
||
order, err2 := v.OrderRepo.GetByOrderNo(ctx, orderNo)
|
||
if err2 != nil {
|
||
return err
|
||
}
|
||
|
||
if !order.Status.CanNotify() {
|
||
return fmt.Errorf("订单状态错误,不能通知:%s", order.Status.GetText())
|
||
}
|
||
|
||
if !order.Channel.IsWeChat() {
|
||
return fmt.Errorf("暂不支持订单%s渠道%s回调通知处理", order.OrderNo, order.Channel.GetText())
|
||
}
|
||
|
||
if order.Type.IsCmb() {
|
||
if orderNotify, err2 = v.Cmb.NotifyConsume(ctx, order, orderOutRequestNo); err2 != nil {
|
||
return err
|
||
}
|
||
}
|
||
|
||
return fmt.Errorf("订单类型错误:%s", order.Type.GetText())
|
||
})
|
||
|
||
if !errPb.IsNeedRetryNotify(err) {
|
||
return err
|
||
}
|
||
|
||
// 状态回调接口失败需要支持重试 重试间隔为1分钟、2分钟、12分钟、60分钟、360分钟
|
||
// 第一次通知失败重试入队
|
||
return v.PushNotifyRetryDelayMQ(ctx, 60, orderNotify.ID)
|
||
}
|