| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- 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<SaasArea> topList = this.getTopAreaList(saasGroup.getGroupCode());
- saasGroup.setAreas(topList);
- this.querySaasAreaTree(topList, true);
- }
-
- /**
- * 获取集团下,顶层区域集合
- *
- * @param groupCode
- * @return
- */
- public List<SaasArea> getTopAreaList(String groupCode) {
- LambdaQueryWrapper<SaasArea> queryWrapper = new SaasArea.Builder().createQueryWrapper().groupCodeEq(groupCode).builderQueryWrapper();
- queryWrapper.last("AND (PARENT_ID IS NULL OR TRIM(PARENT_ID) = '')");
- List<SaasArea> topList = this.saasAreaService.list(queryWrapper);
- return CollectionUtil.isEmpty(topList) ? Lists.newArrayList() : topList;
- }
-
- /**
- * 查询区域项目树
- *
- * @param topList
- * @param groupCode
- * @param isCas 是否级联查询出项目信息
- */
- public void querySaasAreaTree(List<SaasArea> topList, boolean isCas) {
- for (SaasArea saasArea : topList) {
- List<SaasArea> nextList = null;
- if (isCas && SaasCommonConstant.STR_STATUS_1.equals(saasArea.getAreaType())) {
- // 级联查询出项目信息
- LambdaQueryWrapper<SaasProject> projectWrapper = new SaasProject.Builder().createQueryWrapper().areaIdEq(saasArea.getId()).groupCodeEq(saasArea.getGroupCode()).builderQueryWrapper();
- List<SaasProject> list = this.saasProjectService.list(projectWrapper);
- list = CollectionUtil.isEmpty(list) ? Lists.newArrayList() : list;
- saasArea.setProjects(list);
- } else {
- LambdaQueryWrapper<SaasArea> 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<SaasArea> areaList) {
- // 查询可见范围内的项目信息
- SaasProject saasProject = new SaasProject();
- BeanUtils.copyProperties(queryVO, saasProject);
- Page<SaasProject> projectList = this.saasGroupProjectHandler.querySaasProjectList(saasProject, null, null);
- List<SaasProject> records = projectList.getRecords();
- if (CollectionUtil.isEmpty(records)) {
- return saasGroup; //项目信息为空时,直接返回
- }
- // 首先遍历项目信息,形成临时map
- Map<String, List<SaasProject>> tempPro = new HashMap<String, List<SaasProject>>();
- 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<SaasArea> areaList, Map<String, List<SaasProject>> 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<SaasArea> querySaasAreaList(SaasAreaQueryVO queryVO, boolean isAdmin) {
- String groupCode = queryVO.getGroupCode();
- String accountId = queryVO.getAccountId();
-
- LambdaQueryWrapper<SaasArea> 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<SaasArea> 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<SaasProject> 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<SaasArea> 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<SaasArea> totalAreaProjectNum(List<SaasArea> areas, String accountId, boolean isAdmin) {
- for (SaasArea saasArea : areas) {
- if (SaasCommonConstant.STR_STATUS_1.equals(saasArea.getAreaType())) {
- int count = 0;
- if (isAdmin) {
- LambdaQueryWrapper<SaasProject> 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<SaasArea> areas, String groupCode, SaasArea saasArea) {
- for (SaasArea area : areas) {
- List<SaasArea> nextList = null;
- if (SaasCommonConstant.STR_STATUS_1.equals(area.getAreaType())) {
- // 级联统计出项目信息
- LambdaQueryWrapper<SaasProject> projectWrapper = new SaasProject.Builder().createQueryWrapper().areaIdEq(area.getId()).groupCodeEq(groupCode).builderQueryWrapper();
- int count = this.saasProjectService.count(projectWrapper);
- saasArea.setProjectNum(saasArea.getProjectNum() + count);
- } else {
- LambdaQueryWrapper<SaasArea> 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<SaasArea> queryAllowAreaTree(List<SaasArea> topList, String accountId, String groupCode) {
- // 获取权限表中区域信息
- Set<String> 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<SaasArea> areaList, Set<String> 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<SaasAccountProject> 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<SaasArea> saasAreas, String groupCode) {
- if (CollectionUtil.isEmpty(saasAreas)) {
- return true;
- }
- for (SaasArea area : saasAreas) {
- // 如果是底层区域,判断,旗下是否存在项目信息,存在不允许删除
- if (SaasCommonConstant.STR_STATUS_1.equals(area.getAreaType())) {
- LambdaQueryWrapper<SaasProject> 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<SaasArea> queryWrapper = new SaasArea.Builder().createQueryWrapper().parentIdEq(area.getId()).groupCodeEq(groupCode).builderQueryWrapper();
- List<SaasArea> nextAreas = this.saasAreaService.list(queryWrapper);
- this.validAreaProjectExist(nextAreas, groupCode);
- }
- }*/
- }
|