53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package biz
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"go.opentelemetry.io/otel/trace"
|
|
"voucher/internal/biz/vo"
|
|
"voucher/internal/pkg/lock"
|
|
"voucher/internal/pkg/mq"
|
|
)
|
|
|
|
func (v *VoucherBiz) PushOrderMQ(ctx context.Context, orderNo string) error {
|
|
|
|
eventMap := v.bc.RocketMQ.EventMap["order"]
|
|
sendOption := []mq.SendOption{
|
|
mq.WithSendShardingKeysOption(fmt.Sprintf("%s", orderNo)),
|
|
mq.WithOpenTelemetryOption(trace.SpanFromContext(ctx).SpanContext().TraceID().String()),
|
|
}
|
|
|
|
if err := v.MqSendMixRepo.SendSync(ctx, eventMap.Topic, []byte("{}"), sendOption...); err != nil {
|
|
return fmt.Errorf("收单成功,消费队列投递失败[%v]", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (v *VoucherBiz) OrderConsume(ctx context.Context, orderNo string) (err error) {
|
|
|
|
c := vo.OrderConsume.BuildCache([]string{orderNo})
|
|
|
|
return lock.NewMutex(v.rdb.Rdb, c.TTL).Lock(ctx, c.Key, func(ctx context.Context) error {
|
|
|
|
order, err := v.OrderRepo.GetByOrderNo(ctx, orderNo)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if order.Type.IsCmb() {
|
|
|
|
outRequestNo, err := v.Cmb.OrderConsume(ctx, order)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return v.PushNotifyMQ(ctx, orderNo, outRequestNo)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("订单类型错误:%s", order.Type.GetText())
|
|
})
|
|
|
|
}
|