package com.engine.organization.service.impl;
import com.cloudstore.eccom.result.WeaResultMsg;
import com.engine.core.impl.Service;
import com.engine.organization.component.OrganizationWeaTable;
import com.engine.organization.entity.DeleteParam;
import com.engine.organization.entity.company.bo.CompBO;
import com.engine.organization.entity.company.po.CompPO;
import com.engine.organization.entity.department.bo.DepartmentBO;
import com.engine.organization.entity.department.po.DepartmentPO;
import com.engine.organization.entity.hrmresource.param.HrmResourceSearchParam;
import com.engine.organization.entity.hrmresource.vo.HrmResourceVO;
import com.engine.organization.entity.job.bo.JobBO;
import com.engine.organization.entity.job.po.JobPO;
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.mapper.job.JobMapper;
import com.engine.organization.service.HrmResourceService;
import com.engine.organization.util.db.DBType;
import com.engine.organization.util.db.MapperProxyFactory;
import com.engine.organization.util.tree.SearchTreeUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.StringUtil;
import weaver.general.Util;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author:dxfeng
* @createTime: 2022/06/20
* @version: 1.0
*/
public class HrmResourceServiceImpl extends Service implements HrmResourceService {
/**
* 左侧树 类型表示
*
* 0:集团
* 1:分部
* 2:部门
* 3:岗位
*/
private static final String TYPE_COMP = "1";
private static final String TYPE_DEPT = "2";
private static final String TYPE_JOB = "3";
private DepartmentMapper getDepartmentMapper() {
return MapperProxyFactory.getProxy(DepartmentMapper.class);
}
private CompMapper getCompMapper() {
return MapperProxyFactory.getProxy(CompMapper.class);
}
private JobMapper getJobMapper() {
return MapperProxyFactory.getProxy(JobMapper.class);
}
@Override
public Map getSearchTree(SearchTreeParams params) {
String keyword = params.getKeyword();
String id = params.getId();
String type = Util.null2String(params.getType());
List treeList = getFilterCompany(id, type, keyword);
return SearchTreeUtil.getSearchTree(type, treeList);
}
@Override
public Map listPage(HrmResourceSearchParam params) {
Map resultMap = new HashMap<>();
OrganizationWeaTable table = new OrganizationWeaTable<>(user, HrmResourceVO.class);
String sqlWhere = buildSqlWhere(params);
table.setSqlwhere(sqlWhere);
WeaResultMsg result = new WeaResultMsg(false);
result.putAll(table.makeDataResult());
result.success();
resultMap.putAll(result.getResultMap());
return resultMap;
}
/**
* 查询条件
*
* @param params
* @return
*/
private String buildSqlWhere(HrmResourceSearchParam params) {
DBType dbType = DBType.get(new RecordSet().getDBType());
String sqlWhere = " where t.delete_type ='0' ";
String lastName = params.getLastName();
if (StringUtils.isNotBlank(lastName)) {
sqlWhere += " AND t.last_name " + dbType.like(lastName);
}
Long managerId = params.getManagerId();
if (null != managerId) {
sqlWhere += " AND t.manager_id = '" + managerId + "'";
}
Long companyId = params.getCompanyId();
if (null != companyId) {
sqlWhere += " AND t.company_id = '" + companyId + "'";
}
Long departmentId = params.getDepartmentId();
if (null != departmentId) {
sqlWhere += " AND t.department_id = '" + departmentId + "'";
}
String telephone = params.getTelephone();
if (StringUtils.isNotBlank(telephone)) {
sqlWhere += " AND t.telephone " + dbType.like(telephone);
}
String mobile = params.getMobile();
if (StringUtils.isNotBlank(mobile)) {
sqlWhere += " AND t.mobile " + dbType.like(mobile);
}
String mobileCall = params.getMobileCall();
if (StringUtils.isNotBlank(mobileCall)) {
sqlWhere += " AND t.mobile_call " + dbType.like(mobileCall);
}
Long jobTitle = params.getJobTitle();
if (null != jobTitle) {
sqlWhere += " AND t.job_title = '" + jobTitle + "'";
}
return sqlWhere;
}
public List getFilterCompany(String id, String type, String keyword) {
List searchTree = 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();
// 所属分部下的岗位
JobPO jobBuild = JobPO.builder().jobName(keyword).parentComp(parentCompId).build();
searchTree = buildTreeByCompAndDept(departmentBuild, compBuild, jobBuild);
} else if (TYPE_DEPT.equals(type)) {
Long parentDeptId = Long.parseLong(id);
DepartmentPO departmentBuild = DepartmentPO.builder().deptName(keyword).parentDept(parentDeptId).build();
// 所属分部下的岗位
JobPO jobBuild = JobPO.builder().jobName(keyword).parentDept(parentDeptId).build();
searchTree = buildTreeByDeptAndJob(departmentBuild, jobBuild);
} else if (TYPE_JOB.equals(type)) {
// 查询部门信息
List filterDeparts = getJobMapper().listPOsByFilter(JobPO.builder().jobName(keyword).parentJob(Long.parseLong(id)).build());
Set builderJobs = new HashSet<>();
for (JobPO departmentPO : filterDeparts) {
buildParentJobs(departmentPO, builderJobs);
}
searchTree = SearchTreeUtil.builderTreeMode(JobBO.buildSetToSearchTree(builderJobs));
}
return searchTree;
}
/**
* 分部、部门 组装左侧树
*
* @param departmentBuild
* @param compBuild
* @param jobBuild
* @return
*/
private List buildTreeByCompAndDept(DepartmentPO departmentBuild, CompPO compBuild, JobPO jobBuild) {
List jobPOS = getJobMapper().listPOsByFilter(jobBuild);
List filterDeparts = getDepartmentMapper().listByFilter(departmentBuild);
// 添加父级岗位
Set builderJobs = new HashSet<>();
for (JobPO jobPO : jobPOS) {
buildParentJobs(jobPO, builderJobs);
}
// 添加岗位的上级部门或分部
List jobTrees = SearchTreeUtil.builderTreeMode(JobBO.buildSetToSearchTree(builderJobs));
String parentDeptS = jobTrees.stream().map(SearchTree::getParentComp).collect(Collectors.joining(","));
if (!StringUtil.isEmpty(parentDeptS)) {
List compsByIds = getDepartmentMapper().getDeptsByIds(DeleteParam.builder().ids(parentDeptS).build().getIds());
if (CollectionUtils.isNotEmpty(compsByIds)) {
filterDeparts.addAll(compsByIds);
}
}
// 查询分部信息
List filterComps = getCompMapper().listByFilter(compBuild);
Set builderDeparts = new HashSet<>();
for (DepartmentPO departmentPO : filterDeparts) {
buildParentDepts(departmentPO, builderDeparts);
}
List deptTrees = SearchTreeUtil.builderTreeMode(DepartmentBO.buildSetToSearchTree(builderDeparts, false));
// 添加部门的上级分部
String parentCompS = deptTrees.stream().map(SearchTree::getParentComp).collect(Collectors.joining(","));
if (!StringUtil.isEmpty(parentCompS)) {
List compsByIds = getCompMapper().getCompsByIds(DeleteParam.builder().ids(parentCompS).build().getIds());
if (CollectionUtils.isNotEmpty(compsByIds)) {
filterComps.addAll(compsByIds);
}
}
Set builderComps = new HashSet<>();
for (CompPO compPO : filterComps) {
buildParentComps(compPO, builderComps);
}
List searchTrees = SearchTreeUtil.builderTreeMode(deptTrees, jobTrees);
return SearchTreeUtil.builderTreeMode(CompBO.buildSetToSearchTree(builderComps), searchTrees);
}
private List buildTreeByDeptAndJob(DepartmentPO departmentBuild, JobPO jobBuild) {
List jobPOS = getJobMapper().listPOsByFilter(jobBuild);
List filterDeparts = getDepartmentMapper().listByFilter(departmentBuild);
// 添加父级岗位
Set builderJobs = new HashSet<>();
for (JobPO jobPO : jobPOS) {
buildParentJobs(jobPO, builderJobs);
}
// 添加岗位的上级部门或分部
List jobTrees = SearchTreeUtil.builderTreeMode(JobBO.buildSetToSearchTree(builderJobs));
String parentDeptS = jobTrees.stream().map(SearchTree::getParentComp).collect(Collectors.joining(","));
if (!StringUtil.isEmpty(parentDeptS)) {
List compsByIds = getDepartmentMapper().getDeptsByIds(DeleteParam.builder().ids(parentDeptS).build().getIds());
if (CollectionUtils.isNotEmpty(compsByIds)) {
filterDeparts.addAll(compsByIds);
}
}
// 查询分部信息
Set builderDeparts = new HashSet<>();
for (DepartmentPO departmentPO : filterDeparts) {
buildParentDepts(departmentPO, builderDeparts);
}
return SearchTreeUtil.builderTreeMode(DepartmentBO.buildSetToSearchTree(builderDeparts, false), jobTrees);
}
private void buildParentJobs(JobPO jobPO, Set builderJobs) {
builderJobs.add(jobPO);
if (SearchTreeUtil.isTop(jobPO.getParentJob())) {
return;
}
JobPO parentJob = getJobMapper().getJobById(jobPO.getParentJob());
if (null != parentJob) {
buildParentJobs(parentJob, builderJobs);
}
}
/**
* 添加查询元素的父级元素
*
* @param departmentPO
* @param builderDeparts
*/
private void buildParentDepts(DepartmentPO departmentPO, Set builderDeparts) {
builderDeparts.add(departmentPO);
if (SearchTreeUtil.isTop(departmentPO.getParentDept())) {
return;
}
DepartmentPO parentDept = getDepartmentMapper().getDeptById(departmentPO.getParentDept());
if (null != parentDept) {
buildParentDepts(parentDept, builderDeparts);
}
}
/**
* 添加查询元素的父级元素
*
* @param compPO
* @param builderComps
*/
private void buildParentComps(CompPO compPO, Set builderComps) {
builderComps.add(compPO);
if (SearchTreeUtil.isTop(compPO.getParentCompany())) {
return;
}
CompPO parentComp = getCompMapper().listById(compPO.getParentCompany());
if (null != parentComp) {
buildParentComps(parentComp, builderComps);
}
}
}