package com.vci.web.service.impl; import com.vci.constant.FrameWorkBtmTypeConstant; import com.vci.corba.common.PLException; import com.vci.corba.common.data.UserEntityInfo; import com.vci.corba.framework.data.RoleInfo; import com.vci.corba.omd.data.BusinessObject; import com.vci.web.service.SmRoleQueryServiceI; import com.vci.dto.SmRoleDTO; import com.vci.web.service.SmUserQueryServiceI; import com.vci.enumpck.UI.RoleClassifyEnum; import com.vci.enumpck.UI.RoleControlAreaEnum; import com.vci.model.SmRoleForPlatform1; import com.vci.pagemodel.SmUserVO; import com.vci.po.SmRolePO; import com.vci.pagemodel.SmRoleVO; import com.vci.omd.utils.ObjectTool; import com.vci.starter.poi.bo.ReadExcelOption; import com.vci.starter.poi.constant.ExcelLangCodeConstant; import com.vci.starter.poi.util.ExcelUtil; import com.vci.starter.web.constant.QueryOptionConstant; import com.vci.starter.web.exception.VciBaseException; import com.vci.starter.web.pagemodel.*; import com.vci.starter.web.util.LangBaseUtil; import com.vci.starter.web.util.Lcm.BeanUtil; import com.vci.starter.web.util.VciBaseUtil; import com.vci.starter.web.util.WebThreadLocalUtil; import com.vci.starter.web.wrapper.VciQueryWrapperForDO; import com.vci.enumpck.UserTypeEnum; import com.vci.web.service.WebBoServiceI; import com.vci.starter.web.util.Lcm.Func; import com.vci.web.util.PlatformClientUtil; import com.vci.web.util.WebUtil; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import java.io.File; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; /** * 角色的查询服务,兼容来平台,素有的接口都返回新的角色显示对象 * @author xiejun * @date 2020/3/10 */ @Service public class SmRoleQueryServiceImpl implements SmRoleQueryServiceI { /** * 业务数据服务 */ @Autowired private WebBoServiceI boService; /** * 用户查询服务 */ @Autowired @Lazy private SmUserQueryServiceI smUserQueryServiceI; /** * 平台调用客户端 */ @Autowired private PlatformClientUtil platformClientUtil; private final Integer QUERY_IN_LIMIT = 1000; /** * 日志 */ private Logger logger = LoggerFactory.getLogger(getClass()); /** * 根据角色主键获取角色的信息 * @param conditionMap 角色主键 * @return 角色的显示对象,如果角色不存在则返回null,不会抛出异常 * @throws VciBaseException 参数为空或者数据库存在问题的时候会抛出异常 */ @Override public SmRoleVO getRoleByConditionMap(Map conditionMap) throws VciBaseException { if(Func.isEmpty(conditionMap)){ return null; } VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(conditionMap, SmRoleForPlatform1.class); List smRoleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class); if(Func.isNotEmpty(smRoleForPlatform1s) && Func.isNotBlank(smRoleForPlatform1s.get(0).getPluid())){ //角色名不能重复所以直接取第一个元素 return platformRole2SmRoleVO(smRoleForPlatform1s.get(0)); } return null; } /** * 批量获取角色的信息 (根据角色主键) * @param roleOidCollections 角色主键的集合,可以超过1000个 * @return 角色的显示对象,如果角色不存在则返回空的列表,不会抛出异常 * @throws VciBaseException 参数为空或者数据库存在问题的时候会抛出异常 */ @Override public List listRoleByRoleOids( Collection roleOidCollections) throws VciBaseException { if(CollectionUtils.isEmpty(roleOidCollections)){ return new ArrayList<>(); } List roles = new ArrayList<>(); WebUtil.switchCollectionForOracleIn(roleOidCollections).stream().forEach(roleOids->{ VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(null,SmRoleForPlatform1.class); queryWrapper.in("pluid", VciBaseUtil.toInSql(roleOids.toArray(new String[0]))); List roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class); if(!CollectionUtils.isEmpty(roleForPlatform1s)){ roles.addAll(roleForPlatform1s); } }); return platformRole2SmRoleVOs(roles); } /** * 获取角色的列表,默认会以角色名升序排列,角色的编辑页面列表不要使用这个接口 * @param queryMap 查询条件 * @param pageHelper 分页和排序的信息,在兼容老平台的时候会自动兼容,如果属性不存在会自动忽略 * @return 角色的显示对象列表 * @throws VciBaseException 参数为空的时候会抛出异常 */ @Override public DataGrid gridRoles(Map queryMap, PageHelper pageHelper) throws VciBaseException { if(pageHelper == null){ pageHelper = new PageHelper(-1); } pageHelper.addDefaultAsc("plname"); VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(queryMap,SmRoleForPlatform1.class,pageHelper); List roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class); DataGrid dataGrid = new DataGrid<>(); if(!CollectionUtils.isEmpty(roleForPlatform1s)){ dataGrid.setData(platformRole2SmRoleVOs(roleForPlatform1s)); dataGrid.setTotal(boService.countByQueryWrapper(queryWrapper,SmRoleForPlatform1.class)); } return dataGrid; } /** * 根据角色主键获取角色的姓名 * @param roleOid 角色主键 * @return 角色姓名,如果不存在会返回null */ @Override public String getRoleNameByRoleOid(String roleOid) { WebUtil.alertNotNull(roleOid,"角色主键"); Map conditionMap = new HashMap<>(); conditionMap.put("pluid",roleOid); return getRoleByConditionMap(conditionMap).getName(); } /** * 根据角色名查询角色信息 * @param roleNameList * @param queryMap * @return */ @Override public List listRoleByRoleName(Collection roleNameList, Map queryMap) { if(Func.isEmpty(roleNameList)){ return new ArrayList<>(); } if(queryMap == null){ queryMap = new HashMap<>(); } List roles = new ArrayList<>(); if(roleNameList.size() > QUERY_IN_LIMIT){ Map finalQueryMap = queryMap; WebUtil.switchCollectionForOracleIn(roleNameList).stream().forEach(roleNames->{ Map conditionMap = new HashMap<>(); finalQueryMap.forEach((key,value)->{ conditionMap.put(key,value); }); conditionMap.put("plname", QueryOptionConstant.IN + WebUtil.toInSql(roleNames.toArray(new String[0]))); VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(conditionMap,SmRoleForPlatform1.class); List roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class); if(!CollectionUtils.isEmpty(roleForPlatform1s)){ roles.addAll(roleForPlatform1s); } }); }else{ queryMap.put("plname", QueryOptionConstant.IN + WebUtil.toInSql(roleNameList.toArray(new String[0]))); } VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(queryMap,SmRoleForPlatform1.class); List roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class); if(!CollectionUtils.isEmpty(roleForPlatform1s)){ roles.addAll(roleForPlatform1s); } return platformRole2SmRoleVOs(roles); } /** * 根据用户主键获取关联的角色 * @param userOid 用户主键 * @param queryMap 查询条件,如果需要使用用户的属性来查询可以使用pkUser.xxxx * @return 角色的显示对象 */ @Override public List listRoleByUserOid(String userOid, Map queryMap) { return listRoleByUserOid(userOid,queryMap,false); } /** * 使用用户主键查询角色 * @param userOid 用户主键 * @param queryMap 查询条件 * @param notIn 是否为不包含 * @return 角色的显示对象 */ private List listRoleByUserOid(String userOid,Map queryMap,boolean notIn){ if(StringUtils.isBlank(userOid)){ return new ArrayList<>(); } if(queryMap == null){ queryMap = new HashMap<>(); } List roles = new ArrayList<>(); if(userOid.contains(",")){ Map finalQueryMap = queryMap; WebUtil.switchCollectionForOracleIn(WebUtil.str2List(userOid)).stream().forEach(userOids->{ Map conditionMap = new HashMap<>(); finalQueryMap.forEach((key,value)->{ conditionMap.put(key,value); }); conditionMap.put("pluid", notIn ? QueryOptionConstant.NOTIN : QueryOptionConstant.IN + "select plroleuid from pluserrole where pluseruid in (" + WebUtil.toInSql(userOids.toArray(new String[0])) + ")"); VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(conditionMap,SmRoleForPlatform1.class); List roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class); if(!CollectionUtils.isEmpty(roleForPlatform1s)){ roles.addAll(roleForPlatform1s); } }); }else { queryMap.put("pluid", notIn ? QueryOptionConstant.NOTIN : QueryOptionConstant.IN + "select plroleuid from pluserrole where pluseruid ='" + userOid.trim() + "'"); } VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(queryMap,SmRoleForPlatform1.class); List roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class); if(!CollectionUtils.isEmpty(roleForPlatform1s)){ roles.addAll(roleForPlatform1s); } return platformRole2SmRoleVOs(roles); } /** * 根据用户主键获取关联的角色 * @param userOid 用户主键 * @param queryMap 查询条件,如果需要使用用户的属性来查询可以使用pkUser.xxxx * @param pageHelper 分页和排序对象,老平台不支持使用角色编号来排序 * @return 角色的显示对象 */ @Override public DataGrid gridRoleByUserOid(String userOid, Map queryMap, PageHelper pageHelper) { return gridRoleByUserOid(userOid,queryMap,pageHelper,false); } /** * 使用用户查询角色列表 * @param userOid 用户主键 * @param queryMap 查询条件 * @param pageHelper 分页对象 * @param notIn 不包含 * @return 列表数据 */ private DataGrid gridRoleByUserOid(String userOid,Map queryMap,PageHelper pageHelper,boolean notIn){ if(queryMap == null){ queryMap = new HashMap<>(); } if(StringUtils.isBlank(userOid)){ return new DataGrid<>(); } if(userOid.contains(",")){ String[] userOids = userOid.trim().split(","); if(userOids.length>1000){ //这个方法不支持超过1000个的用户查询 throw new VciBaseException("这个方法不支持超过1000个用户的主键来查询"); } queryMap.put("pluid", notIn ? QueryOptionConstant.NOTIN : QueryOptionConstant.IN + "select plroleuid from pluserrole where pluseruid in (" + WebUtil.toInSql(userOids) + ")"); }else { queryMap.put("pluid", notIn ? QueryOptionConstant.NOTIN : QueryOptionConstant.IN + "select plroleuid from pluserrole where pluseruid ='" + userOid.trim() + "'"); } return gridRoles(queryMap,pageHelper); } /** * 获取未关联某个用户的角色 * @param userOid 用户主键 * @param queryMap 查询条件,如果需要使用用户的属性来查询可以使用pkUser.xxxx * @return 角色的显示对象 */ @Override public List listRoleUnInUserOid(String userOid, Map queryMap) { return listRoleByUserOid(userOid,queryMap,true); } /** * 批量根据用户的主键来获取角色 * @param userOidCollection 用户主键集合 * @param queryMap 查询条件,如果需要使用用户的属性来查询可以使用pkUser.xxxx * @return 角色的显示对象,key是用户主键,value是这个用户关联的角色 */ @Override public Map> batchListRoleByUserOids( Collection userOidCollection, Map queryMap) { if(CollectionUtils.isEmpty(userOidCollection)){ return new HashMap<>(); } List roleVOList = new ArrayList<>(); Map> userRoleOidMap = new HashMap<>(); WebUtil.switchCollectionForOracleIn(userOidCollection).stream().forEach(userOids->{ List roleVOS = listRoleByUserOid(userOids.stream().collect(Collectors.joining(",")), queryMap, false); if(!CollectionUtils.isEmpty(roleVOS)){ roleVOList.addAll(roleVOS); String sql = "select pluseruid,plroleuid from pluserrole where pluseruid in (" + WebUtil.toInSql(userOids.toArray(new String[0])) + ")"; List cbos = boService.queryBySql(sql, null); if(!CollectionUtils.isEmpty(cbos)){ cbos.stream().forEach(cbo->{ String userOid = ObjectTool.getNewBOAttributeValue(cbo,"pluseruid"); List roleOids = userRoleOidMap.getOrDefault(userOid,new ArrayList<>()); roleOids.add(ObjectTool.getNewBOAttributeValue(cbo,"plroleuid")); userRoleOidMap.put(userOid,roleOids); }); } } }); if(!CollectionUtils.isEmpty(roleVOList)){ //这儿应该对roleVOList做一次去重处理,因为肯定会有重复的角色被查出来 Map roleVOMap = roleVOList.stream() .collect(Collectors.toMap(SmRoleVO::getOid, Function.identity(), (existing, replacement) -> existing)); //Map roleVOMap = roleVOList.stream().collect(Collectors.toMap(s -> s.getOid(), t -> t)); Map> userRoleVOMap = new HashMap<>(); userRoleOidMap.forEach((userOid,roleOids)->{ List roleVOS = new ArrayList<>(); roleOids.forEach(roleOid->{ if(roleVOMap.containsKey(roleOid)){ roleVOS.add(roleVOMap.get(roleOid)); } }); userRoleVOMap.put(userOid,roleVOS); }); return userRoleVOMap; } return new HashMap<>(); } /** * 根据权限主键获取关联的角色 * @param functionOid 权限主键 * @param queryMap 查询条件 * @return 角色的显示对象 */ @Override public List listRoleByFunctionOid(String functionOid, Map queryMap) { return listRoleByFunctionOid(functionOid,queryMap,false); } /** * 使用用户主键查询角色 * @param functionOid 用户主键 * @param queryMap 查询条件 * @param notIn 是否为不包含 * @return 角色的显示对象 */ private List listRoleByFunctionOid(String functionOid,Map queryMap,boolean notIn){ if(StringUtils.isBlank(functionOid)){ return new ArrayList<>(); } if(queryMap == null){ queryMap = new HashMap<>(); } List roles = new ArrayList<>(); if(functionOid.contains(",")){ Map finalQueryMap = queryMap; WebUtil.switchCollectionForOracleIn(WebUtil.str2List(functionOid)).stream().forEach(functionOids->{ Map conditionMap = new HashMap<>(); finalQueryMap.forEach((key,value)->{ conditionMap.put(key,value); }); conditionMap.put("pluid", notIn ? QueryOptionConstant.NOTIN : QueryOptionConstant.IN + "select plroleoid from plroleright where plfuncoid in (" + WebUtil.toInSql(functionOids.toArray(new String[0])) + ")"); VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(conditionMap,SmRoleForPlatform1.class); List roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class); if(!CollectionUtils.isEmpty(roleForPlatform1s)){ roles.addAll(roleForPlatform1s); } }); }else { queryMap.put("pluid", notIn ? QueryOptionConstant.NOTIN : QueryOptionConstant.IN + "select plroleoid from plroleright where plfuncoid ='" + functionOid.trim() + "'"); VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(queryMap,SmRoleForPlatform1.class); List roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class); if(!CollectionUtils.isEmpty(roleForPlatform1s)){ roles.addAll(roleForPlatform1s); } } return platformRole2SmRoleVOs(roles); } /** * 获取未关联某个权限的角色 * @param functionOid 权限主键 * @param queryMap 查询条件 * @param pageHelper 分页和排序对象,老平台不支持使用角色编号来排序 * @return 角色的显示对象 */ @Override public DataGrid gridRoleByFunctionOid(String functionOid, Map queryMap, PageHelper pageHelper) { return gridRoleByFunctionOid(functionOid,queryMap,pageHelper,false); } /** * 使用权限查询角色列表 * @param functionOid 权限主键 * @param queryMap 查询条件 * @param pageHelper 分页对象 * @param notIn 不包含 * @return 列表数据 */ private DataGrid gridRoleByFunctionOid(String functionOid,Map queryMap,PageHelper pageHelper,boolean notIn){ if(queryMap == null){ queryMap = new HashMap<>(); } if(StringUtils.isBlank(functionOid)){ return new DataGrid<>(); } if(functionOid.contains(",")){ String[] functionOids= functionOid.trim().split(","); if(functionOids.length>1000){ //这个方法不支持超过1000个的用户查询 throw new VciBaseException("这个方法不支持超过1000个权限的主键来查询"); } queryMap.put("pluid", notIn ? QueryOptionConstant.NOTIN : QueryOptionConstant.IN + "select plroleoid from plroleright where plfuncoid in (" + WebUtil.toInSql(functionOids) + ")"); }else { queryMap.put("pluid", notIn ? QueryOptionConstant.NOTIN : QueryOptionConstant.IN + "select plroleoid from plroleright where plfuncoid ='" + functionOid.trim() + "'"); } return gridRoles(queryMap,pageHelper); } /** * 获取未关联某个权限的角色 * @param functionOid 权限主键 * @param queryMap 查询条件 * @return 角色的显示对象 */ @Override public List listRoleUnInFunctionOid(String functionOid, Map queryMap) { return listRoleByFunctionOid(functionOid,queryMap,true); } /** * 根据权限主键获取关联的角色 * @param functionOid 权限主键 * @param queryMap 查询条件 * @param pageHelper 分页和排序对象,老平台不支持使用角色编号来排序 * @return 角色的显示对象 */ @Override public DataGrid gridRoleUnInFunctionOid(String functionOid, Map queryMap, PageHelper pageHelper) { return gridRoleByFunctionOid(functionOid,queryMap,pageHelper,true); } /** * 批量根据权限的主键来获取角色 * @param functionOidCollection 权限主键集合 * @param queryMap 查询条件,如果需要使用用户的属性来查询可以使用pkUser.xxxx * @return 角色的显示对象,key是权限主键,value是这个权限关联的角色 */ @Override public Map> batchListRoleByFunctionOids( Collection functionOidCollection, Map queryMap) { if(CollectionUtils.isEmpty(functionOidCollection)){ return new HashMap<>(); } List roleVOList = new ArrayList<>(); Map> funcRoleOidMap = new HashMap<>(); WebUtil.switchCollectionForOracleIn(functionOidCollection).stream().forEach(userOids->{ List roleVOS = listRoleByFunctionOid(userOids.stream().collect(Collectors.joining(",")), queryMap, false); if(!CollectionUtils.isEmpty(roleVOS)){ roleVOList.addAll(roleVOS); String sql = "select plroleoid,plfuncoid from plroleright where plfuncoid in (" + WebUtil.toInSql(userOids.toArray(new String[0])) + ")"; List cbos = boService.queryBySql(sql, null); if(!CollectionUtils.isEmpty(cbos)){ cbos.stream().forEach(cbo->{ String funcOid = ObjectTool.getBOAttributeValue(cbo,"plfuncoid"); List roleOids = funcRoleOidMap.getOrDefault(funcOid,new ArrayList<>()); roleOids.add(ObjectTool.getBOAttributeValue(cbo,"plroleoid")); funcRoleOidMap.put(funcOid,roleOids); }); } } }); if(!CollectionUtils.isEmpty(roleVOList)){ Map roleVOMap = roleVOList.stream().collect(Collectors.toMap(s -> s.getOid(), t -> t)); Map> userRoleVOMap = new HashMap<>(); funcRoleOidMap.forEach((funcOid,roleOids)->{ List roleVOS = new ArrayList<>(); roleOids.forEach(roleOid->{ if(roleVOMap.containsKey(roleOid)){ roleVOS.add(roleVOMap.get(roleOid)); } }); userRoleVOMap.put(funcOid,roleVOS); }); return userRoleVOMap; } return new HashMap<>(); } /** * 参照角色的列表,超管查全部,其他的都是查普通的(不包含三员) * @param queryMap 查询条件 * @param pageHelper 分页和排序的信息,在兼容老平台的时候会自动兼容,如果属性不存在会自动忽略 * @return 角色的显示对象列表 * @throws VciBaseException 参数为空的时候会抛出异常 */ @Override public DataGrid refGridRoles(Map queryMap, PageHelper pageHelper) throws VciBaseException { //老平台没有生效状态 //根据当前用户来决定能查那些角色:普通用户只能查普通角色,管理员和普通用户只能查普通角色 String usertype = WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUsertype(); if(!UserTypeEnum.SUPPER_ADMIN.getValue().equals(usertype)){ queryMap.put("pltype","2"); } return gridRoles(queryMap, pageHelper); } /** * 角色的树形展示,常用于角色选择用户的页面使用 * @param treeQueryObject 树查询对象 * @return 树的节点 * @throws VciBaseException 参数为空的时候会抛出异常 */ @Override public List refTreeRoles(TreeQueryObject treeQueryObject) throws VciBaseException { VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(treeQueryObject.getConditionMap(),SmRoleForPlatform1.class); queryWrapper.eq("pltype","2"); List roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class); List roleVOList = platformRole2SmRoleVOs(roleForPlatform1s); List list=new ArrayList(); if(!CollectionUtils.isEmpty(roleVOList)){ for (SmRoleVO smRoleVO : roleVOList) { String name=smRoleVO.getName(); Tree tree=new Tree(smRoleVO.getOid(),name); tree.setShowCheckbox(treeQueryObject.isShowCheckBox()); try { tree.setAttributes(WebUtil.objectToMapString(smRoleVO)); } catch (Throwable e) { e.printStackTrace(); } list.add(tree); } } return list; } /** * 成员管理分配角色调用,保存用户角色关联关系 * @param userOids * @param roleIds * @return */ @Override public boolean saveRights(String[] userOids, String[] roleIds) throws PLException { VciBaseUtil.alertNotNull(userOids,"用户主键"); //roleIds允许为空,为空时清空当前用户的权限 UserEntityInfo userEntityInfo = new UserEntityInfo(WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId(),null); return platformClientUtil.getFrameworkService().saveRights(roleIds,userOids,userEntityInfo); } /** * 角色管理分配成员接口调用 * @param userOids * @param roleId * @return * @throws PLException */ @Override public boolean saveRight(String[] userOids, String roleId) throws PLException { VciBaseUtil.alertNotNull(roleId,"角色主键"); //roleIds允许为空,为空时清空当前用户的权限 UserEntityInfo userEntityInfo = new UserEntityInfo(WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId(),null); return platformClientUtil.getFrameworkService().saveRight(roleId,userOids,userEntityInfo); } /** * 新增角色 * @param smRoleDTO * @return true成功,false失败 */ @Override public boolean addRole(SmRoleDTO smRoleDTO) throws PLException { //判空 VciBaseUtil.alertNotNull( smRoleDTO,"添加的角色对象", smRoleDTO.getName(),"角色名"); //角色名判重 Map conditionMap = new HashMap<>(); conditionMap.put("plname",smRoleDTO.getName()); SmRoleVO dbSmRoleVO = getRoleByConditionMap(conditionMap); if(Func.isNotEmpty(dbSmRoleVO)){ throw new VciBaseException("该角色名称已经存在,请修改!"); } SessionInfo loginUser = WebThreadLocalUtil.getCurrentUserSessionInfoInThread(); String userId = loginUser.getUserId(); //角色类型,这里设置了会在changeRoleObjectToRoleInfo中转换 if(UserTypeEnum.SUPPER_ADMIN.getValue().equals(loginUser.getUsertype())){ smRoleDTO.setRoleClassify(RoleClassifyEnum.MANAGE.getValue()); }else { smRoleDTO.setRoleClassify(RoleClassifyEnum.BUSINESS.getValue()); } //生成存储的corba对象 Date date = new Date(); smRoleDTO.setCreateTime(date); smRoleDTO.setCreator(userId); smRoleDTO.setLastModifier(userId); RoleInfo roleInfo = changeSmRoleDTOToRoleInfo(smRoleDTO); UserEntityInfo userEntityInfo = new UserEntityInfo(userId, ""); String oid = platformClientUtil.getFrameworkService().saveRole(roleInfo, userEntityInfo,null); if (Func.isEmpty(oid)) { return false; } return true; } /** * 修改角色 * @param smRoleDTO * @return */ @Override public boolean updateRole(SmRoleDTO smRoleDTO) throws PLException { //判空 VciBaseUtil.alertNotNull( smRoleDTO,"添加的角色对象", smRoleDTO.getName(),"角色名"); //根据角色主键查询,确保修改的角色存在 Map conditionMap = new HashMap<>(); conditionMap.put("pluid",smRoleDTO.getOid()); SmRoleVO dbSmRoleVO = getRoleByConditionMap(conditionMap); //根据主键没查询到了用户 if(Func.isEmpty(dbSmRoleVO) || Func.isBlank(dbSmRoleVO.getOid())){ throw new PLException("500", new String[] { "当前修改的角色不存在!"}); } //查询数据库中的,根据角色名查重 conditionMap.clear(); conditionMap.put("plname",smRoleDTO.getName()); //排除掉当前修改的用户 conditionMap.put("pluid",QueryOptionConstant.NOTEQUAL + smRoleDTO.getName()); SmRoleVO reapeatSmRoleVO = getRoleByConditionMap(conditionMap); //说明修改为的角色名已存在,不能重复 if(Func.isNotEmpty(reapeatSmRoleVO) && smRoleDTO.getName().equals(reapeatSmRoleVO)){ throw new PLException("500", new String[] { "该角色名称已经存在,请修改!"}); } SmRoleDTO smRoleDTO1 = new SmRoleDTO(); BeanUtil.copy(dbSmRoleVO,smRoleDTO1); smRoleDTO1.setName(smRoleDTO.getName()); smRoleDTO1.setDescription(smRoleDTO.getDescription()); String loginUserId = WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId();; //"developer" smRoleDTO.setLastModifier(loginUserId); RoleInfo roleInfo = changeSmRoleDTOToRoleInfo(smRoleDTO1); boolean updateBoolean = platformClientUtil.getFrameworkService().updateRole(roleInfo, new UserEntityInfo(loginUserId, null)); return updateBoolean; } /** * 删除角色 * @param ids * @return * @throws PLException */ @Override public boolean deleteRole(String[] ids) throws PLException { VciBaseUtil.alertNotNull(ids,"要删除的角色主键"); //使用主键判断角色是否被用户引用 for (int i = 0; i < ids.length; i++) { String roleName = this.checkIsUsed(ids[i]); if(Func.isNotBlank(roleName)){ throw new PLException("500",new String[]{"当前选中要删除的角色中存在【"+roleName+"】被引用!"}); } } //具备连带删除的功能,如角色菜单授权表里面的授权信息 return platformClientUtil.getFrameworkService().deleteRole( ids, new UserEntityInfo(WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId(), null) ); } /** * 检查角色是否有在引用 * @param roleOid * @return */ private String checkIsUsed(String roleOid){ List smUserVOS = smUserQueryServiceI.listUserByRoleOid(roleOid, null); if (Func.isNotEmpty(smUserVOS)) { return this.getRoleNameByRoleOid(roleOid); } return null; } /** * 导入角色 * @param file * @return * @throws VciBaseException */ @Override public BaseResult importRole(File file) throws Exception { VciBaseUtil.alertNotNull(file,"excel文件"); if(!file.exists()){ throw new VciBaseException("导入的excel文件不存在,{0}",new String[]{file.getPath()}); } try{ //1、读取excel中的数据,组成对象 ReadExcelOption excelOption = new ReadExcelOption(); //读取excel转换为po对象 List poList = ExcelUtil.readDataObjectFromExcel(file, SmRolePO.class,excelOption,(value, po, fieldName)->{}); //去除都是空的情况 if(CollectionUtils.isEmpty(poList)){ return BaseResult.fail(ExcelLangCodeConstant.IMPORT_CONTENT_NULL,new String[]{}); } //数据库查询是否有已存在的角色名,方便后续做判重处理 List smRoleVOList = this.listRoleByRoleName(poList.stream().map(SmRolePO::getName).collect(Collectors.toSet()),null); List repeatRoleName = new ArrayList<>(); if(Func.isNotEmpty(smRoleVOList)){ repeatRoleName = smRoleVOList.stream().map(SmRoleVO::getName).collect(Collectors.toList()); } //当前excel中是否重复用的判重Map:(key:账号,value:行号) Map excelReapeat = new HashMap<>(); //判断必填属性是否为空,角色是否已存在,以及表格内是否重复 List finalRepeatroleName = repeatRoleName; poList.stream().forEach(smRolePO -> { //先对必填属性判空处理 if(Func.isBlank(smRolePO.getName())){ throw new VciBaseException("第【"+smRolePO.getRowIndex()+"】行,rolenameerror"); }else if(finalRepeatroleName.contains(smRolePO.getName())){//判断角色名是否与库中重复 throw new VciBaseException("第【"+smRolePO.getRowIndex()+"】行,角色名在系统中已经存在,请修改!"); }else if(excelReapeat.containsKey(smRolePO.getName())){//表格中判重 throw new VciBaseException("第【"+excelReapeat.get(smRolePO.getName())+"】行和第【"+smRolePO.getRowIndex()+"】行数据,角色名重复"); } excelReapeat.put(smRolePO.getName(),smRolePO.getRowIndex()); }); //保存逻辑 poList.stream().forEach(smRolePO->{ try { SmRoleDTO smRoleDTO = new SmRoleDTO(); BeanUtil.copy(smRolePO,smRoleDTO); SessionInfo loginUser = WebThreadLocalUtil.getCurrentUserSessionInfoInThread(); String userId = loginUser.getUserId(); //角色类型,这里设置了会在changeRoleObjectToRoleInfo中转换 if(UserTypeEnum.SUPPER_ADMIN.getValue().equals("0")){ smRoleDTO.setRoleClassify(RoleClassifyEnum.MANAGE.getValue()); }else { smRoleDTO.setRoleClassify(RoleClassifyEnum.BUSINESS.getValue()); } //生成存储的corba对象 Date date = new Date(); smRoleDTO.setCreateTime(date); smRoleDTO.setCreator(userId); smRoleDTO.setLastModifier(userId); RoleInfo roleInfo = changeSmRoleDTOToRoleInfo(smRoleDTO); UserEntityInfo userEntityInfo = new UserEntityInfo(userId, ""); //执行保存 platformClientUtil.getFrameworkService().saveRole(roleInfo, userEntityInfo); } catch (PLException e) { e.printStackTrace(); throw new VciBaseException("执行到第【"+smRolePO.getRowIndex()+"】行保存逻辑时出现错误,原因:" + VciBaseUtil.getExceptionMessage(e)); } }); }catch (Exception e){ if(logger.isErrorEnabled()){ logger.error("读取excel内容时或保存角色信息时出现了错误,具体原因:",e.getMessage()); } e.printStackTrace(); return BaseResult.fail(LangBaseUtil.getErrorMsg(e),new String[]{},e); } return BaseResult.success("角色导入成功!"); } /** * 角色DTO对象转corba对象 * @param role * @return */ public RoleInfo changeSmRoleDTOToRoleInfo(SmRoleDTO role) { RoleInfo roleInfo = new RoleInfo(); roleInfo.id = role.getOid() == null ? "" : role.getOid(); roleInfo.name = role.getName(); roleInfo.description = role.getDescription() == null ? "" : role.getDescription(); //管理员角色默认为1,普通角色默认为2,但是都需要设置小一个,因为后台判断当type<2时会拿type+1 if(RoleClassifyEnum.MANAGE.getValue().equals(role.getRoleClassify())){ roleInfo.type = 0; }else{ roleInfo.type = 1; } roleInfo.createTime = role.getCreateTime().getTime(); roleInfo.createUser = role.getCreator() == null ? "" : role.getCreator(); roleInfo.updateTime = System.currentTimeMillis(); roleInfo.updateUser = role.getLastModifier() == null ? "" : role.getLastModifier(); return roleInfo; } /** * 角色的转换,默认全部是bs控制的角色 * @param roleForPlatform1List 原平台的角色 * @return 角色的显示对象 */ private List platformRole2SmRoleVOs(List roleForPlatform1List){ List roleVOList = new ArrayList<>(); if(!CollectionUtils.isEmpty(roleForPlatform1List)){ roleForPlatform1List.stream().forEach(s -> { roleVOList.add(platformRole2SmRoleVO(s)); }); } return roleVOList; } /** * 角色的转换 * @param roleForPlatform1 原平台的角色 * @return 新的角色对象 */ private SmRoleVO platformRole2SmRoleVO(SmRoleForPlatform1 roleForPlatform1){ SmRoleVO roleVO = new SmRoleVO(); roleVO.setOid(roleForPlatform1.getPluid()); roleVO.setId(""); roleVO.setName(roleForPlatform1.getPlname()); if(roleForPlatform1.getPltype() == 1){ roleVO.setRoleClassify(RoleClassifyEnum.MANAGE.getValue()); }else{ roleVO.setRoleClassify(RoleClassifyEnum.BUSINESS.getValue()); } roleVO.setRoleClassifyText(RoleClassifyEnum.getTextByValue(roleVO.getRoleClassify())); roleVO.setRoleControlArea(RoleControlAreaEnum.BS.getValue()); roleVO.setRoleControlAreaText(RoleControlAreaEnum.BS.getText()); roleVO.setDescription(roleForPlatform1.getPldesc()); roleVO.setCreateTime(new Date(roleForPlatform1.getPlcreatetime())); roleVO.setCreator(roleForPlatform1.getPlcreateuser()); roleVO.setLastModifier(roleForPlatform1.getPlupdateuser()); roleVO.setLastModifyTime(new Date(roleForPlatform1.getPlupdatetime())); roleVO.setBtmname(FrameWorkBtmTypeConstant.SM_ROLE_BTM_TYPE); roleVO.setOwner(roleVO.getCreator()); roleVO.setTs(roleVO.getLastModifyTime()); return roleVO; } }