Source/plt-web/plt-web-parent/plt-web/src/main/java/com/vci/frameworkcore/compatibility/impl/OrgDeptQueryServiceImpl.java
@@ -1,11 +1,14 @@
package com.vci.frameworkcore.compatibility.impl;
import com.vci.corba.common.PLException;
import com.vci.corba.omd.data.BusinessObject;
import com.vci.frameworkcore.compatibility.OrgDeptQueryServiceI;
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;
@@ -18,13 +21,17 @@
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;
/**
@@ -40,6 +47,12 @@
    */
   @Autowired
   private WebBoServiceI boService;
   /**
    * 平台调用客户端
    */
   @Autowired
   private PlatformClientUtil platformClientUtil;
   /**
    * 对象操作工具类
@@ -101,6 +114,7 @@
      }
      return departmentVO;
   }
   /**
     * 批量获取部门的信息 (根据部门主键)
     * @param deptOidCollections 部门主键的集合,可以超过1000个
@@ -125,6 +139,7 @@
      });
      return    deptDO2VOs(depts);
   }
   /**
     * 获取部门的列表,默认会以部门名称升序排列,部门的编辑页面列表不要使用这个接口
     * @param queryMap 查询条件
@@ -148,6 +163,7 @@
      }
      return dataGrid;
   }
    /**
     * 根据部门主键获取部门的姓名
     * @param deptOid 部门主键
@@ -173,7 +189,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->{
@@ -185,7 +201,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 {
@@ -194,25 +210,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 用户主键
@@ -285,19 +303,22 @@
         if(!CollectionUtils.isEmpty(deptVOs)){
            deptVOList.addAll(deptVOs);
            String sql = "select pluseruid,pldeptuid from pluserdept where pluseruid in (" + WebUtil.toInSql(userOids.toArray(new String[0])) + ")";
            List<com.vci.client.bof.ClientBusinessObject> cbos = boService.queryBySql(sql, null);
            List<BusinessObject> cbos = boService.queryBySql(sql, null);
            if(!CollectionUtils.isEmpty(cbos)){
               cbos.stream().forEach(cbo->{
                  String userOid = cbo.getAttributeValue("pluseruid");
                  String userOid = ObjectTool.getNewBOAttributeValue(cbo,"pluseruid");
                  List<String> deptOids = userDeptOidMap.getOrDefault(userOid,new ArrayList<>());
                  deptOids.add(cbo.getAttributeValue("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<>();
@@ -312,6 +333,7 @@
      }
      return new HashMap<>();
   }
    /**
     * 获取某个部门的直属下级部门
     * @param pkFatherDepartmment 部门的主键
@@ -330,6 +352,7 @@
      List<OrgDeptForPlatform1> depts = boService.selectByQueryWrapper(queryWrapperForDO, OrgDeptForPlatform1.class);
      return deptDO2VOs(depts);
   }
    /**
     * 获取某个部门的所有层级的下级部门
     * @param pkFatherDepartmment 部门的主键
@@ -343,6 +366,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 树形查询的条件
@@ -363,12 +387,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 树形查询的条件
@@ -397,4 +423,41 @@
      return dataGrid;
   }
   /**
    * 保存部门角色关联信息,带查重功能
    * @param userOIds 用户id
    * @param deptId 部门id
    * @return
    */
   @Override
   @Transactional(rollbackFor = Exception.class)
   public boolean saveUsersDept(String[] userOIds, String deptId) throws PLException {
      if(Func.isEmpty(userOIds) || Func.isBlank(deptId)){
         return false;
      }
      //先进性查重处理
      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;
   }
}