需求描述
泛微ecology9系统同步金智系统的组织结构信息。
实现方案
1、OA提供中间表,金智将变化的组织人员信息分别写入对应的表中。
2、OA通过计划任务扫描表中的数据,并写入OA系统接口中去。
计划任务类
package com.weavernorth.wisedu.hrmsyn.cron;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.weavernorth.wisedu.hrmsyn.cmd.Syn2OACmd;
import com.weavernorth.wisedu.hrmsyn.service.ReceiveDataServiceImpl;
import com.weavernorth.wisedu.hrmsyn.util.OutDataTransUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;
import weaver.interfaces.schedule.BaseCronJob;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @description: 定时同步金智组织结构
* @author: 菜鸟-瓜瓜
* @date: 2022-02-18 17:26
*/
public class CronSynOrg extends BaseCronJob {
private Logger log = LoggerFactory.getLogger(CronSynOrg.class);
@Override
public void execute() {
ReceiveDataServiceImpl service = new ReceiveDataServiceImpl();
log.info("分部同步开始"+TimeUtil.getOnlyCurrentTimeString());
//获取分部
service. getWiseduSubcompany();
log.info("分部同步结束"+TimeUtil.getOnlyCurrentTimeString());
log.info("部门同步开始"+TimeUtil.getOnlyCurrentTimeString());
//获取部门
service.getWiseduDepartment();
log.info("部门同步结束"+TimeUtil.getOnlyCurrentTimeString());
log.info("岗位同步开始"+TimeUtil.getOnlyCurrentTimeString());
//获取岗位
service.getWiseduJobtitle();
log.info("岗位同步结束"+TimeUtil.getOnlyCurrentTimeString());
log.info("人员同步开始"+TimeUtil.getOnlyCurrentTimeString());
//获取人员
service. getWiseduUser();
log.info("人员同步结束"+TimeUtil.getOnlyCurrentTimeString());
}
}
获取HR中的表数据,并调用接口写入
package com.weavernorth.wisedu.hrmsyn.service;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.weavernorth.wisedu.hrmsyn.cmd.Syn2OACmd;
import com.weavernorth.wisedu.hrmsyn.cron.CronSynOrg;
import com.weavernorth.wisedu.hrmsyn.util.OutDataTransUtil;
import com.weavernorth.wisedu.hrmsyn.util.ResultSynOrgUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @author 菜鸟-瓜瓜
* @version 1.0.0
* @ClassName ReceiveDataServiceImpl.java
* @Description 查询金智中间表的分部、部门、人员。同步到OA
* @createTime 2022年01月14日 13:56:00
*/
public class ReceiveDataServiceImpl extends BaseBean {
Logger log = LoggerFactory.getLogger(ReceiveDataServiceImpl.class);
private Syn2OACmd syn2OACmd = new Syn2OACmd();
/**
*获取HR分部信息
*/
public void getWiseduSubcompany() {
JSONArray result = new JSONArray();
RecordSet rs = new RecordSet();
String sql = "select code,shortname,fullname,parent_code,status from uf_mid_subcom order by code";
rs.executeQuery(sql);
JSONObject obj = new JSONObject();
JSONArray array = new JSONArray();
List list = new ArrayList();
log.info("同步分部数量"+rs.getCounts());
while (rs.next()) {
JSONObject item = new JSONObject();
String code = Util.null2String(rs.getString("code"));
//简称无值,改为取全称
String shortname = Util.null2String(rs.getString("shortname"));
String fullname = Util.null2String(rs.getString("fullname"));
String parent_code = Util.null2String(rs.getString("parent_code"));
if ("".equals(parent_code)) {
parent_code = "0";
}
//0或没有值正常 1冻结
String status = Util.null2String(rs.getString("status"));
if ("".equals(status)) {
status = "0";
}
//0表示封存 1 表示 解封
String canceled = "1";
if ("1".equals(status)) {
canceled = "0";
}
String action = getAction("subcompany", code, canceled);
item.putOpt("@action", action);
item.putOpt("code", code);
item.putOpt("shortname", shortname);
item.putOpt("fullname", fullname);
item.putOpt("parent_code", parent_code);
item.putOpt("canceled", canceled);
array.add(item);
Map map = JSON.parseObject(item.toString());
list.add(map);
}
List<List> newList = Lists.partition(list, 1000);
for (List listtmp : newList) {
JSONObject data = new JSONObject();
JSONObject rule = new JSONObject();
rule.putOpt("subcompnay", "subcompanycode");
data.putOpt("rule", rule);
data.putOnce("data", JSONUtil.parseArray(listtmp));
result.add(data);
syn2OACmd.synSubcompany(result);
}
}
/**
*获取HR部门信息
*/
public void getWiseduDepartment() {
JSONArray result = new JSONArray();
RecordSet rs = new RecordSet();
String sql = "select code,shortname,fullname,org_code,parent_code,status from uf_mid_dept order by code ";
rs.executeQuery(sql);
JSONObject obj = new JSONObject();
JSONArray array = new JSONArray();
List list = new ArrayList();
log.info("同步部门数量"+rs.getCounts());
while (rs.next()) {
JSONObject item = new JSONObject();
String code = Util.null2String(rs.getString("code"));
String shortname = Util.null2String(rs.getString("fullname"));
String fullname = Util.null2String(rs.getString("fullname"));
String org_code = Util.null2String(rs.getString("org_code"));
if("".equals(org_code)){
org_code = "0";
}
String parent_code = Util.null2String(rs.getString("parent_code"));
//0或没有值正常 1冻结
String status = Util.null2String(rs.getString("status"));
//0 表示封存,1 表示解封
String canceled = "1";
if ("".equals(status)) {
status = "0";
}
if (!"0".equals(status)) {
canceled = "0";
}
String action = getAction("department", code, canceled);
item.putOpt("@action", action);
item.putOpt("code", code);
item.putOpt("shortname", shortname);
item.putOpt("fullname", fullname);
item.putOpt("org_code", org_code);
item.putOpt("parent_code", parent_code);
item.putOpt("canceled", canceled);
array.add(item);
Map map = JSON.parseObject(item.toString());
list.add(map);
}
List<List> newList = Lists.partition(list, 1000);
for (List listtmp : newList) {
JSONObject data = new JSONObject();
JSONObject rule = new JSONObject();
rule.putOpt("subcompnay", "subcompanycode");
rule.putOpt("department", "departmentcode");
data.putOpt("rule", rule);
data.putOnce("data", JSONUtil.parseArray(listtmp));
result.add(data);
syn2OACmd.synDepartment(result);
}
}
/**
*获取HR岗位信息
*/
public void getWiseduJobtitle() {
JSONArray result = new JSONArray();
RecordSet rs = new RecordSet();
String sql = "select jobtitlecode,jobtitlename,jobtitleremark from uf_mid_job ";
rs.executeQuery(sql);
JSONObject obj = new JSONObject();
JSONArray array = new JSONArray();
List list = new ArrayList();
log.info("new同步岗位数量"+rs.getCounts());
while (rs.next()) {
JSONObject item = new JSONObject();
String jobtitlecode = Util.null2String(rs.getString("jobtitlecode"));
String jobtitlename = Util.null2String(rs.getString("jobtitlename"));
String jobtitleremark = Util.null2String(rs.getString("jobtitleremark"));
item.putOpt("@action", "add");
item.putOpt("jobtitlecode", jobtitlecode);
item.putOpt("jobtitlename", jobtitlename);
item.putOpt("jobtitleremark", jobtitleremark);
Map map = JSON.parseObject(item.toString());
list.add(map);
}
List<List> newList = Lists.partition(list, 1000);
for (List listtmp : newList) {
JSONObject data = new JSONObject();
JSONObject rule = new JSONObject();
rule.putOpt("jobtitle", "jobtitlecode");
data.putOpt("rule", rule);
data.putOnce("data", JSONUtil.parseArray(listtmp));
result.add(data);
syn2OACmd.synJobtile(result);
}
}
/**
*获取HR人员信息
*/
public void getWiseduUser() {
JSONArray result = new JSONArray();
RecordSet rs = new RecordSet();
String sql = "select workcode,subcompanycode,departmentcode,lastname,loginid,sex,jobtitlecode,status from " +
" uf_mid_hrm order by workcode";
rs.executeQuery(sql);
JSONObject obj = new JSONObject();
JSONArray array = new JSONArray();
List list = new ArrayList();
log.info("同步人员数量"+rs.getCounts());
while (rs.next()) {
JSONObject item = new JSONObject();
String workcode = Util.null2String(rs.getString("workcode"));
if ("".equals(workcode)) {
log.info("人员同步时,人员编号不能为空");
}
String subcompanycode = Util.null2String(rs.getString("subcompanycode"));
String departmentcode = Util.null2String(rs.getString("departmentcode"));
String lastname = Util.null2String(rs.getString("lastname"));
String loginid = Util.null2String(rs.getString("loginid"));
String sex = Util.null2String(rs.getString("sex"));
String jobtitlecode = Util.null2String(rs.getString("jobtitlecode"));
String status = Util.null2String(rs.getString("status"));
if("".equals(status)){
status= "正式";
}
String seclevel = OutDataTransUtil.getSeclevelByJobcode(jobtitlecode);
item.putOpt("workcode", workcode);
item.putOpt("lastname", lastname);
item.putOpt("loginid", loginid);
item.putOpt("status", status);
item.putOpt("subcompany", "{JSON}" + new JSONObject().putOpt("subcompanycode", subcompanycode));
item.putOpt("department", "{JSON}" + new JSONObject().putOpt("departmentcode", departmentcode));
String jobtitlename = getJobtileNameByCode(jobtitlecode);
//岗位名称
item.putOpt("jobtitle", jobtitlename);
//职务
item.putOpt("jobactivityid", jobtitlename);
//职务类型
item.putOpt("jobgroupid", jobtitlename);
item.putOpt("sex", sex);
// item.putOpt("managerid", managerid);
// item.putOpt("certificatenum", certificatenum);
// item.putOpt("mobile", mobile);
// item.putOpt("birthday", birthday);
// item.putOpt("nativeplace", nativeplace);
// item.putOpt("residentplace", residentplace);
// userobj.putOpt("companystartdate", hrmObj.getStr("companystartdate"));
item.putOpt("lastmoddate", TimeUtil.getCurrentDateString());
item.putOpt("seclevel", seclevel);
item.putOpt("locationid", "秦皇岛");
array.add(item);
Map map = JSON.parseObject(item.toString());
list.add(map);
}
List<List> newList = Lists.partition(list, 1000);
for (List listtmp : newList) {
JSONObject data = new JSONObject();
JSONObject rule = new JSONObject();
rule.putOpt("resource", "workcode");
rule.putOpt("subcompnay", "subcompanycode");
rule.putOpt("department", "departmentcode");
data.putOpt("rule", rule);
data.putOnce("data", JSONUtil.parseArray(listtmp));
result.add(data);
syn2OACmd.synUser(result);
}
}
/**
* 获取Action接口动作
* @param type
* @param code
* @param canceled
* @return
*/
private String getAction(String type, String code, String canceled) {
log.info("获取action动作入参: type=" + type + " code=" + code + " canceled=" + canceled);
String action = "add";
String tablename = "";
String codeFieldName = "";
if ("subcompany".equals(type)) {
tablename = "hrmsubcompany";
codeFieldName = "subcompanycode";
} else if ("department".equals(type)) {
tablename = "HrmDepartment";
codeFieldName = "departmentcode";
}
RecordSet rs = new RecordSet();
String sql = "select canceled from " + tablename + " where " + codeFieldName + "=?";
rs.executeQuery(sql, code);
if (rs.next()) {
String sysCanceled = Util.null2String(rs.getString("canceled"));
if ("".equals(sysCanceled)) {
sysCanceled = "0";
}
log.info("获取action动作入参: sysCanceled=" + sysCanceled);
//系统有该组织,且解封封存状态一致,那么本次更新操作
if (canceled.equals(sysCanceled)) {
action = "delete";
}
//不相同,则处理状态为delete
else {
action = "edit";
}
} else {
action = "add";
}
log.info("获取action动作出参: action=" + action);
return action;
}
/**
* 根据code获取岗位名称
*
* @param jobtilecode
* @return
*/
private String getJobtileNameByCode(String jobtilecode) {
String jobtitlemark = "";
RecordSet rs = new RecordSet();
String sql = "select jobtitlemark from hrmjobtitles where jobtitlecode =?";
rs.executeQuery(sql, jobtilecode);
if (rs.next()) {
jobtitlemark = rs.getString("jobtitlemark");
}
return jobtitlemark;
}
}
写入数据的具体实现
package com.weavernorth.wisedu.hrmsyn.cmd;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import weaver.general.BaseBean;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;
/**
* @author
* @version 1.0.0
* @ClassName Syn2OAservice.java
* @Description 按照组织结构同步接口要求,封装调用系统接口的方法
* @createTime 2021年12月31日 11:05:00
*/
public class Syn2OACmd extends BaseBean {
private Logger log = LoggerFactory.getLogger(Syn2OACmd.class);
/**
* 同步分部信息
*
* @param subarr
* @param listSubcompanycode
* @return
*/
public void synSubcompany(JSONArray subarr) {
if (subarr != null) {
if (subarr.size() > 0) {
for (int i = 0; i < subarr.size(); i++) {
log.info("OA内部分部同步参数=" + (JSONObject) subarr.get(i));
//同步分部
String resp = null;
try {
resp = post("/api/hrm/resful/synSubcompany", ((JSONObject) subarr.get(i)).toString());
} catch (Exception e) {
log.error("分部同步异常",e);
}
log.info("OA内部分部同步结果=" + resp);
}
}
}
}
/**
* 同步部门信息
*
* @param deptarr
* @param listDepartmentcode
* @return
*/
public void synDepartment(JSONArray deptarr) {
if (deptarr != null) {
if (deptarr.size() > 0) {
for (int i = 0; i < deptarr.size(); i++) {
//同步部门
log.info("OA内部部门同步参数=" + (JSONObject) deptarr.get(i));
String resp = null;
try {
resp = post("/api/hrm/resful/synDepartment", ((JSONObject) deptarr.get(i)).toString());
} catch (Exception e) {
log.error("部门同步异常",e);
}
log.info("OA内部部门同步结果=" + resp);
}
}
}
}
/**
* 同步岗位的方法
*
* @param jobtitleArr
* @param listJobtitlecode
* @return
*/
public void synJobtile(JSONArray jobtitleArr) {
if (jobtitleArr != null) {
if (jobtitleArr.size() > 0) {
for (int i = 0; i < jobtitleArr.size(); i++) {
//同步部门
log.info("OA内部岗位参数=" + (JSONObject) jobtitleArr.get(i));
String resp = null;
try {
resp = post("/api/hrm/resful/synJobtitle", ((JSONObject) jobtitleArr.get(i)).toString());
} catch (Exception e) {
log.error("岗位同步异常",e);
}
log.info("OA内部岗位同步结果=" + resp);
}
}
}
}
/**
* 同步用户信息
*
* @param userarr
* @param listWorkcode
* @return
*/
public void synUser(JSONArray userarr) {
if (userarr != null) {
if (userarr.size() > 0) {
for (int i = 0; i < userarr.size(); i++) {
//同步人员
log.info("OA内部人员同步参数=" + (JSONObject) userarr.get(i));
String resp = null;
try {
resp = post("/api/hrm/resful/synHrmresource", ((JSONObject) userarr.get(i)).toString());
} catch (Exception e) {
log.error("人员同步异常",e);
}
log.info("OA内部人员同步结果=" + resp);
}
}
}
}
private String post(String serverPath, String params) throws Exception {
String address = getPropValue("wn_orgsyn", "host");
String body = HttpRequest.post(address + serverPath)
.header("Content-Type", "application/json;charset=UTF-8")
.body(params)
.timeout(60 * 60 * 1000)//超时,毫秒
.execute()
.body();
return body;
}
}