ludc
2024-07-04 8c4b79d994c5a95dd33d4b0313e2f2d63985e074
Source/plt-web/plt-web-parent/plt-web/src/main/java/com/vci/frameworkcore/compatibility/impl/OrgDeptQueryServiceImpl.java
@@ -1,32 +1,35 @@
package com.vci.frameworkcore.compatibility.impl;
import com.vci.corba.common.PLException;
import com.vci.corba.common.data.UserEntityInfo;
import com.vci.corba.framework.data.DeptInfo;
import com.vci.corba.omd.data.BusinessObject;
import com.vci.frameworkcore.compatibility.OrgDeptQueryServiceI;
import com.vci.frameworkcore.model.dto.OrgDepartmentDTO;
import com.vci.frameworkcore.lcstatuspck.FrameworkDataLCStatus;
import com.vci.frameworkcore.model.OrgDeptForPlatform1;
import com.vci.frameworkcore.model.SmRoleForPlatform1;
import com.vci.frameworkcore.pagemodel.OrgDepartmentVO;
import com.vci.frameworkcore.pagemodel.SmRoleVO;
import com.vci.omd.utils.ObjectTool;
import com.vci.starter.revision.bo.TreeWrapperOptions;
import com.vci.starter.revision.service.RevisionModelUtil;
import com.vci.starter.web.constant.QueryOptionConstant;
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.BeanUtil;
import com.vci.starter.web.util.VciBaseUtil;
import com.vci.starter.web.wrapper.VciQueryWrapperForDO;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
@@ -44,6 +47,12 @@
   private WebBoServiceI boService;
   /**
    * 平台调用客户端
    */
   @Autowired
   private PlatformClientUtil platformClientUtil;
   /**
    * 对象操作工具类
    */
   @Autowired
