This commit is contained in:
@@ -16,6 +16,8 @@ import (
|
||||
cardObservationApp "github.com/break/junhong_cmp_fiber/internal/application/cardobservation"
|
||||
packagedomain "github.com/break/junhong_cmp_fiber/internal/domain/package"
|
||||
"github.com/break/junhong_cmp_fiber/internal/infrastructure/audit"
|
||||
"github.com/break/junhong_cmp_fiber/internal/infrastructure/commissiondelivery"
|
||||
"github.com/break/junhong_cmp_fiber/internal/infrastructure/messaging/outbox"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
packagepkg "github.com/break/junhong_cmp_fiber/internal/service/package"
|
||||
"github.com/break/junhong_cmp_fiber/internal/service/packageprice"
|
||||
@@ -199,7 +201,6 @@ func (h *AutoPurchaseHandler) ProcessTask(ctx context.Context, task *asynq.Task)
|
||||
}
|
||||
}
|
||||
|
||||
var createdOrderID uint
|
||||
if err := h.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
wallet, walletErr := h.walletStore.GetByID(ctx, rechargeOrder.AssetWalletID)
|
||||
if walletErr != nil {
|
||||
@@ -225,7 +226,6 @@ func (h *AutoPurchaseHandler) ProcessTask(ctx context.Context, task *asynq.Task)
|
||||
if err = tx.Create(order).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
createdOrderID = order.ID
|
||||
|
||||
for _, item := range orderItems {
|
||||
item.OrderID = order.ID
|
||||
@@ -277,6 +277,9 @@ func (h *AutoPurchaseHandler) ProcessTask(ctx context.Context, task *asynq.Task)
|
||||
if resourceID == 0 {
|
||||
return pkgerrors.New(pkgerrors.CodeInvalidParam, "自动购包观测事件缺少载体")
|
||||
}
|
||||
if err = commissiondelivery.AppendCommissionCalculate(ctx, tx, outbox.NewRepository(), order.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
requestID := "card-observation:auto-purchase:" + strconv.FormatUint(uint64(order.ID), 10)
|
||||
if err = h.observationSeriesEvents.AppendSeriesRequested(ctx, tx, cardObservationApp.SeriesRequestedEvent{
|
||||
EventID: requestID, Scene: constants.CardObservationScenePackageChanged,
|
||||
@@ -306,30 +309,6 @@ func (h *AutoPurchaseHandler) ProcessTask(ctx context.Context, task *asynq.Task)
|
||||
return err
|
||||
}
|
||||
|
||||
// 事务提交成功后触发佣金计算(不在事务内,防止任务提交后事务回滚的数据一致性问题)
|
||||
if h.asynqClient != nil && createdOrderID > 0 {
|
||||
linkage := auditcontext.From(ctx)
|
||||
payloadBytes, marshalErr := sonic.Marshal(CommissionCalculationPayload{
|
||||
OrderID: createdOrderID, RequestID: linkage.RequestID,
|
||||
CorrelationID: linkage.CorrelationID, ParentEventID: linkage.ParentEventID,
|
||||
})
|
||||
if marshalErr != nil {
|
||||
h.logger.Warn("佣金任务载荷序列化失败",
|
||||
zap.Uint("order_id", createdOrderID),
|
||||
zap.Error(marshalErr))
|
||||
} else {
|
||||
commissionTask := asynq.NewTask(constants.TaskTypeCommission, payloadBytes,
|
||||
asynq.MaxRetry(3),
|
||||
asynq.Queue(constants.QueueForTaskType(constants.TaskTypeCommission)),
|
||||
)
|
||||
if _, enqueueErr := h.asynqClient.EnqueueContext(ctx, commissionTask); enqueueErr != nil {
|
||||
h.logger.Warn("自动购包后提交佣金任务失败",
|
||||
zap.Uint("order_id", createdOrderID),
|
||||
zap.Error(enqueueErr))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
h.logger.Info("自动购包任务执行成功", zap.Uint("recharge_record_id", rechargeOrder.ID))
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user