ludc
2024-07-09 5acc490fa6f77a9ed7b5976ee6a2e22b070df5bf
Source/plt-web/plt-web-parent/plt-web/src/main/java/com/vci/frameworkcore/compatibility/impl/SmRoleQueryServiceImpl.java
@@ -2,38 +2,51 @@
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.frameworkcore.compatibility.SmRoleQueryServiceI;
import com.vci.frameworkcore.constant.FrameWorkBtmTypeConstant;
import com.vci.frameworkcore.dto.SmRoleDTO;
import com.vci.frameworkcore.enumpck.RoleClassifyEnum;
import com.vci.frameworkcore.enumpck.RoleControlAreaEnum;
import com.vci.frameworkcore.model.SmRoleForPlatform1;
import com.vci.frameworkcore.model.dto.SmUserDTO;
import com.vci.frameworkcore.model.po.SmRolePO;
import com.vci.frameworkcore.model.po.SmUserPO;
import com.vci.frameworkcore.pagemodel.OrgDepartmentVO;
import com.vci.frameworkcore.pagemodel.SmRoleVO;
import com.vci.frameworkcore.pagemodel.SmUserVO;
import com.vci.frameworkcore.properties.ConfigReader;
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.enumpck.UserSecretEnum;
import com.vci.starter.web.exception.VciBaseException;
import com.vci.starter.web.pagemodel.DataGrid;
import com.vci.starter.web.pagemodel.PageHelper;
import com.vci.starter.web.pagemodel.Tree;
import com.vci.starter.web.pagemodel.TreeQueryObject;
import com.vci.starter.web.pagemodel.*;
import com.vci.starter.web.util.LangBaseUtil;
import com.vci.web.util.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.web.properties.WebProperties;
import com.vci.web.enumpck.UserTypeEnum;
import com.vci.web.service.WebBoServiceI;
import com.vci.web.util.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.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.io.File;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import static com.vci.frameworkcore.constant.FrameWorkBusLangCodeConstant.DATA_OID_NOT_EXIST;
/**
 * 角色的查询服务,兼容来平台,素有的接口都返回新的角色显示对象
@@ -55,22 +68,31 @@
   @Autowired
   private PlatformClientUtil platformClientUtil;
   private final Integer QUERY_IN_LIMIT = 1000;
   /**
    * 日志
    */
   private Logger logger = LoggerFactory.getLogger(getClass());
   /**
     * 根据角色主键获取角色的信息
     * @param roleOid 角色主键
     * @param conditionMap 角色主键
     * @return 角色的显示对象,如果角色不存在则返回null,不会抛出异常
     * @throws VciBaseException 参数为空或者数据库存在问题的时候会抛出异常
     */
   @Override
   public SmRoleVO getRoleByRoleOid(String roleOid) throws VciBaseException {
      if(StringUtils.isBlank(roleOid)){
   public SmRoleVO getRoleByConditionMap(Map<String,String> conditionMap) throws VciBaseException {
      if(Func.isEmpty(conditionMap)){
         return null;
      }
      SmRoleForPlatform1 role = boService.selectByOid(roleOid, SmRoleForPlatform1.class);
      if(role == null || StringUtils.isBlank(role.getPluid())){
         throw new VciBaseException(DATA_OID_NOT_EXIST);
      VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(conditionMap,SmRoleForPlatform1.class);
      List<SmRoleForPlatform1> smRoleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class);
      if(Func.isNotEmpty(smRoleForPlatform1s) && Func.isNotBlank(smRoleForPlatform1s.get(0).getPluid())){
         //角色名不能重复所以直接取第一个元素
         return platformRole2SmRoleVO(smRoleForPlatform1s.get(0));
      }
      return platformRole2SmRoleVO(role);
      return null;
   }
   /**
@@ -105,15 +127,12 @@
     * @throws VciBaseException 参数为空的时候会抛出异常
     */
   @Override
   public DataGrid<SmRoleVO> gridRoles(Map<String, String> queryMap,
                              PageHelper pageHelper) throws VciBaseException {
   public DataGrid<SmRoleVO> gridRoles(Map<String, String> queryMap, PageHelper pageHelper) throws VciBaseException {
      if(pageHelper == null){
         pageHelper = new PageHelper(-1);
      }
      pageHelper.addDefaultAsc("plname");
      VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(queryMap,SmRoleForPlatform1.class,pageHelper);
      //我们不能显示三员和管理员的角色
      queryWrapper.eq("pltype","2");
      List<SmRoleForPlatform1> roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class);
      DataGrid<SmRoleVO> dataGrid = new DataGrid<>();
      if(!CollectionUtils.isEmpty(roleForPlatform1s)){
@@ -131,10 +150,52 @@
   @Override
   public String getRoleNameByRoleOid(String roleOid) {
      WebUtil.alertNotNull(roleOid,"角色主键");
      return getRoleByRoleOid(roleOid).getName();
      Map<String, String> conditionMap = new HashMap<>();
      conditionMap.put("pluid",roleOid);
      return getRoleByConditionMap(conditionMap).getName();
   }
    /**
   /**
    * 根据角色名查询角色信息
    * @param roleNameList
    * @param queryMap
    * @return
    */
   @Override
   public List<SmRoleVO> listRoleByRoleName(Collection<String> roleNameList, Map<String, String> queryMap) {
      if(Func.isEmpty(roleNameList)){
         return new ArrayList<>();
      }
      if(queryMap == null){
         queryMap = new HashMap<>();
      }
      List<SmRoleForPlatform1> roles = new ArrayList<>();
      if(roleNameList.size() > QUERY_IN_LIMIT){
         Map<String, String> finalQueryMap = queryMap;
         WebUtil.switchCollectionForOracleIn(roleNameList).stream().forEach(roleNames->{
            Map<String,String> 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<SmRoleForPlatform1> 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<SmRoleForPlatform1> roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class);
      if(!CollectionUtils.isEmpty(roleForPlatform1s)){
         roles.addAll(roleForPlatform1s);
      }
      return platformRole2SmRoleVOs(roles);
   }
   /**
     * 根据用户主键获取关联的角色
     * @param userOid 用户主键
     * @param queryMap 查询条件,如果需要使用用户的属性来查询可以使用pkUser.xxxx
@@ -236,20 +297,8 @@
   @Override
   public List<SmRoleVO> listRoleUnInUserOid(String userOid,
         Map<String, String> queryMap) {
      return listRoleByUserOid(userOid,queryMap,true);
   }
   /**
     * 获取未关联某个用户的角色
     * @param userOid 用户主键
     * @param queryMap 查询条件,如果需要使用用户的属性来查询可以使用pkUser.xxxx
     * @param pageHelper 分页和排序对象,老平台不支持使用角色编号来排序
     * @return 角色的显示对象
     */
   @Override
   public DataGrid<SmRoleVO> gridRoleUninUserOid(String userOid,
         Map<String, String> queryMap, PageHelper pageHelper) {
      return gridRoleByUserOid(userOid,queryMap,pageHelper,true);
      return listRoleByUserOid(userOid,queryMap,true);
   }
     /**
@@ -469,7 +518,7 @@
   }
    /**
     * 参照角色的列表,三员管理的三个角色不会显示
     * 参照角色的列表,超管查全部,其他的都是查普通的(不包含三员)
     * @param queryMap 查询条件
     * @param pageHelper 分页和排序的信息,在兼容老平台的时候会自动兼容,如果属性不存在会自动忽略
     * @return 角色的显示对象列表
@@ -479,6 +528,11 @@
   public DataGrid<SmRoleVO> refGridRoles(Map<String, String> 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);
   }
@@ -513,35 +567,224 @@
   }
   /**
    * 该方法只支持一次只允许单个用户分配多个角色,一对多
    * @param userOid
    * 成员管理分配角色调用,保存用户角色关联关系
    * @param userOids
    * @param roleIds
    * @return
    */
   @Override
   public boolean saveRights(String userOid, String[] roleIds) throws PLException {
      VciBaseUtil.alertNotNull(userOid,"用户主键");
   public boolean saveRights(String[] userOids, String[] roleIds) throws PLException {
      VciBaseUtil.alertNotNull(userOids,"用户主键");
      //roleIds允许为空,为空时清空当前用户的权限
      //先查重
      if(Func.isNotEmpty(roleIds)){
         List<String> repeatRoleOidList = new ArrayList<>();
         //循环进行查重,避免in大于1000
         WebUtil.switchCollectionForOracleIn(Arrays.asList(roleIds)).stream().forEach(deptoids->{
            String sql = "select pluseruid,plroleuid from pluserrole where pluseruid = '" + userOid
                  + "' and " + "plroleuid in ("+WebUtil.toInSql(deptoids.toArray(new String[0]))+")";
            List<BusinessObject> cbos = boService.queryBySql(sql, null);
            if(!CollectionUtils.isEmpty(cbos)){
               cbos.stream().forEach(cbo->{
                  repeatRoleOidList.add(ObjectTool.getNewBOAttributeValue(cbo,"plroleuid"));
               });
      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<String, String> 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<String, String> conditionMap = new HashMap<>();
      conditionMap.put("plname",smRoleDTO.getName());
      SmRoleVO dbSmRoleVO = getRoleByConditionMap(conditionMap);
      //根据主键没查询到了用户
      if(Func.isEmpty(dbSmRoleVO) || Func.isBlank(dbSmRoleVO.getOid())){
         throw new PLException("500", new String[] { "当前修改的角色不存在!"});
      }
      //角色名相等,但是主键不相等,说明修改为的角色名已存在,不能重复
      if(dbSmRoleVO.getName().equals(smRoleDTO.getName()) && !dbSmRoleVO.getOid().equals(smRoleDTO.getOid())){
         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,"要删除的角色主键");
      //具备连带删除的功能,如角色菜单授权表里面的授权信息
      return platformClientUtil.getFrameworkService().deleteRole(
            ids,
            new UserEntityInfo(WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId(), null)
      );
   }
   /**
    * 导入角色
    * @param file
    * @return
    * @throws VciBaseException
    */
   @Override
   public BaseResult importRole(File file) throws VciBaseException {
      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<SmRolePO> poList = ExcelUtil.readDataObjectFromExcel(file, SmRolePO.class,excelOption,(value, po, fieldName)->{});
         //去除都是空的情况
         if(CollectionUtils.isEmpty(poList)){
            return BaseResult.fail(ExcelLangCodeConstant.IMPORT_CONTENT_NULL,new String[]{});
         }
         //数据库查询是否有已存在的角色名,方便后续做判重处理
         List<SmRoleVO> smRoleVOList = this.listRoleByRoleName(poList.stream().map(SmRolePO::getName).collect(Collectors.toSet()),null);
         List<String> repeatRoleName = new ArrayList<>();
         if(Func.isNotEmpty(smRoleVOList)){
            repeatRoleName = smRoleVOList.stream().map(SmRoleVO::getName).collect(Collectors.toList());
         }
         //当前excel中是否重复用的判重Map:(key:账号,value:行号)
         Map<String, String> excelReapeat = new HashMap<>();
         //判断必填属性是否为空,角色是否已存在,以及表格内是否重复
         List<String> 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 = "developer";//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));
            }
         });
         //移除重复的
         List<String> tempList = new ArrayList<>(Arrays.asList(roleIds));
         tempList.removeAll(repeatRoleOidList);
         roleIds = tempList.toArray(new String[tempList.size()]);
      }catch (Exception e){
         if(logger.isErrorEnabled()){
            logger.error("读取excel内容时或保存角色信息时出现了错误,具体原因:",e.getMessage());
         }
         e.printStackTrace();
         return BaseResult.fail(LangBaseUtil.getErrorMsg(e),new String[]{},e);
      }
      return platformClientUtil.getFrameworkService().saveRights(roleIds,new String[]{userOid},new UserEntityInfo());
      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;
   }
   /**
@@ -553,7 +796,7 @@
      List<SmRoleVO> roleVOList = new ArrayList<>();
      if(!CollectionUtils.isEmpty(roleForPlatform1List)){
         roleForPlatform1List.stream().forEach(s -> {
            roleVOList.add( platformRole2SmRoleVO(s));
            roleVOList.add(platformRole2SmRoleVO(s));
         });
      }
      return roleVOList;
@@ -576,11 +819,12 @@
      }
      roleVO.setRoleClassifyText(RoleClassifyEnum.getTextByValue(roleVO.getRoleClassify()));
      roleVO.setRoleControlArea(RoleControlAreaEnum.BS.getValue());
      roleVO.setRoleClassifyText(RoleControlAreaEnum.BS.getText());
      roleVO.setCreateTime(roleForPlatform1.getPlcreatetime());
      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(roleForPlatform1.getPlupdatetime());
      roleVO.setLastModifyTime(new Date(roleForPlatform1.getPlupdatetime()));
      roleVO.setBtmname(FrameWorkBtmTypeConstant.SM_ROLE_BTM_TYPE);
      roleVO.setOwner(roleVO.getCreator());
      roleVO.setTs(roleVO.getLastModifyTime());