SaasAreaController.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package com.persagy.person.controller;
  2. import java.util.List;
  3. import java.util.Map;
  4. import org.springframework.beans.BeanUtils;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.validation.annotation.Validated;
  7. import org.springframework.web.bind.annotation.RequestBody;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  12. import com.google.common.collect.Lists;
  13. import com.persagy.common.constant.SaasCommonConstant;
  14. import com.persagy.common.enums.ResponseCode;
  15. import com.persagy.common.utils.ResponseResult;
  16. import com.persagy.common.utils.ResponseResultUtil;
  17. import com.persagy.common.utils.StringUtil;
  18. import com.persagy.person.manage.SaasAreaProjectHandler;
  19. import com.persagy.person.manage.SaasAuthHandler;
  20. import com.persagy.person.pojo.dto.SaasAccount;
  21. import com.persagy.person.pojo.dto.SaasArea;
  22. import com.persagy.person.pojo.dto.SaasProject;
  23. import com.persagy.person.pojo.vo.BaseGroupVO;
  24. import com.persagy.person.pojo.vo.area.SaasAreaCreateVO;
  25. import com.persagy.person.pojo.vo.area.SaasAreaDeleteVO;
  26. import com.persagy.person.pojo.vo.area.SaasAreaQueryVO;
  27. import com.persagy.person.service.ISaasAccountService;
  28. import com.persagy.person.service.ISaasAreaService;
  29. import com.persagy.person.service.ISaasProjectService;
  30. import cn.hutool.core.collection.CollectionUtil;
  31. import io.swagger.annotations.Api;
  32. import io.swagger.annotations.ApiOperation;
  33. /**
  34. * 区域信息
  35. *
  36. * @version 1.0.0
  37. * @company persagy
  38. * @author zhangqiankun
  39. * @date 2021-03-13 15:29:50
  40. */
  41. @Api(tags = "区域信息")
  42. @RestController
  43. @RequestMapping(value = "/area", method = RequestMethod.POST)
  44. public class SaasAreaController {
  45. @Autowired
  46. private SaasAuthHandler saasAuthHandler;
  47. @Autowired
  48. private ISaasAreaService saasAreaService;
  49. @Autowired
  50. private ISaasProjectService saasProjectService;
  51. @Autowired
  52. private ISaasAccountService saasAccountService;
  53. @Autowired
  54. private SaasAreaProjectHandler saasAreaProjectHandler;
  55. /**
  56. * 查询区域树
  57. */
  58. @ApiOperation(value = "查询区域树")
  59. @RequestMapping(value = "querySaasAreaTree")
  60. public ResponseResult querySaasAreaTree(@RequestBody @Validated BaseGroupVO model) {
  61. List<SaasArea> topList = this.saasAreaProjectHandler.getTopAreaList(model.getGroupCode());
  62. this.saasAreaProjectHandler.querySaasAreaTree(topList, false);
  63. return ResponseResultUtil.successResult(topList);
  64. }
  65. /**
  66. * 查询区域权限树,做权限判断
  67. */
  68. @ApiOperation(value = "查询区域权限树")
  69. @RequestMapping(value = "queryAllowAreaTree")
  70. public ResponseResult queryAllowAreaTree(@RequestBody @Validated BaseGroupVO groupVO) {
  71. List<SaasArea> areaList = this.saasAuthHandler.getAllowAreaList(groupVO.getAccountId(), groupVO.getGroupCode());
  72. return ResponseResultUtil.successResult(areaList);
  73. }
  74. /**
  75. * 区域平铺列表查询
  76. */
  77. @ApiOperation(value = "区域平铺列表查询")
  78. @RequestMapping(value = "querySaasAreaList")
  79. public ResponseResult querySaasAreaList(@RequestBody @Validated SaasAreaQueryVO queryVO) {
  80. boolean isAllProjects = true;
  81. // 如果需要统计项目
  82. if (queryVO.isTotal()) {
  83. // 账号数据可见性的权限鉴定
  84. Map<String, String> authMap = this.saasAuthHandler.validGroupProjectAuth(queryVO.getAccountId(), queryVO.getGroupCode());
  85. if (StringUtil.isNotBlank(authMap.get(SaasCommonConstant.GROUP_CODE))) {
  86. queryVO.setGroupCode(authMap.get(SaasCommonConstant.GROUP_CODE));
  87. }
  88. // 添加数据访问控制,账号可见域,0-所有集团项目,1-单集团所有项目(此时,集团编码需存在),2-其他
  89. String accountType = authMap.get(SaasCommonConstant.ACCOUNT_TYPE);
  90. if (SaasCommonConstant.STR_STATUS_2.equals(accountType)) {
  91. isAllProjects = false;
  92. }
  93. }
  94. // 区域树查询
  95. List<SaasArea> list = this.saasAreaProjectHandler.querySaasAreaList(queryVO, isAllProjects);
  96. return ResponseResultUtil.successResult(list, (long)list.size());
  97. }
  98. /**
  99. * 底层区域平铺列表查询(权限过滤)
  100. */
  101. @ApiOperation(value = "底层区域平铺列表查询(权限过滤)")
  102. @RequestMapping(value = "queryAllowSaasAreaList")
  103. public ResponseResult queryAllowSaasAreaList(@RequestBody @Validated SaasAreaQueryVO queryVO) {
  104. boolean isAdmin = false;
  105. // 账号数据可见性的权限鉴定
  106. Map<String, String> authMap = this.saasAuthHandler.validGroupProjectAuth(queryVO.getAccountId(), queryVO.getGroupCode());
  107. if (StringUtil.isNotBlank(authMap.get(SaasCommonConstant.GROUP_CODE))) {
  108. queryVO.setGroupCode(authMap.get(SaasCommonConstant.GROUP_CODE));
  109. }
  110. // 添加数据访问控制,账号可见域,0-所有集团项目,1-单集团所有项目(此时,集团编码需存在),2-其他
  111. String accountType = authMap.get(SaasCommonConstant.ACCOUNT_TYPE);
  112. List<SaasArea> areas = null;
  113. if (SaasCommonConstant.STR_STATUS_2.equals(accountType)) {
  114. // 其他类型时,需要根据权限表数据,去匹配区域树
  115. SaasArea saasArea = new SaasArea();
  116. BeanUtils.copyProperties(queryVO, saasArea);
  117. areas = this.saasAreaService.queryAllowSaasAreaList(queryVO.getAccountId(), saasArea);
  118. } else {
  119. LambdaQueryWrapper<SaasArea> queryWrapper = new SaasArea.Builder().createQueryWrapper().idEq(queryVO.getId())
  120. .groupCodeEq(queryVO.getGroupCode()).areaCodeEq(queryVO.getAreaCode()).areaNameLike(queryVO.getAreaName())
  121. .areaTypeEq(queryVO.getAreaType()).updateUserEq(queryVO.getUpdateUser())
  122. .parentIdEq(queryVO.getParentId()).builderQueryWrapper();
  123. areas = this.saasAreaService.list(queryWrapper);
  124. isAdmin = true;
  125. }
  126. if (CollectionUtil.isEmpty(areas)) {
  127. return ResponseResultUtil.successResult(Lists.newArrayList(), 0L);
  128. }
  129. if (queryVO.isTotal()) {
  130. this.saasAreaProjectHandler.totalAreaProjectNum(areas, queryVO.getAccountId(), isAdmin);
  131. }
  132. return ResponseResultUtil.successResult(areas, (long)areas.size());
  133. }
  134. /**
  135. * 新增,区域编码与区域名称集团下唯一
  136. */
  137. @ApiOperation(value = "保存")
  138. @RequestMapping(value = "createSaasArea")
  139. public ResponseResult createSaasArea(@RequestBody @Validated SaasAreaCreateVO createVO) {
  140. // 验证账号信息是否存在
  141. SaasAccount saasAccount = this.saasAccountService.getOne(createVO.getAccountId(), null, null);
  142. if (saasAccount == null) {
  143. return ResponseResultUtil.errorResult(ResponseCode.A0400.getCode(), "账号信息,不存在");
  144. }
  145. // 验证区域编码与区域名称的唯一性
  146. boolean exist = this.saasAreaService.validAreaCodeAndName(createVO.getGroupCode(), createVO.getAreaCode(), createVO.getAreaName(), null);
  147. if (!exist) {
  148. return ResponseResultUtil.errorResult(ResponseCode.A0402.getCode(), "区域名称已存在,请检查");
  149. }
  150. SaasArea saasArea = new SaasArea();
  151. BeanUtils.copyProperties(createVO, saasArea);
  152. saasArea.setId(null);
  153. saasArea.setUpdateUser(createVO.getAccountId());
  154. exist = this.saasAreaProjectHandler.createSaasArea(saasArea, saasAccount.getId(), saasAccount.getAccountType());
  155. return exist ? ResponseResultUtil.successResult("保存成功", saasArea.getId()) : ResponseResultUtil.errorResult("保存失败");
  156. }
  157. /**
  158. * 更新,区域编码与区域名称集团下唯一
  159. */
  160. @ApiOperation(value = "更新")
  161. @RequestMapping(value = "updateSaasArea")
  162. public ResponseResult updateSaasArea(@RequestBody @Validated SaasAreaCreateVO createVO) {
  163. if (StringUtil.isBlank(createVO.getId())) {
  164. return ResponseResultUtil.errorResult(ResponseCode.A0400.getCode(), "区域ID不可为空");
  165. }
  166. //验证区域编码与区域名称的唯一性
  167. boolean exist = this.saasAreaService.validAreaCodeAndName(createVO.getGroupCode(), createVO.getAreaCode(), createVO.getAreaName(), createVO.getId());
  168. if (!exist) {
  169. return ResponseResultUtil.errorResult(ResponseCode.A0402.getCode(), "区域名称已存在,请检查");
  170. }
  171. SaasArea saasArea = new SaasArea();
  172. BeanUtils.copyProperties(createVO, saasArea);
  173. saasArea.setUpdateUser(createVO.getAccountId());
  174. boolean result = this.saasAreaService.updateById(saasArea);
  175. return result ? ResponseResultUtil.successResult("更新成功") : ResponseResultUtil.errorResult("更新失败");
  176. }
  177. /**
  178. * 物理删除
  179. */
  180. @ApiOperation(value = "删除")
  181. @RequestMapping(value = "deleteSaasArea")
  182. public ResponseResult deleteSaasArea(@RequestBody @Validated SaasAreaDeleteVO deleteVO) {
  183. // 验证账号信息是否存在
  184. SaasAccount saasAccount = this.saasAccountService.getOne(deleteVO.getAccountId(), null, null);
  185. if (saasAccount == null) {
  186. return ResponseResultUtil.errorResult(ResponseCode.A0400.getCode(), "账号信息,不存在");
  187. }
  188. // 验证区域是否存在
  189. SaasArea saasArea = this.saasAreaService.getById(deleteVO.getId());
  190. if (saasArea == null) {
  191. return ResponseResultUtil.errorResult(ResponseCode.C0320.getCode(), "区域信息不存在");
  192. }
  193. // 如果是底层区域,判断,旗下是否存在项目信息,存在不允许删除
  194. if (SaasCommonConstant.STR_STATUS_1.equals(saasArea.getAreaType())) {
  195. LambdaQueryWrapper<SaasProject> queryWrapper = new SaasProject.Builder().createQueryWrapper()
  196. .groupCodeEq(deleteVO.getGroupCode()).areaIdEq(deleteVO.getId())
  197. .last("limit 0, 1").builderQueryWrapper();
  198. SaasProject project = this.saasProjectService.getOne(queryWrapper);
  199. if (project != null) {
  200. return ResponseResultUtil.errorResult(ResponseCode.A0402.getCode(), "此区域下存在项目ID,不允许删除");
  201. }
  202. }
  203. boolean result = this.saasAreaProjectHandler.deleteSaasArea(deleteVO.getGroupCode(), saasArea.getId(), saasAccount.getAccountType());
  204. return result ? ResponseResultUtil.successResult("删除成功") : ResponseResultUtil.errorResult("删除失败");
  205. }
  206. }