90 lines
2.2 KiB
Go
90 lines
2.2 KiB
Go
package biz
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"go.opentelemetry.io/otel/trace"
|
|
"time"
|
|
"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) PushQueryDelayMQ(ctx context.Context, orderNo string) error {
|
|
|
|
eventMap := v.bc.RocketMQ.EventMap["query"]
|
|
sendOption := []mq.SendOption{
|
|
mq.WithSendShardingKeysOption(orderNo),
|
|
mq.WithOpenTelemetryOption(trace.SpanFromContext(ctx).SpanContext().TraceID().String()),
|
|
mq.WithSendDelayLevelOption(300),
|
|
}
|
|
|
|
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) {
|
|
|
|
err = lock.NewMutex(v.rdb.Rdb, time.Second*30).Lock(ctx, fmt.Sprintf("order_consume_%s", orderNo), func(ctx context.Context) error {
|
|
|
|
order, err := v.OrderRepo.GetByOrderNo(ctx, orderNo)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if order.Type.IsCmb() {
|
|
return v.Cmb.OrderConsume(ctx, order)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
return
|
|
}
|
|
|
|
func (v *VoucherBiz) QueryConsume(ctx context.Context, orderNo string) (err error) {
|
|
|
|
err = lock.NewMutex(v.rdb.Rdb, time.Second*30).Lock(ctx, fmt.Sprintf("query_consume_%s", orderNo), func(ctx context.Context) error {
|
|
|
|
order, err := v.OrderRepo.GetByOrderNo(ctx, orderNo)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if order.Type.IsCmb() {
|
|
return v.Cmb.QueryConsume(ctx, order)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
return
|
|
}
|
|
|
|
func (v *VoucherBiz) NotifyConsume(ctx context.Context, orderNo 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 {
|
|
|
|
return nil
|
|
})
|
|
|
|
return
|
|
}
|