From a637bf49486d072ff65771864c50d1586989d940 Mon Sep 17 00:00:00 2001 From: mitao <2763622819@qq.com> Date: 星期一, 15 九月 2025 20:46:52 +0800 Subject: [PATCH] 资产管理-资产类型批量删除接口 --- ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AssetTypeServiceImpl.java | 31 +++++++++++++++++++++++++++++-- ruoyi-system/src/main/java/com/ruoyi/system/service/AssetTypeService.java | 6 ++++++ ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/AssetTypeController.java | 7 +++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/AssetTypeController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/AssetTypeController.java index 59df24a..6d6c444 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/AssetTypeController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/AssetTypeController.java @@ -65,6 +65,13 @@ assetTypeService.deleteAssetType(id); return R.ok(); } + + @ApiOperation("批量删除资产类型") + @PostMapping("/batch-delete") + public R<Void> batchDeleteAssetType(@RequestBody List<Integer> ids) { + assetTypeService.batchDeleteAssetType(ids); + return R.ok(); + } } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/AssetTypeService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/AssetTypeService.java index 65c62d6..a37323f 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/AssetTypeService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/AssetTypeService.java @@ -41,4 +41,10 @@ */ void deleteAssetType(Integer id); + /** + * 批量删除资产类型 + * @param ids 资产类型ID列表 + */ + void batchDeleteAssetType(List<Integer> ids); + } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AssetTypeServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AssetTypeServiceImpl.java index 7c928c0..e7e0f4d 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AssetTypeServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AssetTypeServiceImpl.java @@ -15,6 +15,7 @@ import com.ruoyi.system.vo.asset.AssetTypeTreeVO; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; @@ -191,7 +192,7 @@ childQueryWrapper.eq(AssetType::getParentId, id); long childCount = this.count(childQueryWrapper); if (childCount > 0) { - throw new ServiceException("该资产类型存在子类型,不能删除"); + throw new ServiceException(String.format("【%s】存在子类型,不能删除", existingAssetType.getTypeName())); } // 校验是否有关联的资产记录 @@ -199,11 +200,37 @@ assetMainQueryWrapper.eq(AssetMain::getAssetTypeId, id); long assetMainCount = assetMainService.count(assetMainQueryWrapper); if (assetMainCount > 0) { - throw new ServiceException("该资产类型已关联资产记录,不能删除"); + throw new ServiceException(String.format("【%s】已关联资产记录,不能删除", existingAssetType.getTypeName())); } // 删除资产类型 this.removeById(id); } + @Override + @Transactional(rollbackFor = Exception.class) + public void batchDeleteAssetType(List<Integer> ids) { + // 校验ID列表是否为空 + if (CollUtil.isEmpty(ids)) { + throw new ServiceException("删除的资产类型ID列表不能为空"); + } + + // 收集删除失败的信息 + List<String> failedMessages = new ArrayList<>(); + + // 逐个删除资产类型 + for (Integer id : ids) { + try { + this.deleteAssetType(id); + } catch (ServiceException e) { + failedMessages.add(e.getMessage()); + } + } + + // 如果有删除失败的情况,抛出异常 + if (CollUtil.isNotEmpty(failedMessages)) { + throw new ServiceException("批量删除失败:" + String.join(";", failedMessages)); + } + } + } -- Gitblit v1.7.1