283 lines
13 KiB
Java
283 lines
13 KiB
Java
package shkd.utils;
|
||
|
||
import com.alibaba.fastjson.JSONArray;
|
||
import com.alibaba.fastjson.JSONObject;
|
||
import kd.bos.dataentity.entity.DynamicObject;
|
||
import kd.bos.logging.Log;
|
||
import kd.bos.logging.LogFactory;
|
||
import kd.bos.util.HttpClientUtils;
|
||
import kd.bos.util.StringUtils;
|
||
|
||
import java.util.Date;
|
||
import java.util.HashMap;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
|
||
/**
|
||
* @author :weiyunlong
|
||
* @date :Created 2024/9/11 16:02
|
||
* @description:致远OA集成工具类
|
||
*/
|
||
public class OAUtils {
|
||
|
||
private static final Log logger = LogFactory.getLog(OAUtils.class);
|
||
private static final String oaUrl = System.getProperty("oaurl");//(测试环境外网地址)
|
||
// private static final String oaUrl = "http://172.31.254.240:9090";//本地(内网地址)
|
||
|
||
//致远服务前获取token接口
|
||
public static String getOaToken(String billNo){
|
||
|
||
//String url = System.getProperty("efms.infcompany.url.ip");
|
||
//logger.info("url");
|
||
|
||
//致远服务前获取token接口
|
||
// String userName = "ICSTest";
|
||
// String password = "8f72e360-268e-41ba-9886-af9a802e4de3";
|
||
|
||
String userName = "JDtest";
|
||
String password = "235c78d7-03a6-4b32-8632-7b75a002276b";
|
||
// String oaUrl = System.getProperty("oaurl");
|
||
// String oaUrl = "http://172.31.254.240:9090";
|
||
String linkUrl = oaUrl +"/seeyon/rest/token";
|
||
//接口请求体
|
||
JSONObject linkBody = new JSONObject();
|
||
linkBody.put("userName", userName);
|
||
linkBody.put("password", password);
|
||
String token = "";
|
||
|
||
try {
|
||
String linkPostjson = HttpClientUtils.postjson(linkUrl, null, linkBody.toJSONString());
|
||
logger.info("获取OaToken接口返回结果:\n{}", linkPostjson);
|
||
|
||
if (StringUtils.isNotEmpty(linkPostjson)) {
|
||
|
||
JSONObject jsonObject = JSONObject.parseObject(linkPostjson);
|
||
token = jsonObject.getString("id");
|
||
DobeDWUtils.saveLog(billNo,
|
||
"致远",linkBody.toJSONString(), linkPostjson,
|
||
true,"调用致远服务前获取token接口");
|
||
}
|
||
return token;
|
||
} catch (Exception e) {
|
||
logger.info(String.format("获取OaToken接口异常:%s", e.getMessage()));
|
||
DobeDWUtils.saveLog(billNo,
|
||
"致远",linkBody.toJSONString(), e.getMessage(),
|
||
false,"调用致远服务前获取token接口");
|
||
throw new RuntimeException(e);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* OA接口绑定用户
|
||
* 代办接口之前需要先(用户的绑定 你通过定时任务 增量推就行,或者你们系统有人员进来推一次,这个人绑定成功了 就不需要再绑定,除非人员工号有变动)
|
||
* 先调OA接口绑定用户(相当于校验金蝶用户的number在OA系统是否存在,不存则推送失败)
|
||
* @param approversLists 金蝶系统的审批人集合
|
||
* @param oaToken 致远OA获取的token
|
||
* @return
|
||
*/
|
||
public static String thirdpartyUser(List<DynamicObject> approversLists, String oaToken,String billNo){
|
||
|
||
// String oaUrl = System.getProperty("oaurl");
|
||
String thirdUrl = oaUrl + "/seeyon/rest/thirdpartyUserMapper/binding";
|
||
|
||
Map<String, String> customerHeader = new HashMap<>();
|
||
customerHeader.put("Content-Type", "application/json");
|
||
customerHeader.put("token", oaToken);
|
||
|
||
//组装请求体
|
||
JSONObject thirdBody = new JSONObject();
|
||
JSONArray thirdList = new JSONArray();
|
||
//接口不支持接收人字段为多人,但接口支持批量 so 多人审批,遍历审批人集合,调用OA调待办接口
|
||
for (DynamicObject approver : approversLists) {
|
||
JSONObject jsonObject = new JSONObject();
|
||
jsonObject.put("registerCode", "3004");//系统注册编码:3004
|
||
// jsonObject.put("thirdUserId",approver.getString("number"));//三方系统人员编码(通过人员编码匹配,需要和OA保持一致)
|
||
jsonObject.put("thirdUserId","jdtest");//todo 三方系统人员编码(通过人员编码匹配,需要和OA保持一致)
|
||
// jsonObject.put("thirdLoginName", approver.getString("name"));//三方系统人员登录名
|
||
jsonObject.put("thirdLoginName", "金蝶测试");//todo 三方系统人员登录名
|
||
// jsonObject.put("thirdName", approver.getString("name"));//三方系统人员姓名
|
||
jsonObject.put("thirdName", "金蝶测试");//todo 三方系统人员姓名
|
||
// jsonObject.put("thirdCode", approver.getString("number"));//三方系统人员编码
|
||
jsonObject.put("thirdCode","jdtest");//todo 三方系统人员编码
|
||
thirdList.add(jsonObject);
|
||
}
|
||
thirdBody.put("userlist", thirdList);
|
||
|
||
String thirdPostjson = null;
|
||
try {
|
||
// 发送POST请求并获取响应
|
||
thirdPostjson = HttpClientUtils.postjson(thirdUrl, customerHeader, thirdBody.toJSONString());
|
||
JSONObject jsonObject = JSONObject.parseObject(thirdPostjson);
|
||
|
||
// 检查JSON对象是否为空
|
||
if (jsonObject != null && !jsonObject.isEmpty()) {
|
||
Boolean result = jsonObject.getBoolean("success");
|
||
String logMessage = result ? "OA接口绑定用户成功!" : "OA接口绑定用户失败!";
|
||
logger.info(logMessage);
|
||
|
||
// 记录日志
|
||
DobeDWUtils.saveLog(billNo, "致远", thirdBody.toJSONString(), thirdPostjson, result, "调用致远用户绑定接口");
|
||
// 若绑定用户失败,抛出异常
|
||
if (!result) {
|
||
throw new RuntimeException("OA接口绑定用户失败" + thirdPostjson);
|
||
}
|
||
}
|
||
} catch (Exception e) {
|
||
// 记录异常信息
|
||
String errorMessage = String.format("接口异常:%s", e.getMessage());
|
||
logger.info(errorMessage);
|
||
DobeDWUtils.saveLog(billNo, "致远", thirdBody.toJSONString(), e.getMessage(), false, "调用致远用户绑定接口");
|
||
throw new RuntimeException(e);
|
||
}
|
||
return thirdPostjson;
|
||
|
||
}
|
||
|
||
/**
|
||
* 金蝶系统推送OA待办新增
|
||
* @param thirdPartyMap 流程参数
|
||
*/
|
||
public static void thirdParty(HashMap<String, Object> thirdPartyMap){
|
||
|
||
// String oaUrl = System.getProperty("oaurl");
|
||
String pendingsUrl = oaUrl + "/seeyon/rest/thirdpartyPending/receive/pendings";
|
||
|
||
Map<String, String> customerHeader = new HashMap<>();
|
||
customerHeader.put("Content-Type", "application/json");
|
||
customerHeader.put("token", String.valueOf(thirdPartyMap.get("oaToken")));
|
||
//组装请求体
|
||
JSONObject pendingsBody = new JSONObject();
|
||
JSONArray pendingList = new JSONArray();
|
||
//接口不支持接收人字段为多人,但接口支持批量 so 多人审批,遍历审批人集合,调用OA调待办接口
|
||
List<DynamicObject> approversLists = (List<DynamicObject>)thirdPartyMap.get("approversLists");
|
||
for (DynamicObject approver :approversLists ) {
|
||
JSONObject jsonObject = new JSONObject();
|
||
jsonObject.put("taskId", String.valueOf(thirdPartyMap.get("taskId")));//第三方待办主键(保证唯一)
|
||
jsonObject.put("registerCode", "3004");//系统注册编码
|
||
jsonObject.put("title", String.valueOf(thirdPartyMap.get("title")) );//待办标题
|
||
// jsonObject.put("thirdSenderId", startNumber);//第三方待办发起人主键
|
||
jsonObject.put("thirdSenderId", "jdtest");//todo 第三方待办发起人主键
|
||
// jsonObject.put("senderName", String.valueOf(thirdPartyMap.get("startName")) );//第三方待办发起人姓名
|
||
jsonObject.put("senderName", "金蝶测试" );//todo 第三方待办发起人姓名
|
||
// jsonObject.put("thirdReceiverId", approver.getString("number"));//第三方待办接收人主键(保证唯一)
|
||
jsonObject.put("thirdReceiverId", "jdtest");//todo 第三方待办接收人主键(保证唯一)
|
||
jsonObject.put("creationDate", String.valueOf(thirdPartyMap.get("createDateStr")));//待办创建时间(格式:yyyy-MM-dd HH:mm:ss)
|
||
jsonObject.put("state", "0");//状态:0:未办理;1:已办理
|
||
jsonObject.put("url", String.valueOf(thirdPartyMap.get("url")));//PC端穿透链接
|
||
jsonObject.put("h5url", "test");//移动端穿透链接
|
||
pendingList.add(jsonObject);
|
||
}
|
||
pendingsBody.put("pendingList", pendingList);
|
||
|
||
String pendingPostjson = null;
|
||
try {
|
||
// 发送POST请求并获取响应
|
||
pendingPostjson = HttpClientUtils.postjson(pendingsUrl, customerHeader, pendingsBody.toJSONString());
|
||
JSONObject jsonObject = JSONObject.parseObject(pendingPostjson);
|
||
|
||
// 检查JSON对象是否为空
|
||
if (jsonObject != null && !jsonObject.isEmpty()) {
|
||
Boolean result = jsonObject.getBoolean("success");
|
||
String logMessage = result ? "金蝶系统推送OA待办新增成功!" : "金蝶系统推送OA待办新增失败!";
|
||
|
||
// 记录日志
|
||
logger.info(logMessage);
|
||
DobeDWUtils.saveLog(String.valueOf(thirdPartyMap.get("billNo")),
|
||
"致远", pendingsBody.toJSONString(), pendingPostjson,
|
||
result, "调用推送OA待办新增接口");
|
||
}
|
||
|
||
logger.info("接口返回结果:\n{}", pendingPostjson);
|
||
|
||
} catch (Exception e) {
|
||
// 记录异常信息
|
||
String errorMessage = String.format("接口异常:%s", e.getMessage());
|
||
logger.info(errorMessage);
|
||
DobeDWUtils.saveLog(String.valueOf(thirdPartyMap.get("billNo")),
|
||
"致远", pendingsBody.toJSONString(), e.getMessage(),
|
||
false, "调用推送OA待办新增接口");
|
||
throw new RuntimeException(e);
|
||
}
|
||
|
||
}
|
||
|
||
/**
|
||
* OA待办变更
|
||
* @param updateStateMap 流程参数
|
||
*/
|
||
public static void updatePendingState (HashMap<String, Object> updateStateMap){
|
||
|
||
// String oaUrl = System.getProperty("oaurl");
|
||
String pendingsUrl = oaUrl + "/seeyon/rest/thirdpartyPending/updatePendingState";
|
||
|
||
Map<String, String> customerHeader = new HashMap<>();
|
||
customerHeader.put("Content-Type", "application/json");
|
||
customerHeader.put("token", String.valueOf(updateStateMap.get("oaToken")));
|
||
//组装请求体
|
||
JSONObject pendingsBody = new JSONObject();
|
||
pendingsBody.put("registerCode","3004");
|
||
pendingsBody.put("taskId",String.valueOf(updateStateMap.get("taskId")));
|
||
pendingsBody.put("state",String.valueOf(updateStateMap.get("state")));
|
||
pendingsBody.put("subState",String.valueOf(updateStateMap.get("subState")));
|
||
|
||
String pendingPostjson = null;
|
||
try {
|
||
// 发送POST请求并获取响应
|
||
pendingPostjson = HttpClientUtils.postjson(pendingsUrl, customerHeader, pendingsBody.toJSONString());
|
||
JSONObject jsonObject = JSONObject.parseObject(pendingPostjson);
|
||
|
||
// 检查JSON对象是否为空
|
||
if (jsonObject != null && !jsonObject.isEmpty()) {
|
||
Boolean result = jsonObject.getBoolean("success");
|
||
String logMessage = result ? "金蝶系统推送OA待办变更成功!" : "金蝶系统推送OA待办变更失败!";
|
||
|
||
// 记录日志
|
||
logger.info(logMessage);
|
||
DobeDWUtils.saveLog(String.valueOf(updateStateMap.get("billNo")),
|
||
"致远", pendingsBody.toJSONString(), pendingPostjson,
|
||
result, "调用推送OA待办变更接口");
|
||
}
|
||
|
||
logger.info("接口返回结果:\n{}", pendingPostjson);
|
||
|
||
} catch (Exception e) {
|
||
// 记录异常信息
|
||
String errorMessage = String.format("接口异常:%s", e.getMessage());
|
||
logger.info(errorMessage);
|
||
DobeDWUtils.saveLog(String.valueOf(updateStateMap.get("billNo")),
|
||
"致远", pendingsBody.toJSONString(), e.getMessage(),
|
||
false, "调用推送OA待办变更接口");
|
||
throw new RuntimeException(e);
|
||
}
|
||
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* model值转Long
|
||
*/
|
||
public static long l(Object value) {
|
||
if (value == null) {
|
||
return 0L;
|
||
} else if (value instanceof Long) {
|
||
return (Long)value;
|
||
} else if (value instanceof Number) {
|
||
return ((Number)value).longValue();
|
||
} else if (value instanceof Boolean) {
|
||
return ((Boolean)value).booleanValue() ? 1L : 0L;
|
||
} else if (value instanceof Date) {
|
||
return ((Date)value).getTime();
|
||
} else {
|
||
String s = value.toString().trim();
|
||
if (s.length() == 0) {
|
||
return 0L;
|
||
} else {
|
||
return Long.parseLong(s);
|
||
}
|
||
}
|
||
}
|
||
}
|