package com.yunyan.system.service.impl;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
/*import com.yunyan.business.domain.Member;
import com.yunyan.business.pojo.admin.screen.ScreenPartyVo;
import com.yunyan.business.pojo.admin.vo.AdmSysDeptListVo;*/
import com.yunyan.system.domain.dto.SysDeptImportDataDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import com.yunyan.common.annotation.DataScope;
import com.yunyan.common.constant.UserConstants;
import com.yunyan.common.core.domain.TreeSelect;
import com.yunyan.common.core.domain.entity.SysDept;
import com.yunyan.common.core.domain.entity.SysRole;
import com.yunyan.common.core.domain.entity.SysUser;
import com.yunyan.common.core.text.Convert;
import com.yunyan.common.exception.ServiceException;
import com.yunyan.common.utils.SecurityUtils;
import com.yunyan.common.utils.StringUtils;
import com.yunyan.common.utils.spring.SpringUtils;
import com.yunyan.system.mapper.SysDeptMapper;
import com.yunyan.system.mapper.SysRoleMapper;
import com.yunyan.system.service.ISysDeptService;

import javax.servlet.http.HttpServletResponse;

/**
 * 部门管理 服务实现
 *
 * @author yunyan
 */
@Slf4j
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper,SysDept> implements ISysDeptService
{
    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private SysRoleMapper roleMapper;

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

    /*@DataScope(deptAlias = "d")
    @Override
    public List<AdmSysDeptListVo> selectDeptListAdm(SysDept dept) {
        return deptMapper.selectDeptListAdm(dept);
    }*/

    /**
     * 期限填报日期 部门选择
     * @param dept
     * @return
     */
    @DataScope(deptAlias = "d")
    @Override
    public List<SysDept> selectDeptListByFillDate(SysDept dept)
    {
        return deptMapper.selectDeptList(dept);
    }

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

    @Override
    public List<TreeSelect> selectDeptTreeFillDateList(SysDept dept)
    {
        List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptListByFillDate(dept);
        return buildDeptTreeSelect(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());
    }

    /**
     * 根据角色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 = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId();
        SysDept info = deptMapper.checkDeptNameUnique(dept.getDeptName(), dept.getParentId());
        if (StringUtils.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);
            if (StringUtils.isEmpty(depts))
            {
                throw new ServiceException("没有权限访问部门数据！");
            }
        }
    }

    /**
     * 新增保存部门信息
     *
     * @param dept 部门信息
     * @return 结果
     */
    @Override
    public int insertDept(SysDept dept)
    {
        SysDept info = deptMapper.selectDeptById(dept.getParentId());
        // 如果父节点不为正常状态,则不允许新增子节点
        if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
        {
            throw new ServiceException("部门停用，不允许新增");
        }

        //设置 判断单位级别
        //dept.setDeptLevel(dept.getAncestors().split(",").length);

        dept.setAncestors(info.getAncestors() + "," + dept.getParentId());

        //区域内唯一模板设置 检测
        if(null != dept.getFinalTemplateFlag() && dept.getFinalTemplateFlag().equals(1) ) {
            SysDept sysDeptArea = getfinalExportTypeByAncestors(dept.getAncestors(), null);
            if(null != sysDeptArea) {
                throw new ServiceException("此区域内已设置模板导出，不允许新增");
            }
        }


        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 (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept))
        {
            //设置 判断单位级别
            //dept.setDeptLevel(oldDept.getAncestors().split(",").length);

            String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
            String oldAncestors = oldDept.getAncestors();
            dept.setAncestors(newAncestors);

            //区域内唯一模板设置 检测
            if(null != dept.getFinalTemplateFlag() && dept.getFinalTemplateFlag().equals(1) ) {
                SysDept sysDeptArea = getfinalExportTypeByAncestors(dept.getAncestors(), null);
                if(null != sysDeptArea && !dept.getDeptId().equals(sysDeptArea.getDeptId()) ) {
                    throw new ServiceException("此区域内已设置模板导出，不允许修改");
                }
            }

            updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
        }

        int result = deptMapper.updateDept(dept);
        if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors())
                && !StringUtils.equals("0", dept.getAncestors()))
        {
            // 如果该部门是启用状态，则启用该部门的所有上级部门
            updateParentDeptStatusNormal(dept);
        }
        return result;
    }

    /**
     * 修改该部门的父级部门状态
     *
     * @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);
    }

    /**
     * 递归列表
     */
    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 (StringUtils.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;
    }

    @Override
    public SysDept getDeptByName(String deptName, String area) {
        //不包含 主管单位
        //SysDept sysDept = this.lambdaQuery().eq(SysDept::getDeptName, deptName).ne(SysDept::getDeptLevel, 2).apply(" rownum <= 1").one();
        SysDept sysDept = this.lambdaQuery().eq(SysDept::getDeptName, deptName).eq(SysDept::getImportFlag, 1).eq(StringUtils.isNoneBlank(area), SysDept::getArea, area).apply(" rownum <= 1").one();

        return sysDept;
    }

    @Override
    public SysDept getDeptByDeptCode(String deptCode, String area) {
        SysDept sysDept = this.lambdaQuery().eq(SysDept::getDeptCode, deptCode).eq(SysDept::getImportFlag, 1).eq(StringUtils.isNoneBlank(area), SysDept::getArea, area).apply(" rownum <= 1").one();

        return sysDept;
    }

    @Override
    public SysDept getfinalExportTypeByAncestors(String ancestors, Long deptId) {

        //List<String> deptIdList= Arrays.asList(ancestors.split(","));

        List<String> deptIdList = new ArrayList<String>(Arrays.asList(ancestors.split(","))) ;

        if(null != deptId) {
            String deptIdStr = String.valueOf(deptId);
            deptIdList.add(deptIdStr);
        }

        List<SysDept> sysDeptList = this.lambdaQuery().in(SysDept::getDeptId, deptIdList).eq(SysDept::getFinalTemplateFlag, 1)
                .select(SysDept::getDeptId, SysDept::getFinalTemplateFlag, SysDept::getFinalExportType, SysDept::getBudgetExportType)
                .last(" order by regexp_count(ANCESTORS, ',') ").list();

        if(sysDeptList.isEmpty()) {
            return null;
        }

        SysDept sysDept = sysDeptList.get(0);

        return sysDept;
    }

    @Override
    public SysDept getfinalExportTypeByArea(String area) {

        if(StringUtils.isBlank(area)) {
            return null;
        }


        List<SysDept> sysDeptList = this.lambdaQuery().eq(SysDept::getArea, area).eq(SysDept::getFinalTemplateFlag, 1)
                .select(SysDept::getDeptId, SysDept::getFinalTemplateFlag, SysDept::getFinalExportType, SysDept::getBudgetExportType)
                .last(" order by regexp_count(ANCESTORS, ',') ").list();

        if(sysDeptList.isEmpty()) {
            return null;
        }

        SysDept sysDept = sysDeptList.get(0);

        return sysDept;
    }

    /**
     * 导入部门数据
     *
     * @param deptList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importDept(List<SysDeptImportDataDto> deptList, boolean isUpdateSupport, String operName, String area) {
        if (StringUtils.isNull(deptList) || deptList.size() == 0)
        {
            throw new ServiceException("导入部门数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        //设置 区域的名称
        if(StringUtils.isBlank(area)) {
            //area = "凭祥";
        }

        //区域的部门编码
        String areaDeptCode = "";

        for (SysDeptImportDataDto deptDto : deptList)
        {
            area = deptDto.getArea();
            try
            {
                //设置 区域以及上级的区域
                deptDto.setArea(area);
                deptDto.setAreaParent(area);

                //上级 部门
                LambdaQueryChainWrapper<SysDept> queryWrapperParent = this.lambdaQuery();
                //queryWrapperParent.eq(SysDept::getDeptName, deptDto.getParentName().trim());

                if(StringUtils.isNotBlank(deptDto.getAreaParent())) {
                    area = deptDto.getAreaParent();
                }

                if(StringUtils.isNotBlank(area)) {
                    queryWrapperParent.eq(SysDept::getArea, area );
                } else {
                    queryWrapperParent.isNull(SysDept::getArea);
                }
                /*if(StringUtils.isNotBlank(deptDto.getAreaCodeParent())) {
                    queryWrapperParent.eq(SysDept::getAreaCode, deptDto.getAreaCodeParent() );
                } else {
                    queryWrapperParent.isNull(SysDept::getAreaCode);
                }*/
                if(StringUtils.isNotBlank(deptDto.getParentName())) {
                    queryWrapperParent.eq(SysDept::getDeptName, deptDto.getParentName().trim() );
                } else {
                    //判断 部门编码来查上级部门
                    //queryWrapperParent.isNull(SysDept::getDeptName);
                }

                if(StringUtils.isNotBlank(deptDto.getDeptCodeParent())) {
                    queryWrapperParent.eq(SysDept::getDeptCode, deptDto.getDeptCodeParent() );
                } else {
                    //判断 部门编码来查上级部门
                    //queryWrapperParent.eq(SysDept::getDeptCode, areaDeptCode);
                    if(StringUtils.isNotBlank(deptDto.getDeptCode()) && deptDto.getDeptCode().length() == 6 ) {
                        queryWrapperParent.eq(SysDept::getDeptCode, deptDto.getDeptCode().substring(0, 3) );
                    } else if(StringUtils.isNotBlank(deptDto.getDeptCode()) && deptDto.getDeptCode().length() == 9 ) {
                        queryWrapperParent.eq(SysDept::getDeptCode, deptDto.getDeptCode().substring(0, 6) );
                    }  else if(StringUtils.isNotBlank(deptDto.getDeptCode()) && deptDto.getDeptCode().length() == 12 ) {
                        queryWrapperParent.eq(SysDept::getDeptCode, deptDto.getDeptCode().substring(0, 9) );
                    }/* if(StringUtils.isNotBlank(deptDto.getDeptCode()) && deptDto.getDeptCode().length() == 3 ) {
                        queryWrapperParent.eq(SysDept::getDeptCode, areaDeptCode);
                    }*/ else {
                        queryWrapperParent.isNull(SysDept::getDeptCode);
                    }
                }
                //.eq(SysDept::getDeptLevel, deptDto.getDeptLevelParent()) //单位级别不用作查询

                List<SysDept> sysDeptParentList = queryWrapperParent.list();
                //判断上级部门
                if(sysDeptParentList.isEmpty()) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、序号 [" + deptDto.getIndexNo() +   "]、区域 [" + area +  "]、部门 [" + deptDto.getDeptName() +  "]、部门编码 [" + deptDto.getDeptCode() + "]  的上级部门[" + deptDto.getParentName() + "] 不存在");
                    continue;
                }
                if(sysDeptParentList.size() > 1) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、序号 [" + deptDto.getIndexNo() +   "]、区域 [" + area +  "]、部门 [" + deptDto.getDeptName() +  "]、部门编码 [" + deptDto.getDeptCode() + "]  的上级部门[" + deptDto.getParentName() + "] 存在多个上级");
                    continue;
                }
                SysDept sysDeptParent = sysDeptParentList.get(0);


                // 验证是否存在这个部门
                LambdaQueryChainWrapper<SysDept> queryWrapper= this.lambdaQuery();
                queryWrapper.eq(SysDept::getParentId, sysDeptParent.getDeptId());

                //区域
                if(StringUtils.isNotBlank(deptDto.getArea())) {
                    area = deptDto.getArea();
                }
                if(StringUtils.isNotBlank(area)) {
                    queryWrapper.eq(SysDept::getArea, area );
                } else {
                    queryWrapper.isNull(SysDept::getArea);
                }
                /*if(StringUtils.isNotBlank(deptDto.getAreaCode())) {
                    queryWrapper.eq(SysDept::getAreaCode, deptDto.getAreaCode() );
                } else {
                    queryWrapper.isNull(SysDept::getAreaCode);
                }*/
                if(StringUtils.isNotBlank(deptDto.getDeptCode())) {
                    queryWrapper.eq(SysDept::getDeptCode, deptDto.getDeptCode() );
                } else {
                    queryWrapper.isNull(SysDept::getDeptCode);
                }
                //.eq(SysDept::getDeptLevel, deptDto.getDeptLevel()) //单位级别不用作查询
                List<SysDept> sysDeptList = queryWrapper.list();

                if(sysDeptList.size() > 1) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、序号 [" + deptDto.getIndexNo() +   "]、区域 [" + area +  "]、部门 [" + deptDto.getDeptName() +  "]、部门编码 [" + deptDto.getDeptCode() + "]  的查询存在多个");
                    continue;
                }

                if (sysDeptList.isEmpty())
                {
                    //百色市地区的  如果是 最后3位为 001的, 不采集
                    /*if(StringUtils.isNotBlank(deptDto.getDeptCode()) && deptDto.getDeptCode().endsWith("001") ) {
                        deptDto.setImportFlag(0);
                    } else {
                        deptDto.setImportFlag(1);
                    }*/

                    //不存在部门
                    SysDept sysDept = new SysDept();
                    BeanUtil.copyProperties(deptDto, sysDept);
                    sysDept.setParentId(sysDeptParent.getDeptId());
                    sysDept.setDeptName(sysDept.getDeptName().trim());
                    String newAncestors = sysDeptParent.getAncestors() + "," + sysDeptParent.getDeptId();
                    sysDept.setAncestors(newAncestors);
                    sysDept.setCreateBy(operName);
                    if(StringUtils.isNotBlank(deptDto.getIndexNo()) && StringUtils.isNumeric(deptDto.getIndexNo())) {
                        sysDept.setOrderNum(Integer.valueOf(deptDto.getIndexNo()));
                    } else {
                        sysDept.setOrderNum(888);
                    }

                    this.save(sysDept);

                    successNum++;
                    successMsg.append("<br/>" + successNum + "、序号 [" + deptDto.getIndexNo() +   "]、区域 [" + area +  "]、部门 [" + deptDto.getDeptName() +  "]、部门编码 [" + deptDto.getDeptCode() + "]  导入成功");
                }
                else if ( isUpdateSupport && !sysDeptList.isEmpty() && sysDeptList.size() == 1 )
                {
                    //存在部门, 进行更新
                    /*user.setUpdateBy(operName);
                    this.updateUser(user);*/
                    /*successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 更新成功");*/
                }
                else
                {
                    //存在部门, 不进行更新
                    /*failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 已存在");*/
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、序号 [" + deptDto.getIndexNo() +   "]、区域 [" + area +  "]、部门 [" + deptDto.getDeptName() +  "]、部门编码 [" + deptDto.getDeptCode() + "] 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public void importTemplate(HttpServletResponse response, String sheetName) {
        //假如以中文名下载的话，设置下载文件名称
        String filename = sheetName;
        InputStream bis = null;
        BufferedOutputStream out = null;
        try {
            //转码，免得文件名中文乱码
            filename = URLEncoder.encode(filename, "UTF-8")+".xls";
            //过滤
            //新的获取方式, jar包的路径目录时获取不到的情况
            ClassPathResource classPathResource = new ClassPathResource("template/部门导入模板.xlsx");
            InputStream inputStream =classPathResource.getInputStream();
            bis = new BufferedInputStream(inputStream);
            response.setContentType("application/x-xlsx;charset=utf-8");
            //设置文件下载头
            response.addHeader("Content-Disposition", "attachment;filename=" + filename);
            out = new BufferedOutputStream(response.getOutputStream());
            int len = 0;
            while ((len = bis.read()) != -1) {
                out.write(len);
                out.flush();
            }
        } catch (IOException e) {
            log.error("模板下载失败");
            throw new ServiceException("模板下载失败");
        } finally {
            if (out != null) {
                IOUtils.closeQuietly(out);
            }
            if (bis != null) {
                IOUtils.closeQuietly(bis);
            }
        }
    }

    @Override
    public int checkChildDeptAttrBoth(Long deptId) {
        List<SysDept> sysDeptList = this.lambdaQuery().select(SysDept::getDeptAttr, SysDept::getDeptId).eq(SysDept::getParentId, deptId).list();
        final boolean[] deptAttr1 = {false};
        final boolean[] deptAttr2 = {false};

        sysDeptList.stream().forEach(i-> {
            Integer deptAttr = i.getDeptAttr();
            if(null == deptAttr) {
                deptAttr = 1;
            }
            if( 1 == deptAttr ) {
                deptAttr1[0] = true;
            }
            if( 2 == deptAttr ) {
                deptAttr2[0] = true;
            }
        });

        return deptAttr1[0] && deptAttr2[0] ? 1 : 0;
    }

    @Override
    public List<SysDept> checkChildDeptAttrAndSumTotal(List<String> deptCodeList, String area) {
        if(deptCodeList.isEmpty()) {
            deptCodeList.add("-1");
        }
        List<SysDept> sysDeptList = this.lambdaQuery().select(SysDept::getDeptAttr, SysDept::getDeptId, SysDept::getDeptCode).in(SysDept::getDeptCode, deptCodeList).eq(SysDept::getArea, area).list();

        return sysDeptList;
    }
}
