This commit is contained in:
@@ -71,7 +71,7 @@ func (s *Service) Create(ctx context.Context, req *dto.CreateExchangeRequest) (*
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if asset.AssetStatus != constants.AssetStatusSold {
|
||||
if !isExchangeableAssetStatus(asset.AssetStatus) {
|
||||
return nil, oldAssetStatusError(asset.AssetStatus)
|
||||
}
|
||||
|
||||
@@ -454,11 +454,14 @@ func (s *Service) resolveAssetByIdentifier(ctx context.Context, expectedAssetTyp
|
||||
return nil, errors.New(errors.CodeAssetNotFound)
|
||||
}
|
||||
|
||||
// isExchangeableAssetStatus 判断旧资产状态是否允许发起换货(在库和已销售均可换货)
|
||||
func isExchangeableAssetStatus(status int) bool {
|
||||
return status == constants.AssetStatusInStock || status == constants.AssetStatusSold
|
||||
}
|
||||
|
||||
// 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:
|
||||
@@ -500,7 +503,7 @@ func (s *Service) createDirectExchange(ctx context.Context, req *dto.CreateExcha
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if lockedOldAsset.AssetStatus != constants.AssetStatusSold {
|
||||
if !isExchangeableAssetStatus(lockedOldAsset.AssetStatus) {
|
||||
return oldAssetStatusError(lockedOldAsset.AssetStatus)
|
||||
}
|
||||
if err = s.ensureNoActiveExchangeWithTx(ctx, tx, lockedOldAsset.AssetType, lockedOldAsset.AssetID); err != nil {
|
||||
@@ -776,7 +779,7 @@ func (s *Service) validateExchangeAssetsWithTx(ctx context.Context, tx *gorm.DB,
|
||||
if oldAsset.AssetType != newAsset.AssetType {
|
||||
return errors.New(errors.CodeExchangeAssetTypeMismatch)
|
||||
}
|
||||
if oldAsset.AssetStatus != constants.AssetStatusSold {
|
||||
if !isExchangeableAssetStatus(oldAsset.AssetStatus) {
|
||||
return oldAssetStatusError(oldAsset.AssetStatus)
|
||||
}
|
||||
if newAsset.AssetStatus != constants.AssetStatusInStock {
|
||||
@@ -848,7 +851,7 @@ func (s *Service) updateAssetStatusesForCompletion(ctx context.Context, tx *gorm
|
||||
now := time.Now()
|
||||
if oldAsset.AssetType == constants.ExchangeAssetTypeIotCard {
|
||||
result := tx.WithContext(ctx).Model(&model.IotCard{}).
|
||||
Where("id = ? AND asset_status = ?", oldAsset.AssetID, constants.AssetStatusSold).
|
||||
Where("id = ? AND asset_status IN ?", oldAsset.AssetID, []int{constants.AssetStatusInStock, constants.AssetStatusSold}).
|
||||
Updates(map[string]any{"asset_status": constants.AssetStatusExchanged, "updated_at": now})
|
||||
if result.Error != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, result.Error, "更新旧卡状态失败")
|
||||
@@ -868,7 +871,7 @@ func (s *Service) updateAssetStatusesForCompletion(ctx context.Context, tx *gorm
|
||||
return nil
|
||||
}
|
||||
result := tx.WithContext(ctx).Model(&model.Device{}).
|
||||
Where("id = ? AND asset_status = ?", oldAsset.AssetID, constants.AssetStatusSold).
|
||||
Where("id = ? AND asset_status IN ?", oldAsset.AssetID, []int{constants.AssetStatusInStock, constants.AssetStatusSold}).
|
||||
Updates(map[string]any{"asset_status": constants.AssetStatusExchanged, "updated_at": now})
|
||||
if result.Error != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, result.Error, "更新旧设备状态失败")
|
||||
|
||||
Reference in New Issue
Block a user