package com.vctgo.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vctgo.common.core.constant.UserConstants;
import com.vctgo.common.core.exception.ServiceException;
import com.vctgo.common.core.text.Convert;
import com.vctgo.common.core.utils.SpringUtils;
import com.vctgo.common.core.utils.StrUtils;
import com.vctgo.common.datascope.annotation.DataScope;
import com.vctgo.common.mybatisplus.aop.TemporaryIgnoreTables;
import com.vctgo.common.security.utils.SecurityUtils;
import com.vctgo.system.api.domain.SysDept;
import com.vctgo.system.api.domain.SysRole;
import com.vctgo.system.api.domain.SysTenant;
import com.vctgo.system.api.domain.SysUser;
import com.vctgo.system.domain.SysReGardenBranch;
import com.vctgo.system.domain.vo.TreeSelect;
import com.vctgo.system.mapper.*;
import com.vctgo.system.service.ISysDeptService;
import com.vctgo.system.util.ConstantUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 部门管理 服务实现
 *
 * @author vctgo
 */
@Service
public class SysDeptServiceImpl implements ISysDeptService
{
    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private SysTenantMapper tenantMapper;

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private SysReGardenBranchMapper reGardenBranchMapper;

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询部门管理数据
     *
     * @param dept 部门信息
     * @return 部门信息集合
     */
    @TemporaryIgnoreTables
    @Override
    @DataScope(deptAlias = "d")
    public List<SysDept> selectDeptList(SysDept dept) {

        //获取当前租户类型，判断当前租户是集团租户还是独立幼儿园租户
        SysTenant sysTenant = tenantMapper.selectById(SecurityUtils.getTenantId());
        List<SysTenant> branchTenantList = new ArrayList<>();
        if (sysTenant.getTenantType() == 1) {
            //更新幼儿园sys_dept表的parent_id值
            dept.getParams().put("isGardenTenant", sysTenant.getTenantType());
            SecurityUtils.setTemporaryIgnoreTables(Arrays.asList("sys_dept", "sys_user"));
            //查询当前租户存在哪些幼儿园租户
            SysTenant conditionTenant = new SysTenant();
            conditionTenant.setGardenId(sysTenant.getGardenId());
            branchTenantList = tenantMapper.selectSysTenantList(conditionTenant);
        } else if (sysTenant.getTenantType() == 2) {
            //查询该集团幼儿园的组织数据
            dept.getParams().put("isSingleTenant", sysTenant.getId());
        } else if (sysTenant.getTenantType() == 3) {
            //查询该独立幼儿园的组织数据
            dept.getParams().put("isSingleTenant", sysTenant.getId());
        }
        dept.getParams().put("dataScope", "");
        dept.setTenantId(sysTenant.getId());
        boolean temporaryIgnore = 1 == sysTenant.getTenantType() || 2 == sysTenant.getTenantType();
        if (temporaryIgnore) {
            SecurityUtils.setTemporaryIgnoreTables(Collections.singletonList("sys_user"));
        }
        List<SysDept> sysDeptList = deptMapper.selectDeptList(dept);
        if (temporaryIgnore) {
            SecurityUtils.removeTemporaryIgnoreTables(Collections.singletonList("sys_user"));
        }
        List<SysDept> sortSysDeptList = new ArrayList<>();
        List<SysDept> sortGardenDeptList = new ArrayList<>();
        List<SysDept> sortKindergartenDeptList = new ArrayList<>();
        List<SysDept> sortChildrenDeptList = new ArrayList<>();
        for (SysDept sysDept : sysDeptList) {
            if ("1".equals(sysDept.getDeptType())) {
                sortGardenDeptList.add(sysDept);
            } else if ("2".equals(sysDept.getDeptType())) {
                sortKindergartenDeptList.add(sysDept);
            } else if ("3".equals(sysDept.getDeptType())) {
                sortChildrenDeptList.add(sysDept);
            }
        }
        //需求需要将集团的部门排在集团下（集团部门与集团部门属于同一租户）,
        //由于同级别的幼儿园租户需要按照创建时间倒序排列，故需要重新排序
        sortSysDeptList.addAll(sortGardenDeptList);
        sortSysDeptList.addAll(sortChildrenDeptList);
        sortKindergartenDeptList = sortKindergartenDeptList.stream().sorted(Comparator.comparing(SysDept::getCreateTime, Comparator.reverseOrder())).collect(Collectors.toList());
        sortSysDeptList.addAll(sortKindergartenDeptList);

        if (sysTenant.getTenantType() == 1) {
            //以下方法仅为了更新集团下的幼儿园parent_id值
            //查询集团租户下的幼儿园
            List<Long> branchTenantIdList = branchTenantList.stream().map(SysTenant::getId).filter(tenantId -> !sysTenant.getId().equals(tenantId)).collect(Collectors.toList());
            //查询集团租户所对应的部门id
            SysDept gardenDept = new SysDept();
            gardenDept.getParams().put("gardenTenantId", sysTenant.getId());
            gardenDept.setDeptType(String.valueOf(sysTenant.getTenantType()));
            List<SysDept> gardenDeptList = deptMapper.selectDeptList(gardenDept);
            Long parentDeptId = gardenDeptList.get(0).getDeptId();
            sortSysDeptList.forEach(obj -> {
                if (branchTenantIdList.contains(obj.getTenantId()) && !"3".equals(obj.getDeptType())) {
                    obj.setParentId(parentDeptId);
                }
                //统计幼儿园或者各部门的人数
                if ("2".equals(obj.getDeptType()) || "3".equals(obj.getDeptType())) {
                    Long userCount = deptMapper.staticsUserCountByDeptId(obj.getDeptId());
                    obj.setUserCount(userCount);
                } else if ("1".equals(obj.getDeptType())) {
                    Long userCount = deptMapper.staticsUserCountByTenantId(obj.getTenantId());
                    obj.setUserCount(userCount);
                }
            });
        } else {
            sortSysDeptList.forEach(obj -> {
                Long userCount = deptMapper.staticsUserCountByDeptId(obj.getDeptId());
                obj.setUserCount(userCount);
            });
        }
        return sortSysDeptList;
    }

