package org.jeecg.modules.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.constant.SendChangeMsgConstant;
import org.jeecg.modules.system.entity.SysArea;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.mapper.SysAreaMapper;
import org.jeecg.modules.system.service.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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


/**
 * @Description: sys_area
 * @Author: jeecg-boot
 * @Date: 2022-01-05
 * @Version: V1.0
 */
@Service
public class SysAreaServiceImpl extends ServiceImpl<SysAreaMapper, SysArea> implements ISysAreaService {

    @Autowired
    private ISysTenantService tenantService;
    @Autowired
    private ISysChangeMsgService sysChangeMsgService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDepartService sysDepartService;
    @Autowired
    private ISysUserDepartService sysUserDepartService;
    @Autowired
    private ISysAreaService sysAreaService;
	@Override
    public void addSysArea(SysArea sysArea) {
        if (oConvertUtils.isEmpty(sysArea.getParentId())) {
            sysArea.setParentId(ISysAreaService.ROOT_PID_VALUE);
        } else {
            //如果当前节点父ID不为空 则设置父节点的hasChildren 为1
            SysArea parent = baseMapper.selectById(sysArea.getParentId());
            if (parent != null && !"1".equals(parent.getHasChild())) {
                parent.setHasChild("1");
                baseMapper.updateById(parent);
            }
        }
        baseMapper.insert(sysArea);
    }

    @Override
    public void updateSysArea(SysArea sysArea) {
        SysArea entity = this.getById(sysArea.getId());
        if (entity == null) {
            throw new JeecgBootException("未找到对应实体");
        }
        String old_pid = entity.getParentId();
        String new_pid = sysArea.getParentId();
        if (!old_pid.equals(new_pid)) {
            updateOldParentNode(old_pid);
            if (oConvertUtils.isEmpty(new_pid)) {
                sysArea.setParentId(ISysAreaService.ROOT_PID_VALUE);
            }
            if (!ISysAreaService.ROOT_PID_VALUE.equals(sysArea.getParentId())) {
                baseMapper.updateTreeNodeStatus(sysArea.getParentId(), ISysAreaService.HASCHILD);
            }
        }
        baseMapper.updateById(sysArea);
    }
	
	@Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSysArea(String id) throws JeecgBootException {
        //查询选中节点下所有子节点一并删除
        id = this.queryTreeChildIds(id);
        if (id.indexOf(",") > 0) {
            StringBuilder sb = new StringBuilder();
            String[] idArr = id.split(",");
            for (String idVal : idArr) {
                if (idVal != null) {
                    SysArea sysArea = this.getById(idVal);
                    String pidVal = sysArea.getParentId();
                    //查询此节点上一级是否还有其他子节点
                    List<SysArea> dataList = baseMapper.selectList(new QueryWrapper<SysArea>().eq("parent_id", pidVal).notIn("id", Arrays.asList(idArr)));
                    if ((dataList == null || dataList.size() == 0) && !Arrays.asList(idArr).contains(pidVal)
                            && !sb.toString().contains(pidVal)) {
                        //如果当前节点原本有子节点 现在木有了，更新状态
                        sb.append(pidVal).append(",");
                    }
                }
            }
            //批量删除节点
            baseMapper.deleteBatchIds(Arrays.asList(idArr));
            //修改已无子节点的标识
            String[] pidArr = sb.toString().split(",");
            for(String pid : pidArr){
                this.updateOldParentNode(pid);
            }
        }else{
            SysArea sysArea = this.getById(id);
            if (sysArea == null) {
                throw new JeecgBootException("未找到对应实体");
            }
            updateOldParentNode(sysArea.getParentId());
            baseMapper.deleteById(id);
        }
    }
	
	@Override
    public List<SysArea> queryTreeListNoPage(QueryWrapper<SysArea> queryWrapper) {
        List<SysArea> dataList = baseMapper.selectList(queryWrapper);
        List<SysArea> mapList = new ArrayList<>();
        for (SysArea data : dataList) {
            String pidVal = data.getParentId();
            //递归查询子节点的根节点
            if (pidVal != null && !"0".equals(pidVal)) {
                SysArea rootVal = this.getTreeRoot(pidVal);
                if (rootVal != null && !mapList.contains(rootVal)) {
                    mapList.add(rootVal);
                }
            } else {
                if (!mapList.contains(data)) {
                    mapList.add(data);
                }
            }
        }
        return mapList;
    }