@@ -56,13 +65,38 @@
     * @throws VciBaseException 参数为空或者数据库存在问题的时候会抛出异常
     */
   @Override
   public OrgDepartmentVO getDeptByDeptOid(String deptOid)
         throws VciBaseException {
   public OrgDepartmentVO getDeptByDeptOid(String deptOid) throws VciBaseException {
      if(StringUtils.isBlank(deptOid)){
         return null;
      }
      OrgDeptForPlatform1 deptForPlatform1 = boService.selectByOid(deptOid, OrgDeptForPlatform1.class);
      return deptDO2VO(deptForPlatform1);
   }
   /**
    * 根据父部门主键获取和部门名称部门的信息
    * @param parentDeptOid 父部门主键
    * @param conditionMap 部门名称
    * @return 部门的显示对象,如果部门不存在则返回null,不会抛出异常
    * @throws VciBaseException 参数为空或者数据库存在问题的时候会抛出异常
    */
   @Override
   public OrgDepartmentVO getDeptByDeptPOidAndCondition(String parentDeptOid,Map<String,String> conditionMap) throws VciBaseException {
      if(Func.isEmpty(conditionMap)){
         conditionMap = new HashMap();
      }
      VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(new HashMap<>(),OrgDeptForPlatform1.class);
      //父主键为空查顶层
      if(Func.isBlank(parentDeptOid)){
         queryWrapper.isNull("plparentuid");
      }else{
         queryWrapper.eq("plparentuid",parentDeptOid);
      }
      List<OrgDeptForPlatform1> roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, OrgDeptForPlatform1.class);
      if(Func.isEmpty(roleForPlatform1s)){
         return null;
      }
      return deptDO2VO(roleForPlatform1s.get(0));
   }
   /**
@@ -103,6 +137,7 @@
      }
      return departmentVO;
   }
   /**
     * 批量获取部门的信息 (根据部门主键)
     * @param deptOidCollections 部门主键的集合,可以超过1000个
@@ -125,8 +160,9 @@
            depts.addAll(roleForPlatform1s);
         }
      });
      return    deptDO2VOs(depts);
      return deptDO2VOs(depts);
   }
   /**
     * 获取部门的列表,默认会以部门名称升序排列,部门的编辑页面列表不要使用这个接口
     * @param queryMap 查询条件
@@ -140,6 +176,7 @@
      if(pageHelper == null){
         pageHelper = new PageHelper(-1);
      }
      //根据部门编号排序
      pageHelper.addDefaultAsc("plnum");
      VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(queryMap,OrgDeptForPlatform1.class,pageHelper);
      List<OrgDeptForPlatform1> deptForPlatform1s = boService.selectByQueryWrapper(queryWrapper, OrgDeptForPlatform1.class);
@@ -150,6 +187,7 @@
      }
      return dataGrid;
   }
    /**
     * 根据部门主键获取部门的姓名
     * @param deptOid 部门主键
@@ -175,7 +213,7 @@
      if(queryMap == null){
         queryMap = new HashMap<>();
      }
      List<OrgDeptForPlatform1> roles = new ArrayList<>();
      List<OrgDeptForPlatform1> deptForPlatform1s = new ArrayList<>();
      if(userOid.contains(",")){
         Map<String, String> finalQueryMap = queryMap;
         WebUtil.switchCollectionForOracleIn(WebUtil.str2List(userOid)).stream().forEach(userOids->{
@@ -187,7 +225,7 @@
            VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(conditionMap,OrgDeptForPlatform1.class);
            List<OrgDeptForPlatform1> roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, OrgDeptForPlatform1.class);
            if(!CollectionUtils.isEmpty(roleForPlatform1s)){
               roles.addAll(roleForPlatform1s);
               deptForPlatform1s.addAll(roleForPlatform1s);
            }
         });
      }else {
@@ -196,25 +234,27 @@
      VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(queryMap,OrgDeptForPlatform1.class);
      List<OrgDeptForPlatform1> roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, OrgDeptForPlatform1.class);
      if(!CollectionUtils.isEmpty(roleForPlatform1s)){
         roles.addAll(roleForPlatform1s);
         deptForPlatform1s.addAll(roleForPlatform1s);
      }
      return deptDO2VOs(roles);
      return deptDO2VOs(deptForPlatform1s);
   }
    /**
   /**
     * 根据用户主键获取关联的部门
     * @param userOid 用户主键
     * @param queryMap 查询条件,如果需要使用用户的属性来查询可以使用pkUser.xxxx
     * @return 部门的显示对象
     */
   @Override
   public OrgDepartmentVO listDeptByUserOid(String userOid,
   public List<OrgDepartmentVO> listDeptByUserOid(String userOid,
         Map<String, String> queryMap) {
      List<OrgDepartmentVO> departmentVOS = listDeptByUserOid(userOid, queryMap, false);
      if(CollectionUtils.isEmpty(departmentVOS)){
         return null;
         return new ArrayList<>();
      }
      return departmentVOS.get(0);
      return departmentVOS;
   }
   /**
     * 获取未关联某个用户的部门
     * @param userOid 用户主键
@@ -290,16 +330,19 @@
            List<BusinessObject> cbos = boService.queryBySql(sql, null);
            if(!CollectionUtils.isEmpty(cbos)){
               cbos.stream().forEach(cbo->{
                  String userOid = ObjectTool.getBOAttributeValue(cbo,"pluseruid");
                  String userOid = ObjectTool.getNewBOAttributeValue(cbo,"pluseruid");
                  List<String> deptOids = userDeptOidMap.getOrDefault(userOid,new ArrayList<>());
                  deptOids.add(ObjectTool.getBOAttributeValue(cbo,"pldeptuid"));
                  deptOids.add(ObjectTool.getNewBOAttributeValue(cbo,"pldeptuid"));
                  userDeptOidMap.put(userOid,deptOids);
               });
            }
         }
      });
      if(!CollectionUtils.isEmpty(deptVOList)){
         Map<String, OrgDepartmentVO> deptVOMap = deptVOList.stream().collect(Collectors.toMap(s -> s.getOid(), t -> t));
         //这儿应该对deptVOList做一次去重处理,因为肯定会有重复的部门被查出来
         Map<String, OrgDepartmentVO> deptVOMap = deptVOList.stream()
               .collect(Collectors.toMap(OrgDepartmentVO::getOid, Function.identity(), (existing, replacement) -> existing));
         //Map<String, OrgDepartmentVO> deptVOMap = deptVOList.stream().distinct().collect(Collectors.toMap(s -> s.getOid(), t -> t));
         Map<String, List<OrgDepartmentVO>> userDeptVOMap = new HashMap<>();
         userDeptOidMap.forEach((userOid,deptOids)->{
            List<OrgDepartmentVO> deptVOS = new ArrayList<>();
@@ -314,6 +357,7 @@
      }
      return new HashMap<>();
   }
    /**
     * 获取某个部门的直属下级部门
     * @param pkFatherDepartmment 部门的主键
@@ -332,6 +376,7 @@
      List<OrgDeptForPlatform1> depts = boService.selectByQueryWrapper(queryWrapperForDO, OrgDeptForPlatform1.class);
      return deptDO2VOs(depts);
   }
    /**
     * 获取某个部门的所有层级的下级部门
     * @param pkFatherDepartmment 部门的主键
@@ -345,6 +390,7 @@
      queryWrapperForDO.in("pluid","select pluid from pldept start with " + (StringUtils.isBlank(pkFatherDepartmment)?" (plparentuid is null or plparentuid = '') ":"plparentuid = '" + pkFatherDepartmment.trim() + "'") +  "connect by PRIOR pluid=plparentuid");
      return deptDO2VOs(boService.selectByQueryWrapper(queryWrapperForDO, OrgDeptForPlatform1.class));
   }
   /**
     * 参照树形数据的部门信息
     * @param treeQueryObject 树形查询的条件
@@ -365,12 +411,14 @@
         VciQueryWrapperForDO queryWrapperForDO = new VciQueryWrapperForDO(conditionMap,OrgDeptForPlatform1.class);
         orgDepartmentVOList = deptDO2VOs(boService.selectByQueryWrapper(queryWrapperForDO,OrgDeptForPlatform1.class));
      }
      TreeWrapperOptions treeWrapperOptions = new TreeWrapperOptions("pkFatherDepartment");
      TreeWrapperOptions treeWrapperOptions = new TreeWrapperOptions();
      BeanUtil.convert(treeQueryObject,treeWrapperOptions);
      treeWrapperOptions.setParentFieldName("pkFatherDepartment");
      return revisionModelUtil.doList2Trees(orgDepartmentVOList,treeWrapperOptions,dept->{
         return dept.getId() + " " + dept.getName() + (FrameworkDataLCStatus.DISABLED.getValue().equals(dept.getLcStatus())?"【停用】":"");
      });
   }
     /**
     * 参照树形表格的部门信息,上级部门的是表格中的树形列
     * @param treeQueryObject 树形查询的条件
@@ -399,4 +447,183 @@
      return dataGrid;
   }
   /**
    * 保存部门角色关联信息,带查重功能
    * @param userOIds 用户id
    * @param deptId 部门id
    * @return
    */
   @Override
   @Transactional(rollbackFor = Exception.class)
   public boolean saveUsersDept(String[] userOIds, String deptId) throws PLException {
      //先进性查重处理
      List<String> repeatUserOidList = new ArrayList<>();
      //循环进行查重,避免in大于1000
      WebUtil.switchCollectionForOracleIn(Arrays.asList(userOIds)).stream().forEach(userOId->{
         String sql = "select pluseruid,pldeptuid from pluserdept where pldeptuid = '" + deptId
               + "' and " + "pluseruid in ("+WebUtil.toInSql(userOId.toArray(new String[0]))+")";
         List<BusinessObject> cbos = boService.queryBySql(sql, null);
         if(!CollectionUtils.isEmpty(cbos)){
            cbos.stream().forEach(cbo->{
               repeatUserOidList.add(ObjectTool.getNewBOAttributeValue(cbo,"pluseruid"));
            });
         }
      });
      //从即将要执行保存的用户oid中移除当前用户已经存在关联关系的oid
      //移除重复的
      List<String> tempList = new ArrayList<>(Arrays.asList(userOIds));
      tempList.removeAll(repeatUserOidList);
      userOIds = tempList.toArray(new String[tempList.size()]);
      if(Func.isNotEmpty(userOIds)){
         platformClientUtil.getFrameworkService().saveUserDept(userOIds, deptId,null);
      }
      return true;
   }
   /**
    * 增加部门信息
    * @param orgDepartmentDTO
    * @return
    */
   @Override
   public boolean addDept(OrgDepartmentDTO orgDepartmentDTO) throws PLException {
      //判空
      VciBaseUtil.alertNotNull(
            orgDepartmentDTO,"添加的部门对象",
            orgDepartmentDTO.getName(),"部门名");
      //部门名判重
      Map<String, String> conditionMap = new HashMap<>();
      conditionMap.put("plname",orgDepartmentDTO.getName());
      conditionMap.put("plnum",QueryOptionConstant.OR + orgDepartmentDTO.getId());
      OrgDepartmentVO departmentVO = getDeptByDeptPOidAndCondition(orgDepartmentDTO.getPkFatherDepartment(), conditionMap);
      if(Func.isNotEmpty(departmentVO)){
         throw new VciBaseException("同一父节点下该部门名称或编号已经存在,请修改!");
      }
      //SessionInfo loginUser = WebThreadLocalUtil.getCurrentUserSessionInfoInThread();
      String userId = "developer";//loginUser.getUserId();
      //生成存储的corba对象
      orgDepartmentDTO.setCreateTime(new Date());
      orgDepartmentDTO.setCreator(userId);
      orgDepartmentDTO.setLastModifier(userId);
      orgDepartmentDTO.setStatus((short) 0);
      DeptInfo deptInfo = changeOrgDeptDTOToDeptInfo(orgDepartmentDTO);
      UserEntityInfo userEntityInfo = new UserEntityInfo(userId, "");
      String oid = platformClientUtil.getFrameworkService().saveDepartment(deptInfo, userEntityInfo);
      if (Func.isEmpty(oid)) {
         return false;
      }
      return true;
   }
   /**
    * 修改部门信息
    * @param orgDepartmentDTO
    * @return
    */
   @Override
   public boolean updateDept(OrgDepartmentDTO orgDepartmentDTO) throws PLException {
      //判空
      VciBaseUtil.alertNotNull(
            orgDepartmentDTO,"修改的部门对象",
            orgDepartmentDTO.getOid(),"用户主键",
            orgDepartmentDTO.getId(),"用户名"
      );
      //按oid查询数据库中已存在的
      OrgDepartmentVO departmentVO = getDeptByDeptOid(orgDepartmentDTO.getOid());
      //根据主键没查询到
      OrgDepartmentDTO orgDepartmentDTO1 = new OrgDepartmentDTO();
      //默认属性给赋值上
      //BeanUtil.convert(departmentVO,orgDepartmentDTO1);
      Func.copy(departmentVO,orgDepartmentDTO);
      //orgDepartmentDTO1.setName(orgDepartmentDTO.getName());
      //orgDepartmentDTO1.setCode(orgDepartmentDTO.getCode());
      //orgDepartmentDTO1.setDescription(orgDepartmentDTO.getDescription());
      String loginUserId = "developer";//WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId();
      orgDepartmentDTO1.setLastModifier(loginUserId);
      DeptInfo deptInfo = changeOrgDeptDTOToDeptInfo(orgDepartmentDTO1);
      boolean updateBoolean = platformClientUtil.getFrameworkService().updateDepartment(deptInfo, new UserEntityInfo(loginUserId, null));
      return updateBoolean;
   }
   /**
    * Dto对象转corb对象
    * @param orgDepartmentDTO
    * @return
    */
   public DeptInfo changeOrgDeptDTOToDeptInfo(OrgDepartmentDTO orgDepartmentDTO) {
      DeptInfo departmentInfo = new DeptInfo();
      departmentInfo.id = orgDepartmentDTO.getOid() == null ? "" : orgDepartmentDTO.getOid();
      departmentInfo.name = orgDepartmentDTO.getName() == null ? "" : orgDepartmentDTO.getName();
      departmentInfo.num = orgDepartmentDTO.getId() == null ? "" : orgDepartmentDTO.getId();
      departmentInfo.code = orgDepartmentDTO.getCode() == null ? "" : orgDepartmentDTO.getCode();
      departmentInfo.specialties = orgDepartmentDTO.getSpecialties() == null ? "" : orgDepartmentDTO.getSpecialties();
      departmentInfo.status = orgDepartmentDTO.getStatus() == 0 ? 0 : orgDepartmentDTO.getStatus();
      departmentInfo.description = orgDepartmentDTO.getDescription() == null ? "" : orgDepartmentDTO.getDescription();
      departmentInfo.parentId = orgDepartmentDTO.getPkFatherDepartment() == null ? "" : orgDepartmentDTO.getPkFatherDepartment();
      departmentInfo.createUser = orgDepartmentDTO.getCreator() == null ? "" : orgDepartmentDTO.getCreator();
      departmentInfo.createTime = orgDepartmentDTO.getCreateTime().getTime();
      departmentInfo.updateTime = System.currentTimeMillis();
      departmentInfo.updateUser = orgDepartmentDTO.getLastModifier() == null ? "" : orgDepartmentDTO.getLastModifier();
      //departmentInfo.grantor = orgDepartmentDTO.getGrantor() == null ? "" : orgDepartmentDTO.getGrantor();
      return departmentInfo;
   }
   /**
    * 删除部门
    * @param ids
    * @return
    */
   @Override
   public boolean deleteDept(String[] ids) throws PLException {
      VciBaseUtil.alertNotNull(ids,"要删除的部门主键");
      //具备连带删除的功能,如角色菜单授权表里面的授权信息
      return platformClientUtil.getFrameworkService().deleteDepartment(
            ids,
            new UserEntityInfo("developer"/*WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId()*/, null)
      );
   }
   /**
    * 获取所有部门的信息
    * @return key:部门由名称组成的路径(/间隔),value对应最小层级的部门信息
    */
   @Override
   public Map<String, OrgDepartmentVO> getDeptAllTreeMap() {
      List<OrgDepartmentVO> orgDepartmentVOList = listAllLevelChildrenDeptByParentOid(null, null);
      Map<String, OrgDepartmentVO> stringOrgDepartmentVOMap = convertToMap(orgDepartmentVOList);
      return stringOrgDepartmentVOMap;
   }
   public Map<String, OrgDepartmentVO> convertToMap(List<OrgDepartmentVO> orgDepartmentVOList) {
      Map<String, OrgDepartmentVO> map = new HashMap<>();
      for (OrgDepartmentVO orgDepartmentVO : orgDepartmentVOList) {
         String key = buildKey(orgDepartmentVO, orgDepartmentVOList);
         map.put(key, orgDepartmentVO);
      }
      return map;
   }
   private String buildKey(OrgDepartmentVO orgDepartmentVO, List<OrgDepartmentVO> orgDepartmentVOList) {
      StringBuilder keyBuilder = new StringBuilder();
      OrgDepartmentVO current = orgDepartmentVO;
      while (current != null) {
         keyBuilder.insert(0, current.getName());
         keyBuilder.insert(0, "/");
         current = getParentDepartment(current.getPkFatherDepartment(), orgDepartmentVOList);
      }
      keyBuilder.deleteCharAt(0);
      return keyBuilder.toString();
   }
   private OrgDepartmentVO getParentDepartment(String pkFatherDepartment, List<OrgDepartmentVO> orgDepartmentVOList) {
      for (OrgDepartmentVO orgDepartmentVO : orgDepartmentVOList) {
         if (pkFatherDepartment != null && pkFatherDepartment.equals(orgDepartmentVO.getOid())) {
            return orgDepartmentVO;
         }
      }
      return null;
   }
}