zhaowenyu
2026-03-07 d7fef91a4cbb702988dc820bcde82c166253ceec
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,7 +61,16 @@
     */
    @Override
    public int insertArdInspectionGroup(ArdInspectionGroup ardInspectionGroup) {
        if (ardInspectionGroup.getGroupParentId() == null){
            ardInspectionGroup.setGroupId((long) (ardInspectionGroupMapper.selectMaxGroupId() + 100));
            ardInspectionGroup.setGroupParentId(0L);
            ardInspectionGroup.setAncestors("0");
        }else {
            ArdInspectionGroup inspectionGroup = ardInspectionGroupMapper.selectArdInspectionGroupByGroupId(ardInspectionGroup.getGroupParentId());
            ardInspectionGroup.setAncestors(inspectionGroup.getAncestors() + "," + ardInspectionGroup.getGroupParentId());
        }
        ardInspectionGroup.setCreateTime(DateUtils.getNowDate());
        ardInspectionGroup.setDelFlag("0");
        return ardInspectionGroupMapper.insertArdInspectionGroup(ardInspectionGroup);
    }
@@ -71,7 +82,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)){
@@ -121,8 +131,9 @@
    @Override
    public boolean checkInspectionGroupNameUnique(ArdInspectionGroup ardInspectionGroup) {
        Long groupId = StringUtils.isNull(ardInspectionGroup.getGroupId()) ? -1L : ardInspectionGroup.getGroupId();
        ArdInspectionGroup info = ardInspectionGroupMapper.checkInspectionGroupNameUnique(ardInspectionGroup.getGroupName(),ardInspectionGroup.getGroupParentId());
        if (StringUtils.isNotNull(info) && info.getGroupId().longValue() != groupId.longValue())
        ArdInspectionGroup infoA = ardInspectionGroupMapper.checkInspectionGroupNameUniqueA(ardInspectionGroup.getGroupName(),ardInspectionGroup.getGroupParentId());
        ArdInspectionGroup infoB = ardInspectionGroupMapper.checkInspectionGroupNameUniqueB(ardInspectionGroup.getGroupName());
        if (StringUtils.isNotNull(infoB) && infoB.getGroupId().longValue() != groupId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
@@ -170,4 +181,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;
    }
}