    /**
     * 查询部门管理数据
     *
     * @param dept 部门信息
     * @return 部门信息集合
     */
    @TemporaryIgnoreTables
    @DataScope(deptAlias = "d")
    public List<SysDept> selectDeptTreeData(SysDept dept) {
        //获取当前租户类型，判断当前租户是集团租户还是独立幼儿园租户
        SysTenant sysTenant = tenantMapper.selectById(SecurityUtils.getTenantId());
        List<SysTenant> branchTenantList = new ArrayList<>();
        if (sysTenant.getTenantType() == 1) {
            //更新幼儿园sys_dept表的parent_id值
            dept.getParams().put("isGardenTenant", sysTenant.getTenantType());
            SecurityUtils.setTemporaryIgnoreTables(Collections.singletonList("sys_dept"));
            //查询当前租户存在哪些幼儿园租户
            SysTenant conditionTenant = new SysTenant();
            conditionTenant.setGardenId(sysTenant.getGardenId());
            branchTenantList = tenantMapper.selectSysTenantList(conditionTenant);
        } else if (sysTenant.getTenantType() == 2) {
            //查询该集团幼儿园的组织数据
            dept.getParams().put("isSingleTenant", sysTenant.getId());
        } else if (sysTenant.getTenantType() == 3) {
            //查询该独立幼儿园的组织数据
            dept.getParams().put("isSingleTenant", sysTenant.getId());
        }
        dept.getParams().put("dataScope", "");
        dept.setTenantId(sysTenant.getId());
        List<SysDept> sysDeptList = deptMapper.selectDeptTreeData(dept);
        List<SysDept> sortSysDeptList = new ArrayList<>();
        List<SysDept> sortGardenDeptList = new ArrayList<>();
        List<SysDept> sortKindergartenDeptList = new ArrayList<>();
        List<SysDept> sortChildrenDeptList = new ArrayList<>();
        for (SysDept sysDept : sysDeptList) {
            if ("1".equals(sysDept.getDeptType())) {
                sortGardenDeptList.add(sysDept);
            } else if ("2".equals(sysDept.getDeptType())) {
                sortKindergartenDeptList.add(sysDept);
            } else if ("3".equals(sysDept.getDeptType())) {
                sortChildrenDeptList.add(sysDept);
            }
        }
        //需求需要将集团的部门排在集团下（集团部门与集团部门属于同一租户）,
        //由于需要人员组织树形结构中集团租户下的所有幼儿园租户同级别下同时按照排序字段正序排列、创建时间倒序排列，故需要重新排序
        sortSysDeptList.addAll(sortGardenDeptList);
        sortSysDeptList.addAll(sortChildrenDeptList);
        sortKindergartenDeptList = sortKindergartenDeptList.stream().sorted(Comparator.comparing(SysDept::getOrderNum).thenComparing(SysDept::getCreateTime, Comparator.reverseOrder())).collect(Collectors.toList());
        sortSysDeptList.addAll(sortKindergartenDeptList);
        if (sysTenant.getTenantType() == 1) {
            //以下方法仅为了更新集团下的幼儿园parent_id值
            //查询集团租户下的幼儿园
            List<Long> branchTenantIdList = branchTenantList.stream().map(SysTenant::getId).filter(tenantId -> !sysTenant.getId().equals(tenantId)).collect(Collectors.toList());
            //查询集团租户所对应的部门id
            SysDept gardenDept = new SysDept();
            gardenDept.getParams().put("gardenTenantId", sysTenant.getId());
            gardenDept.setDeptType(String.valueOf(sysTenant.getTenantType()));
            List<SysDept> gardenDeptList = deptMapper.selectDeptList(gardenDept);
            Long parentDeptId = gardenDeptList.get(0).getDeptId();
            sortSysDeptList.forEach(obj -> {
                if (branchTenantIdList.contains(obj.getTenantId()) && !"3".equals(obj.getDeptType())) {
                    obj.setParentId(parentDeptId);
                }
            });
        }
        return sortSysDeptList;
    }

