All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 8m32s
79 lines
2.1 KiB
Go
79 lines
2.1 KiB
Go
package task
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/bytedance/sonic"
|
|
"github.com/hibiken/asynq"
|
|
"go.uber.org/zap"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/break/junhong_cmp_fiber/internal/service/commission_calculation"
|
|
"github.com/break/junhong_cmp_fiber/pkg/auditcontext"
|
|
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
|
)
|
|
|
|
const (
|
|
TypeCommissionCalculation = "commission:calculate"
|
|
)
|
|
|
|
type CommissionCalculationPayload struct {
|
|
OrderID uint `json:"order_id"`
|
|
RequestID string `json:"request_id,omitempty"`
|
|
CorrelationID string `json:"correlation_id,omitempty"`
|
|
ParentEventID string `json:"parent_event_id,omitempty"`
|
|
}
|
|
|
|
type CommissionCalculationHandler struct {
|
|
db *gorm.DB
|
|
service *commission_calculation.Service
|
|
logger *zap.Logger
|
|
}
|
|
|
|
func NewCommissionCalculationHandler(
|
|
db *gorm.DB,
|
|
service *commission_calculation.Service,
|
|
logger *zap.Logger,
|
|
) *CommissionCalculationHandler {
|
|
return &CommissionCalculationHandler{
|
|
db: db,
|
|
service: service,
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
func (h *CommissionCalculationHandler) HandleCommissionCalculation(ctx context.Context, task *asynq.Task) error {
|
|
var payload CommissionCalculationPayload
|
|
if err := sonic.Unmarshal(task.Payload(), &payload); err != nil {
|
|
h.logger.Error("解析佣金计算任务载荷失败",
|
|
zap.Error(err),
|
|
zap.String("task_id", task.ResultWriter().TaskID()),
|
|
)
|
|
return asynq.SkipRetry
|
|
}
|
|
correlationID := payload.CorrelationID
|
|
if correlationID == "" {
|
|
correlationID = task.ResultWriter().TaskID()
|
|
}
|
|
ctx = auditcontext.With(ctx, auditcontext.Context{
|
|
ActorKind: constants.AuditActorSystemTask, ActorID: constants.AuditActorIDCommissionCalculationWorker,
|
|
ActorName: "订单佣金计算任务", Source: constants.AuditSourceWorker,
|
|
RequestID: payload.RequestID, CorrelationID: correlationID,
|
|
ParentEventID: payload.ParentEventID,
|
|
})
|
|
|
|
if err := h.service.CalculateCommission(ctx, payload.OrderID); err != nil {
|
|
h.logger.Error("佣金计算失败",
|
|
zap.Uint("order_id", payload.OrderID),
|
|
zap.Error(err),
|
|
)
|
|
return err
|
|
}
|
|
|
|
h.logger.Info("佣金计算成功",
|
|
zap.Uint("order_id", payload.OrderID),
|
|
)
|
|
|
|
return nil
|
|
}
|