zhaowenyu
2026-02-05 04de920362c9dcc1cc6077b54acba8eeff5090fd
ard-inspection/src/main/java/com/ard/inspection/service/impl/ArdInspectionGroupServiceImpl.java
@@ -1,8 +1,10 @@
package com.ard.inspection.service.impl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import com.ard.common.annotation.DataScope;
import com.ard.common.constant.UserConstants;
import com.ard.common.core.domain.entity.SysUser;
import com.ard.common.exception.ServiceException;
@@ -11,6 +13,7 @@
import com.ard.common.utils.StringUtils;
import com.ard.common.utils.spring.SpringUtils;
import com.ard.inspection.domain.ArdInspectionGroup;
import com.ard.inspection.domain.GroupTreeSelect;
import com.ard.inspection.mapper.ArdInspectionGroupMapper;
import com.ard.inspection.service.IArdInspectionGroupService;
import org.springframework.beans.factory.annotation.Autowired;
@@ -46,7 +49,6 @@
     * @return 精细化巡检分组
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<ArdInspectionGroup> selectArdInspectionGroupList(ArdInspectionGroup ardInspectionGroup) {
        return ardInspectionGroupMapper.selectArdInspectionGroupList(ardInspectionGroup);
    }
@@ -59,6 +61,8 @@
     */
    @Override
    public int insertArdInspectionGroup(ArdInspectionGroup ardInspectionGroup) {
        ArdInspectionGroup inspectionGroup = ardInspectionGroupMapper.selectArdInspectionGroupByGroupId(ardInspectionGroup.getGroupParentId());
        ardInspectionGroup.setAncestors(inspectionGroup.getAncestors() + "," + ardInspectionGroup.getGroupParentId());
        ardInspectionGroup.setCreateTime(DateUtils.getNowDate());
        return ardInspectionGroupMapper.insertArdInspectionGroup(ardInspectionGroup);
    }
@@ -71,7 +75,6 @@
     */
    @Override
    public int updateArdInspectionGroup(ArdInspectionGroup ardInspectionGroup) {
        ArdInspectionGroup newGroup = ardInspectionGroupMapper.selectArdInspectionGroupByGroupId(ardInspectionGroup.getGroupParentId());
        ArdInspectionGroup oldGroup = ardInspectionGroupMapper.selectArdInspectionGroupByGroupId(ardInspectionGroup.getGroupId());
        if(StringUtils.isNotNull(newGroup) && StringUtils.isNotNull(oldGroup)){
@@ -170,4 +173,98 @@
        return result > 0;
    }
    /**
     * 查询分组树结构信息
     *
     * @param ardInspectionGroup 精细化巡检分组
     * @return 分组树信息集合
     */
    @Override
    public List<GroupTreeSelect> selectGroupTreeList(ArdInspectionGroup ardInspectionGroup)
    {
        List<ArdInspectionGroup> groups = selectArdInspectionGroupList(ardInspectionGroup);
        return buildGroupTreeSelect(groups);
    }
    /**
     * 构建前端所需要下拉树结构
     *
     * @param groups 分组列表
     * @return 下拉树结构列表
     */
    @Override
    public List<GroupTreeSelect> buildGroupTreeSelect(List<ArdInspectionGroup> groups){
        List<ArdInspectionGroup> groupTrees = buildGroupTree(groups);
        return groupTrees.stream().map(GroupTreeSelect::new).collect(Collectors.toList());
    }
    /**
     * 构建前端所需要树结构
     *
     * @param groups 分组列表
     * @return 树结构列表
     */
    @Override
    public List<ArdInspectionGroup> buildGroupTree(List<ArdInspectionGroup> groups){
        List<ArdInspectionGroup> returnList = new ArrayList<ArdInspectionGroup>();
        List<Long> tempList = groups.stream().map(ArdInspectionGroup::getGroupId).collect(Collectors.toList());
        for (ArdInspectionGroup group : groups)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(group.getGroupParentId()))
            {
                recursionFn(groups, group);
                returnList.add(group);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = groups;
        }
        return returnList;
    }
    /**
     * 递归列表
     */
    private void recursionFn(List<ArdInspectionGroup> list, ArdInspectionGroup t)
    {
        // 得到子节点列表
        List<ArdInspectionGroup> childList = getChildList(list, t);
        t.setChildren(childList);
        for (ArdInspectionGroup tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<ArdInspectionGroup> getChildList(List<ArdInspectionGroup> list, ArdInspectionGroup t)
    {
        List<ArdInspectionGroup> tlist = new ArrayList<ArdInspectionGroup>();
        Iterator<ArdInspectionGroup> it = list.iterator();
        while (it.hasNext())
        {
            ArdInspectionGroup n = (ArdInspectionGroup) it.next();
            if (StringUtils.isNotNull(n.getGroupParentId()) && n.getGroupParentId().longValue() == t.getGroupId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<ArdInspectionGroup> list, ArdInspectionGroup t)
    {
        return getChildList(list, t).size() > 0;
    }
}