    /**
     * 查询关联租户信息的部门管理数据列表
     *
     * @param dept 部门信息
     * @return 部门信息集合
     */
    @TemporaryIgnoreTables
    @Override
    @DataScope(deptAlias = "d")
    public List<SysDept> selectAssociateTenantDeptList(SysDept dept){
        //获取当前租户类型，判断当前租户是集团租户还是独立幼儿园租户
        SysTenant sysTenant = tenantMapper.selectById(SecurityUtils.getTenantId());
        if(SecurityUtils.getTenantId() != 9999L && sysTenant.getTenantType() == 1){
            dept.getParams().put("isGardenTenant", sysTenant.getTenantType());
            SecurityUtils.setTemporaryIgnoreTables(Arrays.asList("sys_dept"));
        }
        dept.getParams().put("tenantType", String.valueOf(sysTenant.getTenantType()));

        //根据当前租户查询相应的集团或者幼儿园
        dept.getParams().put("dataScope", "");
        dept.setTenantId(SecurityUtils.getTenantId());
        return deptMapper.selectAssociateTenantDeptList(dept);
    }

    /**
     * 查询部门树结构信息
     *
     * @param dept 部门信息
     * @return 部门树信息集合
     */
    @Override
    public List<TreeSelect> selectDeptTreeList(SysDept dept)
    {
        List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptTreeData(dept);
        return buildDeptTreeSelect(depts);
    }

    /**
     * 查询部门树结构信息
     *
     * @param dept 部门信息
     * @return 部门树信息集合
     */
    @Override
    public List<TreeSelect> selectNewDeptTreeList(SysDept dept)
    {
        List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(dept);
        return rebuildNewDeptTree(depts);
    }

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

