|
|
|
package com.engine.organization.service.impl;
|
|
|
|
|
|
|
|
import com.api.hrm.bean.TreeNode;
|
|
|
|
import com.engine.core.impl.Service;
|
|
|
|
import com.engine.organization.entity.QueryParam;
|
|
|
|
import com.engine.organization.entity.comp.bo.CompBO;
|
|
|
|
import com.engine.organization.entity.comp.po.CompPO;
|
|
|
|
import com.engine.organization.entity.department.bo.DepartmentBO;
|
|
|
|
import com.engine.organization.entity.department.po.DepartmentPO;
|
|
|
|
import com.engine.organization.entity.searchtree.SearchTree;
|
|
|
|
import com.engine.organization.entity.searchtree.SearchTreeParams;
|
|
|
|
import com.engine.organization.mapper.comp.CompMapper;
|
|
|
|
import com.engine.organization.mapper.department.DepartmentMapper;
|
|
|
|
import com.engine.organization.service.JobService;
|
|
|
|
import com.engine.organization.util.db.MapperProxyFactory;
|
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
|
import weaver.conn.RecordSet;
|
|
|
|
import weaver.general.StringUtil;
|
|
|
|
import weaver.general.Util;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description: TODO
|
|
|
|
* @author:dxfeng
|
|
|
|
* @createTime: 2022/05/26
|
|
|
|
* @version: 1.0
|
|
|
|
*/
|
|
|
|
public class JobServiceImpl extends Service implements JobService {
|
|
|
|
|
|
|
|
private static final String TYPE_GROUP = "0";
|
|
|
|
private static final String TYPE_COMP = "1";
|
|
|
|
private static final String TYPE_DEPT = "2";
|
|
|
|
|
|
|
|
private CompMapper getCompMapper() {
|
|
|
|
return MapperProxyFactory.getProxy(CompMapper.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
private DepartmentMapper getDepartmentMapper() {
|
|
|
|
return MapperProxyFactory.getProxy(DepartmentMapper.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Map<String, Object> getSearchTree(SearchTreeParams params) {
|
|
|
|
Map<String, Object> dataMap = new HashMap<>();
|
|
|
|
SearchTree topGroup = getTopGroup();
|
|
|
|
// 集团
|
|
|
|
List<SearchTree> companyList = new ArrayList<>();
|
|
|
|
companyList.add(topGroup);
|
|
|
|
String keyword = params.getKeyword();
|
|
|
|
String type = Util.null2String(params.getType());
|
|
|
|
String id = params.getId();
|
|
|
|
|
|
|
|
List<TreeNode> treeDatas = getFilterCompany(id, type, keyword);
|
|
|
|
// 未点击,初始化树结构
|
|
|
|
if (StringUtil.isEmpty(type)) {
|
|
|
|
dataMap.put("companys", companyList);
|
|
|
|
SearchTree rootCompany = getTopGroup();
|
|
|
|
rootCompany.setSubs(treeDatas);
|
|
|
|
rootCompany.setIsParent(CollectionUtils.isNotEmpty(rootCompany.getSubs()));
|
|
|
|
Map<String, Object> rootCompanyMap = new HashMap<>();
|
|
|
|
rootCompanyMap.put("rootCompany", rootCompany);
|
|
|
|
dataMap.put("datas", rootCompanyMap);
|
|
|
|
} else {
|
|
|
|
// 根据ID查询
|
|
|
|
dataMap.put("datas", treeDatas);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dataMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 添加查询元素的父级元素
|
|
|
|
*
|
|
|
|
* @param departmentPO
|
|
|
|
* @param builderDeparts
|
|
|
|
*/
|
|
|
|
private void buildParentDepts(DepartmentPO departmentPO, Set<DepartmentPO> builderDeparts) {
|
|
|
|
builderDeparts.add(departmentPO);
|
|
|
|
if (isTop(departmentPO.getParentDept())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DepartmentPO parentDept = getDepartmentMapper().getDeptById(departmentPO.getParentDept());
|
|
|
|
buildParentDepts(parentDept, builderDeparts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 添加查询元素的父级元素
|
|
|
|
*
|
|
|
|
* @param compPO
|
|
|
|
* @param builderComps
|
|
|
|
*/
|
|
|
|
private void buildParentComps(CompPO compPO, Set<CompPO> builderComps) {
|
|
|
|
builderComps.add(compPO);
|
|
|
|
if (isTop(compPO.getParentCompany())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CompPO parentComp = getCompMapper().listById(compPO.getParentCompany());
|
|
|
|
buildParentComps(parentComp, builderComps);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取集团
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
private SearchTree getTopGroup() {
|
|
|
|
RecordSet rs = new RecordSet();
|
|
|
|
String sql = "select * from HrmCompany ";
|
|
|
|
rs.executeQuery(sql);
|
|
|
|
SearchTree groupTree = new SearchTree();
|
|
|
|
groupTree.setCanClick(false);
|
|
|
|
groupTree.setCanceled(false);
|
|
|
|
groupTree.setCompanyid("1");
|
|
|
|
groupTree.setIcon("icon-coms-LargeArea");
|
|
|
|
groupTree.setId("0");
|
|
|
|
groupTree.setIsVirtual("0");
|
|
|
|
while (rs.next()) {
|
|
|
|
groupTree.setName(rs.getString("COMPANYNAME"));
|
|
|
|
}
|
|
|
|
groupTree.setSelected(false);
|
|
|
|
groupTree.setType(TYPE_GROUP);
|
|
|
|
return groupTree;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 根据keyword查询数据
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
* @param type
|
|
|
|
* @param keyword
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
private List<TreeNode> getFilterCompany(String id, String type, String keyword) {
|
|
|
|
List<TreeNode> compSearchTree = new ArrayList<>();
|
|
|
|
// 通过分部、公司 组装数据
|
|
|
|
if (StringUtil.isEmpty(id) || TYPE_COMP.equals(type)) {
|
|
|
|
Long parentCompId = StringUtil.isEmpty(id) ? null : Long.parseLong(id);
|
|
|
|
DepartmentPO departmentBuild = DepartmentPO.builder().deptName(keyword).parentComp(parentCompId).build();
|
|
|
|
CompPO compBuild = CompPO.builder().compName(keyword).parentCompany(parentCompId).build();
|
|
|
|
buildTreeByCompAndDept(departmentBuild, compBuild, compSearchTree);
|
|
|
|
} else if (TYPE_DEPT.equals(type)) {
|
|
|
|
//
|
|
|
|
// 查询部门信息
|
|
|
|
List<DepartmentPO> filterDeparts = getDepartmentMapper().listByFilter(DepartmentPO.builder().deptName(keyword).parentDept(Long.parseLong(id)).build());
|
|
|
|
Set<DepartmentPO> builderDeparts = new HashSet<>();
|
|
|
|
for (DepartmentPO departmentPO : filterDeparts) {
|
|
|
|
buildParentDepts(departmentPO, builderDeparts);
|
|
|
|
}
|
|
|
|
List<SearchTree> deptTrees = builderTreeMode(DepartmentBO.buildSetToSearchTree(builderDeparts));
|
|
|
|
// 排序,设置是否为叶子节点
|
|
|
|
List<TreeNode> collect = deptTrees.stream().peek(item ->
|
|
|
|
item.setIsParent(CollectionUtils.isNotEmpty(item.getSubs()))
|
|
|
|
).sorted(Comparator.comparing(item -> Integer.parseInt(item.getId()))).collect(Collectors.toList());
|
|
|
|
if (CollectionUtils.isNotEmpty(collect)) {
|
|
|
|
compSearchTree.addAll(collect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return compSearchTree;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 分部、部门 组装左侧树
|
|
|
|
*
|
|
|
|
* @param departmentBuild
|
|
|
|
* @param compBuild
|
|
|
|
* @param compSearchTree
|
|
|
|
*/
|
|
|
|
private void buildTreeByCompAndDept(DepartmentPO departmentBuild, CompPO compBuild, List<TreeNode> compSearchTree) {
|
|
|
|
List<DepartmentPO> filterDeparts = getDepartmentMapper().listByFilter(departmentBuild);
|
|
|
|
// 查询分部信息
|
|
|
|
List<CompPO> filterComps = getCompMapper().listByFilter(compBuild);
|
|
|
|
Set<DepartmentPO> builderDeparts = new HashSet<>();
|
|
|
|
for (DepartmentPO departmentPO : filterDeparts) {
|
|
|
|
buildParentDepts(departmentPO, builderDeparts);
|
|
|
|
}
|
|
|
|
List<SearchTree> deptTrees = builderTreeMode(DepartmentBO.buildSetToSearchTree(builderDeparts));
|
|
|
|
// 添加部门的上级分部
|
|
|
|
String parentCompS = deptTrees.stream().map(SearchTree::getParentComp).collect(Collectors.joining(","));
|
|
|
|
if (!StringUtil.isEmpty(parentCompS)) {
|
|
|
|
List<CompPO> compsByIds = getCompMapper().getCompsByIds(QueryParam.builder().ids(parentCompS).build().getIds());
|
|
|
|
if (CollectionUtils.isNotEmpty(compsByIds)) {
|
|
|
|
filterComps.addAll(compsByIds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Set<CompPO> builderComps = new HashSet<>();
|
|
|
|
for (CompPO compPO : filterComps) {
|
|
|
|
buildParentComps(compPO, builderComps);
|
|
|
|
}
|
|
|
|
List<TreeNode> compTrees = builderTreeMode(CompBO.buildSetToSearchTree(builderComps), deptTrees);
|
|
|
|
|
|
|
|
// 排序,设置是否为叶子节点
|
|
|
|
List<TreeNode> collect = compTrees.stream().peek(item ->
|
|
|
|
item.setIsParent(CollectionUtils.isNotEmpty(item.getSubs()))
|
|
|
|
).sorted(Comparator.comparing(item -> Integer.parseInt(item.getId()))).collect(Collectors.toList());
|
|
|
|
if (CollectionUtils.isNotEmpty(collect)) {
|
|
|
|
compSearchTree.addAll(collect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 判断是为顶层数据
|
|
|
|
*
|
|
|
|
* @param pid
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
private boolean isTop(Long pid) {
|
|
|
|
return null == pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 判断是为顶层数据
|
|
|
|
*
|
|
|
|
* @param pid
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
private boolean isTop(String pid) {
|
|
|
|
return StringUtil.isEmpty(pid) || "0".equals(pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 处理树层级
|
|
|
|
*
|
|
|
|
* @param treeList
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
private List<SearchTree> builderTreeMode(List<SearchTree> treeList) {
|
|
|
|
Map<String, List<TreeNode>> collects = treeList.stream().collect(Collectors.groupingBy(TreeNode::getPid));
|
|
|
|
return treeList.stream().peek(e -> e.setSubs(collects.get(e.getId()))).peek(item -> {
|
|
|
|
if (CollectionUtils.isNotEmpty(item.getSubs())) {
|
|
|
|
item.setIsParent(true);
|
|
|
|
}
|
|
|
|
}).filter(item -> isTop(item.getPid())).collect(Collectors.toList());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 组合分部、部门层级关系
|
|
|
|
*
|
|
|
|
* @param treeList
|
|
|
|
* @param deptTrees
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
private List<TreeNode> builderTreeMode(List<TreeNode> treeList, List<SearchTree> deptTrees) {
|
|
|
|
Map<String, List<TreeNode>> parentMap = treeList.stream().collect(Collectors.groupingBy(TreeNode::getPid));
|
|
|
|
Map<String, List<SearchTree>> childMap = deptTrees.stream().collect(Collectors.groupingBy(SearchTree::getParentComp));
|
|
|
|
boolean isAdd = !childMap.isEmpty();
|
|
|
|
return treeList.stream().peek(e -> {
|
|
|
|
List<TreeNode> treeNodes = new ArrayList<>();
|
|
|
|
List<TreeNode> nodes = parentMap.get(e.getId());
|
|
|
|
if (CollectionUtils.isNotEmpty(nodes)) {
|
|
|
|
treeNodes.addAll(nodes);
|
|
|
|
}
|
|
|
|
if (isAdd && CollectionUtils.isNotEmpty(childMap.get(e.getId()))) {
|
|
|
|
treeNodes.addAll(childMap.get(e.getId()));
|
|
|
|
}
|
|
|
|
e.setSubs(treeNodes);
|
|
|
|
}).peek(item -> {
|
|
|
|
if (CollectionUtils.isNotEmpty(item.getSubs())) {
|
|
|
|
item.setIsParent(true);
|
|
|
|
}
|
|
|
|
}).filter(item -> isTop(item.getPid())).collect(Collectors.toList());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|