package com.product.org.admin.service; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; import com.product.admin.service.CodeService; import com.product.common.lang.StringUtils; import com.product.core.cache.DataPoolCacheImpl; import com.product.core.dao.BaseDao; import com.product.core.entity.DataTableEntity; import com.product.core.entity.FieldMetaEntity; import com.product.core.entity.FieldSetEntity; import com.product.core.exception.BaseException; import com.product.core.service.support.AbstractBaseService; import com.product.core.spring.context.SpringBeanUtil; import com.product.core.spring.context.SpringMVCContextHolder; import com.product.core.transfer.Transactional; import com.product.org.admin.config.CmnConst; import com.product.org.admin.config.SystemCode; import com.product.org.admin.service.idel.ITimeTaskService; import com.product.org.admin.util.InternationalLanguage; import com.product.util.BaseUtil; /** * * Copyright LX-BASE * * @Title: LX-BASE- * @Project: TimeTaskChangeService * @Date: 2020-06-10 15:24 * @Author: 杜洪波 * @Description:定时任务变更(部门变更,人员变更,人员离职,人员工转正),每日扫描 */ @Component("task-service") public class TimeTaskChangeService extends AbstractBaseService implements ITimeTaskService{ @Autowired private BaseDao baseDao; @Autowired private CodeService codeService; @Autowired ApplicationContext applicationContext; @Autowired TimeTaskChangeService timeTaskChangeServices; @Autowired private StaffManagerService staffManagerService; static final String RESULT="result"; /** * 定时任务按天执行汇总(员工状态刷新,员工转正,员工离职,员工变更,组织机构变更) * * @throws BaseException */ public void timeTaskForDay() throws BaseException { Date date = new Date(); long currentTime = date.getTime(); staffStatusChange(currentTime); //员工状态变更 staffBecomeRegular(currentTime);//员工转正 staffChange(currentTime); //员工变更 staffDimission(currentTime); //员工离职 deptChange(currentTime); //部门变更 } /** * 员工状态定时任务变更 * @param currentTime * @throws BaseException */ public void staffStatusChange(long currentTime) throws BaseException{ DataTableEntity dtNoChange=baseDao.listTable("SELECT * FROM product_sys_staff_employment_info WHERE staff_uuid IN (SELECT uuid FROM product_sys_staffs WHERE staff_status=0 OR staff_status=1)", new Object[] {}); if (dtNoChange != null && dtNoChange.getRows() > 0) { for (int i = 0; i < dtNoChange.getRows(); i++) { FieldSetEntity fseNoChange = dtNoChange.getFieldSetEntity(i); String staff_uuid = fseNoChange.getString(CmnConst.STAFF_UUID); // 转正员工uuid Date entryTime=fseNoChange.getDate("employment_date"); // 加入公司日期时间 Date probationTime = fseNoChange.getDate("probation_end_date"); // 试用结束日期 int timeZone = getStaffForCompanyTimeZone(staff_uuid); boolean isMatch1=matchTime(currentTime, entryTime, timeZone, 4); boolean isMatch2=matchTime(currentTime, probationTime, timeZone, 4); if (isMatch2) { baseDao.executeUpdate("UPDATE product_sys_staffs SET staff_status=2 WHERE uuid=?",new Object[] {staff_uuid}); autoCreateApplicationForm(staff_uuid,entryTime,probationTime); }else { if (isMatch1) { baseDao.executeUpdate("UPDATE product_sys_staffs SET staff_status=1 WHERE uuid=?",new Object[] {staff_uuid}); } } } } } /** * 自动生成转正申请单 * @param staff_uuid * @param probationTime */ public void autoCreateApplicationForm(String staff_uuid,Date entryTime, Date probationTime) { FieldSetEntity fseStaff=baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_STAFFS, staff_uuid, false); if (fseStaff!=null) { FieldSetEntity fse=new FieldSetEntity(); FieldMetaEntity fseMate=new FieldMetaEntity(); fseMate.setTableName(new String[] {CmnConst.PRODUCT_SYS_STAFF_COMPLETE_PROBATION}); fse.setMeta(fseMate); fse.setValue(CmnConst.ORG_LEVEL_UUID, fseStaff.getString(CmnConst.ORG_LEVEL_UUID)); fse.setValue(CmnConst.STAFF_UUID, staff_uuid); fse.setValue("employment_date", entryTime); fse.setValue(CmnConst.PROBATION_END_DATE, automaticDateGrowth(probationTime)); fse.setValue(CmnConst.RESULT_STATUS, 0); baseDao.add(fse); } } /** * 日期自增+1天 * @param probationTime */ public Date automaticDateGrowth(Date probationTime) { Calendar cal = Calendar.getInstance(); cal.setTime(probationTime); cal.add(Calendar.DATE,1); probationTime =cal.getTime(); return probationTime; } /** * 员工转正 * timeZone==-1 找不到员工对应公司信息,或者公司配置时区错误,跳过 timeZone==0 公司未配置时区信息,则按照系统标准时区匹配 * @param currentTime 当前时间 * @throws BaseException */ public void staffBecomeRegular(long currentTime) throws BaseException { // 获取员工转正未执行数据 DataTableEntity dtNoExecute = baseDao.listTable(CmnConst.PRODUCT_SYS_STAFF_COMPLETE_PROBATION,"result_status=0 "); if (dtNoExecute != null && dtNoExecute.getRows() > 0) { //遍历员工转正未执行数据 for (int i = 0; i < dtNoExecute.getRows(); i++) { FieldSetEntity fseNoExecute = dtNoExecute.getFieldSetEntity(i); String staff_uuid = fseNoExecute.getString(CmnConst.STAFF_UUID); // 转正员工uuid Date excutedateTime = fseNoExecute.getDate("probation_end_date"); // 执行时间 int timeZone = getStaffForCompanyTimeZone(staff_uuid); if (timeZone == -1) { continue; } //判断时间是否匹配 boolean isMatch = matchTime(currentTime, excutedateTime, timeZone, 4); if (isMatch) { //执行转正 try { ITimeTaskService service=(ITimeTaskService)getProxyInstance(timeTaskChangeServices); isMatch = service.changeRegular(fseNoExecute,staff_uuid); } catch (BaseException e) { e.printStackTrace(); SpringMVCContextHolder.getSystemLogger().error(e); isMatch=false; } catch (Exception e) { e.printStackTrace(); SpringMVCContextHolder.getSystemLogger().error(e); isMatch=false; } } if (isMatch) { DataPoolCacheImpl.getInstance().reFreshCacheDataByPermission(fseNoExecute.getString(CmnConst.STAFF_UUID)); executeServiceChange(fseNoExecute,CmnConst.PRODUCT_SYS_STAFF_COMPLETE_PROBATION); } } } } /** * 员工离职 * @param currentTime * @throws BaseException */ public void staffDimission(long currentTime) throws BaseException { DataTableEntity dtNoExecute = baseDao.listTable(CmnConst.PRODUCT_SYS_STAFF_TERMINATION,"result_status = 0 OR result_status = 1 "); if (dtNoExecute != null && dtNoExecute.getRows() > 0) { for (int i = 0; i < dtNoExecute.getRows(); i++) { FieldSetEntity fseNoExecute = dtNoExecute.getFieldSetEntity(i); int result_status = fseNoExecute.getInteger(CmnConst.RESULT_STATUS); //执行状态 String staff_uuid = fseNoExecute.getString(CmnConst.APP_STAFF_UUID); //转正员工uuid Date excutedateTime = fseNoExecute.getDate("change_effective"); //执行时间 int timeZone = getStaffForCompanyTimeZone(staff_uuid); //时区误差 //判断时间是否匹配 boolean isMatch = matchTime(currentTime, excutedateTime, timeZone, 4); if (isMatch) { if (result_status == 0) { //执行变更 try { ITimeTaskService service =(ITimeTaskService)getProxyInstance(timeTaskChangeServices); isMatch=service.changeDimission(fseNoExecute); } catch (BaseException e) { SpringMVCContextHolder.getSystemLogger().error(e); isMatch=false; } catch (Exception e) { SpringMVCContextHolder.getSystemLogger().error(e); isMatch=false; } } if (isMatch) { DataPoolCacheImpl.getInstance().reFreshCacheDataByPermission(fseNoExecute.getString(CmnConst.APP_STAFF_UUID)); executeServiceChange(fseNoExecute,CmnConst.PRODUCT_SYS_STAFF_TERMINATION); } } } } } /** * 员工变更 * @param currentTime * @throws BaseException */ public void staffChange(long currentTime) throws BaseException { DataTableEntity dtNoExecute = baseDao.listTable(CmnConst.PRODUCT_SYS_STAFF_MOVEMENT,"result_status =0 OR result_status=1 "); if (dtNoExecute != null && dtNoExecute.getRows() > 0) { for (int i = 0; i < dtNoExecute.getRows(); i++) { FieldSetEntity fseNoExecute = dtNoExecute.getFieldSetEntity(i); int result_status = fseNoExecute.getInteger(CmnConst.RESULT_STATUS); String org_level_uuid = fseNoExecute.getString("original_org_level_uuid"); Date excutedateTime = fseNoExecute.getDate("movement_effective_utc_date"); // 执行时间 int timeZone = getcompanyTImeZone(org_level_uuid); // 判断时间是否匹配 boolean isMatch = matchTime(currentTime, excutedateTime, timeZone, 4); if (isMatch) { if (result_status == 0) { try { ITimeTaskService service =(ITimeTaskService)getProxyInstance(timeTaskChangeServices); isMatch=service.staffChange(fseNoExecute); } catch (BaseException e) { SpringMVCContextHolder.getSystemLogger().error(e); isMatch=false; } catch (Exception e) { SpringMVCContextHolder.getSystemLogger().error(e); isMatch=false; } } if (isMatch) { DataPoolCacheImpl.getInstance().reFreshCacheDataByPermission(fseNoExecute.getString(CmnConst.STAFF_UUID)); executeServiceChange(fseNoExecute,CmnConst.PRODUCT_SYS_STAFF_MOVEMENT); } } } } } /** * 执行时间匹配 * * @param currentTime 系统标准时间戳 * @param excutedateTime 数据执行时间戳 * @param timeZone 时区时间差(分钟) * @param type 执行周期类型(1:年 2:月 3:日 4:时 5:分 6:秒) * @return */ public boolean matchTime(long currentTime, Date excutedateTime, int timeZone, int type) { timeZone = timeZone * 60 * 1000; // 时间差(分钟)-->时间戳 long endTime = currentTime + timeZone; // 执行数据最终时间戳 /* int errorTime = 0; // 执行周期误差 switch (type) { case 1: errorTime = 365 * 30 * 24 * 60 * 60 * 1000; // 一年误差 break; case 2: errorTime = 30 * 24 * 60 * 60 * 1000; // 一月误差 break; case 3: errorTime = 7 * 24 * 60 * 60 * 100; // 一星期误差 break; case 4: errorTime = 24 * 60 * 60 * 1000; // 一天误差 break; case 5: errorTime = 60 * 60 * 1000; // 一小时误差 break; case 6: errorTime = 60 * 1000; // 一分钟误差 break; default: break; } if (endTime - excutedateTime.getTime() < errorTime && endTime >= excutedateTime.getTime()) { */ if (endTime >= excutedateTime.getTime()) { return true; } return false; } /** * 获取员工所属公司 * * @param uuid * @return * @throws BaseException */ public int getStaffForCompanyTimeZone(String staff_uuid) throws BaseException { // 获取staff信息 FieldSetEntity fseStaffInfo = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_STAFFS, staff_uuid, false); if (fseStaffInfo == null) { return -1; } return getcompanyTImeZone(fseStaffInfo.getString(CmnConst.ORG_LEVEL_UUID)); } /** * 根据公司uuid获取公司所属时区 * * @param org_level_uuid * @return * @throws BaseException */ public int getcompanyTImeZone(String org_level_uuid) throws BaseException { // 获取公司配置时区信息 FieldSetEntity fseCompanyTimeZone = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_COMPANY_TIME_ZONE,"org_level_uuid=?", new Object[] { org_level_uuid }, false); if (fseCompanyTimeZone == null) { return 0; } // 获取时区信息 FieldSetEntity fseTimeZone = baseDao.getFieldSetEntity(CmnConst.PRODUCT_COMMON_TIME_ZONE,fseCompanyTimeZone.getString("zone_uuid"), false); if (fseTimeZone == null) { return -1; } return fseTimeZone.getInteger("utc_time_zone"); } /** * 执行业务代码变更 * @param fseNoExecute * @param table_name * @throws BaseException */ public void executeServiceChange(FieldSetEntity fseNoExecute,String table_name) throws BaseException { FieldSetEntity fseServiceCode = baseDao.getFieldSetEntityByFilter("product_sys_org_levels_change_process", "is_used=1 and change_table_uuid =?",new Object[] { table_name }, false); if (fseServiceCode != null) { String serviceBeanName = fseServiceCode.getString("server_component_bean"); String seriveMethodNmae = fseServiceCode.getString("bean_method"); String methodParam = fseServiceCode.getString("in_paras_fields"); String changeType=fseServiceCode.getString("chang_type"); String resultInfo=null; try { boolean succ=serviceBean(serviceBeanName, seriveMethodNmae, methodParam, fseNoExecute); if (succ) { fseNoExecute.setValue(CmnConst.RESULT_STATUS, 2); baseDao.update(fseNoExecute); }else { SpringMVCContextHolder.getSystemLogger().error("业务逻辑处理不成功!"); } resultInfo="succ"; } catch (Exception e) { fseNoExecute.setValue("logic_table_name", table_name); SpringMVCContextHolder.getSystemLogger().error(e); resultInfo=e.getMessage(); } finally { FieldSetEntity fseChangHistory=new FieldSetEntity(); FieldMetaEntity fseMate=new FieldMetaEntity(); fseMate.setTableName(new Object[] {"product_sys_change_history"}); fseChangHistory.setMeta(fseMate); fseChangHistory.setValue("change_table_uuid", table_name); fseChangHistory.setValue("chang_type", changeType); fseChangHistory.setValue("json_value", ""); fseChangHistory.setValue("change_uuid", fseNoExecute.getUUID()); fseChangHistory.setValue("remark", resultInfo); baseDao.add(fseChangHistory); } } } /** * 解析bean * @param serviceBeanName beanName * @param seriveMethodNmae methodNmae * @param methodParam 传入参数 * @param fseNoExecute 业务数据 * @return * @throws InvocationTargetException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws Exception */ public boolean serviceBean(String serviceBeanName, String seriveMethodNmae, String methodParam,FieldSetEntity fseNoExecute) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { boolean succ=true; Object bbean=SpringBeanUtil.getBean(serviceBeanName); SpringMVCContextHolder.getSystemLogger().error("1找到业务逻辑处理bean1:"+bbean); Object bean = applicationContext.getBean(serviceBeanName); SpringMVCContextHolder.getSystemLogger().error("找到业务逻辑处理bean:"+bean); // List parameterList = new ArrayList<>(); // String[] fields = methodParam.split(","); // int fieldLength = fields.length; // for (int i = 0; i < fieldLength; i++) { // parameterList.add(fseNoExecute.getString(fields[i])); // } // 判断执行方法 // Method method = getMethod(bean, seriveMethodNmae, methodParam, fieldLength); Method method = getMethod(bean, seriveMethodNmae); if (method!=null) { SpringMVCContextHolder.getSystemLogger().error("找到业务逻辑处理方法:"+seriveMethodNmae); method.invoke(bean,fseNoExecute); SpringMVCContextHolder.getSystemLogger().error("业务逻辑处理完成"); }else { SpringMVCContextHolder.getSystemLogger().error("未找到spring bean 方法:"+seriveMethodNmae); succ=false; } return succ; } /** * 解析方法 * @param bean * @param methodName * @param parameterType * @param paramCOunt * @return * @throws Exception */ public Method getMethod(Object bean, String methodName) { Class class1 = bean.getClass(); Method[] methods = class1.getMethods(); for (int j = 0; j < methods.length; j++) { Method method = methods[j]; if (method.getName().equalsIgnoreCase(methodName) && method.getParameterTypes().length == 1) { Class parameterType = method.getParameterTypes()[0]; SpringMVCContextHolder.getSystemLogger().info("找到"+methodName+"的参数:"+parameterType.getName()); return method; } } return null; } /** * 解析方法 * @param bean * @param methodName * @param parameterType * @param paramCOunt * @return * @throws Exception */ public Method getMethod(Object bean, String methodName, String parameterType, int paramCOunt) { Class class1 = bean.getClass(); Method[] methods = class1.getMethods(); for (int j = 0; j < methods.length; j++) { Method method = methods[j]; if (method.getName().equalsIgnoreCase(methodName) && method.getParameterTypes().length == paramCOunt) { Class[] parameterTypes = method.getParameterTypes(); for (int i = 0; i < parameterTypes.length; i++) { Class parameterTypeioy = parameterTypes[i]; if (parameterTypeioy == null) { break; } if (parameterTypeioy instanceof Object) { continue; } break; } return method; } } return null; } /** * 员工转正 * @param fseNoExecute * @param staff_uuid * @throws * @return */ @Transactional public boolean changeRegular(FieldSetEntity fseNoExecute,String staff_uuid) throws BaseException,Exception{ baseDao.executeUpdate("update product_sys_staffs set staff_status=3 where uuid='"+staff_uuid+"'"); String sql = "update product_sys_staffs set staff_status=3 where uuid=?"; baseDao.executeUpdate(sql, new Object[] { staff_uuid }); fseNoExecute.setValue(CmnConst.RESULT_STATUS, 1); baseDao.update(fseNoExecute); return true; } /** * 员工离职 * 1.获取离职原因,变更员工状态 * 2.变更员工账户状态 * 3.变更员工下级员工上级领导 * 4.变更执行状态 * @param fs * @return * @throws BaseException */ @Transactional public boolean changeDimission(FieldSetEntity fs) throws BaseException,Exception { String staff_uuid = fs.getString(CmnConst.APP_STAFF_UUID); // 离职申请人uuid String handover_staff_uuid = fs.getString(CmnConst.HANDOVER_STAFF_UUID); // 交接人uuid // 获取离职申请人信息 FieldSetEntity staffsFS = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_STAFFS, staff_uuid, false); if (staffsFS==null) { throw new BaseException(SystemCode.SYSTEM_DIMISSION_FAIL.getValue(),SystemCode.SYSTEM_DIMISSION_FAIL.getText(), this.getClass(), "changeDimission"); }else { baseDao.listInternationDataTable(staffsFS, null); } //判断交接人是否为空 if(!StringUtils.isEmpty(handover_staff_uuid)) { // 获取交接人信息 FieldSetEntity handover_staffFS = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_STAFFS, handover_staff_uuid, false); if (handover_staffFS==null) { throw new BaseException(SystemCode.SYSTEM_DIMISSION_FAIL.getValue(),SystemCode.SYSTEM_DIMISSION_FAIL.getText(), this.getClass(), "changeDimission"); } //获取离职申请人编码 String tricode = staffsFS.getString(CmnConst.TRICODE); String handOverTricode=handover_staffFS.getString(CmnConst.TRICODE); boolean update2=baseDao.executeUpdate("update product_sys_staffs set direct_leader_code=? where direct_leader_code=?", new Object[] {handOverTricode,tricode}); if (!update2) { throw new BaseException(SystemCode.SYSTEM_DIMISSION_FAIL.getValue(),SystemCode.SYSTEM_DIMISSION_FAIL.getText(), this.getClass(), "changeDimission"); } } //获取离职原因 String dimission_reason = fs.getString("dimission_reason"); if (CmnConst.RESIGN_REASON_RETIREMENT.equals(dimission_reason)) { staffsFS.setValue("staff_status", 7); } else { staffsFS.setValue("staff_status", 6); } //修改员工状态 boolean update = baseDao.update(staffsFS); if (!update) { throw new BaseException(SystemCode.SYSTEM_DIMISSION_FAIL.getValue(),SystemCode.SYSTEM_DIMISSION_FAIL.getText(), this.getClass(), "changeDimission"); } //获取对应user登录信息 String user_id = staffsFS.getString("user_id"); FieldSetEntity userFS = baseDao.getFieldSetEntityByFilter("product_sys_users", "user_id=?",new String[] { user_id }, false); if (userFS == null) { throw new BaseException(SystemCode.SYSTEM_DIMISSION_FAIL.getValue(),SystemCode.SYSTEM_DIMISSION_FAIL.getText(), this.getClass(), "changeDimission"); } //禁用登录账户 userFS.setValue("status", 0); boolean updateUser = baseDao.update(userFS); if (!updateUser) { throw new BaseException(SystemCode.SYSTEM_DIMISSION_FAIL.getValue(),SystemCode.SYSTEM_DIMISSION_FAIL.getText(), this.getClass(), "changeDimission"); } //修改定时任务执行状态 fs.setValue(CmnConst.RESULT_STATUS, "1"); boolean update3 = baseDao.update(fs); if (!update3) { throw new BaseException(SystemCode.SYSTEM_DIMISSION_FAIL.getValue(),SystemCode.SYSTEM_DIMISSION_FAIL.getText(), this.getClass(), "changeDimission"); } return update3; } /** * 员工变更 * 1.变更员工信息 * 2.变更执行状态 * @param fseStaffChange * @return * @throws BaseException */ @Transactional public boolean staffChange(FieldSetEntity fseStaffChange) throws BaseException,Exception { boolean flag = true; //查询变更员工信息 FieldSetEntity fseStaff = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_STAFFS, fseStaffChange.getString(CmnConst.STAFF_UUID), false); if (fseStaff==null) { throw new BaseException(SystemCode.SYSTEM_STAFF_INFO_NO_EXIST.getValue(), SystemCode.SYSTEM_STAFF_INFO_NO_EXIST.getText()); }else { baseDao.listInternationDataTable(fseStaff, null); } //公司是否变化 if (!fseStaffChange.getString("original_org_level_uuid").equals(fseStaffChange.getString("transferred_org_level_uuid"))) { fseStaff.setValue(CmnConst.ORG_LEVEL_UUID, fseStaffChange.getString("transferred_org_level_uuid")); } //部门是否变化 if (!fseStaffChange.getString("original_staff_dept_uuid").equals(fseStaffChange.getString(CmnConst.TRANSFERRED_STAFF_DEPT_UUID))) { fseStaff.setValue("dept_uuid", fseStaffChange.getString(CmnConst.TRANSFERRED_STAFF_DEPT_UUID)); //获取新部门信息 FieldSetEntity fseOrgLevel = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS,fseStaffChange.getString(CmnConst.TRANSFERRED_STAFF_DEPT_UUID), false); if (fseOrgLevel==null) { throw new BaseException(SystemCode.SYSTEM_GET_DEPARTMENTINFO_FAIL.getValue(), SystemCode.SYSTEM_GET_DEPARTMENTINFO_FAIL.getText()); } String orgLevelCode = fseOrgLevel.getString(CmnConst.ORG_LEVEL_CODE); fseStaff.setValue(CmnConst.TRICODE, codeService.createCode(CmnConst.PRODUCT_SYS_STAFFS, CmnConst.TRICODE, orgLevelCode)); } //岗位是否变化 if (!fseStaffChange.getString("original_staff_post_uuid").equals(fseStaffChange.getString("transferred_staff_post_uuid"))) { fseStaff.setValue("job_post_uuid", fseStaffChange.getString("transferred_staff_post_uuid")); } //岗位等级是否变化 if (!fseStaffChange.getString("original_staff_grade_uuid").equals(fseStaffChange.getString("transferred_staff_grade_uuid"))) { fseStaff.setValue("job_post_grade_uuid", fseStaffChange.getString("transferred_staff_grade_uuid")); } //判断角色是否变化 if (!fseStaffChange.getString("original_role_uuid").equals(fseStaffChange.getString("transferred_role_uuid"))) { fseStaff.setValue("role_uuids", fseStaffChange.getString("transferred_role_uuid")); } //上级领导变化 String originLeaderUUID=fseStaffChange.getString("original_leader_uuid"); String newLeaderUUID=fseStaffChange.getString("transferred_leader_uuid"); //判断直属领导是否变化 // if (!fseStaffChange.getString("original_leader_uuid").equals(fseStaffChange.getString("transferred_leader_uuid"))) { // fseStaff.setValue(CmnConst.DIRECT_LEADER_CODE, fseStaffChange.getString(CmnConst.DIRECT_LEADER_CODE)); if (!originLeaderUUID.equals(newLeaderUUID)) { FieldSetEntity fseNewLeader=null; //新领导 String newLeaderCode=""; String originLeaderCode=fseStaff.getString(CmnConst.LEADER_TRICODE); if (!StringUtils.isEmpty(originLeaderUUID) && StringUtils.isEmpty(newLeaderUUID)) { //上级领导 有-->无 newLeaderCode=codeService.createCode(CmnConst.PRODUCT_SYS_STAFFS, CmnConst.LEADER_TRICODE, ""); } else { //上级领导 无-->有 或 旧-->新 fseNewLeader=baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_STAFFS, newLeaderUUID, false); newLeaderCode=codeService.createCode(CmnConst.PRODUCT_SYS_STAFFS, CmnConst.LEADER_TRICODE, fseNewLeader.getString(CmnConst.LEADER_TRICODE)); } fseStaff.setValue(CmnConst.LEADER_TRICODE, newLeaderCode); staffManagerService.changeSubLeaderTricode(originLeaderCode,newLeaderCode); } // } if (!StringUtils.isEmpty(fseStaffChange.getString("original_cost_center_uuid")) && !StringUtils.isEmpty(fseStaffChange.getString(CmnConst.COST_CENTER_UUID))) { fseStaff.setValue(CmnConst.COST_CENTER_UUID, fseStaffChange.getString(CmnConst.COST_CENTER_UUID)); baseDao.executeUpdate("update product_sys_staff_cost_center set cost_center_uuid=? where staff_uuid=?", new Object[] {fseStaffChange.getString(CmnConst.COST_CENTER_UUID),fseStaffChange.getString(CmnConst.STAFF_UUID)}); } //人员信息变更 flag = baseDao.update(fseStaff); if (!flag) { throw new BaseException(SystemCode.SYSTEM_STAFF_UPDATE_FIAL.getValue(), SystemCode.SYSTEM_STAFF_UPDATE_FIAL.getText()); } //设置人员变更数据状态 fseStaffChange.setValue(CmnConst.RESULT_STATUS, "1"); //变更数据状态 flag = baseDao.update(fseStaffChange); return flag; } /** * 部门变更 * @param fse * @return * @throws BaseException */ public void deptChange(long currentTime) throws BaseException { DataTableEntity dtNoExecute=baseDao.listTable(CmnConst.PRODUCT_SYS_ORG_LEVELS_CHANGE, "result_status=0 OR result_status=1"); if (dtNoExecute!=null && dtNoExecute.getRows()>0) { for (int i = 0; i < dtNoExecute.getRows(); i++) { FieldSetEntity fseNoExecute=dtNoExecute.getFieldSetEntity(i); int result_status = fseNoExecute.getInteger(CmnConst.RESULT_STATUS); String org_level_uuid = fseNoExecute.getString(CmnConst.ORG_LEVEL_UUID); Date excutedateTime = fseNoExecute.getDate("change_effective"); // 执行时间 int timeZone = getcompanyTImeZone(org_level_uuid); // 判断时间是否匹配 boolean isMatch = matchTime(currentTime, excutedateTime, timeZone, 4); if (isMatch) { String deptChangeType = fseNoExecute.getString("change_status_uuid"); if (result_status == 0) { try { ITimeTaskService service =(ITimeTaskService)getProxyInstance(timeTaskChangeServices); if ("1".equals(deptChangeType)) { isMatch = service.deptCancel(fseNoExecute); } else if ("2".equals(deptChangeType)) { isMatch = service.deptMerge(fseNoExecute); } else if ("3".equals(deptChangeType)) { isMatch = service.deptMove(fseNoExecute); } } catch (BaseException e) { SpringMVCContextHolder.getSystemLogger().error(e); isMatch=false; } catch (Exception e) { SpringMVCContextHolder.getSystemLogger().error(e); isMatch=false; } } if (isMatch) { DataPoolCacheImpl.getInstance().reFreshCacheDataByPermission(fseNoExecute.getString(CmnConst.ORG_LEVEL_UUID)); executeServiceChange(fseNoExecute, CmnConst.PRODUCT_SYS_ORG_LEVELS_CHANGE); } } } } } /** * 部门撤消 * @param fse * @return * @throws BaseException */ @Transactional public boolean deptCancel(FieldSetEntity fse) throws BaseException { String originDeptUuid=fse.getString(CmnConst.ORIGIN_DEPT_UUID); FieldSetEntity fseOriginDept=baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS, originDeptUuid, false); if (fseOriginDept==null) { throw new BaseException(SystemCode.SYSTEM_DEPT_CAHNGE_FIND_FAIL.getValue(), SystemCode.SYSTEM_DEPT_CAHNGE_FIND_FAIL.getText()); } String originDeptCode=fseOriginDept.getString(CmnConst.ORG_LEVEL_CODE); //获取所有相关部门信息 DataTableEntity dtDepts=baseDao.listTable(CmnConst.PRODUCT_SYS_ORG_LEVELS, "org_level_code like '"+originDeptCode+"%'", new Object[] {}); baseDao.listInternationDataTable(dtDepts, null); if (dtDepts!=null && dtDepts.getRows()>0) { Object [] uuids = dtDepts.getUuids(); List params = new ArrayList(); params.addAll(Arrays.asList(uuids)); //判断是否包含员工未移动 DataTableEntity dtstaff=baseDao.listTable(CmnConst.PRODUCT_SYS_STAFFS, BaseUtil.buildQuestionMarkFilter("dept_uuid", params.size(), true), params.toArray()); if (dtstaff!=null && dtstaff.getRows()>0) { return false; } //遍历相关部门 for (int i = 0; i < dtDepts.getRows(); i++) { //标记相关部门未撤消 FieldSetEntity fseDept=dtDepts.getFieldSetEntity(i); fseDept.setValue("org_level_status", 1); } baseDao.update(dtDepts); } fse.setValue(CmnConst.RESULT_STATUS, 1); return baseDao.update(fse); } /** * 部门合并 * 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.listInternationDataTable(baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS, originDeptUuid, false),null); //获取保留部门国际化信息 FieldSetEntity fseMergeDept = baseDao.listInternationDataTable(baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS, mergeDeptUuid, false),null); if (fseMergeDept == null || fseOriginDept==null) { throw new BaseException(SystemCode.SYSTEM_DEPT_MERGE_NO_EXIST.getValue(), SystemCode.SYSTEM_DEPT_MERGE_NO_EXIST.getText()); } //原部门国际化全称 Map originDeptNameLanguage = InternationalLanguage.parseLanguage(fseOriginDept.getSubDataTable(CmnConst.ORG_LEVEL_ALL)); //新部门国际化全称 Map mergeDeptParentAllLanguage = InternationalLanguage.parseLanguage(fseMergeDept.getSubDataTable(CmnConst.ORG_LEVEL_ALL)); //创建国际化语言变更对象 Map changeLanguage=InternationalLanguage.replacelanguage(originDeptNameLanguage, mergeDeptParentAllLanguage); 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 like'"+originDeptCode+"%'", new Object[]{}); if (dtDepts!=null && dtDepts.getRows()>0) { Object[] uuids = dtDepts.getUuids(); if (uuids != null) { // 变更所有下级组织机构国际化全称 for ( Map.Entry entries : originDeptNameLanguage.entrySet() ){ String key = entries.getKey(); String originAll = originDeptNameLanguage.get(key); String newAll = changeLanguage.get(key); List params = new ArrayList<>(); params.add(originAll); params.add(newAll); params.addAll(Arrays.asList(uuids)); params.add(key); String sql = "update product_sys_language_cont_values set field_value=replace(field_value,?,?) where table_name= 'product_sys_org_levels' and field_name='org_level_all' and " + BaseUtil.buildQuestionMarkFilter(CmnConst.DATA_UUID, uuids.length, true) + " and language_code=?"; baseDao.executeUpdate(sql, params.toArray()); } } } //map(变更部门uuid,新编码) or map("result","true or false"),变更合并部门所有子部门的组织机构编码,及员工编码 Map orgLevelCodeMap = new HashMap(); orgLevelCodeMap = changeDeptSub(originDeptCode, mergeDeptCode, orgLevelCodeMap); //判断修改是出现否错误 if (!(Boolean) orgLevelCodeMap.get(RESULT)) { throw new BaseException(SystemCode.SYSTEM_CODE_CREATE_FIAL.getValue(), SystemCode.SYSTEM_CODE_CREATE_FIAL.getText()); } //变更合并部门的岗位至保留部门 if (!originOrgUuid.equals(mergeOrgUuid)) { baseDao.executeUpdate("update product_sys_job_posts set org_level_uuid=? where dept_uuid=?", new Object[] {mergeOrgUuid,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=? where dept_uuid=?", new Object[] {mergeOrgUuid,originOrgUuid}); } baseDao.executeUpdate("update product_sys_staffs set dept_uuid=? where dept_uuid=?", new Object[] {mergeDeptUuid,originDeptUuid}); //变更合并部门的人员组织机构编码 orgLevelCodeMap.put(originDeptCode, mergeDeptCode); flag = changStaffCode(orgLevelCodeMap); //变更当前部门状态为被合并 if (flag == true) { fseOriginDept.setValue("org_level_status", 2); flag = baseDao.update(fseOriginDept); } fse.setValue(CmnConst.RESULT_STATUS, 1); return baseDao.update(fse); } /** * 部门迁移 * 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; } //原部门父级组织机构国际化全称 Map originDeptParentLanguage = InternationalLanguage.parseLanguage(fseOriginDeptParent.getSubDataTable(CmnConst.ORG_LEVEL_ALL)); //新迁入部门国际化全称 Map moveDeptParentAllLanguage = InternationalLanguage.parseLanguage(fseMoveDept.getSubDataTable(CmnConst.ORG_LEVEL_ALL)); //创建国际化语言变更对象 Map changeLanguage=InternationalLanguage.replacelanguage(originDeptParentLanguage, moveDeptParentAllLanguage); //获取所有后代部门信息 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) { //变更所有下级组织机构国际化全称 for ( Map.Entry entries : originDeptParentLanguage.entrySet() ){ String key = entries.getKey(); String originAll = originDeptParentLanguage.get(key); String newAll = changeLanguage.get(key); List params = new ArrayList(); params.add(originAll); params.add(newAll); params.addAll(Arrays.asList(uuids)); params.add(key); String sql = "update product_sys_language_cont_values set field_value=REPLACE(field_value,?,?) where table_name= 'product_sys_org_levels' and field_name='org_level_all' and " + BaseUtil.buildQuestionMarkFilter(CmnConst.DATA_UUID, uuids.length, true) + " and language_code=? "; baseDao.executeUpdate(sql, params.toArray()); } //判断迁移前后公司是否修改,如已修改,则变更员工所属公司,岗位所属公司 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); } /** * 更改子部门的部门编码和父级部门编码(部门合并) * @param originDeptCode 原部门编码 * @param newDeptCode 保留部门编码 * @param orgLevelCodeMap 相关部门集合 * @return * @throws BaseException */ @Transactional public Map changeDeptSub(String originDeptCode, String newDeptCode,Map orgLevelCodeMap) throws BaseException { //根据部门编码,查找相应子级部门 boolean flag = true; DataTableEntity dtOrgLevelSub = baseDao.listInternationDataTable(baseDao.listTable(CmnConst.PRODUCT_SYS_ORG_LEVELS, "org_level_code_parent=?",new Object[] { originDeptCode }),null); FieldSetEntity fseOrgLevelSub = null; if (dtOrgLevelSub.getRows() > 0) { for (int i = 0; i < dtOrgLevelSub.getRows(); i++) { fseOrgLevelSub = dtOrgLevelSub.getData().get(i); // 获取子部门合并前的编码 String originDeptSubCode = fseOrgLevelSub.getString(CmnConst.ORG_LEVEL_CODE); // 创建子部门新编码 String newDeptSubCode = codeService.createCode(CmnConst.PRODUCT_SYS_ORG_LEVELS, CmnConst.ORG_LEVEL_CODE, newDeptCode); fseOrgLevelSub.setValue(CmnConst.ORG_LEVEL_CODE, newDeptSubCode); fseOrgLevelSub.setValue("org_level_code_parent", newDeptCode); // 执行修改 flag = baseDao.update(fseOrgLevelSub); DataPoolCacheImpl.getInstance().reloadCodeManager(CmnConst.PRODUCT_SYS_ORG_LEVELS, CmnConst.ORG_LEVEL_CODE); if (!flag) { orgLevelCodeMap.put(RESULT, flag); return orgLevelCodeMap; } orgLevelCodeMap.put(originDeptSubCode, newDeptSubCode); // 递归调用,遍历下级子部门 orgLevelCodeMap = changeDeptSub(originDeptSubCode, newDeptSubCode, orgLevelCodeMap); } } orgLevelCodeMap.put(RESULT, flag); return orgLevelCodeMap; } /** * 更改人员编码(部门合并) * @return boolean * @throws BaseException */ @Transactional public boolean changStaffCode(Map orgLevelCodeMap) throws BaseException { boolean flag = true; for ( Map.Entry entries : orgLevelCodeMap.entrySet() ){ String key = entries.getKey(); if (RESULT.equals(key)) { String originDeptCode=key; //原部门编码 String newDeptCode=orgLevelCodeMap.get(key).toString(); //新部门编码 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,newDeptCode,originDeptCode,newDeptCode,originDeptCode,newDeptCode,originDeptCode,newDeptCode,originDeptCode,newDeptCode,originDeptCode,newDeptCode,originDeptCode+"%"}); } } return flag; } }