UpgradeController.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package com.persagy.person.controller;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.data.redis.core.RedisTemplate;
  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.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  13. import com.esotericsoftware.minlog.Log;
  14. import com.persagy.common.constant.SaasCommonConstant;
  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.pojo.dto.SaasAccount;
  19. import com.persagy.person.pojo.dto.SaasAccountProject;
  20. import com.persagy.person.pojo.dto.SaasArea;
  21. import com.persagy.person.pojo.dto.SaasProject;
  22. import com.persagy.person.pojo.vo.BaseUserVO;
  23. import com.persagy.person.service.ISaasAccountProjectService;
  24. import com.persagy.person.service.ISaasAccountService;
  25. import com.persagy.person.service.ISaasAreaService;
  26. import com.persagy.person.service.ISaasProjectService;
  27. import cn.hutool.core.collection.CollectionUtil;
  28. import io.swagger.annotations.Api;
  29. import io.swagger.annotations.ApiOperation;
  30. /**
  31. * 此类,用做每次迭代升级时,无法通过sql兼容老数据,所需定义的接口信息
  32. * @version
  33. * @description
  34. * @company persagy
  35. * @author zhangqiankun
  36. * @since 2021年4月13日: 下午5:46:44
  37. */
  38. @Api(tags = "迭代-升级接口管理")
  39. @RestController
  40. @RequestMapping(value = "/upgrade", method = RequestMethod.POST)
  41. public class UpgradeController {
  42. @Autowired
  43. private ISaasAreaService saasAreaService;
  44. @Autowired
  45. private ISaasAccountService saasAccountService;
  46. @Autowired
  47. private ISaasProjectService saasProjectService;
  48. @Autowired
  49. private RedisTemplate<String, Object> redisTemplate;
  50. @Autowired
  51. private ISaasAccountProjectService saasAccountProjectService;
  52. /**
  53. * 20210413号迭代升级,新增区域权限表,兼容老数据接口,本接口需在部署的时候,手动调用一次,必须返回成功,才算完成
  54. * 为防止多次调用,这里采用 REPLACE INTO
  55. */
  56. @ApiOperation(value = "新增区域权限,兼容老数据接口")
  57. @RequestMapping(value = "updateAccountAreaAuth")
  58. public ResponseResult updateAccountAreaAuth(@RequestBody @Validated BaseUserVO userVO) {
  59. List<SaasAccountProject> data = new ArrayList<SaasAccountProject>();
  60. // 查询出所有的账号信息,包含停用的
  61. List<SaasAccount> accounts = this.saasAccountService.list();
  62. // 查询出以被分配的项目权限信息
  63. List<SaasAccountProject> accountProject = this.saasAccountProjectService.list();
  64. if (CollectionUtil.isEmpty(accountProject)) {
  65. // 获取所有区域信息
  66. List<SaasArea> areas = this.saasAreaService.list();
  67. // 形成,账号、集团、区域关联关系
  68. for (SaasAccount account : accounts) {
  69. for (SaasArea area : areas) {
  70. SaasAccountProject saasAccountArea = new SaasAccountProject();
  71. saasAccountArea.setAccountId(account.getId());
  72. saasAccountArea.setGroupCode(account.getGroupCode());
  73. saasAccountArea.setAreaId(area.getId());
  74. data.add(saasAccountArea);
  75. }
  76. }
  77. this.saasAccountProjectService.replaceAccountProject(data);
  78. return ResponseResultUtil.successResult();
  79. }
  80. // 获取不存在项目的,空集合
  81. // accountProject 不为空,更新对应记录的areaId
  82. for (SaasAccountProject project : accountProject) {
  83. String areaId = null;
  84. // 更新数据
  85. LambdaUpdateWrapper<SaasAccountProject> wrapper = new SaasAccountProject.Builder().createUpdateWrapper().accountIdEq(project.getAccountId())
  86. .groupCodeEq(project.getGroupCode()).projectIdEq(project.getProjectId()).builderUpdateWrapper();
  87. if (StringUtil.isBlank(project.getProjectId())) {
  88. LambdaQueryWrapper<SaasArea> queryWrapper = new SaasArea.Builder().createQueryWrapper().groupCodeEq(project.getGroupCode()).areaCodeEq(SaasCommonConstant.STR_STATUS_MINUS_1).builderQueryWrapper();
  89. SaasArea saasArea = this.saasAreaService.getOne(queryWrapper);
  90. areaId = saasArea.getId();
  91. wrapper.apply("(PROJECT_ID IS NULL OR TRIM(PROJECT_ID) = '')");
  92. } else {
  93. LambdaQueryWrapper<SaasProject> queryWrapper = new SaasProject.Builder().createQueryWrapper().groupCodeEq(project.getGroupCode()).projectIdEq(project.getProjectId()).builderQueryWrapper();
  94. SaasProject saasProject = this.saasProjectService.getOne(queryWrapper);
  95. if (saasProject == null) {
  96. Log.error("项目信息不存在" + project.getProjectId());
  97. continue;
  98. }
  99. areaId = saasProject.getAreaId();
  100. wrapper.apply("PROJECT_ID IS NOT NULL AND TRIM(PROJECT_ID) != ''");
  101. }
  102. wrapper.set(SaasAccountProject::getAreaId, areaId);
  103. this.saasAccountProjectService.update(wrapper);
  104. }
  105. return ResponseResultUtil.successResult();
  106. }
  107. /**
  108. * 20210413号迭代升级,清空redis缓存
  109. * 为防止多次调用,这里采用 REPLACE INTO
  110. */
  111. @ApiOperation(value = "新增区域权限表,兼容老数据接口")
  112. @RequestMapping(value = "clearRedisCache")
  113. public ResponseResult clearRedisCache(@RequestBody @Validated BaseUserVO userVO) {
  114. // 查询出所有的账号信息,包含停用的
  115. List<SaasAccount> accounts = this.saasAccountService.list();
  116. for (SaasAccount saasAccount : accounts) {
  117. this.redisTemplate.delete(saasAccount.getId());
  118. }
  119. return ResponseResultUtil.successResult();
  120. }
  121. }