SaasAreaProjectHandler.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. package com.persagy.person.manage;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.Set;
  6. import org.springframework.beans.BeanUtils;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Component;
  9. import org.springframework.transaction.annotation.Transactional;
  10. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  11. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  12. import com.google.common.collect.Lists;
  13. import com.persagy.common.constant.SaasCommonConstant;
  14. import com.persagy.common.exception.BusinessException;
  15. import com.persagy.person.pojo.dto.SaasAccountProject;
  16. import com.persagy.person.pojo.dto.SaasArea;
  17. import com.persagy.person.pojo.dto.SaasGroup;
  18. import com.persagy.person.pojo.dto.SaasProject;
  19. import com.persagy.person.pojo.vo.area.SaasAreaQueryVO;
  20. import com.persagy.person.pojo.vo.project.SaasProjectQueryVO;
  21. import com.persagy.person.service.ISaasAccountProjectService;
  22. import com.persagy.person.service.ISaasAreaService;
  23. import com.persagy.person.service.ISaasProjectService;
  24. import cn.hutool.core.collection.CollectionUtil;
  25. import cn.hutool.core.util.BooleanUtil;
  26. /**
  27. * 区域项目信息管理
  28. *
  29. * @version 1.0.0
  30. * @company persagy
  31. * @author zhangqiankun
  32. * @date 2021-03-13 15:29:50
  33. */
  34. @Component
  35. public class SaasAreaProjectHandler {
  36. @Autowired
  37. private ISaasAreaService saasAreaService;
  38. @Autowired
  39. private ISaasProjectService saasProjectService;
  40. @Autowired
  41. private SaasGroupProjectHandler saasGroupProjectHandler;
  42. @Autowired
  43. private ISaasAccountProjectService saasAccountProjectService;
  44. /**
  45. * 查询区域项目树,结构:集团->区域->项目,此方法不做数据权限的控制
  46. * @param queryVO
  47. * @param saasGroup
  48. */
  49. public void queryAreaProjectTree(SaasGroup saasGroup) {
  50. List<SaasArea> topList = this.getTopAreaList(saasGroup.getGroupCode());
  51. saasGroup.setAreas(topList);
  52. this.querySaasAreaTree(topList, true);
  53. }
  54. /**
  55. * 获取集团下,顶层区域集合
  56. *
  57. * @param groupCode
  58. * @return
  59. */
  60. public List<SaasArea> getTopAreaList(String groupCode) {
  61. LambdaQueryWrapper<SaasArea> queryWrapper = new SaasArea.Builder().createQueryWrapper().groupCodeEq(groupCode).builderQueryWrapper();
  62. queryWrapper.last("AND (PARENT_ID IS NULL OR TRIM(PARENT_ID) = '')");
  63. List<SaasArea> topList = this.saasAreaService.list(queryWrapper);
  64. return CollectionUtil.isEmpty(topList) ? Lists.newArrayList() : topList;
  65. }
  66. /**
  67. * 查询区域项目树
  68. *
  69. * @param topList
  70. * @param groupCode
  71. * @param isCas 是否级联查询出项目信息
  72. */
  73. public void querySaasAreaTree(List<SaasArea> topList, boolean isCas) {
  74. for (SaasArea saasArea : topList) {
  75. List<SaasArea> nextList = null;
  76. if (isCas && SaasCommonConstant.STR_STATUS_1.equals(saasArea.getAreaType())) {
  77. // 级联查询出项目信息
  78. LambdaQueryWrapper<SaasProject> projectWrapper = new SaasProject.Builder().createQueryWrapper().areaIdEq(saasArea.getId()).groupCodeEq(saasArea.getGroupCode()).builderQueryWrapper();
  79. List<SaasProject> list = this.saasProjectService.list(projectWrapper);
  80. list = CollectionUtil.isEmpty(list) ? Lists.newArrayList() : list;
  81. saasArea.setProjects(list);
  82. } else {
  83. LambdaQueryWrapper<SaasArea> queryWrapper = new SaasArea.Builder().createQueryWrapper().groupCodeEq(saasArea.getGroupCode()).parentIdEq(saasArea.getId()).builderQueryWrapper();
  84. nextList = this.saasAreaService.list(queryWrapper);
  85. }
  86. if (CollectionUtil.isEmpty(nextList)) {
  87. nextList = Lists.newArrayList();
  88. }
  89. saasArea.setChildrens(nextList);
  90. this.querySaasAreaTree(nextList, isCas);
  91. }
  92. }
  93. /**
  94. * 区域项目树查询,根据条件做数据权限的控制,剔除不存在项目的区域信息
  95. *
  96. * @param queryVO 条件
  97. * @param saasGroup 集团信息,不可为空
  98. * @param areaList 区域信息
  99. */
  100. public SaasGroup queryAllowAreaProjectTree(SaasProjectQueryVO queryVO, SaasGroup saasGroup, List<SaasArea> areaList) {
  101. // 查询可见范围内的项目信息
  102. SaasProject saasProject = new SaasProject();
  103. BeanUtils.copyProperties(queryVO, saasProject);
  104. Page<SaasProject> projectList = this.saasGroupProjectHandler.querySaasProjectList(saasProject, null, null);
  105. List<SaasProject> records = projectList.getRecords();
  106. if (CollectionUtil.isEmpty(records)) {
  107. return saasGroup; //项目信息为空时,直接返回
  108. }
  109. // 首先遍历项目信息,形成临时map
  110. Map<String, List<SaasProject>> tempPro = new HashMap<String, List<SaasProject>>();
  111. for (SaasProject project : records) {
  112. if (tempPro.containsKey(project.getAreaId())) {
  113. tempPro.get(project.getAreaId()).add(project);
  114. } else {
  115. tempPro.put(project.getAreaId(), Lists.newArrayList(project));
  116. }
  117. }
  118. // 组合成结果树
  119. boolean showEmptyArea = BooleanUtil.isTrue(queryVO.getShowEmptyArea());
  120. this.queryAllowAreaProjectTree(areaList, tempPro, !showEmptyArea);
  121. return saasGroup;
  122. }
  123. /**
  124. * 区域项目树封装,剔除不存在项目的区域信息
  125. *
  126. * @param areaList 区域集合
  127. * @param tempPro 临时项目信息集合 key-areaId,value-projectInfo
  128. * @param delEmptyArea 是否剔除空区域
  129. */
  130. private void queryAllowAreaProjectTree(List<SaasArea> areaList, Map<String, List<SaasProject>> tempPro, boolean delEmptyArea) {
  131. for (int i = areaList.size() - 1; i >= 0; i--) {
  132. SaasArea saasArea = areaList.get(i);
  133. //底层区域
  134. if (SaasCommonConstant.STR_STATUS_1.equals(saasArea.getAreaType())) {
  135. if (tempPro.containsKey(saasArea.getId())) {
  136. saasArea.setProjects(tempPro.get(saasArea.getId()));
  137. } else if (delEmptyArea) {
  138. areaList.remove(i); //移除
  139. }
  140. } else if (CollectionUtil.isEmpty(saasArea.getChildrens()) && delEmptyArea) {
  141. areaList.remove(i); //移除
  142. } else {
  143. this.queryAllowAreaProjectTree(saasArea.getChildrens(), tempPro, delEmptyArea);
  144. // 最后还需判断一次,递归回路删除上级区域信息
  145. if (delEmptyArea && CollectionUtil.isEmpty(saasArea.getChildrens()) && CollectionUtil.isEmpty(saasArea.getProjects())) {
  146. areaList.remove(i); //移除
  147. }
  148. }
  149. }
  150. }
  151. /**
  152. * 查询区域平铺列表
  153. * @param queryVO
  154. * @param isAdmin 是否需要查询出对应集团与区域下所有的项目数
  155. */
  156. public List<SaasArea> querySaasAreaList(SaasAreaQueryVO queryVO, boolean isAdmin) {
  157. String groupCode = queryVO.getGroupCode();
  158. String accountId = queryVO.getAccountId();
  159. LambdaQueryWrapper<SaasArea> queryWrapper = new SaasArea.Builder().createQueryWrapper().idEq(queryVO.getId())
  160. .groupCodeEq(groupCode).areaCodeEq(queryVO.getAreaCode()).areaNameLike(queryVO.getAreaName())
  161. .areaTypeEq(queryVO.getAreaType()).updateUserEq(queryVO.getUpdateUser())
  162. .parentIdEq(queryVO.getParentId()).builderQueryWrapper();
  163. List<SaasArea> areas = this.saasAreaService.list(queryWrapper);
  164. if (CollectionUtil.isEmpty(areas)) {
  165. return Lists.newArrayList();
  166. }
  167. if (queryVO.isTotal()) { // 统计出项目数量
  168. for (SaasArea saasArea : areas) {
  169. if (SaasCommonConstant.STR_STATUS_1.equals(saasArea.getAreaType())) {
  170. int count = 0;
  171. if (isAdmin) {
  172. LambdaQueryWrapper<SaasProject> projectWrapper = new SaasProject.Builder().createQueryWrapper().areaIdEq(saasArea.getId()).groupCodeEq(groupCode).builderQueryWrapper();
  173. count = this.saasProjectService.count(projectWrapper);
  174. } else {
  175. // 权限过滤掉项目
  176. count = this.saasProjectService.countAllowProjectNum(groupCode, accountId, saasArea.getId());
  177. }
  178. saasArea.setProjectNum(count);
  179. continue;
  180. }
  181. // 非底层区域,递归查询
  182. queryWrapper = new SaasArea.Builder().createQueryWrapper().groupCodeEq(groupCode).parentIdEq(saasArea.getId()).builderQueryWrapper();
  183. List<SaasArea> nextList = this.saasAreaService.list(queryWrapper);
  184. if (CollectionUtil.isEmpty(nextList)) {
  185. saasArea.setProjectNum(0);
  186. continue;
  187. }
  188. saasArea.setProjectNum(0); //初始化数量
  189. this.queryAreaProjectNum(nextList, groupCode, saasArea);
  190. }
  191. }
  192. return areas;
  193. }
  194. /**
  195. * 底层区域平铺列表查询(权限过滤)
  196. * @param areas
  197. * @param isAdmin 是否需要查询出对应集团与区域下所有的项目数
  198. */
  199. public List<SaasArea> totalAreaProjectNum(List<SaasArea> areas, String accountId, boolean isAdmin) {
  200. for (SaasArea saasArea : areas) {
  201. if (SaasCommonConstant.STR_STATUS_1.equals(saasArea.getAreaType())) {
  202. int count = 0;
  203. if (isAdmin) {
  204. LambdaQueryWrapper<SaasProject> projectWrapper = new SaasProject.Builder().createQueryWrapper().areaIdEq(saasArea.getId()).groupCodeEq(saasArea.getGroupCode()).builderQueryWrapper();
  205. count = this.saasProjectService.count(projectWrapper);
  206. } else {
  207. // 权限过滤掉项目
  208. count = this.saasProjectService.countAllowProjectNum(saasArea.getGroupCode(), accountId, saasArea.getId());
  209. }
  210. saasArea.setProjectNum(count);
  211. }
  212. }
  213. return areas;
  214. }
  215. /**
  216. * 查询出当前区域集合,所包含的项目总数
  217. * @param areas
  218. * @param groupCode
  219. * @param num
  220. */
  221. private void queryAreaProjectNum(List<SaasArea> areas, String groupCode, SaasArea saasArea) {
  222. for (SaasArea area : areas) {
  223. List<SaasArea> nextList = null;
  224. if (SaasCommonConstant.STR_STATUS_1.equals(area.getAreaType())) {
  225. // 级联统计出项目信息
  226. LambdaQueryWrapper<SaasProject> projectWrapper = new SaasProject.Builder().createQueryWrapper().areaIdEq(area.getId()).groupCodeEq(groupCode).builderQueryWrapper();
  227. int count = this.saasProjectService.count(projectWrapper);
  228. saasArea.setProjectNum(saasArea.getProjectNum() + count);
  229. } else {
  230. LambdaQueryWrapper<SaasArea> queryWrapper = new SaasArea.Builder().createQueryWrapper().groupCodeEq(groupCode).parentIdEq(area.getId()).builderQueryWrapper();
  231. nextList = this.saasAreaService.list(queryWrapper);
  232. }
  233. if (CollectionUtil.isEmpty(nextList)) {
  234. nextList = Lists.newArrayList();
  235. }
  236. this.queryAreaProjectNum(nextList, groupCode, saasArea);
  237. }
  238. }
  239. /**
  240. * 查询被允许访问的区域树,账号类型为其他
  241. *
  242. * @param topList 顶级区域集合
  243. * @param accountId
  244. * @param groupCode
  245. */
  246. public List<SaasArea> queryAllowAreaTree(List<SaasArea> topList, String accountId, String groupCode) {
  247. // 获取权限表中区域信息
  248. Set<String> areaIds = this.saasAccountProjectService.queryAccountAreaIds(accountId, groupCode);
  249. if (CollectionUtil.isEmpty(areaIds)) {
  250. return Lists.newArrayList();
  251. }
  252. this.queryAllowAreaTree(topList, areaIds);
  253. return topList;
  254. }
  255. /**
  256. * 区域权限树封装,剔除不被允许访问的区域
  257. *
  258. * @param areaList 区域集合
  259. * @param tempPro 临时项目信息集合 key-areaId,value-projectInfo
  260. * @param delEmptyArea 是否剔除空区域
  261. */
  262. private void queryAllowAreaTree(List<SaasArea> areaList, Set<String> areaIds) {
  263. for (int i = areaList.size() - 1; i >= 0; i--) {
  264. SaasArea saasArea = areaList.get(i);
  265. //底层区域
  266. if (SaasCommonConstant.STR_STATUS_1.equals(saasArea.getAreaType())) {
  267. if (!areaIds.contains(saasArea.getId())) {
  268. areaList.remove(i); //移除
  269. }
  270. } else {
  271. this.queryAllowAreaTree(saasArea.getChildrens(), areaIds);
  272. // 最后还需判断一次,递归回路删除上级区域信息
  273. if (CollectionUtil.isEmpty(saasArea.getChildrens()) && !areaIds.contains(saasArea.getId())) {
  274. areaList.remove(i); //移除
  275. }
  276. }
  277. }
  278. }
  279. /**
  280. * 添加区域信息时,判断此账号是否需要添加一条权限信息
  281. *
  282. * @param saasArea
  283. */
  284. @Transactional
  285. public boolean createSaasArea(SaasArea saasArea, String accountId, String accountType) {
  286. boolean result = this.saasAreaService.save(saasArea);
  287. if (result && SaasCommonConstant.STR_STATUS_2.equals(accountType)) {
  288. // 其他账号类型时,添加一条账号、集团、分区的权限信息
  289. SaasAccountProject saasAccountProject = new SaasAccountProject();
  290. saasAccountProject.setAccountId(accountId);
  291. saasAccountProject.setGroupCode(saasArea.getGroupCode());
  292. saasAccountProject.setAreaId(saasArea.getId());
  293. result = this.saasAccountProjectService.save(saasAccountProject);
  294. if (!result) {
  295. throw new BusinessException("此账号,添加此新分区权限时,添加失败");
  296. }
  297. }
  298. return result;
  299. }
  300. /**
  301. * 删除分区信息
  302. *
  303. * @param groupCode
  304. * @param areaId
  305. * @param accountType
  306. * @return
  307. */
  308. @Transactional
  309. public boolean deleteSaasArea(String groupCode, String areaId, String accountType) {
  310. boolean result = this.saasAreaService.removeById(areaId);
  311. if (result && SaasCommonConstant.STR_STATUS_2.equals(accountType)) {
  312. // 其他账号类型时,删除所有账号的此集团、此分区的权限信息,这里不要进行返回值判断
  313. LambdaQueryWrapper<SaasAccountProject> queryWrapper = new SaasAccountProject.Builder().createQueryWrapper().groupCodeEq(groupCode).areaIdEq(areaId).builderQueryWrapper();
  314. this.saasAccountProjectService.remove(queryWrapper);
  315. }
  316. return result;
  317. }
  318. /**
  319. * 验证此区域是否允许被删除
  320. *
  321. * @param parentId
  322. * @param groupCode
  323. * @param result 调用时传true
  324. * @return 没有下级默认允许-true,
  325. */
  326. /*public boolean validAreaProjectExist(List<SaasArea> saasAreas, String groupCode) {
  327. if (CollectionUtil.isEmpty(saasAreas)) {
  328. return true;
  329. }
  330. for (SaasArea area : saasAreas) {
  331. // 如果是底层区域,判断,旗下是否存在项目信息,存在不允许删除
  332. if (SaasCommonConstant.STR_STATUS_1.equals(area.getAreaType())) {
  333. LambdaQueryWrapper<SaasProject> projectWrapper = new SaasProject.Builder().createQueryWrapper()
  334. .groupCodeEq(groupCode).areaIdEq(area.getId()).last("limit 0, 1").builderQueryWrapper();
  335. SaasProject project = this.saasProjectService.getOne(projectWrapper);
  336. if (project != null) {
  337. return false;
  338. }
  339. }
  340. // 继续递归
  341. LambdaQueryWrapper<SaasArea> queryWrapper = new SaasArea.Builder().createQueryWrapper().parentIdEq(area.getId()).groupCodeEq(groupCode).builderQueryWrapper();
  342. List<SaasArea> nextAreas = this.saasAreaService.list(queryWrapper);
  343. this.validAreaProjectExist(nextAreas, groupCode);
  344. }
  345. }*/
  346. }