/* * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the dreamlu.net developer nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * Author: Chill 庄骞 (smallchill@163.com) */ package org.springblade.modules.label.service.impl; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import io.swagger.models.auth.In; import org.springblade.common.node.TreeIntegerNode; import org.springblade.common.utils.NodeTreeUtil; import org.springblade.modules.label.entity.LabelEntity; import org.springblade.modules.label.mapper.LabelMapper; import org.springblade.modules.label.service.ILabelService; import org.springblade.modules.label.vo.LabelVO; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Optional; /** * 标签管理 服务实现类 * * @author BladeX * @since 2023-10-28 */ @Service public class LabelServiceImpl extends ServiceImpl implements ILabelService { @Override public IPage selectLabelPage(IPage page, LabelVO label) { return page.setRecords(baseMapper.selectLabelPage(page, label)); } /** * 标签查询,按父id查询下级 * * @param label * @return */ // @Override // public Object getLabelList(LabelVO label) { // Map labelList = baseMapper.getLabelList(label); // List list = new ArrayList<>(); // // 遍历 // labelList.forEach((id, treeNode) -> { // if (treeNode.getHasChildren()) { // list.add(id); // } // }); // if (list.size() > 0) { // // 查询子集 // Map childrenLabelList = baseMapper.getChildrenLabelList(list); // List list2 = new ArrayList<>(); // for (TreeIntegerNode value : childrenLabelList.values()) { // if(value.getHasChildren()){ // list2.add(value.getId()); // } // } // Map childrenLabelList2 = baseMapper.getChildrenLabelList(list2); // // 合并集合 // labelList.putAll(childrenLabelList); // labelList.putAll(childrenLabelList2); // } // // 处理并返回 // return NodeTreeUtil.getNodeTree(labelList); // } @Override public Object getLabelList(LabelVO label) { // 使用Optional避免空指针异常 Optional> labelListOptional = Optional.ofNullable(baseMapper.getLabelList(label)); if (!labelListOptional.isPresent()) { return null; // 返回空Map代替null } Map labelList = labelListOptional.get(); List list = new ArrayList<>(); // 遍历,筛选有子节点的标签 labelList.forEach((id, treeNode) -> { if (treeNode.getHasChildren()) { list.add(id); } }); if (!list.isEmpty()) { // 使用批量查询以提高性能 Map childrenLabelList = baseMapper.getChildrenLabelList(list); List list2 = new ArrayList<>(); // 筛选出有子节点的标签 childrenLabelList.values().forEach(value -> { if(value.getHasChildren()){ list2.add(value.getId()); } }); // 避免重复查询,直接合并结果 labelList.putAll(childrenLabelList); if (!list2.isEmpty()) { // 如果有第二层子节点,进行查询并合并 Map childrenLabelList2 = baseMapper.getChildrenLabelList(list2); labelList.putAll(childrenLabelList2); } } // 处理并返回标签树 return NodeTreeUtil.getNodeTree(labelList); // 假设NodeTreeUtil.getNodeTree已做空值检查 } @Override public List tree(LabelVO label) { Map labelTreeList = baseMapper.getLabelTreeList(label); List nodeTree = NodeTreeUtil.getNodeTree(labelTreeList); nodeTree.forEach(node -> recursion(node)); return nodeTree; } private void recursion(TreeIntegerNode node) { if (node.getChildren() != null && node.getChildren().size() > 0) { // BigDecimal bigDecimal = BigDecimal.valueOf(0); // node.setCount(node.getChildren().stream().map(item -> { // bigDecimal.add(BigDecimal.valueOf(item.getCount())); // })); node.getChildren().forEach(node2 -> recursion(node2)); } else { node.setChildren(null); } } /** * 查询人员标签(不包含人这一级) * @param label * @return */ @Override public List getPersonLabelList(LabelVO label) { Map labelTreeList = baseMapper.getPersonLabelList(label); List nodeTree = NodeTreeUtil.getNodeTree(labelTreeList); nodeTree.forEach(node -> recursion(node)); return nodeTree; } }