package com.persagy.person.manage; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.google.common.collect.Lists; import com.persagy.common.constant.SaasCommonConstant; import com.persagy.common.exception.BusinessException; import com.persagy.person.pojo.dto.SaasAccountProject; import com.persagy.person.pojo.dto.SaasArea; import com.persagy.person.pojo.dto.SaasGroup; import com.persagy.person.pojo.dto.SaasProject; import com.persagy.person.pojo.vo.area.SaasAreaQueryVO; import com.persagy.person.pojo.vo.project.SaasProjectQueryVO; import com.persagy.person.service.ISaasAccountProjectService; import com.persagy.person.service.ISaasAreaService; import com.persagy.person.service.ISaasProjectService; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.BooleanUtil; /** * 区域项目信息管理 * * @version 1.0.0 * @company persagy * @author zhangqiankun * @date 2021-03-13 15:29:50 */ @Component public class SaasAreaProjectHandler { @Autowired private ISaasAreaService saasAreaService; @Autowired private ISaasProjectService saasProjectService; @Autowired private SaasGroupProjectHandler saasGroupProjectHandler; @Autowired private ISaasAccountProjectService saasAccountProjectService; /** * 查询区域项目树,结构:集团->区域->项目,此方法不做数据权限的控制 * @param queryVO * @param saasGroup */ public void queryAreaProjectTree(SaasGroup saasGroup) { List topList = this.getTopAreaList(saasGroup.getGroupCode()); saasGroup.setAreas(topList); this.querySaasAreaTree(topList, true); } /** * 获取集团下,顶层区域集合 * * @param groupCode * @return */ public List getTopAreaList(String groupCode) { LambdaQueryWrapper queryWrapper = new SaasArea.Builder().createQueryWrapper().groupCodeEq(groupCode).builderQueryWrapper(); queryWrapper.last("AND (PARENT_ID IS NULL OR TRIM(PARENT_ID) = '')"); List topList = this.saasAreaService.list(queryWrapper); return CollectionUtil.isEmpty(topList) ? Lists.newArrayList() : topList; } /** * 查询区域项目树 * * @param topList * @param groupCode * @param isCas 是否级联查询出项目信息 */ public void querySaasAreaTree(List topList, boolean isCas) { for (SaasArea saasArea : topList) { List nextList = null; if (isCas && SaasCommonConstant.STR_STATUS_1.equals(saasArea.getAreaType())) { // 级联查询出项目信息 LambdaQueryWrapper projectWrapper = new SaasProject.Builder().createQueryWrapper().areaIdEq(saasArea.getId()).groupCodeEq(saasArea.getGroupCode()).builderQueryWrapper(); List list = this.saasProjectService.list(projectWrapper); list = CollectionUtil.isEmpty(list) ? Lists.newArrayList() : list; saasArea.setProjects(list); } else { LambdaQueryWrapper queryWrapper = new SaasArea.Builder().createQueryWrapper().groupCodeEq(saasArea.getGroupCode()).parentIdEq(saasArea.getId()).builderQueryWrapper(); nextList = this.saasAreaService.list(queryWrapper); } if (CollectionUtil.isEmpty(nextList)) { nextList = Lists.newArrayList(); } saasArea.setChildrens(nextList); this.querySaasAreaTree(nextList, isCas); } } /** * 区域项目树查询,根据条件做数据权限的控制,剔除不存在项目的区域信息 * * @param queryVO 条件 * @param saasGroup 集团信息,不可为空 * @param areaList 区域信息 */ public SaasGroup queryAllowAreaProjectTree(SaasProjectQueryVO queryVO, SaasGroup saasGroup, List areaList) { // 查询可见范围内的项目信息 SaasProject saasProject = new SaasProject(); BeanUtils.copyProperties(queryVO, saasProject); Page projectList = this.saasGroupProjectHandler.querySaasProjectList(saasProject, null, null); List records = projectList.getRecords(); if (CollectionUtil.isEmpty(records)) { return saasGroup; //项目信息为空时,直接返回 } // 首先遍历项目信息,形成临时map Map> tempPro = new HashMap>(); for (SaasProject project : records) { if (tempPro.containsKey(project.getAreaId())) { tempPro.get(project.getAreaId()).add(project); } else { tempPro.put(project.getAreaId(), Lists.newArrayList(project)); } } // 组合成结果树 boolean showEmptyArea = BooleanUtil.isTrue(queryVO.getShowEmptyArea()); this.queryAllowAreaProjectTree(areaList, tempPro, !showEmptyArea); return saasGroup; } /** * 区域项目树封装,剔除不存在项目的区域信息 * * @param areaList 区域集合 * @param tempPro 临时项目信息集合 key-areaId,value-projectInfo * @param delEmptyArea 是否剔除空区域 */ private void queryAllowAreaProjectTree(List areaList, Map> tempPro, boolean delEmptyArea) { for (int i = areaList.size() - 1; i >= 0; i--) { SaasArea saasArea = areaList.get(i); //底层区域 if (SaasCommonConstant.STR_STATUS_1.equals(saasArea.getAreaType())) { if (tempPro.containsKey(saasArea.getId())) { saasArea.setProjects(tempPro.get(saasArea.getId())); } else if (delEmptyArea) { areaList.remove(i); //移除 } } else if (CollectionUtil.isEmpty(saasArea.getChildrens()) && delEmptyArea) { areaList.remove(i); //移除 } else { this.queryAllowAreaProjectTree(saasArea.getChildrens(), tempPro, delEmptyArea); // 最后还需判断一次,递归回路删除上级区域信息 if (delEmptyArea && CollectionUtil.isEmpty(saasArea.getChildrens()) && CollectionUtil.isEmpty(saasArea.getProjects())) { areaList.remove(i); //移除 } } } } /** * 查询区域平铺列表 * @param queryVO * @param isAdmin 是否需要查询出对应集团与区域下所有的项目数 */ public List querySaasAreaList(SaasAreaQueryVO queryVO, boolean isAdmin) { String groupCode = queryVO.getGroupCode(); String accountId = queryVO.getAccountId(); LambdaQueryWrapper queryWrapper = new SaasArea.Builder().createQueryWrapper().idEq(queryVO.getId()) .groupCodeEq(groupCode).areaCodeEq(queryVO.getAreaCode()).areaNameLike(queryVO.getAreaName()) .areaTypeEq(queryVO.getAreaType()).updateUserEq(queryVO.getUpdateUser()) .parentIdEq(queryVO.getParentId()).builderQueryWrapper(); List areas = this.saasAreaService.list(queryWrapper); if (CollectionUtil.isEmpty(areas)) { return Lists.newArrayList(); } if (queryVO.isTotal()) { // 统计出项目数量 for (SaasArea saasArea : areas) { if (SaasCommonConstant.STR_STATUS_1.equals(saasArea.getAreaType())) { int count = 0; if (isAdmin) { LambdaQueryWrapper projectWrapper = new SaasProject.Builder().createQueryWrapper().areaIdEq(saasArea.getId()).groupCodeEq(groupCode).builderQueryWrapper(); count = this.saasProjectService.count(projectWrapper); } else { // 权限过滤掉项目 count = this.saasProjectService.countAllowProjectNum(groupCode, accountId, saasArea.getId()); } saasArea.setProjectNum(count); continue; } // 非底层区域,递归查询 queryWrapper = new SaasArea.Builder().createQueryWrapper().groupCodeEq(groupCode).parentIdEq(saasArea.getId()).builderQueryWrapper(); List nextList = this.saasAreaService.list(queryWrapper); if (CollectionUtil.isEmpty(nextList)) { saasArea.setProjectNum(0); continue; } saasArea.setProjectNum(0); //初始化数量 this.queryAreaProjectNum(nextList, groupCode, saasArea); } } return areas; } /** * 底层区域平铺列表查询(权限过滤) * @param areas * @param isAdmin 是否需要查询出对应集团与区域下所有的项目数 */ public List totalAreaProjectNum(List areas, String accountId, boolean isAdmin) { for (SaasArea saasArea : areas) { if (SaasCommonConstant.STR_STATUS_1.equals(saasArea.getAreaType())) { int count = 0; if (isAdmin) { LambdaQueryWrapper projectWrapper = new SaasProject.Builder().createQueryWrapper().areaIdEq(saasArea.getId()).groupCodeEq(saasArea.getGroupCode()).builderQueryWrapper(); count = this.saasProjectService.count(projectWrapper); } else { // 权限过滤掉项目 count = this.saasProjectService.countAllowProjectNum(saasArea.getGroupCode(), accountId, saasArea.getId()); } saasArea.setProjectNum(count); } } return areas; } /** * 查询出当前区域集合,所包含的项目总数 * @param areas * @param groupCode * @param num */ private void queryAreaProjectNum(List areas, String groupCode, SaasArea saasArea) { for (SaasArea area : areas) { List nextList = null; if (SaasCommonConstant.STR_STATUS_1.equals(area.getAreaType())) { // 级联统计出项目信息 LambdaQueryWrapper projectWrapper = new SaasProject.Builder().createQueryWrapper().areaIdEq(area.getId()).groupCodeEq(groupCode).builderQueryWrapper(); int count = this.saasProjectService.count(projectWrapper); saasArea.setProjectNum(saasArea.getProjectNum() + count); } else { LambdaQueryWrapper queryWrapper = new SaasArea.Builder().createQueryWrapper().groupCodeEq(groupCode).parentIdEq(area.getId()).builderQueryWrapper(); nextList = this.saasAreaService.list(queryWrapper); } if (CollectionUtil.isEmpty(nextList)) { nextList = Lists.newArrayList(); } this.queryAreaProjectNum(nextList, groupCode, saasArea); } } /** * 查询被允许访问的区域树,账号类型为其他 * * @param topList 顶级区域集合 * @param accountId * @param groupCode */ public List queryAllowAreaTree(List topList, String accountId, String groupCode) { // 获取权限表中区域信息 Set areaIds = this.saasAccountProjectService.queryAccountAreaIds(accountId, groupCode); if (CollectionUtil.isEmpty(areaIds)) { return Lists.newArrayList(); } this.queryAllowAreaTree(topList, areaIds); return topList; } /** * 区域权限树封装,剔除不被允许访问的区域 * * @param areaList 区域集合 * @param tempPro 临时项目信息集合 key-areaId,value-projectInfo * @param delEmptyArea 是否剔除空区域 */ private void queryAllowAreaTree(List areaList, Set areaIds) { for (int i = areaList.size() - 1; i >= 0; i--) { SaasArea saasArea = areaList.get(i); //底层区域 if (SaasCommonConstant.STR_STATUS_1.equals(saasArea.getAreaType())) { if (!areaIds.contains(saasArea.getId())) { areaList.remove(i); //移除 } } else { this.queryAllowAreaTree(saasArea.getChildrens(), areaIds); // 最后还需判断一次,递归回路删除上级区域信息 if (CollectionUtil.isEmpty(saasArea.getChildrens()) && !areaIds.contains(saasArea.getId())) { areaList.remove(i); //移除 } } } } /** * 添加区域信息时,判断此账号是否需要添加一条权限信息 * * @param saasArea */ @Transactional public boolean createSaasArea(SaasArea saasArea, String accountId, String accountType) { boolean result = this.saasAreaService.save(saasArea); if (result && SaasCommonConstant.STR_STATUS_2.equals(accountType)) { // 其他账号类型时,添加一条账号、集团、分区的权限信息 SaasAccountProject saasAccountProject = new SaasAccountProject(); saasAccountProject.setAccountId(accountId); saasAccountProject.setGroupCode(saasArea.getGroupCode()); saasAccountProject.setAreaId(saasArea.getId()); result = this.saasAccountProjectService.save(saasAccountProject); if (!result) { throw new BusinessException("此账号,添加此新分区权限时,添加失败"); } } return result; } /** * 删除分区信息 * * @param groupCode * @param areaId * @param accountType * @return */ @Transactional public boolean deleteSaasArea(String groupCode, String areaId, String accountType) { boolean result = this.saasAreaService.removeById(areaId); if (result && SaasCommonConstant.STR_STATUS_2.equals(accountType)) { // 其他账号类型时,删除所有账号的此集团、此分区的权限信息,这里不要进行返回值判断 LambdaQueryWrapper queryWrapper = new SaasAccountProject.Builder().createQueryWrapper().groupCodeEq(groupCode).areaIdEq(areaId).builderQueryWrapper(); this.saasAccountProjectService.remove(queryWrapper); } return result; } /** * 验证此区域是否允许被删除 * * @param parentId * @param groupCode * @param result 调用时传true * @return 没有下级默认允许-true, */ /*public boolean validAreaProjectExist(List saasAreas, String groupCode) { if (CollectionUtil.isEmpty(saasAreas)) { return true; } for (SaasArea area : saasAreas) { // 如果是底层区域,判断,旗下是否存在项目信息,存在不允许删除 if (SaasCommonConstant.STR_STATUS_1.equals(area.getAreaType())) { LambdaQueryWrapper projectWrapper = new SaasProject.Builder().createQueryWrapper() .groupCodeEq(groupCode).areaIdEq(area.getId()).last("limit 0, 1").builderQueryWrapper(); SaasProject project = this.saasProjectService.getOne(projectWrapper); if (project != null) { return false; } } // 继续递归 LambdaQueryWrapper queryWrapper = new SaasArea.Builder().createQueryWrapper().parentIdEq(area.getId()).groupCodeEq(groupCode).builderQueryWrapper(); List nextAreas = this.saasAreaService.list(queryWrapper); this.validAreaProjectExist(nextAreas, groupCode); } }*/ }