ArchivesDeptServiceImpl.java 4.74 KB
package com.ruoyi.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.domain.ArchivesDept;
import com.ruoyi.mapper.ArchivesDeptMapper;
import com.ruoyi.util.TreeSelect;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.service.IArchivesDeptService;

/**
 * 部门Service业务层处理
 * 
 * @author li
 * @date 2022-08-01
 */
@Service
public class ArchivesDeptServiceImpl implements IArchivesDeptService 
{
    @Autowired
    private ArchivesDeptMapper archivesDeptMapper;
    /**
     * 查询部门
     * 
     * @param deptId 部门主键
     * @return 部门
     */
    @Override
    public ArchivesDept selectArchivesDeptByDeptId(Long deptId)
    {
        return archivesDeptMapper.selectArchivesDeptByDeptId(deptId);
    }

    /**
     * 查询部门列表
     * 
     * @param archivesDept 部门
     * @return 部门
     */
    @Override
    public List<ArchivesDept> selectArchivesDeptList(ArchivesDept archivesDept)
    {
        return archivesDeptMapper.selectArchivesDeptList(archivesDept);
    }

    /**
     * 新增部门
     * 
     * @param archivesDept 部门
     * @return 结果
     */
    @Override
    public int insertArchivesDept(ArchivesDept archivesDept)
    {
        archivesDept.setCreateTime(DateUtils.getNowDate());
        return archivesDeptMapper.insertArchivesDept(archivesDept);
    }

    /**
     * 修改部门
     * 
     * @param archivesDept 部门
     * @return 结果
     */
    @Override
    public int updateArchivesDept(ArchivesDept archivesDept)
    {
        archivesDept.setUpdateTime(DateUtils.getNowDate());
        return archivesDeptMapper.updateArchivesDept(archivesDept);
    }

    /**
     * 批量删除部门
     * 
     * @param deptIds 需要删除的部门主键
     * @return 结果
     */
    @Override
    public int deleteArchivesDeptByDeptIds(Long[] deptIds)
    {
        return archivesDeptMapper.deleteArchivesDeptByDeptIds(deptIds);
    }

    /**
     * 删除部门信息
     * 
     * @param deptId 部门主键
     * @return 结果
     */
    @Override
    public int deleteArchivesDeptByDeptId(Long deptId)
    {
        return archivesDeptMapper.deleteArchivesDeptByDeptId(deptId);
    }



    /**
     * 构建前端所需要下拉树结构
     *
     * @param depts 部门列表
     * @return 下拉树结构列表
     */
    public List<TreeSelect> buildDeptTreeSelect(List<ArchivesDept> depts){

        List<ArchivesDept> deptTrees = buildDeptTree(depts);       
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }
    /**
     * 构建前端所需要树结构
     *
     * @param depts 部门列表
     * @return 树结构列表
     */
    @Override
    public List<ArchivesDept> buildDeptTree(List<ArchivesDept> depts)
    {
        List<ArchivesDept> returnList = new ArrayList<ArchivesDept>();
        List<Long> tempList = new ArrayList<Long>();
        for (ArchivesDept dept : depts)
        {
            tempList.add(dept.getDeptId());
        }
        for (ArchivesDept dept : depts)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentId()))
            {
                recursionFn(depts, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = depts;
        }
        return returnList;
    }
    /**
     * 递归列表
     */
    private void recursionFn(List<ArchivesDept> list, ArchivesDept t)
    {
        // 得到子节点列表
        List<ArchivesDept> childList = getChildList(list, t);
        t.setChildren(childList);
        for (ArchivesDept tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<ArchivesDept> getChildList(List<ArchivesDept> list, ArchivesDept t)
    {
        List<ArchivesDept> tlist = new ArrayList<ArchivesDept>();
        Iterator<ArchivesDept> it = list.iterator();
        while (it.hasNext())
        {
            ArchivesDept n = (ArchivesDept) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<ArchivesDept> list, ArchivesDept t)
    {
        return getChildList(list, t).size() > 0;
    }



}