    /**
     * 构建前端所需要下拉树结构
     *
     * @param depts 部门列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildDeptTreeSelect(List<SysDept> depts)
    {
        List<SysDept> deptTrees = buildDeptTree(depts);
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param depts 部门列表
     * @return 下拉树结构列表
     */
    public List<TreeSelect> rebuildNewDeptTree(List<SysDept> depts) {
        //根据当前租户id查询其是否是幼儿园
        SysTenant sysTenant = tenantMapper.selectById(SecurityUtils.getTenantId());
        Long parentTenantId = 0L;
//        if (sysTenant.getTenantType() == 1 || sysTenant.getTenantType() == 3) {
//            parentTenantId = sysTenant.getId();
//        } else if (sysTenant.getTenantType() == 2) {
//            SysReGardenBranch parentReTenant = reGardenBranchMapper.selectById(sysTenant.getId());
//            parentTenantId = parentReTenant.getParentId();
//        } else {
//            parentTenantId = 0L;
//        }
        List<SysDept> deptTrees = buildDeptTree(depts);
        Map<Long, List<SysDept>> deptTreeMap = deptTrees.stream().collect(Collectors.groupingBy(SysDept::getTenantId));
        SysReGardenBranch sysReGardenBranch = new SysReGardenBranch();
        sysReGardenBranch.setId(SecurityUtils.getTenantId());
        List<SysReGardenBranch> reGardenBranchList = reGardenBranchMapper.selectSysReGardenBranchList(sysReGardenBranch);
        List<SysReGardenBranch> gardenDeptList = reGardenBranchList.stream().filter(obj -> ConstantUtils.DEPT_TYPE_1.equals(obj.getDeptType())).collect(Collectors.toList());
        List<SysReGardenBranch> branchDeptList = reGardenBranchList.stream().filter(obj -> ConstantUtils.DEPT_TYPE_2.equals(obj.getDeptType())).collect(Collectors.toList());
        List<SysDept> branchSysDeptList = branchDeptList.stream().map(obj -> {
            SysDept sysDept = new SysDept();
            sysDept.setDeptId(obj.getId());
            sysDept.setParentId(parentTenantId);
            sysDept.setDeptName(obj.getTenantName());
            sysDept.setTenantId(obj.getId());
            sysDept.setDeptType(obj.getDeptType());
            sysDept.setChildren(deptTreeMap.containsKey(obj.getId()) ? deptTreeMap.get(obj.getId()) : new ArrayList<>());
            return sysDept;
        }).collect(Collectors.toList());
        List<SysDept> deptList = new ArrayList<>();
        if (SecurityUtils.getTenantType() == 1) {
            deptList = gardenDeptList.stream().map(obj -> {
                SysDept sysDept = new SysDept();
                sysDept.setDeptId(obj.getId());
                sysDept.setParentId(obj.getParentId());
                sysDept.setDeptName(obj.getTenantName());
                sysDept.setTenantId(obj.getId());
                sysDept.setDeptType(obj.getDeptType());
                if (deptTreeMap.containsKey(obj.getId())) {
                    branchSysDeptList.addAll(deptTreeMap.get(obj.getId()));
                }
                sysDept.setChildren(branchSysDeptList);
                return sysDept;
            }).collect(Collectors.toList());
        } else {
            deptList = branchDeptList.stream().map(obj -> {
                SysDept sysDept = new SysDept();
                sysDept.setDeptId(obj.getId());
                sysDept.setParentId(obj.getParentId());
                sysDept.setDeptName(obj.getTenantName());
                sysDept.setTenantId(obj.getId());
                sysDept.setDeptType(obj.getDeptType());
                sysDept.setChildren(deptTreeMap.containsKey(obj.getId()) ? deptTreeMap.get(obj.getId()) : new ArrayList<>());
                return sysDept;
            }).collect(Collectors.toList());
        }
        return deptList.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 根据角色ID查询部门树信息
     *
     * @param roleId 角色ID
     * @return 选中部门列表
     */
    @Override
    public List<Long> selectDeptListByRoleId(Long roleId)
    {
        SysRole role = roleMapper.selectRoleById(roleId);
        return deptMapper.selectDeptListByRoleId(roleId, role.isDeptCheckStrictly());
    }

    /**
     * 根据部门ID查询信息
     *
     * @param deptId 部门ID
     * @return 部门信息
     */
    @Override
    public SysDept selectDeptById(Long deptId)
    {
        return deptMapper.selectDeptById(deptId);
    }

    /**
     * 根据ID查询所有子部门（正常状态）
     *
     * @param deptId 部门ID
     * @return 子部门数
     */
    @Override
    public int selectNormalChildrenDeptById(Long deptId)
    {
        return deptMapper.selectNormalChildrenDeptById(deptId);
    }

    /**
     * 是否存在子节点
     *
     * @param deptId 部门ID
     * @return 结果
     */
    @Override
    public boolean hasChildByDeptId(Long deptId)
    {
        int result = deptMapper.hasChildByDeptId(deptId);
        return result > 0;
    }

    /**
     * 查询部门是否存在用户
     *
     * @param deptId 部门ID
     * @return 结果 true 存在 false 不存在
     */
    @Override
    public boolean checkDeptExistUser(Long deptId)
    {
        int result = deptMapper.checkDeptExistUser(deptId);
        return result > 0;
    }

    /**
     * 校验部门名称是否唯一
     *
     * @param dept 部门信息
     * @return 结果
     */
    @Override
    public boolean checkDeptNameUnique(SysDept dept)
    {
        Long deptId = StrUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId();
        Long tenantId = SecurityUtils.getTenantId();
        if(dept.getParentId() != null) {
            SysDept parentSysDept = deptMapper.selectDeptById(dept.getParentId());
            tenantId = parentSysDept.getTenantId();
        }
        //需求要求同一租户下的部门名称不可重复（此处排除父级部门的校验，不包含租户）
        SysDept info = deptMapper.checkDeptNameUnique(dept.getDeptName(),
                dept.getParentId(), tenantId);
        if (StrUtils.isNotNull(info) && info.getDeptId().longValue() != deptId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验部门是否有数据权限
     *
     * @param deptId 部门id
     */
    @Override
    public void checkDeptDataScope(Long deptId)
    {
        if (!SysUser.isAdmin(SecurityUtils.getUserId()))
        {
            SysDept dept = new SysDept();
            dept.setDeptId(deptId);
            List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(dept);
//            List<SysDept> depts = deptMapper.selectDeptList(dept);
            if (StrUtils.isEmpty(depts))
            {
                throw new ServiceException("没有权限访问部门数据！");
            }
        }
    }

    /**
     * 新增保存部门信息
     *
     * @param dept 部门信息
     * @return 结果
     */
    @TemporaryIgnoreTables
    @Override
    public int insertDept(SysDept dept) {
        if (StrUtils.isEmpty(dept.getDeptType())) {
            throw new IllegalArgumentException("参数异常，请检查");
        }
        //判断当前账号是否为集团账号
        Long tenantId = null;
        Long parentId = null;
        String ancestors = null;
        SysDept parentDept = deptMapper.selectDeptById(dept.getParentId());
        if (ConstantUtils.DEPT_TYPE_1.equals(dept.getParentDeptType())
                || ConstantUtils.DEPT_TYPE_2.equals(dept.getParentDeptType())) {
//            SysTenant tenantInfo = tenantMapper.selectById(dept.getParentId());
//            if(tenantInfo.getStatus() != 0){
//                throw new ServiceException("租户已禁用，不允许新增部门");
//            }
            ancestors = "0" + "," + parentDept.getDeptId();
        } else if (ConstantUtils.DEPT_TYPE_3.equals(dept.getParentDeptType())) {
            if (!UserConstants.DEPT_NORMAL.equals(parentDept.getStatus())) {
                throw new ServiceException("部门停用，不允许新增");
            }
            ancestors = parentDept.getAncestors() + "," + dept.getParentId();
        }

        dept.setTenantId(parentDept.getTenantId());
        dept.setParentId(dept.getParentId());
        dept.setAncestors(ancestors);
        dept.setDeptType(ConstantUtils.DEPT_TYPE_3);
        return deptMapper.insertDept(dept);
    }

    /**
     * 修改保存部门信息
     *
     * @param dept 部门信息
     * @return 结果
     */
    @Override
    public int updateDept(SysDept dept)
    {
        SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId());
        SysDept oldDept = deptMapper.selectDeptById(dept.getDeptId());
        if (StrUtils.isNotNull(newParentDept) && StrUtils.isNotNull(oldDept))
        {
            String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
            String oldAncestors = oldDept.getAncestors();
            dept.setAncestors(newAncestors);
            updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
        }
        if(StrUtils.isEmpty(dept.getLeaderId())){
            dept.setLeaderId(null);
            dept.setLeader(null);
        }
        int result = deptMapper.updateDept(dept);
        if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StrUtils.isNotEmpty(dept.getAncestors())
                && !StrUtils.equals("0", dept.getAncestors()))
        {
            // 如果该部门是启用状态，则启用该部门的所有上级部门
            updateParentDeptStatusNormal(dept);
        }
        return result;
    }

    /**
     * 设置集团或者幼儿园的主管
     *
     * @param dept 部门信息
     * @return 结果
     */
    @Override
    public int manageLeader(SysDept dept) {
        return deptMapper.manageLeader(dept);
    }

    /**
     * 修改该部门的父级部门状态
     *
     * @param dept 当前部门
     */
    private void updateParentDeptStatusNormal(SysDept dept)
    {
        String ancestors = dept.getAncestors();
        Long[] deptIds = Convert.toLongArray(ancestors);
        deptMapper.updateDeptStatusNormal(deptIds);
    }

    /**
     * 修改子元素关系
     *
     * @param deptId 被修改的部门ID
     * @param newAncestors 新的父ID集合
     * @param oldAncestors 旧的父ID集合
     */
    public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors)
    {
        List<SysDept> children = deptMapper.selectChildrenDeptById(deptId);
        for (SysDept child : children)
        {
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
        }
        if (children.size() > 0)
        {
            deptMapper.updateDeptChildren(children);
        }
    }

    /**
     * 删除部门管理信息
     *
     * @param deptId 部门ID
     * @return 结果
     */
    @Override
    public int deleteDeptById(Long deptId)
    {
        return deptMapper.deleteDeptById(deptId);
    }

    @Override
    public int copyTenantDeptData(Long tenantId) {
        //查询指定幼儿园租户的部门数据
        LambdaQueryWrapper<SysDept> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SysDept::getTenantId, tenantId);
        deptMapper.selectList(queryWrapper);
        return 0;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SysDept> list, SysDept t)
    {
        // 得到子节点列表
        List<SysDept> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysDept tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

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

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