package com.product.org.admin.service;
|
|
|
import java.util.Arrays;
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import com.alibaba.druid.util.StringUtils;
|
import com.product.admin.service.CodeService;
|
import com.product.core.dao.BaseDao;
|
import com.product.core.entity.DataTableEntity;
|
import com.product.core.entity.FieldSetEntity;
|
import com.product.core.exception.BaseException;
|
import com.product.core.permission.PermissionService;
|
import com.product.core.service.support.AbstractBaseService;
|
import com.product.core.service.support.QueryFilterService;
|
import com.product.core.spring.context.SpringMVCContextHolder;
|
import com.product.core.transfer.Transactional;
|
import com.product.org.admin.config.CmnCode;
|
import com.product.org.admin.config.CmnConst;
|
import com.product.org.admin.config.SystemCode;
|
import com.product.org.admin.service.idel.IDeptChangeService;
|
import com.product.util.BaseUtil;
|
|
/**
|
*
|
* Copyright LX-BASE
|
*
|
* @Title: LX-BASE-
|
* @Project: DeptChangesService
|
* @Date: 2020-06-04 20:07
|
* @Author: 杜洪波
|
* @Description: 部门变更
|
*/
|
@Component("deptChange")
|
public class DeptChangeService extends AbstractBaseService implements IDeptChangeService{
|
|
@Autowired
|
private BaseDao baseDao;
|
|
@Autowired
|
CodeService codeService;
|
|
@Autowired
|
PermissionService permissionService;
|
|
@Autowired
|
DeptChangeService deptChangeService;
|
|
@Autowired
|
public QueryFilterService queryFilterService;
|
|
/**
|
* 部门变更列表
|
*
|
* @param fse
|
* @return
|
* @throws BaseException
|
*/
|
public DataTableEntity listDeptChange(FieldSetEntity fse) throws BaseException {
|
//郑盟 2020年12月22日 下午18:34
|
String dataFilter=permissionService.getDataFilter("org_level_uuid,new_org_level_uuid") ;
|
String queryFilter="";
|
if (!BaseUtil.dataTableIsEmpty(fse.getSubDataTable("systemSeniorQueryString"))) {
|
if (!StringUtils.isEmpty(dataFilter)) {
|
queryFilter =" and"+queryFilterService.getQueryFilter(fse);
|
}else {
|
queryFilter =queryFilterService.getQueryFilter(fse);
|
}
|
}
|
return baseDao.listTable("product_sys_org_levels_change",dataFilter+queryFilter ,
|
new Object[] {},new Object[] {}, null,fse.getInteger("pagesize"), fse.getInteger("cpage"));
|
}
|
|
/**
|
* 部门变更详情
|
*
|
* @param uuid
|
* @return
|
* @throws BaseException
|
*/
|
public FieldSetEntity findDeptChange(String uuid) throws BaseException {
|
return baseDao.getFieldSetEntity("product_sys_org_levels_change", uuid, false);
|
}
|
|
/**
|
* 部门变更新增验证
|
*
|
* @param deptUUid
|
* @return
|
* @throws BaseException
|
*/
|
public String deptChangeAdd(FieldSetEntity fseChangeInfo) throws BaseException {
|
DataTableEntity dtDeptChange = baseDao.listTable(CmnConst.PRODUCT_SYS_ORG_LEVELS_CHANGE, "origin_dept_uuid=? and (result_status is null or result_status=1)", new Object[] { fseChangeInfo.getString("origin_dept_uuid") });
|
if (dtDeptChange != null && dtDeptChange.getRows() > 0) {
|
throw new BaseException(SystemCode.SYSTEM_CURRENT_DEPT_CHANGE_EXIST.getValue(), SystemCode.SYSTEM_CURRENT_DEPT_CHANGE_EXIST.getText(), this.getClass(), "deptChangeAddValitation");
|
}
|
|
//设置创建人和创建时间
|
fseChangeInfo.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());
|
fseChangeInfo.setValue(CmnConst.CREATED_UTC_DATETIME, new Date());
|
fseChangeInfo.setValue("result_status", 0);
|
|
boolean isMatch=true;
|
|
//获取部门变更类型
|
String deptChangeType = fseChangeInfo.getString("change_status_uuid");
|
|
IDeptChangeService service =(IDeptChangeService)getProxyInstance(deptChangeService);
|
if ("2".equals(deptChangeType)) { //合并部门
|
isMatch = service.deptMerge(fseChangeInfo);
|
} else if ("3".equals(deptChangeType)) { //迁移部门
|
isMatch = service.deptMove(fseChangeInfo);
|
}
|
|
if (isMatch) {
|
//新增记录
|
return baseDao.add(fseChangeInfo);
|
}else {
|
throw new BaseException(CmnCode.DEPT_CHANGE_ERROR.getValue(), CmnCode.DEPT_CHANGE_ERROR.getText());
|
}
|
}
|
|
|
/**
|
* 部门合并
|
* 1:变更后代部门全称
|
* 2:变更后代部门组织机构编码
|
* 3:变更原部门岗位至合并部门
|
* 4:变更原部门员工至合并部门
|
* 5:重新生成相关部门下员工的员工编码
|
* 6.将原部门标记为被合并
|
* 7.将变更标识为1
|
* @param fse
|
* @return
|
* @throws BaseException
|
*/
|
@Transactional
|
public boolean deptMerge(FieldSetEntity fse) throws BaseException {
|
|
boolean flag = true;
|
|
String originOrgUuid = fse.getString(CmnConst.ORG_LEVEL_UUID); //合并部门所属公司UUID
|
String mergeOrgUuid = fse.getString("new_org_level_uuid"); //保留部门所属公司UUID
|
String originDeptUuid = fse.getString(CmnConst.ORIGIN_DEPT_UUID); //获取原部门UUID
|
String mergeDeptUuid = fse.getString("new_dept_uuid"); //获取保留部门UUID
|
|
//获取原部门信息
|
FieldSetEntity fseOriginDept = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS, originDeptUuid, false);
|
//获取保留部门信息
|
FieldSetEntity fseMergeDept = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS, mergeDeptUuid, false);
|
if (fseMergeDept == null || fseOriginDept==null) {
|
throw new BaseException(SystemCode.SYSTEM_DEPT_MERGE_NO_EXIST.getValue(), SystemCode.SYSTEM_DEPT_MERGE_NO_EXIST.getText());
|
}
|
|
String originDeptCode = fseOriginDept.getString(CmnConst.ORG_LEVEL_CODE); //获取原部门编码
|
String mergeDeptCode = fseMergeDept.getString(CmnConst.ORG_LEVEL_CODE); //获取保留部门编码
|
|
//获取子一代部门信息
|
DataTableEntity dtDepts=baseDao.listTable(CmnConst.PRODUCT_SYS_ORG_LEVELS, "org_level_code_parent =?", new Object[]{originDeptCode});
|
|
//判断是否包含子一代部门变化
|
if (!BaseUtil.dataTableIsEmpty(dtDepts)) {
|
Object[] uuids = dtDepts.getUuids();
|
if (uuids != null) {
|
|
//生成的第一个编码,后续编码需要根据该编码生成
|
String finalCode="";
|
|
//遍历子部门,变更上下级编码
|
for (int i = 0; i < dtDepts.getRows(); i++) {
|
FieldSetEntity fseSubDept=dtDepts.getFieldSetEntity(i);
|
// 获取子部门合并前的编码
|
String originDeptSubCode = fseSubDept.getString(CmnConst.ORG_LEVEL_CODE);
|
// 创建子部门新编码
|
String newDeptSubCode="";
|
if(i==0) { //生成code管理器编码
|
newDeptSubCode = codeService.createCode(CmnConst.PRODUCT_SYS_ORG_LEVELS, CmnConst.ORG_LEVEL_CODE, mergeDeptCode);
|
finalCode=newDeptSubCode;
|
}else { //自动生成后续编码
|
newDeptSubCode = numCodeparse(finalCode,i);
|
}
|
|
fseSubDept.setValue(CmnConst.ORG_LEVEL_CODE, newDeptSubCode);
|
fseSubDept.setValue(CmnConst.ORG_LEVEL_CODE_PARENT, mergeDeptCode);
|
baseDao.update(fseSubDept);
|
|
//变更该部门后代部门 编码与上级编码
|
StringBuilder sqlSubDeptChangeCode=new StringBuilder();
|
sqlSubDeptChangeCode.append(" update product_sys_org_levels ");
|
sqlSubDeptChangeCode.append(" set org_level_code=concat(replace(substring(org_level_code,1,locate(?,org_level_code)+length(?)),?,?),substring(org_level_code,locate(?,org_level_code)+length(?)+1)), ");
|
sqlSubDeptChangeCode.append(" org_level_code_parent=concat(replace(substring(org_level_code_parent,1,locate(?,org_level_code_parent)+length(?)),?,?),substring(org_level_code_parent,locate(?,org_level_code_parent)+length(?)+1)) ");
|
sqlSubDeptChangeCode.append(" where org_level_code like ? ");
|
baseDao.executeUpdate(sqlSubDeptChangeCode.toString(), new Object[] {originDeptSubCode,originDeptSubCode,originDeptSubCode,newDeptSubCode,originDeptSubCode,originDeptSubCode,
|
originDeptSubCode,originDeptSubCode,originDeptSubCode,newDeptSubCode,originDeptSubCode,originDeptSubCode,originDeptSubCode+"-%"});
|
|
//变更该部门及后代部门所有员工 编码与上级领导编码
|
StringBuilder sqlStaffChangeCode=new StringBuilder();
|
sqlStaffChangeCode.append(" update product_sys_staffs ");
|
sqlStaffChangeCode.append(" set tricode=concat(replace(substring(tricode,1,locate(?,tricode)+length(?)),?,?),substring(tricode,locate(?,tricode)+length(?)+1)), ");
|
sqlStaffChangeCode.append(" direct_leader_code=concat(replace(substring(direct_leader_code,1,locate(?,direct_leader_code)+length(?)),?,?),substring(direct_leader_code,locate(?,direct_leader_code)+length(?)+1)) ");
|
sqlStaffChangeCode.append(" where tricode like ? ");
|
|
flag=baseDao.executeUpdate(sqlStaffChangeCode.toString(), new Object[] {originDeptSubCode,originDeptSubCode,originDeptSubCode,newDeptSubCode,originDeptSubCode,originDeptSubCode,
|
originDeptSubCode,originDeptSubCode,originDeptSubCode,newDeptSubCode,originDeptSubCode,originDeptSubCode,originDeptSubCode+"-%"});
|
|
}
|
}
|
|
}
|
|
|
//获取被合并部门的员工信息
|
DataTableEntity dtStaffs=baseDao.listTable(CmnConst.PRODUCT_SYS_STAFFS, "dept_uuid = ?", new Object[]{originDeptUuid});
|
if (!BaseUtil.dataTableIsEmpty(dtStaffs)) {
|
|
//员工编码变更存储对象
|
Map<String, String> changeStaffCode=new HashMap<>();
|
|
//生成一个固定编码
|
String finalCode="";
|
|
//遍历变化的员工
|
for (int i = 0; i < dtStaffs.getRows(); i++) {
|
FieldSetEntity fseStaff=dtStaffs.getFieldSetEntity(i);
|
fseStaff.remove("show_name");
|
fseStaff.remove("family_name");
|
fseStaff.remove("given_name");
|
fseStaff.remove("preferred_name");
|
|
String newStaffTricode="";
|
if(i==0) {
|
newStaffTricode = codeService.createCode(CmnConst.PRODUCT_SYS_STAFFS, CmnConst.TRICODE, mergeDeptCode);
|
finalCode=newStaffTricode;
|
}else {
|
newStaffTricode = numCodeparse(finalCode,i);
|
}
|
|
String originStaffTricode=fseStaff.getString(CmnConst.TRICODE);
|
|
fseStaff.setValue(CmnConst.TRICODE, newStaffTricode);
|
|
changeStaffCode.put(originStaffTricode, newStaffTricode);
|
}
|
baseDao.update(dtStaffs);
|
|
//修改影响到的其余下属员工
|
for ( Map.Entry<String, String> entries : changeStaffCode.entrySet() ){
|
String originStaffTriCode=entries.getKey(); //原部门编码
|
String newStaffTriCode=entries.getValue(); //新部门编码
|
|
StringBuilder sqlStaffChangeCode=new StringBuilder();
|
sqlStaffChangeCode.append(" update product_sys_staffs set ");
|
sqlStaffChangeCode.append(" direct_leader_code=concat(replace(substring(direct_leader_code,1,locate(?,direct_leader_code)+length(?)),?,?),substring(direct_leader_code,locate(?,direct_leader_code)+length(?)+1)) ");
|
sqlStaffChangeCode.append(" where direct_leader_code like ? ");
|
|
flag=baseDao.executeUpdate(sqlStaffChangeCode.toString(), new Object[] {originStaffTriCode,originStaffTriCode,originStaffTriCode,newStaffTriCode,originStaffTriCode,originStaffTriCode,originStaffTriCode+"%"});
|
}
|
}
|
|
|
|
//变更合并部门的岗位所属部门至保留部门
|
if (!originOrgUuid.equals(mergeOrgUuid)) {
|
baseDao.executeUpdate("update product_sys_job_posts set org_level_uuid=?,dept_uuid=? where dept_uuid=?", new Object[] {mergeOrgUuid,mergeDeptUuid,originOrgUuid});
|
}
|
baseDao.executeUpdate("update product_sys_job_posts set dept_uuid=? where dept_uuid=?", new Object[] {mergeDeptUuid,originDeptUuid});
|
|
//变更合并部门的人员所属部门至保留部门
|
if (!originOrgUuid.equals(mergeOrgUuid)) {
|
baseDao.executeUpdate("update product_sys_staffs set org_level_uuid=?,dept_uuid=? where dept_uuid=?", new Object[] {mergeOrgUuid,mergeDeptUuid,originOrgUuid});
|
}
|
baseDao.executeUpdate("update product_sys_staffs set dept_uuid=? where dept_uuid=?", new Object[] {mergeDeptUuid,originDeptUuid});
|
|
//变更当前部门状态为被合并
|
if (flag == true) {
|
fseOriginDept.setValue("org_level_status", 2);
|
flag = baseDao.update(fseOriginDept);
|
}
|
fse.setValue(CmnConst.RESULT_STATUS, 1);
|
|
return flag;
|
}
|
|
/**
|
* 手动处理编码 (循环产生编码拓展)
|
* @param finalCode
|
* @param i
|
* @return
|
*/
|
public String numCodeparse(String finalCode,int i) {
|
|
//获取编码前缀和后缀
|
String prefix=finalCode.substring(0, finalCode.lastIndexOf("-"));
|
String suffix=finalCode.substring(finalCode.lastIndexOf("-")+1, finalCode.length());
|
|
//通过后缀生成新编码
|
int newSuffixNum=Integer.parseInt(suffix);
|
newSuffixNum+=i;
|
|
String newCode="";
|
//生成新的编码
|
if (newSuffixNum<10) {
|
newCode=prefix+"-00"+newSuffixNum;
|
}else if (newSuffixNum>9 && newSuffixNum<100) {
|
newCode=prefix+"-0"+newSuffixNum;
|
}else {
|
newCode=prefix+"-"+newSuffixNum;
|
}
|
return newCode;
|
}
|
|
/**
|
* 部门迁移
|
* 1.变更原部门及后代部门国际化全称
|
* 2.判断是否跨公司迁移(变更岗位和员工所属公司UUID)
|
* 3.变更原部门及后代部门的组织机构编码及父编码
|
* 4.变更原部门及后代部门下的员工编码
|
* 5.将变更标识为1
|
* @param fse
|
* @param originDeptCode 原部门编码
|
* @param originDeptUuid 原部门uuid
|
* @return
|
* @throws BaseException
|
*/
|
@Transactional
|
public boolean deptMove(FieldSetEntity fse) throws BaseException {
|
boolean flag = true;
|
|
String originDeptUuid=fse.getString(CmnConst.ORIGIN_DEPT_UUID); //获取原部门UUID
|
String moveDeptUuid = fse.getString("new_dept_uuid"); //获取迁入部门uuid
|
String originOrgUuid=fse.getString(CmnConst.ORG_LEVEL_UUID); //原部门所属公司
|
String moveOrgUuid=fse.getString("new_org_level_uuid"); //迁入部门所属公司
|
if (StringUtils.isEmpty(moveDeptUuid)) { //杜洪波 updateTime 2020-12-17 09:48:00
|
moveDeptUuid=moveOrgUuid;
|
}
|
|
//获取原部门国际化信息
|
FieldSetEntity fseOriginDept=baseDao.listInternationDataTable(baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS, originDeptUuid, false),null);
|
|
//获取迁入部门国际化信息
|
FieldSetEntity fseMoveDept = baseDao.listInternationDataTable(baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS, moveDeptUuid, false),null);
|
if (fseMoveDept == null || fseOriginDept==null) {
|
return false;
|
}
|
|
String originDeptCode = fseOriginDept.getString(CmnConst.ORG_LEVEL_CODE); //获取原部门编码
|
String originDeptParentCode = fseOriginDept.getString(CmnConst.ORG_LEVEL_CODE_PARENT); //获取原部门父级编码
|
String moveDeptCode = fseMoveDept.getString(CmnConst.ORG_LEVEL_CODE); //获取迁入部门编码
|
|
//获取原部门上级国际化信息
|
FieldSetEntity fseOriginDeptParent = baseDao.listInternationDataTable(baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_ORG_LEVELS, "org_level_code=?", new Object[] {originDeptParentCode}, false),null);
|
if (fseOriginDeptParent==null) {
|
return false;
|
}
|
|
|
//获取所有后代部门信息
|
DataTableEntity dtDepts=baseDao.listTable(CmnConst.PRODUCT_SYS_ORG_LEVELS, "org_level_code like'"+originDeptCode+"%'", new Object[]{});
|
if (dtDepts!=null && dtDepts.getRows()>0) {
|
Object[] uuids = dtDepts.getUuids();
|
if (uuids != null) {
|
|
//判断迁移前后公司是否修改,如已修改,则变更员工所属公司,岗位所属公司
|
if (!originOrgUuid.equals(moveOrgUuid)) {
|
baseDao.executeUpdate("update product_sys_staffs set org_level_uuid=? where " + BaseUtil.buildQuestionMarkFilter(CmnConst.DATA_UUID, uuids.length, true), new Object[] {moveOrgUuid,Arrays.asList(uuids)});
|
baseDao.executeUpdate("update product_sys_job_posts set org_level_uuid=? where " + BaseUtil.buildQuestionMarkFilter(CmnConst.DATA_UUID, uuids.length, true), new Object[] {moveOrgUuid,Arrays.asList(uuids)});
|
}
|
}
|
}
|
|
//生成新部门编码
|
String newMoveCode = codeService.createCode(CmnConst.PRODUCT_SYS_ORG_LEVELS, CmnConst.ORG_LEVEL_CODE, moveDeptCode);
|
fseOriginDept.setValue(CmnConst.ORG_LEVEL_CODE, newMoveCode);
|
fseOriginDept.setValue(CmnConst.ORG_LEVEL_CODE_PARENT, moveDeptCode);
|
baseDao.update(fseOriginDept);
|
|
//变更迁移部门及后代部门组织机构编码及父编码
|
StringBuilder sqlSubDeptChangeCode=new StringBuilder();
|
sqlSubDeptChangeCode.append(" update product_sys_org_levels ");
|
sqlSubDeptChangeCode.append(" set org_level_code=concat(replace(substring(org_level_code,1,locate(?,org_level_code)+length(?)),?,?),substring(org_level_code,locate(?,org_level_code)+length(?)+1)), ");
|
sqlSubDeptChangeCode.append(" org_level_code_parent=concat(replace(substring(org_level_code_parent,1,locate(?,org_level_code_parent)+length(?)),?,?),substring(org_level_code_parent,locate(?,org_level_code_parent)+length(?)+1)) ");
|
sqlSubDeptChangeCode.append(" where org_level_code like ? ");
|
|
baseDao.executeUpdate(sqlSubDeptChangeCode.toString(), new Object[] {originDeptCode,originDeptCode,originDeptCode,newMoveCode,originDeptCode,originDeptCode,
|
originDeptCode,originDeptCode,originDeptCode,newMoveCode,originDeptCode,originDeptCode,originDeptCode+"-%"});
|
|
//变更迁移部门及后代部门的员工编码和上级领导编码
|
StringBuilder sqlStaffChangeCode=new StringBuilder();
|
sqlStaffChangeCode.append(" update product_sys_staffs ");
|
sqlStaffChangeCode.append(" set tricode=concat(replace(substring(tricode,1,locate(?,tricode)+length(?)),?,?),substring(tricode,locate(?,tricode)+length(?)+1)), ");
|
sqlStaffChangeCode.append(" direct_leader_code=concat(replace(substring(direct_leader_code,1,locate(?,direct_leader_code)+length(?)),?,?),substring(direct_leader_code,locate(?,direct_leader_code)+length(?)+1)) ");
|
sqlStaffChangeCode.append(" where tricode like ? ");
|
|
flag=baseDao.executeUpdate(sqlStaffChangeCode.toString(), new Object[] {originDeptCode,originDeptCode,originDeptCode,newMoveCode,originDeptCode,originDeptCode,originDeptCode,originDeptCode,originDeptCode,newMoveCode,originDeptCode,originDeptCode,originDeptCode+"%"});
|
|
if (flag) {
|
fse.setValue(CmnConst.RESULT_STATUS, 1);
|
}
|
|
return baseDao.update(fse);
|
}
|
}
|