订单相关以及佣金相关修复
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m57s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m57s
This commit is contained in:
@@ -78,8 +78,11 @@ func (s *Service) CalculateCommission(ctx context.Context, orderID uint) error {
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "获取订单失败")
|
||||
}
|
||||
|
||||
if order.CommissionStatus == model.CommissionStatusCalculated {
|
||||
s.logger.Warn("订单佣金已计算,跳过", zap.String("order_id", fmt.Sprint(orderID)))
|
||||
if order.CommissionStatus == model.CommissionStatusCompleted || order.CommissionStatus == model.CommissionStatusPendingReview {
|
||||
s.logger.Warn("订单佣金流程已结束,跳过",
|
||||
zap.String("order_id", fmt.Sprint(orderID)),
|
||||
zap.Int("commission_status", order.CommissionStatus),
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -88,13 +91,8 @@ func (s *Service) CalculateCommission(ctx context.Context, orderID uint) error {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "计算成本价差佣金失败")
|
||||
}
|
||||
|
||||
for _, record := range costDiffRecords {
|
||||
if err := tx.Create(record).Error; err != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "创建成本价差佣金记录失败")
|
||||
}
|
||||
if err := s.creditCommissionInTx(ctx, tx, record); err != nil {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "佣金入账失败")
|
||||
}
|
||||
if err := s.persistCommissionRecordsInTx(ctx, tx, costDiffRecords); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 代购订单不触发一次性佣金和累计充值更新
|
||||
@@ -110,8 +108,16 @@ func (s *Service) CalculateCommission(ctx context.Context, orderID uint) error {
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Model(&model.Order{}).Where("id = ?", orderID).Update("commission_status", model.CommissionStatusCalculated).Error; err != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "更新订单佣金状态失败")
|
||||
commissionStatus, commissionResult, err := s.resolveOrderCommissionOutcomeInTx(tx, order.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Model(&model.Order{}).Where("id = ?", orderID).Updates(map[string]any{
|
||||
"commission_status": commissionStatus,
|
||||
"commission_result": commissionResult,
|
||||
}).Error; err != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "更新订单佣金结果失败")
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -190,12 +196,7 @@ func (s *Service) CalculateCostDiffCommission(ctx context.Context, order *model.
|
||||
Status: constants.CommissionStatusPendingReview,
|
||||
Remark: fmt.Sprintf("套餐系列[%d]未分配给该代理(shop_id=%d),成本价差链路断裂,请人工核查", *order.SeriesID, currentShop.ID),
|
||||
}
|
||||
if createErr := s.commissionRecordStore.Create(ctx, pendingRecord); createErr != nil {
|
||||
s.logger.Warn("创建待审佣金记录失败",
|
||||
zap.Uint("shop_id", currentShop.ID),
|
||||
zap.Uint("order_id", order.ID),
|
||||
zap.Error(createErr))
|
||||
}
|
||||
records = append(records, pendingRecord)
|
||||
break
|
||||
}
|
||||
|
||||
@@ -284,13 +285,8 @@ func (s *Service) triggerOneTimeCommissionForCardInTx(ctx context.Context, tx *g
|
||||
return err
|
||||
}
|
||||
|
||||
for _, record := range records {
|
||||
if err := tx.Create(record).Error; err != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "创建一次性佣金记录失败")
|
||||
}
|
||||
if err := s.creditCommissionInTx(ctx, tx, record); err != nil {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "一次性佣金入账失败")
|
||||
}
|
||||
if err := s.persistCommissionRecordsInTx(ctx, tx, records); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
card.SetFirstRechargeTriggeredBySeries(seriesID, true)
|
||||
@@ -368,13 +364,8 @@ func (s *Service) triggerOneTimeCommissionForDeviceInTx(ctx context.Context, tx
|
||||
return err
|
||||
}
|
||||
|
||||
for _, record := range records {
|
||||
if err := tx.Create(record).Error; err != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "创建一次性佣金记录失败")
|
||||
}
|
||||
if err := s.creditCommissionInTx(ctx, tx, record); err != nil {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "一次性佣金入账失败")
|
||||
}
|
||||
if err := s.persistCommissionRecordsInTx(ctx, tx, records); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
device.SetFirstRechargeTriggeredBySeries(seriesID, true)
|
||||
@@ -544,12 +535,7 @@ func (s *Service) calculateChainOneTimeCommission(ctx context.Context, bottomSho
|
||||
Status: constants.CommissionStatusPendingReview,
|
||||
Remark: fmt.Sprintf("套餐系列[%d]未分配给该代理(shop_id=%d),一次性佣金链路断裂,请人工核查", seriesID, parentShopID),
|
||||
}
|
||||
if createErr := s.commissionRecordStore.Create(ctx, pendingRecord); createErr != nil {
|
||||
s.logger.Warn("创建待审佣金记录失败",
|
||||
zap.Uint("shop_id", parentShopID),
|
||||
zap.Uint("order_id", order.ID),
|
||||
zap.Error(createErr))
|
||||
}
|
||||
records = append(records, pendingRecord)
|
||||
break
|
||||
}
|
||||
|
||||
@@ -686,6 +672,48 @@ func (s *Service) creditCommissionInTx(ctx context.Context, tx *gorm.DB, record
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) persistCommissionRecordsInTx(ctx context.Context, tx *gorm.DB, records []*model.CommissionRecord) error {
|
||||
for _, record := range records {
|
||||
if err := tx.Create(record).Error; err != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "创建佣金记录失败")
|
||||
}
|
||||
if record.Status == constants.CommissionStatusPendingReview {
|
||||
continue
|
||||
}
|
||||
if err := s.creditCommissionInTx(ctx, tx, record); err != nil {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "佣金入账失败")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) resolveOrderCommissionOutcomeInTx(tx *gorm.DB, orderID uint) (int, int, error) {
|
||||
var pendingReviewCount int64
|
||||
if err := tx.Model(&model.CommissionRecord{}).
|
||||
Where("order_id = ? AND status = ?", orderID, constants.CommissionStatusPendingReview).
|
||||
Count(&pendingReviewCount).Error; err != nil {
|
||||
return 0, 0, errors.Wrap(errors.CodeDatabaseError, err, "统计待人工处理佣金记录失败")
|
||||
}
|
||||
|
||||
if pendingReviewCount > 0 {
|
||||
return model.CommissionStatusPendingReview, model.CommissionResultChainBroken, nil
|
||||
}
|
||||
|
||||
var releasedCount int64
|
||||
if err := tx.Model(&model.CommissionRecord{}).
|
||||
Where("order_id = ? AND status = ? AND amount > 0", orderID, constants.CommissionStatusReleased).
|
||||
Count(&releasedCount).Error; err != nil {
|
||||
return 0, 0, errors.Wrap(errors.CodeDatabaseError, err, "统计已发放佣金记录失败")
|
||||
}
|
||||
|
||||
if releasedCount > 0 {
|
||||
return model.CommissionStatusCompleted, model.CommissionResultHasAmount, nil
|
||||
}
|
||||
|
||||
return model.CommissionStatusCompleted, model.CommissionResultNoAmount, nil
|
||||
}
|
||||
|
||||
func (s *Service) CreditCommission(ctx context.Context, record *model.CommissionRecord) error {
|
||||
return s.db.Transaction(func(tx *gorm.DB) error {
|
||||
return s.creditCommissionInTx(ctx, tx, record)
|
||||
|
||||
Reference in New Issue
Block a user