From 236fb6ff1f6a955db5f78560204e042e977e2167 Mon Sep 17 00:00:00 2001
From: mitao <2763622819@qq.com>
Date: 星期一, 22 九月 2025 20:47:52 +0800
Subject: [PATCH] 资产入库审批通过保存资产数据

---
 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AssetTypeServiceImpl.java |   26 +++++++++++++++++++++++++-
 1 files changed, 25 insertions(+), 1 deletions(-)

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 583b616..786edc5 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
@@ -3,6 +3,7 @@
 import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.collection.CollUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.SecurityUtils;
@@ -14,6 +15,7 @@
 import com.ruoyi.system.service.AssetTypeService;
 import com.ruoyi.system.vo.asset.AssetTypeTreeVO;
 import lombok.RequiredArgsConstructor;
+import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -21,6 +23,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.Set;
 import java.util.stream.Collectors;
 
 /**
@@ -32,7 +35,7 @@
  * @since 2025-09-15
  */
 @Service
-@RequiredArgsConstructor
+@RequiredArgsConstructor(onConstructor_ = {@Lazy})
 public class AssetTypeServiceImpl extends ServiceImpl<AssetTypeMapper, AssetType> implements AssetTypeService {
 
     private final AssetMainService assetMainService;
@@ -53,10 +56,31 @@
         // 转换为VO对象
         List<AssetTypeTreeVO> assetTypeVOs = BeanUtil.copyToList(allAssetTypes, AssetTypeTreeVO.class);
         
+        // 预查询:找出存在资产关联的资产类型ID集合(去重)
+        List<Integer> allTypeIds = allAssetTypes.stream().map(AssetType::getId).collect(Collectors.toList());
+        QueryWrapper<AssetMain> usedTypeQuery = new QueryWrapper<>();
+        usedTypeQuery.select("distinct asset_type_id").in("asset_type_id", allTypeIds);
+        List<AssetMain> usedTypeRows = assetMainService.list(usedTypeQuery);
+        Set<Integer> usedTypeIdSet = usedTypeRows.stream()
+                .map(AssetMain::getAssetTypeId)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toSet());
+
         // 按父级ID分组
         Map<Integer, List<AssetTypeTreeVO>> parentIdMap = assetTypeVOs.stream()
                 .collect(Collectors.groupingBy(AssetTypeTreeVO::getParentId));
         
+        // 计算可删除标记:一级仅判断是否有子分类;二级仅判断是否有关联资产
+        for (AssetTypeTreeVO vo : assetTypeVOs) {
+            if (Objects.equals(vo.getLevel(), 1)) {
+                boolean hasChildren = parentIdMap.containsKey(vo.getId()) && CollUtil.isNotEmpty(parentIdMap.get(vo.getId()));
+                vo.setCanDelete(!hasChildren);
+            } else {
+                boolean hasAssets = usedTypeIdSet.contains(vo.getId());
+                vo.setCanDelete(!hasAssets);
+            }
+        }
+
         // 构建树形结构
         List<AssetTypeTreeVO> rootNodes = parentIdMap.get(0);
         if (CollUtil.isEmpty(rootNodes)) {

--
Gitblit v1.7.1