| | |
| | | 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; |
| | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.Set; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | // 转换为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)) { |