feat:(feature/REQ-2750) 增加私有接口,清洗角色权限点的数据

This commit is contained in:
lilong 2024-09-18 14:12:43 +08:00
parent d333d02cee
commit 67ebcd73dc

View File

@ -1,6 +1,7 @@
package cn.axzo.tyr.server.controller;
import cn.axzo.basics.common.exception.ServiceException;
import cn.axzo.framework.rocketmq.Event;
import cn.axzo.maokai.common.enums.SaasCooperateShipCooperateTypeEnum;
import cn.axzo.tyr.client.common.enums.RoleTypeEnum;
import cn.axzo.tyr.client.model.enums.DictWorkSpaceTypeEnum;
@ -8,6 +9,9 @@ import cn.axzo.tyr.client.model.enums.PermissionType;
import cn.axzo.tyr.client.model.enums.WorkspaceTypeCodeEnum;
import cn.axzo.tyr.client.model.req.ListRoleReq;
import cn.axzo.tyr.client.model.req.ListSaasRoleGroupParam;
import cn.axzo.tyr.client.model.req.PagePgroupPermissionRelationReq;
import cn.axzo.tyr.client.model.req.PageSaasFeatureResourceReq;
import cn.axzo.tyr.client.model.res.SaasFeatureResourceResp;
import cn.axzo.tyr.client.model.res.SaasRoleGroupDTO;
import cn.axzo.tyr.client.model.res.SaasRoleRes;
import cn.axzo.tyr.client.model.roleuser.dto.SaasRoleUserV2DTO;
@ -15,13 +19,21 @@ import cn.axzo.tyr.client.model.roleuser.req.ListRoleUserRelationParam;
import cn.axzo.tyr.client.model.vo.DeleteRoleVO;
import cn.axzo.tyr.client.model.vo.SaasRoleGroupVO;
import cn.axzo.tyr.client.model.vo.SaveOrUpdateRoleVO;
import cn.axzo.tyr.server.config.MqProducer;
import cn.axzo.tyr.server.event.payload.RolePermissionCreatedPayload;
import cn.axzo.tyr.server.model.ResourcePermission;
import cn.axzo.tyr.server.repository.dao.SaasPgroupRoleRelationDao;
import cn.axzo.tyr.server.repository.dao.SaasRoleDao;
import cn.axzo.tyr.server.repository.dao.SaasRoleUserRelationDao;
import cn.axzo.tyr.server.repository.entity.SaasPgroupPermissionRelation;
import cn.axzo.tyr.server.repository.entity.SaasPgroupRoleRelation;
import cn.axzo.tyr.server.repository.entity.SaasRole;
import cn.axzo.tyr.server.repository.entity.SaasRoleGroup;
import cn.axzo.tyr.server.repository.entity.SaasRoleGroupRelation;
import cn.axzo.tyr.server.repository.entity.SaasRoleUserRelation;
import cn.axzo.tyr.server.service.RoleService;
import cn.axzo.tyr.server.service.SaasFeatureResourceService;
import cn.axzo.tyr.server.service.SaasPgroupPermissionRelationService;
import cn.axzo.tyr.server.service.SaasRoleGroupRelationService;
import cn.axzo.tyr.server.service.SaasRoleGroupService;
import cn.axzo.tyr.server.service.SaasRoleUserRelationService;
@ -42,6 +54,7 @@ import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
@ -58,10 +71,14 @@ import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import static cn.axzo.tyr.server.event.inner.EventTypeEnum.ROLE_PERMISSION_CREATED;
import static cn.axzo.tyr.server.repository.entity.SaasPgroupPermissionRelation.NEW_FEATURE;
@Slf4j
@RestController
@RequiredArgsConstructor
@ -80,6 +97,16 @@ public class PrivateRoleController {
private RoleService roleService;
@Autowired
private SaasRoleUserRelationService saasRoleUserRelationService;
@Autowired
private SaasFeatureResourceService saasFeatureResourceService;
@Autowired
private SaasPgroupPermissionRelationService saasPgroupPermissionRelationService;
@Autowired
private SaasPgroupRoleRelationDao saasPgroupRoleRelationDao;
@Autowired
private MqProducer mqProducer;
private static final String TARGET_TYPE = "saasFeatureResourceId";
@PostMapping("/api/private/superAdmin/create")
@ -234,7 +261,7 @@ public class PrivateRoleController {
@PostMapping("/api/private/roleGroupAndRole/init")
public Object initRoleGroup(@RequestPart("file") MultipartFile file) throws IOException {
AdminPermissionImportTableAnalysisEventListener listener = new AdminPermissionImportTableAnalysisEventListener();
AdminPermissionImportTableAnalysisEventListener<ImportExcel> listener = new AdminPermissionImportTableAnalysisEventListener<>();
List<ImportExcel> projects = EasyExcel.read(file.getInputStream(), ImportExcel.class, listener)
.extraRead(CellExtraTypeEnum.MERGE)
@ -364,20 +391,23 @@ public class PrivateRoleController {
}
@Slf4j
public static class AdminPermissionImportTableAnalysisEventListener extends AnalysisEventListener<ImportExcel> {
public static class AdminPermissionImportTableAnalysisEventListener<T> extends AnalysisEventListener<T> {
public AdminPermissionImportTableAnalysisEventListener() {
}
private List<ImportExcel> list = new ArrayList<>();
private List<T> list = new ArrayList<>();
private List<CellExtra> cellExtraList = new ArrayList<>();
private int totalColumns = -1;
@Override
public void invoke(ImportExcel excelData, AnalysisContext analysisContext) {
public void invoke(T excelData, AnalysisContext analysisContext) {
log.info(" data -> {}", excelData);
list.add(excelData);
}
@Override
@ -406,7 +436,7 @@ public class PrivateRoleController {
}
}
public List<ImportExcel> getList() {
public List<T> getList() {
return list;
}
@ -418,7 +448,7 @@ public class PrivateRoleController {
private static final int HEAD_ROW_NUM = 1;
private void mergeExcelData(List<ImportExcel> excelDataList, List<CellExtra> cellExtraList, int headRowNum) {
private void mergeExcelData(List<T> excelDataList, List<CellExtra> cellExtraList, int headRowNum) {
cellExtraList.forEach(cellExtra -> {
int firstRowIndex = cellExtra.getFirstRowIndex() - headRowNum;
int lastRowIndex = cellExtra.getLastRowIndex() - headRowNum;
@ -435,8 +465,8 @@ public class PrivateRoleController {
});
}
private void setInitValueToList(Object filedValue, Integer rowIndex, Integer columnIndex, List<ImportExcel> data) {
ImportExcel object = data.get(rowIndex);
private void setInitValueToList(Object filedValue, Integer rowIndex, Integer columnIndex, List<T> data) {
T object = data.get(rowIndex);
for (Field field : object.getClass().getDeclaredFields()) {
field.setAccessible(true);
@ -454,9 +484,9 @@ public class PrivateRoleController {
}
}
private Object getInitValueFromList(Integer firstRowIndex, Integer firstColumnIndex, List<ImportExcel> data) {
private Object getInitValueFromList(Integer firstRowIndex, Integer firstColumnIndex, List<T> data) {
Object filedValue = null;
ImportExcel object = data.get(firstRowIndex);
T object = data.get(firstRowIndex);
for (Field field : object.getClass().getDeclaredFields()) {
field.setAccessible(true);
ExcelProperty annotation = field.getAnnotation(ExcelProperty.class);
@ -485,6 +515,472 @@ public class PrivateRoleController {
return "ok";
}
@PostMapping("/api/private/rolePermission/init")
public Object initRolePermission(@RequestPart("file") MultipartFile file) throws IOException, NoSuchFieldException, IllegalAccessException {
AdminPermissionImportTableAnalysisEventListener<ImportExcel> roleListener = new AdminPermissionImportTableAnalysisEventListener<>();
Map<String, String> roleCodes = EasyExcel.read(file.getInputStream(), ImportExcel.class, roleListener)
.extraRead(CellExtraTypeEnum.MERGE)
.sheet(0)
.doReadSync()
.stream()
.map(e -> (ImportExcel)e)
.collect(Collectors.toMap(e -> e.getProductType() + "_" + e.getRoleGroupName() + "_" + e.getRoleName(),
ImportExcel::getRoleCode));
List<ImportRolePermissionExcel> rolePermissions = EasyExcel.read(file.getInputStream())
.head(ImportRolePermissionExcel.class)
.headRowNumber(0)
.sheet(4)
.doReadSync();
List<ImportRolePermissionExcel> roles = rolePermissions.subList(0, 3);
ImportRolePermissionExcel product = roles.get(0);
ImportRolePermissionExcel roleGroup = roles.get(1);
ImportRolePermissionExcel role = roles.get(2);
Map<String, String> roleFields = Maps.newHashMap();
String lastProductName = null;
String lastRoleGroupName = null;
String lastRoleName = null;
for (int i = 8; i <= 165; i++) {
String field = "role" + i;
Class<?> clazzProduct = product.getClass();
Field fieldProduct = clazzProduct.getDeclaredField(field);
fieldProduct.setAccessible(true);
String productName = Optional.ofNullable(fieldProduct.get(product))
.map(String::valueOf)
.orElse(lastProductName);
lastProductName = productName;
Class<?> clazzRoleGroup = roleGroup.getClass();
Field fieldRoleGroup = clazzRoleGroup.getDeclaredField(field);
fieldRoleGroup.setAccessible(true);
String roleGroupName = Optional.ofNullable(fieldRoleGroup.get(roleGroup))
.map(String::valueOf)
.orElse(lastRoleGroupName);
lastRoleGroupName = roleGroupName;
Class<?> clazzRole = role.getClass();
Field fieldRole = clazzRole.getDeclaredField(field);
fieldRole.setAccessible(true);
String roleName = Optional.ofNullable(fieldRole.get(role))
.map(String::valueOf)
.orElse(lastRoleName);
lastRoleName = roleName;
roleFields.put(field, roleCodes.get(productName + "_" + roleGroupName + "_" + roleName));
}
List<ImportRolePermissionExcel> permissions = rolePermissions.subList(4, rolePermissions.size());
Map<String, SaasFeatureResourceResp> featureResources = saasFeatureResourceService.list(PageSaasFeatureResourceReq.builder()
.uniCodes(permissions.stream()
.map(ImportRolePermissionExcel::getUniCode)
.collect(Collectors.toSet()))
.build())
.stream()
.collect(Collectors.toMap(SaasFeatureResourceResp::getUniCode, Function.identity()));
Map<String, SaasRoleRes> roleResMap = roleService.list(ListRoleReq.builder()
.roleCodes(roleFields.values().stream().collect(Collectors.toSet()))
.build())
.stream()
.collect(Collectors.toMap(SaasRoleRes::getRoleCode, Function.identity()));
Map<Long, SaasPgroupRoleRelation> rolePgroups = saasPgroupRoleRelationDao.findByRoleIds(roleResMap.values().stream()
.map(SaasRoleRes::getId)
.collect(Collectors.toSet()))
.stream()
.collect(Collectors.toMap(SaasPgroupRoleRelation::getRoleId, Function.identity()));
List<SaasPgroupPermissionRelation> saasPgroupPermissionRelations = Lists.newArrayList();
Set<Long> roleIds = Sets.newHashSet();
for (ImportRolePermissionExcel permission : permissions) {
SaasFeatureResourceResp featureResource = featureResources.get(permission.getUniCode());
if (Objects.isNull(featureResource)) {
log.info("unicode not found,{}", permission.getUniCode());
} else {
for (int i = 8; i <= 165; i++) {
String field = "role" + i;
Class<?> clazzRole = permission.getClass();
Field fieldRole = clazzRole.getDeclaredField(field);
fieldRole.setAccessible(true);
Boolean rolePermission = Boolean.valueOf(String.valueOf(fieldRole.get(permission)));
if (BooleanUtils.isTrue(rolePermission)) {
String roleCode = roleFields.get(field);
SaasRoleRes saasRoleRes = roleResMap.get(roleCode);
SaasPgroupRoleRelation saasPgroupRoleRelation = rolePgroups.get(saasRoleRes.getId());
SaasPgroupPermissionRelation target = new SaasPgroupPermissionRelation();
target.setGroupId(saasPgroupRoleRelation.getGroupId());
target.setFeatureId(featureResource.getId());
target.setCreateBy(2051297L);
target.setUpdateBy(2051297L);
target.setFeatureType(featureResource.getFeatureType());
target.setType(NEW_FEATURE);
target.setTerminal(featureResource.getTerminal());
saasPgroupPermissionRelations.add(target);
roleIds.add(saasRoleRes.getId());
}
}
}
}
saasPgroupPermissionRelationService.saveBatch(saasPgroupPermissionRelations);
Event event = Event.builder()
.targetType(TARGET_TYPE)
.eventCode(ROLE_PERMISSION_CREATED.getEventCode())
.data(RolePermissionCreatedPayload.builder()
.roleIds(roleIds)
.build())
.build();
mqProducer.send(event);
return "ok";
}
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class ImportRolePermissionExcel {
@ExcelProperty(value = "权限点编码",index = 6)
private String uniCode;
@ExcelProperty(value = "",index = 8)
private String role8;
@ExcelProperty(value = "",index = 9)
private String role9;
@ExcelProperty(value = "",index = 10)
private String role10;
@ExcelProperty(value = "",index = 11)
private String role11;
@ExcelProperty(value = "",index = 12)
private String role12;
@ExcelProperty(value = "",index = 13)
private String role13;
@ExcelProperty(value = "",index = 14)
private String role14;
@ExcelProperty(value = "",index = 15)
private String role15;
@ExcelProperty(value = "",index = 16)
private String role16;
@ExcelProperty(value = "",index = 17)
private String role17;
@ExcelProperty(value = "",index = 18)
private String role18;
@ExcelProperty(value = "",index = 19)
private String role19;
@ExcelProperty(value = "",index = 20)
private String role20;
@ExcelProperty(value = "",index = 21)
private String role21;
@ExcelProperty(value = "",index = 22)
private String role22;
@ExcelProperty(value = "",index = 23)
private String role23;
@ExcelProperty(value = "",index = 24)
private String role24;
@ExcelProperty(value = "",index = 25)
private String role25;
@ExcelProperty(value = "",index = 26)
private String role26;
@ExcelProperty(value = "",index = 27)
private String role27;
@ExcelProperty(value = "",index = 28)
private String role28;
@ExcelProperty(value = "",index = 29)
private String role29;
@ExcelProperty(value = "",index = 30)
private String role30;
@ExcelProperty(value = "",index = 31)
private String role31;
@ExcelProperty(value = "",index = 32)
private String role32;
@ExcelProperty(value = "",index = 33)
private String role33;
@ExcelProperty(value = "",index = 34)
private String role34;
@ExcelProperty(value = "",index = 35)
private String role35;
@ExcelProperty(value = "",index = 36)
private String role36;
@ExcelProperty(value = "",index = 37)
private String role37;
@ExcelProperty(value = "",index = 38)
private String role38;
@ExcelProperty(value = "",index = 39)
private String role39;
@ExcelProperty(value = "",index = 40)
private String role40;
@ExcelProperty(value = "",index = 41)
private String role41;
@ExcelProperty(value = "",index = 42)
private String role42;
@ExcelProperty(value = "",index = 43)
private String role43;
@ExcelProperty(value = "",index = 44)
private String role44;
@ExcelProperty(value = "",index = 45)
private String role45;
@ExcelProperty(value = "",index = 46)
private String role46;
@ExcelProperty(value = "",index = 47)
private String role47;
@ExcelProperty(value = "",index = 48)
private String role48;
@ExcelProperty(value = "",index = 49)
private String role49;
@ExcelProperty(value = "",index = 50)
private String role50;
@ExcelProperty(value = "",index = 51)
private String role51;
@ExcelProperty(value = "",index = 52)
private String role52;
@ExcelProperty(value = "",index = 53)
private String role53;
@ExcelProperty(value = "",index = 54)
private String role54;
@ExcelProperty(value = "",index = 55)
private String role55;
@ExcelProperty(value = "",index = 56)
private String role56;
@ExcelProperty(value = "",index = 57)
private String role57;
@ExcelProperty(value = "",index = 58)
private String role58;
@ExcelProperty(value = "",index = 59)
private String role59;
@ExcelProperty(value = "",index = 60)
private String role60;
@ExcelProperty(value = "",index = 61)
private String role61;
@ExcelProperty(value = "",index = 62)
private String role62;
@ExcelProperty(value = "",index = 63)
private String role63;
@ExcelProperty(value = "",index = 64)
private String role64;
@ExcelProperty(value = "",index = 65)
private String role65;
@ExcelProperty(value = "",index = 66)
private String role66;
@ExcelProperty(value = "",index = 67)
private String role67;
@ExcelProperty(value = "",index = 68)
private String role68;
@ExcelProperty(value = "",index = 69)
private String role69;
@ExcelProperty(value = "",index = 70)
private String role70;
@ExcelProperty(value = "",index = 71)
private String role71;
@ExcelProperty(value = "",index = 72)
private String role72;
@ExcelProperty(value = "",index = 73)
private String role73;
@ExcelProperty(value = "",index = 74)
private String role74;
@ExcelProperty(value = "",index = 75)
private String role75;
@ExcelProperty(value = "",index = 76)
private String role76;
@ExcelProperty(value = "",index = 77)
private String role77;
@ExcelProperty(value = "",index = 78)
private String role78;
@ExcelProperty(value = "",index = 79)
private String role79;
@ExcelProperty(value = "",index = 80)
private String role80;
@ExcelProperty(value = "",index = 81)
private String role81;
@ExcelProperty(value = "",index = 82)
private String role82;
@ExcelProperty(value = "",index = 83)
private String role83;
@ExcelProperty(value = "",index = 84)
private String role84;
@ExcelProperty(value = "",index = 85)
private String role85;
@ExcelProperty(value = "",index = 86)
private String role86;
@ExcelProperty(value = "",index = 87)
private String role87;
@ExcelProperty(value = "",index = 88)
private String role88;
@ExcelProperty(value = "",index = 89)
private String role89;
@ExcelProperty(value = "",index = 90)
private String role90;
@ExcelProperty(value = "",index = 91)
private String role91;
@ExcelProperty(value = "",index = 92)
private String role92;
@ExcelProperty(value = "",index = 93)
private String role93;
@ExcelProperty(value = "",index = 94)
private String role94;
@ExcelProperty(value = "",index = 95)
private String role95;
@ExcelProperty(value = "",index = 96)
private String role96;
@ExcelProperty(value = "",index = 97)
private String role97;
@ExcelProperty(value = "",index = 98)
private String role98;
@ExcelProperty(value = "",index = 99)
private String role99;
@ExcelProperty(value = "",index = 100)
private String role100;
@ExcelProperty(value = "",index = 101)
private String role101;
@ExcelProperty(value = "",index = 102)
private String role102;
@ExcelProperty(value = "",index = 103)
private String role103;
@ExcelProperty(value = "",index = 104)
private String role104;
@ExcelProperty(value = "",index = 105)
private String role105;
@ExcelProperty(value = "",index = 106)
private String role106;
@ExcelProperty(value = "",index = 107)
private String role107;
@ExcelProperty(value = "",index = 108)
private String role108;
@ExcelProperty(value = "",index = 109)
private String role109;
@ExcelProperty(value = "",index = 110)
private String role110;
@ExcelProperty(value = "",index = 111)
private String role111;
@ExcelProperty(value = "",index = 112)
private String role112;
@ExcelProperty(value = "",index = 113)
private String role113;
@ExcelProperty(value = "",index = 114)
private String role114;
@ExcelProperty(value = "",index = 115)
private String role115;
@ExcelProperty(value = "",index = 116)
private String role116;
@ExcelProperty(value = "",index = 117)
private String role117;
@ExcelProperty(value = "",index = 118)
private String role118;
@ExcelProperty(value = "",index = 119)
private String role119;
@ExcelProperty(value = "",index = 120)
private String role120;
@ExcelProperty(value = "",index = 121)
private String role121;
@ExcelProperty(value = "",index = 122)
private String role122;
@ExcelProperty(value = "",index = 123)
private String role123;
@ExcelProperty(value = "",index = 124)
private String role124;
@ExcelProperty(value = "",index = 125)
private String role125;
@ExcelProperty(value = "",index = 126)
private String role126;
@ExcelProperty(value = "",index = 127)
private String role127;
@ExcelProperty(value = "",index = 128)
private String role128;
@ExcelProperty(value = "",index = 129)
private String role129;
@ExcelProperty(value = "",index = 130)
private String role130;
@ExcelProperty(value = "",index = 131)
private String role131;
@ExcelProperty(value = "",index = 132)
private String role132;
@ExcelProperty(value = "",index = 133)
private String role133;
@ExcelProperty(value = "",index = 134)
private String role134;
@ExcelProperty(value = "",index = 135)
private String role135;
@ExcelProperty(value = "",index = 136)
private String role136;
@ExcelProperty(value = "",index = 137)
private String role137;
@ExcelProperty(value = "",index = 138)
private String role138;
@ExcelProperty(value = "",index = 139)
private String role139;
@ExcelProperty(value = "",index = 140)
private String role140;
@ExcelProperty(value = "",index = 141)
private String role141;
@ExcelProperty(value = "",index = 142)
private String role142;
@ExcelProperty(value = "",index = 143)
private String role143;
@ExcelProperty(value = "",index = 144)
private String role144;
@ExcelProperty(value = "",index = 145)
private String role145;
@ExcelProperty(value = "",index = 146)
private String role146;
@ExcelProperty(value = "",index = 147)
private String role147;
@ExcelProperty(value = "",index = 148)
private String role148;
@ExcelProperty(value = "",index = 149)
private String role149;
@ExcelProperty(value = "",index = 150)
private String role150;
@ExcelProperty(value = "",index = 151)
private String role151;
@ExcelProperty(value = "",index = 152)
private String role152;
@ExcelProperty(value = "",index = 153)
private String role153;
@ExcelProperty(value = "",index = 154)
private String role154;
@ExcelProperty(value = "",index = 155)
private String role155;
@ExcelProperty(value = "",index = 156)
private String role156;
@ExcelProperty(value = "",index = 157)
private String role157;
@ExcelProperty(value = "",index = 158)
private String role158;
@ExcelProperty(value = "",index = 159)
private String role159;
@ExcelProperty(value = "",index = 160)
private String role160;
@ExcelProperty(value = "",index = 161)
private String role161;
@ExcelProperty(value = "",index = 162)
private String role162;
@ExcelProperty(value = "",index = 163)
private String role163;
@ExcelProperty(value = "",index = 164)
private String role164;
@ExcelProperty(value = "",index = 165)
private String role165;
}
@Data
@Builder
@NoArgsConstructor