定时同步待办到旗舰版任务
This commit is contained in:
parent
27b6562053
commit
8fea59e694
|
|
@ -0,0 +1,353 @@
|
|||
package nc.impl.baseapp.ds;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.google.gson.Gson;
|
||||
import nc.api.arap.resource.IgnoreSslUtil;
|
||||
import nc.bs.dao.DAOException;
|
||||
import nc.bs.logging.Logger;
|
||||
import nc.bs.trade.business.HYSuperDMO;
|
||||
import nc.vo.bd.defdoc.DefdocVO;
|
||||
import nc.vo.pub.BusinessException;
|
||||
import nccloud.commons.lang.StringUtils;
|
||||
import nccloud.message.vo.NCCApproveMessageVO;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.*;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
public class DSMessageService {
|
||||
private HYSuperDMO superDMO = null;
|
||||
private String appKey = "";
|
||||
private String appSecret = "";
|
||||
private String baseUrl = "";
|
||||
private String tokenUrl = "/iuap-api-auth/open-auth/selfAppAuth/getAccessToken";
|
||||
private String toBipUrl = "";
|
||||
|
||||
public HYSuperDMO getSuperDMO() {
|
||||
if (superDMO == null) {
|
||||
superDMO = new HYSuperDMO();
|
||||
}
|
||||
return superDMO;
|
||||
}
|
||||
|
||||
|
||||
public void updateMessage(String update, String donePK) throws IOException, NoSuchAlgorithmException, InvalidKeyException, BusinessException {
|
||||
Map<String, String> bipParamMap = checkBipParam("BIP-sq");
|
||||
if (bipParamMap.isEmpty()) {
|
||||
Logger.error("SyncSrmDSMessageService-bipParamMap is empty");
|
||||
return;
|
||||
}
|
||||
|
||||
JSONObject reqData=new JSONObject();
|
||||
// reqData.put("donePK", donePK);
|
||||
Date date=new Date();
|
||||
Random rand = new Random();
|
||||
reqData.put("srcMsgId", "YS:"+date+update);
|
||||
reqData.put("businessKey", "YS_"+donePK);
|
||||
reqData.put("appId", bipParamMap.get("appid"));
|
||||
if (bipParamMap.isEmpty()) {
|
||||
Logger.error("SyncSrmDSMessageService-bipParamMap is empty");
|
||||
return;
|
||||
}
|
||||
baseUrl = bipParamMap.get("baseUrl");
|
||||
appKey = bipParamMap.get("srmappkey");
|
||||
appSecret = bipParamMap.get("appSecret");
|
||||
toBipUrl = bipParamMap.get("ipuPuReqQuery");
|
||||
String accessToken= getAccessToken(baseUrl,bipParamMap);
|
||||
if (accessToken != "") {
|
||||
Map<String, String> tokenParam = new HashMap<>();
|
||||
tokenParam.put("access_token", accessToken);
|
||||
Map<String, String> headers = new HashMap<String, String>();
|
||||
headers.put("Content-Type", "application/json");
|
||||
|
||||
|
||||
Logger.error("SyncDSMessageService-param = " + reqData.toString());
|
||||
String url = baseUrl + "/iuap-api-gateway/yonbip/uspace/rest/openapi/idempotent/todo/push/done";
|
||||
// String resultString = doPost(url,reqData );
|
||||
|
||||
String resultString = doSendHttp(url, "POST", tokenParam, "", headers,reqData.toString()
|
||||
);
|
||||
Gson gson = new Gson();
|
||||
Logger.error("SyncDSMessageService-res = " + resultString);
|
||||
Map updateMap = gson.fromJson(resultString, Map.class);
|
||||
if ("200".equals(updateMap.get("code"))) {// 保存更新成功后需要更新日志表
|
||||
// getSuperDMO().updateArray(saleOrderHVOList.toArray(new SaleOrderHVO[0]));
|
||||
} else {
|
||||
throw new BusinessException("【"+updateMap.get("message")+"】");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
public static String doSendHttp(String baseUrl, String method, Map<String, String> paramMap, String mediaType,
|
||||
Map<String, String> headers, String json) {
|
||||
HttpURLConnection urlConnection = null;
|
||||
InputStream in = null;
|
||||
OutputStream out = null;
|
||||
BufferedReader bufferedReader = null;
|
||||
String result = null;
|
||||
try {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append(baseUrl);
|
||||
if (paramMap != null) {
|
||||
sb.append("?");
|
||||
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
String value = entry.getValue();
|
||||
sb.append(key + "=" + value).append("&");
|
||||
baseUrl = sb.toString().substring(0, sb.toString().length() - 1);
|
||||
}
|
||||
}
|
||||
URL urlobj = new URL(baseUrl);
|
||||
if ("https".equalsIgnoreCase(urlobj.getProtocol())) {// 判定网址是否信任,不信任则调用忽略信任工具类SslUtil
|
||||
IgnoreSslUtil.ignoreSsl();
|
||||
}
|
||||
urlConnection = (HttpURLConnection) urlobj.openConnection();
|
||||
urlConnection.setConnectTimeout(50000);
|
||||
urlConnection.setRequestMethod(method);
|
||||
urlConnection.setDoInput(true);
|
||||
urlConnection.setUseCaches(false);
|
||||
// 如果设置了自定义头,则打印它们
|
||||
if (headers != null && !headers.isEmpty()) {
|
||||
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
||||
urlConnection.addRequestProperty(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
if (json != null && json.length() > 0) {
|
||||
urlConnection.setDoInput(true);
|
||||
urlConnection.setDoOutput(true);
|
||||
out = urlConnection.getOutputStream();
|
||||
out.write(json.getBytes("utf-8"));
|
||||
out.flush();
|
||||
}
|
||||
int resCode = urlConnection.getResponseCode();
|
||||
String ecod = urlConnection.getContentEncoding();
|
||||
if (resCode == HttpURLConnection.HTTP_OK || resCode == HttpURLConnection.HTTP_CREATED
|
||||
|| resCode == HttpURLConnection.HTTP_ACCEPTED) {
|
||||
if (!nc.vo.cmp.util.StringUtils.isEmpty(ecod) && ecod.equals("gzip")) {
|
||||
in = new GZIPInputStream(urlConnection.getInputStream());
|
||||
} else {
|
||||
in = urlConnection.getInputStream();
|
||||
}
|
||||
} else {
|
||||
in = urlConnection.getErrorStream();
|
||||
}
|
||||
bufferedReader = new BufferedReader(new InputStreamReader(in, "utf-8"));
|
||||
StringBuffer temp = new StringBuffer();
|
||||
String line = bufferedReader.readLine();
|
||||
while (line != null) {
|
||||
temp.append(line).append("\r\n");
|
||||
line = bufferedReader.readLine();
|
||||
}
|
||||
if (ecod == null || ecod.equals("gzip")) {
|
||||
ecod = Charset.forName("utf-8").name();
|
||||
}
|
||||
result = new String(temp.toString().getBytes("utf-8"), ecod);
|
||||
} catch (Exception e) {
|
||||
JSONObject js = new JSONObject();
|
||||
js.put("", -1);
|
||||
js.put("message", "调用外系统接口失败:" + e.getMessage());
|
||||
result = js.toString();
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (null != bufferedReader) {
|
||||
try {
|
||||
bufferedReader.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (null != out) {
|
||||
try {
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (null != in) {
|
||||
try {
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
urlConnection.disconnect();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* 检查bip参数是否完整
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private Map<String, String> checkBipParam(String code) {
|
||||
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
|
||||
String strWhere = " pk_defdoclist in (select pk_defdoclist from bd_defdoclist where code='" + code + "' and dr=0 ) and dr=0";
|
||||
|
||||
try {
|
||||
DefdocVO[] defdocVOs = (DefdocVO[]) getSuperDMO().queryByWhereClause(DefdocVO.class, strWhere);
|
||||
if (defdocVOs != null && defdocVOs.length > 0) {
|
||||
for (DefdocVO defdocVO : defdocVOs) {
|
||||
|
||||
map.put(defdocVO.getCode().trim(), defdocVO.getName());
|
||||
|
||||
}
|
||||
}
|
||||
} catch (DAOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return map;
|
||||
|
||||
}
|
||||
|
||||
private String doPost(String requestUrl, JSONObject json) throws IOException {
|
||||
URL u = new URL(requestUrl);
|
||||
try {
|
||||
if ("https".equalsIgnoreCase(u.getProtocol())) {// 判定网址是否信任,不信任则调用忽略信任工具类SslUtil
|
||||
nc.vo.so.m30.util.IgnoreSslUtil.ignoreSsl();
|
||||
}
|
||||
HttpsURLConnection connection = (HttpsURLConnection) u.openConnection();
|
||||
// 设置请求方法
|
||||
connection.setRequestMethod("POST");
|
||||
// 设置请求属性
|
||||
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
|
||||
// 发送POST请求必须设置如下两行
|
||||
connection.setDoOutput(true);
|
||||
connection.setDoInput(true);
|
||||
byte[] outputInBytes = json.toJSONString().getBytes(StandardCharsets.UTF_8);
|
||||
// 写入数据到请求体
|
||||
OutputStream os = connection.getOutputStream();
|
||||
os.write(outputInBytes);
|
||||
// 获取响应码
|
||||
int responseCode = connection.getResponseCode();
|
||||
// System.out.println("Response Code: " + responseCode);
|
||||
// 读取响应
|
||||
String response = "";
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
|
||||
br.readLine();
|
||||
// System.out.println("Response: " + response);
|
||||
}
|
||||
// 关闭连接
|
||||
connection.disconnect();
|
||||
return response;
|
||||
} catch (Exception e) {
|
||||
Logger.error("SyncSrmDSMessageService-doPost:" + e.getMessage(), e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private String getAccessToken(String baseUrl, Map<String, String> bipParamMap)
|
||||
throws NoSuchAlgorithmException, InvalidKeyException, IOException {
|
||||
String tokenUrl = baseUrl + "/iuap-api-auth/open-auth/selfAppAuth/getAccessToken";
|
||||
// String appKey = "f10c4bf17b1d4e1fb08eb82bf8540eab";
|
||||
String appKey = bipParamMap.get("appKey");
|
||||
|
||||
// String appSecret = "71dc2a58ca378c1a1143231a62e73e75a60e9236";
|
||||
String appSecret = bipParamMap.get("appSecret");
|
||||
String accessToken = "";
|
||||
Map<String, String> params = new HashMap<>();
|
||||
|
||||
params.put("appKey", appKey);
|
||||
String timestamp = String.valueOf(System.currentTimeMillis());
|
||||
params.put("timestamp", timestamp);
|
||||
// 计算签名
|
||||
Map<String, String> treeMap;
|
||||
if (params instanceof TreeMap) {
|
||||
treeMap = params;
|
||||
} else {
|
||||
treeMap = new TreeMap<>(params);
|
||||
}
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
for (Map.Entry<String, String> entry : treeMap.entrySet()) {
|
||||
stringBuilder.append(entry.getKey()).append(entry.getValue());
|
||||
}
|
||||
Mac mac = Mac.getInstance("HmacSHA256");
|
||||
|
||||
mac.init(new SecretKeySpec(appSecret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
|
||||
byte[] signData = mac.doFinal(stringBuilder.toString().getBytes(StandardCharsets.UTF_8));
|
||||
String base64String = Base64.getEncoder().encodeToString(signData);
|
||||
String signature = URLEncoder.encode(base64String, "UTF-8");
|
||||
params.put("signature", signature);
|
||||
|
||||
String responseString = doGet(tokenUrl, params);
|
||||
|
||||
Gson gson = new Gson();
|
||||
Map result = gson.fromJson(responseString, Map.class);
|
||||
if (StringUtils.equals("00000", result.get("code").toString())) {
|
||||
Map<String, Object> tokenInfo = (Map<String, Object>) result.get("data");
|
||||
accessToken = (String) tokenInfo.get("access_token");
|
||||
|
||||
}
|
||||
|
||||
return accessToken;
|
||||
}
|
||||
private static String doGet(String path, Map<String, String> params) throws IOException {
|
||||
HttpURLConnection conn = null;
|
||||
InputStream is = null;
|
||||
BufferedReader br = null;
|
||||
StringBuilder result = new StringBuilder();
|
||||
try {
|
||||
if (params != null) {
|
||||
String paramStr = "";
|
||||
for (String key : params.keySet()) {
|
||||
if (!paramStr.isEmpty()) {
|
||||
paramStr += '&';
|
||||
}
|
||||
paramStr += key + '=' + params.get(key);
|
||||
}
|
||||
|
||||
if (path.indexOf('?') > 0) {
|
||||
path += '&' + paramStr;
|
||||
} else {
|
||||
path += '?' + paramStr;
|
||||
}
|
||||
}
|
||||
// 创建远程url连接对象
|
||||
URL url = new URL(path);
|
||||
if ("https".equalsIgnoreCase(url.getProtocol())) {// 判定网址是否信任,不信任则调用忽略信任工具类SslUtil
|
||||
IgnoreSslUtil.ignoreSsl();
|
||||
}
|
||||
// 通过远程url连接对象打开一个连接,强转成HTTPURLConnection类
|
||||
conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
// 设置连接超时时间和读取超时时间
|
||||
conn.setConnectTimeout(120000);
|
||||
conn.setReadTimeout(120000);
|
||||
conn.setRequestProperty("Accept", "application/json");
|
||||
// 发送请求
|
||||
conn.connect();
|
||||
// 通过conn取得输入流,并使用Reader读取
|
||||
if (200 == conn.getResponseCode()) {
|
||||
is = conn.getInputStream();
|
||||
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
result.append(line);
|
||||
System.out.println(line);
|
||||
}
|
||||
} else {
|
||||
System.out.println("ResponseCode is an error code:" + conn.getResponseCode());
|
||||
}
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,108 @@
|
|||
package nc.impl.baseapp.task;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import nc.bs.dao.BaseDAO;
|
||||
import nc.bs.dao.DAOException;
|
||||
import nc.bs.logging.Log;
|
||||
import nc.bs.pub.pa.PreAlertObject;
|
||||
import nc.bs.pub.taskcenter.BgWorkingContext;
|
||||
import nc.bs.pub.taskcenter.IBackgroundWorkPlugin;
|
||||
import nc.bs.trade.business.HYSuperDMO;
|
||||
import nc.impl.baseapp.ds.DSMessageService;
|
||||
// import nc.itf.baseapp.ds.XxtzLogger;
|
||||
import nc.jdbc.framework.processor.MapListProcessor;
|
||||
import nc.vo.bd.defdoc.DefdocVO;
|
||||
import nc.vo.pub.BusinessException;
|
||||
import nc.vo.pub.lang.UFDateTime;
|
||||
import nccloud.message.vo.NCCApproveMessageVO;
|
||||
/**
|
||||
* 定时同步待办已办到先行通
|
||||
*/
|
||||
public class SyncTodoToXXTTask implements IBackgroundWorkPlugin{
|
||||
private static final String LOG_INFO_NAME = "dldzlog";
|
||||
private static final Log logger = Log.getInstance(LOG_INFO_NAME);
|
||||
private HYSuperDMO superDMO = null;
|
||||
private String appKey = "";
|
||||
private String appSecret = "";
|
||||
private String baseUrl = "";
|
||||
private String tokenUrl = "/iuap-api-auth/open-auth/selfAppAuth/getAccessToken";
|
||||
private String toBipUrl = "";
|
||||
private Integer num = 0;
|
||||
@Override
|
||||
public PreAlertObject executeTask(BgWorkingContext context) throws BusinessException {
|
||||
DSMessageService messageService=new DSMessageService();
|
||||
List<String> donePKs= queryDone();
|
||||
if(donePKs!=null&&!donePKs.isEmpty()) {
|
||||
for(String donePK:donePKs) {
|
||||
try {
|
||||
messageService.updateMessage(String.valueOf(num), donePK);
|
||||
num++;
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
num=0;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private List<String> queryDone() throws BusinessException{
|
||||
String query = "SELECT * FROM (\n" +
|
||||
" SELECT \n" +
|
||||
" a.pk_detail\n" +
|
||||
" FROM sm_msg_approve a\n" +
|
||||
" LEFT JOIN sm_msg_user \n" +
|
||||
" ON a.pk_message = sm_msg_user.pk_message\n" +
|
||||
" WHERE \n" +
|
||||
" sm_msg_user.isdelete = 'Y' \n" +
|
||||
" OR sm_msg_user.isread = 'Y'\n" +
|
||||
" ORDER BY a.sendtime DESC \n" +
|
||||
") WHERE ROWNUM <= 60";
|
||||
List<String> pks=new ArrayList<>();
|
||||
BaseDAO dao = new BaseDAO();
|
||||
List<Map<String,Object>> approveMessages = (List<Map<String,Object>>)dao.executeQuery(query, new MapListProcessor());
|
||||
if(approveMessages!=null&&!approveMessages.isEmpty()) {
|
||||
for(Map<String,Object> approveMessage:approveMessages) {
|
||||
String pk_detail = (String)approveMessage.get("pk_detail");
|
||||
pks.add(pk_detail);
|
||||
}
|
||||
}
|
||||
return pks;
|
||||
}
|
||||
/**
|
||||
* 检查bip参数是否完整
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private Map<String, String> checkBipParam(String code) {
|
||||
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
|
||||
String strWhere = " pk_defdoclist in (select pk_defdoclist from bd_defdoclist where code='" + code + "' and dr=0 ) and dr=0";
|
||||
|
||||
try {
|
||||
DefdocVO[] defdocVOs = (DefdocVO[]) getSuperDMO().queryByWhereClause(DefdocVO.class, strWhere);
|
||||
if (defdocVOs != null && defdocVOs.length > 0) {
|
||||
for (DefdocVO defdocVO : defdocVOs) {
|
||||
|
||||
map.put(defdocVO.getCode().trim(), defdocVO.getName());
|
||||
|
||||
}
|
||||
}
|
||||
} catch (DAOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return map;
|
||||
|
||||
}
|
||||
public HYSuperDMO getSuperDMO() {
|
||||
if (superDMO == null) {
|
||||
superDMO = new HYSuperDMO();
|
||||
}
|
||||
return superDMO;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue