package com.persagy.person.controller; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.esotericsoftware.minlog.Log; import com.persagy.common.constant.SaasCommonConstant; import com.persagy.common.utils.ResponseResult; import com.persagy.common.utils.ResponseResultUtil; import com.persagy.common.utils.StringUtil; import com.persagy.person.pojo.dto.SaasAccount; import com.persagy.person.pojo.dto.SaasAccountProject; import com.persagy.person.pojo.dto.SaasArea; import com.persagy.person.pojo.dto.SaasProject; import com.persagy.person.pojo.vo.BaseUserVO; import com.persagy.person.service.ISaasAccountProjectService; import com.persagy.person.service.ISaasAccountService; import com.persagy.person.service.ISaasAreaService; import com.persagy.person.service.ISaasProjectService; import cn.hutool.core.collection.CollectionUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; /** * 此类,用做每次迭代升级时,无法通过sql兼容老数据,所需定义的接口信息 * @version * @description * @company persagy * @author zhangqiankun * @since 2021年4月13日: 下午5:46:44 */ @Api(tags = "迭代-升级接口管理") @RestController @RequestMapping(value = "/upgrade", method = RequestMethod.POST) public class UpgradeController { @Autowired private ISaasAreaService saasAreaService; @Autowired private ISaasAccountService saasAccountService; @Autowired private ISaasProjectService saasProjectService; @Autowired private RedisTemplate redisTemplate; @Autowired private ISaasAccountProjectService saasAccountProjectService; /** * 20210413号迭代升级,新增区域权限表,兼容老数据接口,本接口需在部署的时候,手动调用一次,必须返回成功,才算完成 * 为防止多次调用,这里采用 REPLACE INTO */ @ApiOperation(value = "新增区域权限,兼容老数据接口") @RequestMapping(value = "updateAccountAreaAuth") public ResponseResult updateAccountAreaAuth(@RequestBody @Validated BaseUserVO userVO) { List data = new ArrayList(); // 查询出所有的账号信息,包含停用的 List accounts = this.saasAccountService.list(); // 查询出以被分配的项目权限信息 List accountProject = this.saasAccountProjectService.list(); if (CollectionUtil.isEmpty(accountProject)) { // 获取所有区域信息 List areas = this.saasAreaService.list(); // 形成,账号、集团、区域关联关系 for (SaasAccount account : accounts) { for (SaasArea area : areas) { SaasAccountProject saasAccountArea = new SaasAccountProject(); saasAccountArea.setAccountId(account.getId()); saasAccountArea.setGroupCode(account.getGroupCode()); saasAccountArea.setAreaId(area.getId()); data.add(saasAccountArea); } } this.saasAccountProjectService.replaceAccountProject(data); return ResponseResultUtil.successResult(); } // 获取不存在项目的,空集合 // accountProject 不为空,更新对应记录的areaId for (SaasAccountProject project : accountProject) { String areaId = null; // 更新数据 LambdaUpdateWrapper wrapper = new SaasAccountProject.Builder().createUpdateWrapper().accountIdEq(project.getAccountId()) .groupCodeEq(project.getGroupCode()).projectIdEq(project.getProjectId()).builderUpdateWrapper(); if (StringUtil.isBlank(project.getProjectId())) { LambdaQueryWrapper queryWrapper = new SaasArea.Builder().createQueryWrapper().groupCodeEq(project.getGroupCode()).areaCodeEq(SaasCommonConstant.STR_STATUS_MINUS_1).builderQueryWrapper(); SaasArea saasArea = this.saasAreaService.getOne(queryWrapper); areaId = saasArea.getId(); wrapper.apply("(PROJECT_ID IS NULL OR TRIM(PROJECT_ID) = '')"); } else { LambdaQueryWrapper queryWrapper = new SaasProject.Builder().createQueryWrapper().groupCodeEq(project.getGroupCode()).projectIdEq(project.getProjectId()).builderQueryWrapper(); SaasProject saasProject = this.saasProjectService.getOne(queryWrapper); if (saasProject == null) { Log.error("项目信息不存在" + project.getProjectId()); continue; } areaId = saasProject.getAreaId(); wrapper.apply("PROJECT_ID IS NOT NULL AND TRIM(PROJECT_ID) != ''"); } wrapper.set(SaasAccountProject::getAreaId, areaId); this.saasAccountProjectService.update(wrapper); } return ResponseResultUtil.successResult(); } /** * 20210413号迭代升级,清空redis缓存 * 为防止多次调用,这里采用 REPLACE INTO */ @ApiOperation(value = "新增区域权限表,兼容老数据接口") @RequestMapping(value = "clearRedisCache") public ResponseResult clearRedisCache(@RequestBody @Validated BaseUserVO userVO) { // 查询出所有的账号信息,包含停用的 List accounts = this.saasAccountService.list(); for (SaasAccount saasAccount : accounts) { this.redisTemplate.delete(saasAccount.getId()); } return ResponseResultUtil.successResult(); } }