diff --git a/internal/service/exchange/service.go b/internal/service/exchange/service.go index c18c1d4..904246f 100644 --- a/internal/service/exchange/service.go +++ b/internal/service/exchange/service.go @@ -72,7 +72,7 @@ func (s *Service) Create(ctx context.Context, req *dto.CreateExchangeRequest) (* return nil, err } if asset.AssetStatus != constants.AssetStatusSold { - return nil, errors.New(errors.CodeExchangeStatusInvalid, "旧资产状态不允许换货") + return nil, oldAssetStatusError(asset.AssetStatus) } if _, err = s.exchangeStore.FindActiveByOldAsset(ctx, asset.AssetType, asset.AssetID); err == nil { @@ -454,6 +454,20 @@ func (s *Service) resolveAssetByIdentifier(ctx context.Context, expectedAssetTyp return nil, errors.New(errors.CodeAssetNotFound) } +// oldAssetStatusError 根据旧资产的实际状态返回具体的错误原因 +func oldAssetStatusError(status int) error { + switch status { + case constants.AssetStatusInStock: + return errors.New(errors.CodeExchangeStatusInvalid, "该资产尚未销售给客户,无法发起换货") + case constants.AssetStatusExchanged: + return errors.New(errors.CodeExchangeStatusInvalid, "该资产已完成过换货,如需再次换货请使用换货后的新资产") + case constants.AssetStatusDeactivated: + return errors.New(errors.CodeExchangeStatusInvalid, "该资产已停用,无法发起换货") + default: + return errors.New(errors.CodeExchangeStatusInvalid, "该资产当前状态不允许换货") + } +} + func normalizeExchangeFlowType(flowType string) string { flowType = strings.TrimSpace(flowType) if flowType == "" { @@ -487,7 +501,7 @@ func (s *Service) createDirectExchange(ctx context.Context, req *dto.CreateExcha return err } if lockedOldAsset.AssetStatus != constants.AssetStatusSold { - return errors.New(errors.CodeExchangeStatusInvalid, "旧资产状态不允许换货") + return oldAssetStatusError(lockedOldAsset.AssetStatus) } if err = s.ensureNoActiveExchangeWithTx(ctx, tx, lockedOldAsset.AssetType, lockedOldAsset.AssetID); err != nil { return err @@ -763,7 +777,7 @@ func (s *Service) validateExchangeAssetsWithTx(ctx context.Context, tx *gorm.DB, return errors.New(errors.CodeExchangeAssetTypeMismatch) } if oldAsset.AssetStatus != constants.AssetStatusSold { - return errors.New(errors.CodeExchangeStatusInvalid, "旧资产状态不允许换货") + return oldAssetStatusError(oldAsset.AssetStatus) } if newAsset.AssetStatus != constants.AssetStatusInStock { return errors.New(errors.CodeExchangeNewAssetNotInStock) @@ -840,7 +854,7 @@ func (s *Service) updateAssetStatusesForCompletion(ctx context.Context, tx *gorm return errors.Wrap(errors.CodeDatabaseError, result.Error, "更新旧卡状态失败") } if result.RowsAffected == 0 { - return errors.New(errors.CodeExchangeStatusInvalid, "旧资产状态不允许换货") + return errors.New(errors.CodeExchangeStatusInvalid, "旧资产状态已被修改,请重试") } result = tx.WithContext(ctx).Model(&model.IotCard{}). Where("id = ? AND asset_status = ?", newAsset.AssetID, constants.AssetStatusInStock). @@ -860,7 +874,7 @@ func (s *Service) updateAssetStatusesForCompletion(ctx context.Context, tx *gorm return errors.Wrap(errors.CodeDatabaseError, result.Error, "更新旧设备状态失败") } if result.RowsAffected == 0 { - return errors.New(errors.CodeExchangeStatusInvalid, "旧资产状态不允许换货") + return errors.New(errors.CodeExchangeStatusInvalid, "旧资产状态已被修改,请重试") } result = tx.WithContext(ctx).Model(&model.Device{}). Where("id = ? AND asset_status = ?", newAsset.AssetID, constants.AssetStatusInStock).