    @Override
    public String[] findParentIdPath(String Id) {
        List<String> paths = new ArrayList<>();
        List<String> parentPath = findParentPath(Id, paths);
        Collections.reverse(parentPath);
        return parentPath.toArray(new String[parentPath.size()]);
    }

    @Override
    public IPage<SysArea> queryPage(Integer pageNo, Integer pageSize, String name, String code, Integer isSpecial, String status) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //获取用户权限信息
        Map<String, Object> userOtherInfo = sysUserService.getUserOtherInfo(loginUser);
        if (userOtherInfo == null) {
            return null;
        }
        Boolean isAreaAdmin = (Boolean) userOtherInfo.get("isAreaAdmin");
        Boolean isBasicOperation = (Boolean) userOtherInfo.get("isBasicOperation");
        Boolean isDeptAdmin = (Boolean) userOtherInfo.get("isDeptAdmin");
        Boolean isAdmin = (Boolean) userOtherInfo.get("isAdmin");
        SysDepart currentDept = (SysDepart) userOtherInfo.get("currentDept");
        SysArea currentArea = (SysArea) userOtherInfo.get("currentArea");
        QueryWrapper<SysArea> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("area_code");
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("area_name", name);
        }
        if (!StringUtils.isEmpty(code)) {
            wrapper.eq("area_code", code);
        }
        if (!oConvertUtils.isEmpty(isSpecial)) {
            wrapper.eq("is_special", isSpecial);
        }
        if(!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }
        if (!isAdmin&&!isBasicOperation){
            if (isDeptAdmin&&!isAreaAdmin){
                String[] parentIdPath = this.findParentIdPath(currentArea.getId());
                wrapper.in("id",parentIdPath);
            }else if (isAreaAdmin) {
                List<String> childId = this.quickGetChild(currentArea.getId());
                childId.add(currentArea.getId());
                String[] parentIdPath = this.findParentIdPath(currentArea.getId());
                List<String> childId2 = Arrays.asList(parentIdPath);
                childId.addAll(childId2);
                wrapper.in("id",childId);
            }else {
                String[] parentIdPath = this.findParentIdPath(currentArea.getId());
                wrapper.in("id",parentIdPath);
            }
        }
        Page<SysArea> page = new Page<>(pageNo, pageSize);
        return this.page(page,wrapper);
    }

    @Override
    public List<SysArea> listWithTree() {
        //1、查出所有分类
        List<SysArea> entities = baseMapper.selectList(null);
        //2、组成父子的树形结构
        //2.1、找到所有的一级分类
        List<SysArea> level1Menus = entities.stream().filter(SysArea ->
                SysArea.getParentId().equals("0")
        ).map((menu) -> {
            menu.setChildren(getChildrens(menu, entities));
            return menu;
        }).sorted(Comparator.comparingInt(menu -> (menu.getAreaOrder() == null ? 0 : menu.getAreaOrder()))).collect(Collectors.toList());
        return level1Menus;
    }

    //递归查找所有菜单的子菜单
    private List<SysArea> getChildrens(SysArea root, List<SysArea> all) {
        List<SysArea> children = all.stream().filter(SysArea -> {
            return SysArea.getParentId().equals(root.getId());
        }).map(SysArea -> {
            SysArea.setChildren(getChildrens(SysArea, all));
            return SysArea;
        }).sorted(Comparator.comparingInt(menu -> (menu.getAreaOrder() == null ? 0 : menu.getAreaOrder()))).collect(Collectors.toList());
        return children;
    }

    @Override
    public List<SysArea> listWithTreeByNameAndCode() {
        //1、查出所有分类

        List<SysArea> entities = baseMapper.selectList(null);
        List<String> list = new ArrayList<String>();
        //2、组成父子的树形结构
        //2.1、找到所有的一级分类
        return entities.stream().filter(SysArea ->
                SysArea.getParentId().equals("0")
        ).map((menu) -> {
            menu.setChildren(getChildrens1(menu, entities));
            return menu;
        }).sorted(Comparator.comparingInt(menu -> (menu.getAreaOrder() == null ? 0 : menu.getAreaOrder()))).collect(Collectors.toList());
    }


    //递归查找所有菜单的子菜单
    private List<SysArea> getChildrens1(SysArea root, List<SysArea> all) {

        List<SysArea> children = all.stream().filter(SysArea -> {
            return SysArea.getParentId().equals(root.getId());
        }).map(SysArea -> {
            SysArea.setChildren(getChildrens1(SysArea, all));
            return SysArea;
        }).sorted((menu1, menu2) -> {
            return (menu1.getAreaOrder() == null ? 10 : menu1.getAreaOrder()) - (menu2.getAreaOrder() == null ? 10 : menu2.getAreaOrder());
        }).collect(Collectors.toList());
        return children;
    }

    @Override
    public List<SysArea> listWithTreeById() {
        //1、查出所有分类
        List<SysArea> entities = baseMapper.selectList(null);
        //2、组成父子的树形结构
        //2.1、找到所有的一级分类
        List<SysArea> level1Menus = entities.stream().filter(SysArea ->
                SysArea.getParentId().equals("0")
        ).map((menu) -> {
            menu.setChildren(getChildrens(menu, entities));
            return menu;
        }).sorted((menu1, menu2) -> {
            return (menu1.getAreaOrder() == null ? 0 : menu1.getAreaOrder()) - (menu2.getAreaOrder() == null ? 0 : menu2.getAreaOrder());
        }).collect(Collectors.toList());
        return level1Menus;
    }

    @Override
    public String[] findParentIdPathName(String id) {
        List<String> paths = new ArrayList<>();
        List<String> parentPath = findParentPathName(id, paths);
        Collections.reverse(parentPath);
        return parentPath.toArray(new String[parentPath.size()]);
    }



    @Override
    public  List<String> listAreaId(String id) {
        //1、查出所有区划
        List<SysArea> entities = baseMapper.selectList(null);
        //2、组成父子的树形结构
        //2.1、找到所有的一级区划
        return entities.stream().filter(SysArea ->
                SysArea.getParentId().equals(id)
        ).map((menu) -> {
            List<SysArea> childrenss = getChildrenss(menu, entities);
            menu.setChildren(childrenss);
            List<String> collect1 = childrenss.stream().map(ch -> {
                String id1 = ch.getId();
                return id1;
            }).collect(Collectors.toList());
            List<String> arrayList = new ArrayList<>();
            String join1;
            if (!oConvertUtils.isEmpty(collect1)) {
                join1 = StringUtils.join(collect1, ",");
                arrayList.add(join1);
            }
            arrayList.add(menu.getId());
            return menu.getId();
        }).collect(Collectors.toList());
    }

    @Override
    public List<String> getChildId(String id) {
        List<String> paths = new ArrayList<>();
        paths.add(id);
        return findChildPath(id,paths);
    }

    @Override
    public List<SysArea> getAllChild() {
        List<SysArea> list = new ArrayList<>();
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //获取用户权限信息
        Map<String, Object> userOtherInfo = sysUserService.getUserOtherInfo(loginUser);
        if (userOtherInfo == null) {
            return list;
        }
        Boolean isAreaAdmin = (Boolean) userOtherInfo.get("isAreaAdmin");
        Boolean isDeptAdmin = (Boolean) userOtherInfo.get("isDeptAdmin");
        Boolean isBasicOperation = (Boolean) userOtherInfo.get("isBasicOperation");
        Boolean isAdmin = (Boolean) userOtherInfo.get("isAdmin");
        SysDepart currentDept = (SysDepart) userOtherInfo.get("currentDept");
        SysArea currentArea = (SysArea) userOtherInfo.get("currentArea");
        isAdmin = true;
        if (!isAdmin&&!isBasicOperation){
            if (isDeptAdmin&&!isAreaAdmin){
                return getSysAreas(currentArea);
            }else if (isAreaAdmin) {
                List<SysArea> entities = this.getChildArea(currentArea.getId());
                String[] parentIdPath = this.findParentIdPath(currentArea.getId());
                List<SysArea> collect = null;
                for (int i = parentIdPath.length-1; i >= 0; i--) {
                    if (i==parentIdPath.length-1){
                        SysArea byId = this.getById(parentIdPath[i]);
                        byId.setChildren(entities);
                        collect = Stream.of(byId).collect(Collectors.toList());
                    }
                    if (i<parentIdPath.length-1){
                        SysArea byId = this.getById(parentIdPath[i]);
                        byId.setChildren(collect);
                        collect = Stream.of(byId).collect(Collectors.toList());
                    }
                }
                return collect;
            }else {
                return getSysAreas(currentArea);
            }
        }else {
            return this.listWithTree();
        }
    }

    @Override
    public List<SysArea> getDepartAllChild() {
        List<SysArea> list = new ArrayList<>();
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //获取用户权限信息
        Map<String, Object> userOtherInfo = sysUserService.getUserOtherInfo(loginUser);
        if (userOtherInfo == null) {
            return list;
        }

        Boolean isBasicOperation = (Boolean) userOtherInfo.get("isBasicOperation");
        Boolean isAreaAdmin = (Boolean) userOtherInfo.get("isAreaAdmin");
        Boolean isDeptAdmin = (Boolean) userOtherInfo.get("isDeptAdmin");
        Boolean isAdmin = (Boolean) userOtherInfo.get("isAdmin");
        SysDepart currentDept = (SysDepart) userOtherInfo.get("currentDept");
        SysArea currentArea = (SysArea) userOtherInfo.get("currentArea");

        if (!isAdmin&&!isBasicOperation){
            if (isDeptAdmin&&!isAreaAdmin){
                return getDeptSysAreas(currentArea);
            }else if (isAreaAdmin) {
                List<SysArea> entities = this.getDeptChildArea(currentArea.getId());
                String[] parentIdPath = this.findParentIdPath(currentArea.getId());
                List<SysArea> collect = null;
                for (int i = parentIdPath.length-1; i >= 0; i--) {
                    if (i==parentIdPath.length-1){
                        SysArea byId = this.getById(parentIdPath[i]);
                        byId.setChildren(entities);
                        collect = Stream.of(byId).collect(Collectors.toList());
                    }
                    if (i<parentIdPath.length-1){
                        SysArea byId = this.getById(parentIdPath[i]);
                        if (byId.getStatus().equals("1")){
                            byId.setChildren(collect);
                            collect = Stream.of(byId).collect(Collectors.toList());
                        }
                    }
                }
                return collect;
            }else {
                return getDeptSysAreas(currentArea);
            }
        }else {
            return this.listDeptWithTree();
        }
    }


    public List<SysArea> listDeptWithTree() {
        //1、查出所有分类
        List<SysArea> entities = baseMapper.selectList(new QueryWrapper<SysArea>().eq("status", "1"));
        //2、组成父子的树形结构
        //2.1、找到所有的一级分类
        return entities.stream().filter(SysArea ->
                "0".equals(SysArea.getParentId())
        ).map((menu) -> {
            menu.setChildren(getChildrens(menu, entities));
            return menu;
        }).sorted(Comparator.comparingInt(menu -> (menu.getAreaOrder() == null ? 0 : menu.getAreaOrder()))).collect(Collectors.toList());
    }


    @Override
    @Transactional
    public void changeRelatedStatus(String id, String status) {
        List<String> childIds = this.quickGetChild(id);
        childIds.add(id);
        try {
            if (!childIds.isEmpty()){
                childIds.forEach(ids ->{
                    List<SysDepart> deptList = sysDepartService.list(new QueryWrapper<SysDepart>().eq("area_id", ids).eq("status", "1"));
                    boolean update = sysAreaService.update(new UpdateWrapper<SysArea>().eq("id", ids).set("status", "0"));
                    SysArea sysArea = sysAreaService.getById(ids);
                    tenantService.sendAreaMsg(sysArea, SendChangeMsgConstant.UPDATE);
                    if (deptList != null && !deptList.isEmpty()) {
                        deptList.forEach(this::ChangeDeptAndUserStatus);
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void ChangeDeptAndUserStatus(SysDepart depart) {
	    tenantService.sendUserByDept(depart);
    }

    @Override
    public void changeRelatedArea(SysArea sysArea, SysArea sysAreaEntity) {
        try {
            if (!sysArea.getAreaName().equals(sysAreaEntity.getAreaName())) {
                List<SysArea> areaList = sysAreaService.list(new QueryWrapper<SysArea>().eq("parent_id", sysArea.getId()));
                if (!oConvertUtils.isEmpty(areaList)) {
                    areaList.forEach(list -> {
                        list.setParentAreaName(sysArea.getAreaName());
                        boolean ok = sysAreaService.updateById(list);
                        SysArea sysArea1 = sysAreaService.getById(list.getParentId());
                        tenantService.sendAreaMsg(sysArea1, SendChangeMsgConstant.UPDATE);
                    });
                }
                List<SysDepart> departList = sysDepartService.list(new QueryWrapper<SysDepart>().eq("area_id", sysAreaEntity.getId()));
                if (!oConvertUtils.isEmpty(departList)) {
                    departList.forEach(depart ->{
                        depart.setAreaName(sysArea.getAreaName());
                        boolean ok = sysDepartService.updateById(depart);
                        tenantService.sendDeptMsg(depart, SendChangeMsgConstant.UPDATE);
                        List<SysUser> sysUsers = sysUserDepartService.queryUserByDepId(depart.getId());
                        if (!oConvertUtils.isEmpty(sysUsers)){
                            sysUsers.forEach(user ->{
                                user.setAreaName(sysArea.getAreaName());
                                boolean ob = sysUserService.updateById(user);
                                tenantService.sendUserMsg(user,depart, SendChangeMsgConstant.UPDATE);
                            });
                        }
                    });
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<String> quickGetChild(String id) {
        return baseMapper.quickGetChild(id);
    }

    @Nullable
    private List<SysArea> getDeptSysAreas(SysArea currentArea) {
        String[] parentIdPath = this.findParentIdPath(currentArea.getId());
        List<SysArea> collect = null;
        for (int i = parentIdPath.length - 1; i >= 0; i--) {
            if (i == parentIdPath.length - 1) {
                SysArea byId = this.getById(parentIdPath[i]);
                if (byId.getStatus().equals("1")) {
                    collect = Stream.of(byId).collect(Collectors.toList());
                }
            }
            if (i < parentIdPath.length - 1) {
                SysArea byId = this.getById(parentIdPath[i]);
                byId.setChildren(collect);
                if (byId.getStatus().equals("1")){
                    collect = Stream.of(byId).collect(Collectors.toList());
                }
            }
        }
        return collect;
    }

    public List<SysArea> getDeptChildArea(String id) {
        //1、查出所有区划
        List<SysArea> entities = baseMapper.selectList(new QueryWrapper<SysArea>().eq("status", "1"));
        //2、组成父子的树形结构
        //2.1、找到所有的一级区划
        return getTreeAreaList(id, entities);
    }

    @NotNull
    private List<SysArea> getTreeAreaList(String id, List<SysArea> entities) {
        return entities.stream().filter(SysArea ->
                SysArea.getParentId().equals(id)
        ).map((menu) -> {
            menu.setChildren(getChildrens(menu, entities));
            return menu;
        }).sorted(Comparator.comparingInt(menu -> (menu.getAreaOrder() == null ? 0 : menu.getAreaOrder()))).collect(Collectors.toList());
    }


    @Nullable
    private List<SysArea> getSysAreas(SysArea currentArea) {
        String[] parentIdPath = this.findParentIdPath(currentArea.getId());
        List<SysArea> collect = null;
        for (int i = parentIdPath.length - 1; i >= 0; i--) {
            if (i == parentIdPath.length - 1) {
                SysArea byId = this.getById(parentIdPath[i]);
                collect = Stream.of(byId).collect(Collectors.toList());
            }
            if (i < parentIdPath.length - 1) {
                SysArea byId = this.getById(parentIdPath[i]);
                byId.setChildren(collect);
                collect = Stream.of(byId).collect(Collectors.toList());
            }
        }
        return collect;
    }

    private List<String> findChildPath(String id, List<String> paths) {
        List<String> list = baseMapper.queryCidByPid(id);
        for (String s : list) {
            paths.add(s);
            //递归查询
            findChildPath(s, paths);
        }
        return paths;
    }

    //递归查找所有菜单的子菜单
    private List<SysArea> getChildrenss(SysArea root, List<SysArea> all) {
        List<SysArea> collect = all.stream().filter(SysArea -> SysArea.getParentId().equals(root.getId())).map(SysArea -> {
            getChildrenss(SysArea, all);
            return SysArea;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<SysArea> getChildArea(String id) {
        //1、查出所有区划
        List<SysArea> entities = baseMapper.selectList(null);
        //2、组成父子的树形结构
        //2.1、找到所有的一级区划
        return getTreeAreaList(id, entities);
    }


    private List<String> findParentPathName(String Id, List<String> paths) {
        //1、收集当前节点id
        SysArea byId = this.getById(Id);
        paths.add(byId.getAreaName());
        if(!byId.getParentId().equals(String.valueOf(0))){
            findParentPathName(byId.getParentId(),paths);
        }
        return paths;
    }




    private List<String> findParentPath(String Id, List<String> paths) {
        //1、收集当前节点id
        paths.add(Id);
        SysArea byId = this.getById(Id);
        if(!byId.getParentId().equals(String.valueOf(0))){
            findParentPath(byId.getParentId(),paths);
        }
        return paths;
    }

    /**
	 * 根据所传pid查询旧的父级节点的子节点并修改相应状态值
	 * @param pid
	 */
	private void updateOldParentNode(String pid) {
        if (!ISysAreaService.ROOT_PID_VALUE.equals(pid)) {
            Integer count = baseMapper.selectCount(new QueryWrapper<SysArea>().eq("parent_id", pid));
            if (count == null || count <= 1) {
                baseMapper.updateTreeNodeStatus(pid, ISysAreaService.NOCHILD);
            }
        }
    }

    /**
     * 递归查询节点的根节点
     * @param pidVal
     * @return
     */
    private SysArea getTreeRoot(String pidVal) {
        SysArea data = baseMapper.selectById(pidVal);
        if (data != null && !"0".equals(data.getParentId())) {
            return this.getTreeRoot(data.getParentId());
        } else {
            return data;
        }
    }

    /**
     * 根据id查询所有子节点id
     * @param ids
     * @return
     */
    private String queryTreeChildIds(String ids) {
        //获取id数组
        String[] idArr = ids.split(",");
        StringBuffer sb = new StringBuffer();
        for (String pidVal : idArr) {
            if(pidVal != null){
                if(!sb.toString().contains(pidVal)){
                    if(sb.toString().length() > 0){
                        sb.append(",");
                    }
                    sb.append(pidVal);
                    this.getTreeChildIds(pidVal,sb);
                }
            }
        }
        return sb.toString();
    }

    /**
     * 递归查询所有子节点
     * @param pidVal
     * @param sb
     * @return
     */
    private StringBuffer getTreeChildIds(String pidVal,StringBuffer sb){
        List<SysArea> dataList = baseMapper.selectList(new QueryWrapper<SysArea>().eq("parent_id", pidVal));
        if(dataList != null && dataList.size()>0){
            for (SysArea tree : dataList) {
                if (!sb.toString().contains(tree.getId())) {
                    sb.append(",").append(tree.getId());
                }
                this.getTreeChildIds(tree.getId(), sb);
            }
        }
        return sb;
    }

}
