SaasAreaController.java 10 KB

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