|
|
@@ -0,0 +1,702 @@
|
|
|
+package com.muzhi.baiqueling.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.aliyun.dingtalkworkflow_1_0.models.GetProcessInstanceResponse;
|
|
|
+import com.aliyun.dingtalkyida_1_0.models.CreateOrUpdateFormDataResponse;
|
|
|
+import com.aliyun.tea.TeaException;
|
|
|
+import com.aliyun.teautil.models.RuntimeOptions;
|
|
|
+import com.muzhi.baiqueling.common.EnvironmentVariables;
|
|
|
+import com.muzhi.baiqueling.common.R;
|
|
|
+import com.muzhi.baiqueling.service.AccessTokenService;
|
|
|
+import com.muzhi.baiqueling.service.DmsService;
|
|
|
+import com.muzhi.baiqueling.service.SapService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import okhttp3.*;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.MalformedURLException;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLConnection;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.nio.file.StandardCopyOption;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.LocalTime;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Author ZhangKan
|
|
|
+ * @Data 15:39
|
|
|
+ */
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class DmsServiceImpl implements DmsService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AccessTokenService accessTokenService;
|
|
|
+
|
|
|
+ public static com.aliyun.dingtalkworkflow_1_0.Client createClient() throws Exception {
|
|
|
+ com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
|
|
|
+ config.protocol = "https";
|
|
|
+ config.regionId = "central";
|
|
|
+ return new com.aliyun.dingtalkworkflow_1_0.Client(config);
|
|
|
+ }
|
|
|
+
|
|
|
+ //宜搭接口调用
|
|
|
+ public static com.aliyun.dingtalkyida_1_0.Client createClient_yida() throws Exception {
|
|
|
+ com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
|
|
|
+ config.protocol = "https";
|
|
|
+ config.regionId = "central";
|
|
|
+ return new com.aliyun.dingtalkyida_1_0.Client(config);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * DMS接口环境地址
|
|
|
+ * 测试环境:http://121.37.206.131:17000
|
|
|
+ * 正式环境:https://dms.pechoin.com
|
|
|
+ * */
|
|
|
+ private static String URL_test = "http://121.37.206.131:17000";
|
|
|
+ private static String URL = "https://dms.pechoin.com";
|
|
|
+
|
|
|
+ public void createDmsXSZB(String processInstanceId, String year,
|
|
|
+ String kehuId, String kehumingcheng, String htbh, String htmc,
|
|
|
+ String htksrq, String htjsrq, String htje,
|
|
|
+ String fenxiaoqudao, String pinpai, String htzt, String file){
|
|
|
+ String message = null;
|
|
|
+ String success = null;
|
|
|
+ JSONObject jsonObjectDMS = null;
|
|
|
+
|
|
|
+ String error_type = null;
|
|
|
+ String error_code = null;
|
|
|
+
|
|
|
+ htzt = "3";//TODO 正式环境该参数需切换成传文本
|
|
|
+
|
|
|
+ Map map1 = new HashMap();
|
|
|
+ map1.put("id", "");//主键
|
|
|
+ map1.put("contract_number", htbh);//合同编号
|
|
|
+ map1.put("contract_name", htmc);//合同名称
|
|
|
+ map1.put("begin_time", htksrq);//合同开始日期
|
|
|
+ map1.put("end_time", htjsrq);//合同结束日期
|
|
|
+ map1.put("total_amount", htje);//合同总金额
|
|
|
+ map1.put("channel", fenxiaoqudao);//渠道
|
|
|
+ map1.put("brand", pinpai);//品牌
|
|
|
+ map1.put("channel_code", kehuId);//经销商编号(智能合同:客户编号)
|
|
|
+ map1.put("status", htzt);//合同状态【执行中=0;已终止=1;诉讼中=2;履约完成=3;-->默认=3(按照智能合同归档完成状态进行推送)】
|
|
|
+ map1.put("text_data", "");//合同正文
|
|
|
+ map1.put("annex_data", file);//合同附件
|
|
|
+ map1.put("channel_name", kehumingcheng);//客户名称
|
|
|
+ map1.put("type", "2");//合同类型【默认=2(销售合同)】
|
|
|
+ map1.put("old_id", "");//原合同id
|
|
|
+
|
|
|
+ List listMX = new ArrayList();
|
|
|
+ listMX.add(map1);
|
|
|
+
|
|
|
+ //封装查询参数
|
|
|
+ Map mapAll = new HashMap();
|
|
|
+ mapAll.put("tn_contract_system_data_new_use", listMX);
|
|
|
+
|
|
|
+ String mapJsonString = JSON.toJSONString(mapAll);
|
|
|
+ System.out.println("封装DMS【销售合同】更新接口的参数:" + mapJsonString);
|
|
|
+
|
|
|
+ /**调用dms销售合同写入接口*/
|
|
|
+ OkHttpClient client = new OkHttpClient().newBuilder()
|
|
|
+ .build();
|
|
|
+ MediaType mediaType = MediaType.parse("application/json");
|
|
|
+ RequestBody body = RequestBody.create(mediaType, mapJsonString);
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(URL + "/api/teapi/dy-biz/1712024982718124130/1934778210931839075")
|
|
|
+ .method("POST", body)
|
|
|
+ .addHeader("token", accessTokenService.getAccessToken_dms())
|
|
|
+ .addHeader("Content-Type", "application/json")
|
|
|
+ .build();
|
|
|
+ try {
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ String bodyStr = response.body().string();
|
|
|
+ jsonObjectDMS = JSON.parseObject(bodyStr);
|
|
|
+ System.out.println("返回体body{}:" + jsonObjectDMS);
|
|
|
+
|
|
|
+ if (!jsonObjectDMS.isEmpty() && jsonObjectDMS != null) {
|
|
|
+ //{"error_type":500,"error_code":"入参的类型不正确,请检查参数!"}
|
|
|
+ try {
|
|
|
+ error_type = jsonObjectDMS.getString("error_type");
|
|
|
+ error_code = jsonObjectDMS.getString("error_code");
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ if (error_type != null){
|
|
|
+ log.info("推送dms【销售合同】接口返回的错误信息(error_code):" + error_type);
|
|
|
+ }else {
|
|
|
+ message = jsonObjectDMS.getJSONObject("resp_data").getJSONObject("result").getString("message");
|
|
|
+ success = jsonObjectDMS.getJSONObject("resp_data").getJSONObject("result").getString("status");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ System.out.println("推送dms【销售合同】接口返回的结果信息(message):" + message);
|
|
|
+ System.out.println("推送dms【销售合同】接口返回的成功与否(success):" + success);
|
|
|
+
|
|
|
+ if (success != null && success.equals("success")){
|
|
|
+ log.info("推送dms【销售合同】接口返回成功!");
|
|
|
+ /**调用更新宜搭日志记录表*/
|
|
|
+ createYiDaoLog("DMS","同步成功",message,null,null,null,processInstanceId,null,null,null, kehuId,
|
|
|
+ kehumingcheng, htksrq, htjsrq, htje, fenxiaoqudao, pinpai, htzt, null);
|
|
|
+ }else {
|
|
|
+ log.info("推送dms【销售合同】接口返回失败!");
|
|
|
+ /**调用更新宜搭日志记录表*/
|
|
|
+ createYiDaoLog("DMS","同步失败",message + "-->error_code:" + error_code,null,null,null,processInstanceId,null,null,null, kehuId,
|
|
|
+ kehumingcheng, htksrq, htjsrq, htje, fenxiaoqudao, pinpai, htzt,null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取钉钉合同审批单信息(销售审批),用于判断单据内容
|
|
|
+ public JSONObject getDingHetongXS(String slid){
|
|
|
+
|
|
|
+ JSONObject result = null;
|
|
|
+ try {
|
|
|
+ com.aliyun.dingtalkworkflow_1_0.Client client = createClient();
|
|
|
+ com.aliyun.dingtalkworkflow_1_0.models.GetProcessInstanceHeaders getProcessInstanceHeaders = new com.aliyun.dingtalkworkflow_1_0.models.GetProcessInstanceHeaders();
|
|
|
+ getProcessInstanceHeaders.xAcsDingtalkAccessToken = accessTokenService.getAccessToken();
|
|
|
+ com.aliyun.dingtalkworkflow_1_0.models.GetProcessInstanceRequest getProcessInstanceRequest = new com.aliyun.dingtalkworkflow_1_0.models.GetProcessInstanceRequest()
|
|
|
+ .setProcessInstanceId(slid);
|
|
|
+
|
|
|
+ GetProcessInstanceResponse processInstanceWithOptions = client.getProcessInstanceWithOptions(getProcessInstanceRequest, getProcessInstanceHeaders, new RuntimeOptions());
|
|
|
+ String s = JSON.toJSONString(processInstanceWithOptions.getBody());
|
|
|
+// System.out.println("审批实例详情:" + s);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(s);
|
|
|
+ result = jsonObject.getJSONObject("result");
|
|
|
+// formComponentValues = result.getJSONArray("formComponentValues");
|
|
|
+// System.out.println("审批实例详情,表单内容:" + formComponentValues);
|
|
|
+// if (formComponentValues.size() > 0 && formComponentValues != null && !formComponentValues.isEmpty()){
|
|
|
+// for (int i = 0; i < formComponentValues.size(); i++) {
|
|
|
+// String name = formComponentValues.getJSONObject(i).getString("name");
|
|
|
+// switch (name){
|
|
|
+// case "客户编码":
|
|
|
+// if (formComponentValues.getJSONObject(i).getString("value") != null){
|
|
|
+// String value = formComponentValues.getJSONObject(i).getString("value");
|
|
|
+// System.out.println("***《《《客户编码》》》***-->" + value);
|
|
|
+//
|
|
|
+// map.put("客户编码", value);
|
|
|
+// }
|
|
|
+// break;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+ } catch (TeaException err) {
|
|
|
+ if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
|
|
|
+ // err 中含有 code 和 message 属性,可帮助开发定位问题
|
|
|
+ }
|
|
|
+ } catch (Exception _err) {
|
|
|
+ TeaException err = new TeaException(_err.getMessage(), _err);
|
|
|
+ if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
|
|
|
+ // err 中含有 code 和 message 属性,可帮助开发定位问题
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ //创建DMS销售合同【通用】
|
|
|
+ public void createDmsXSHT(String processInstanceId, String year, JSONArray formComponentValues,
|
|
|
+ String htbh, String htmc, String htksrq, String htjsrq, String htje, String file){
|
|
|
+ log.info("【销售合同通用】钉钉审批单数据:" + formComponentValues);
|
|
|
+ log.info("传参解析中......");
|
|
|
+
|
|
|
+ //客户id
|
|
|
+ String kehuId = null;
|
|
|
+ //客户名称
|
|
|
+ String kehumingcheng = null;
|
|
|
+ //分销渠道
|
|
|
+ String fenxiaoqudao = null;
|
|
|
+ //品牌
|
|
|
+ String pinpai = null;
|
|
|
+
|
|
|
+ for (int i = 0; i < formComponentValues.size(); i++) {
|
|
|
+ String name = formComponentValues.getJSONObject(i).getString("name");
|
|
|
+ if (name != null){
|
|
|
+ switch (name){
|
|
|
+ case "分销渠道":
|
|
|
+ if (formComponentValues.getJSONObject(i).getString("value") != null){
|
|
|
+ fenxiaoqudao = formComponentValues.getJSONObject(i).getString("value");
|
|
|
+ System.out.println("***《《《分销渠道》》》***-->" + fenxiaoqudao);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "品牌":
|
|
|
+ if (formComponentValues.getJSONObject(i).getString("value") != null){
|
|
|
+ pinpai = formComponentValues.getJSONObject(i).getString("value");
|
|
|
+ System.out.println("***《《《品牌》》》***-->" + pinpai);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "客户编码":
|
|
|
+ if (formComponentValues.getJSONObject(i).getString("value") != null){
|
|
|
+ kehuId = formComponentValues.getJSONObject(i).getString("value");
|
|
|
+ System.out.println("***《《《客户编码》》》***-->" + kehuId);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "客户名称":
|
|
|
+ if (formComponentValues.getJSONObject(i).getString("value") != null){
|
|
|
+ kehumingcheng = formComponentValues.getJSONObject(i).getString("value");
|
|
|
+ System.out.println("***《《《客户名称》》》***-->" + kehumingcheng);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**调用DMS合同信息写入接口*/
|
|
|
+ createDmsXSZB(processInstanceId, year, kehuId, kehumingcheng, htbh, htmc, htksrq, htjsrq, htje, fenxiaoqudao, pinpai, "已归档", file);
|
|
|
+ }
|
|
|
+
|
|
|
+ //新增或更新宜搭日志记录表
|
|
|
+ public void createYiDaoLog(String type, String status, String logStr,
|
|
|
+ String contractId, String contractNo, String contractName,
|
|
|
+ String processInstanceId, String contractStatus, String amountType, String year, String kehuid,
|
|
|
+ String kehumingcheng, String htksrq, String htjsrq, String htje, String qudao, String pinpai, String htzt,
|
|
|
+ String fileNewUrl) {
|
|
|
+ /**将接收参数先同步创建至宜搭日志记录表*/
|
|
|
+ //新增或更新表单实例【宜搭】
|
|
|
+ Map map = new HashMap();
|
|
|
+ map.put("selectField_mdlip8xx", type);//目标系统(SAP、每刻报销、DMS)
|
|
|
+ map.put("selectField_madnfm71", status);//同步状态「参数已记录、同步成功、同步失败」
|
|
|
+ map.put("textareaField_madnfm73", logStr);//日志说明
|
|
|
+ map.put("textField_mdo6t6wo", kehuid);//客户编号
|
|
|
+
|
|
|
+ map.put("textField_mewmhr4x", kehumingcheng);//客户名称
|
|
|
+ map.put("textField_mewmhr4l", qudao);//渠道
|
|
|
+ map.put("textField_mewmhr4m", pinpai);//品牌
|
|
|
+
|
|
|
+ if (status.equals("参数已记录")){
|
|
|
+ map.put("textField_manvwy3j", processInstanceId);//钉钉审批数据实例ID
|
|
|
+ map.put("textField_mdlip8yf", contractId);//合同id(智能合同)
|
|
|
+ map.put("textField_mdlip8yg", contractNo);//合同编号(钉钉审批)
|
|
|
+ map.put("textField_mdlip8yh", contractName);//合同名称
|
|
|
+ map.put("textField_mdlip8yo", contractStatus);//合同状态
|
|
|
+ map.put("textField_mdlip8yp", amountType);//金额类型
|
|
|
+ map.put("textField_mdlip8yq", year);//合同所属年份
|
|
|
+
|
|
|
+ map.put("textField_mewmhr4d", htksrq);//合同开始日期
|
|
|
+ map.put("textField_mewmhr4e", htjsrq);//合同结束日期
|
|
|
+ map.put("numberField_mewmhr4k", htje);//合同金额
|
|
|
+ map.put("numberField_mewmhr4t", htzt);//dms合同状态
|
|
|
+
|
|
|
+ map.put("textField_mewmhr4w", fileNewUrl);//【百雀羚服务器】文件地址
|
|
|
+ }
|
|
|
+
|
|
|
+ String mapJson = JSON.toJSONString(map);
|
|
|
+ System.out.println("mapJson:" + mapJson);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 宜搭表单判断查询条件
|
|
|
+ * 项目推送TB日志记录
|
|
|
+ * */
|
|
|
+ //宜搭表单判断查询条件
|
|
|
+ Map map1 = new HashMap();
|
|
|
+ //字段编号
|
|
|
+ map1.put("key", "textField_manvwy3j");
|
|
|
+ //同步类型
|
|
|
+ map1.put("value", processInstanceId);
|
|
|
+ //控件类型
|
|
|
+ map1.put("type", "TEXT");
|
|
|
+ //逻辑判断符
|
|
|
+ map1.put("operator", "eq");
|
|
|
+ //控件名称
|
|
|
+ map1.put("componentName", "TextField");
|
|
|
+
|
|
|
+ Map map2 = new HashMap();
|
|
|
+ //字段编号
|
|
|
+ map2.put("key", "selectField_mdlip8xx");
|
|
|
+ //同步类型
|
|
|
+ map2.put("value", type);
|
|
|
+ //控件类型
|
|
|
+ map2.put("type", "ARRAY");
|
|
|
+ //逻辑判断符
|
|
|
+ map2.put("operator", "eq");
|
|
|
+ //控件名称
|
|
|
+ map2.put("componentName", "SelectField");
|
|
|
+
|
|
|
+ List list11 = new ArrayList();
|
|
|
+ list11.add(map1);
|
|
|
+ list11.add(map2);
|
|
|
+
|
|
|
+ String listJson = JSON.toJSONString(list11);
|
|
|
+ System.out.println("查询条件listJson:" + listJson);
|
|
|
+
|
|
|
+ JSONObject jsonObjectYD = null;
|
|
|
+ String slidFalg = null;
|
|
|
+ try {
|
|
|
+ com.aliyun.dingtalkyida_1_0.Client client11 = createClient_yida();
|
|
|
+ com.aliyun.dingtalkyida_1_0.models.CreateOrUpdateFormDataHeaders createOrUpdateFormDataHeaders = new com.aliyun.dingtalkyida_1_0.models.CreateOrUpdateFormDataHeaders();
|
|
|
+ createOrUpdateFormDataHeaders.xAcsDingtalkAccessToken = accessTokenService.getAccessToken();
|
|
|
+ com.aliyun.dingtalkyida_1_0.models.CreateOrUpdateFormDataRequest createOrUpdateFormDataRequest = new com.aliyun.dingtalkyida_1_0.models.CreateOrUpdateFormDataRequest()
|
|
|
+ //应用秘钥
|
|
|
+ .setSystemToken(EnvironmentVariables.require("YIDA_SYSTEM_TOKEN"))
|
|
|
+ //表单编码
|
|
|
+ .setFormUuid("FORM-349A5D1A8CBD4271BCEDBE94A60A7BCDOSXX")//云璞POC:FORM-64009DF27F5F479BBC82CD6C1768B5FAC0DY
|
|
|
+ .setUserId("yida_pub_account")
|
|
|
+ .setSearchCondition(listJson)
|
|
|
+ //应用编码
|
|
|
+ .setAppType("APP_TQ5O9UKY2WDJV3QQE1VO")//云璞POC:APP_WLGLCYJR57ZJ51VAE8VK
|
|
|
+ .setFormDataJson(mapJson);
|
|
|
+
|
|
|
+ CreateOrUpdateFormDataResponse orUpdateFormDataWithOptions = client11.createOrUpdateFormDataWithOptions(createOrUpdateFormDataRequest, createOrUpdateFormDataHeaders, new RuntimeOptions());
|
|
|
+ String s = JSON.toJSONString(orUpdateFormDataWithOptions.getBody());
|
|
|
+ jsonObjectYD = JSON.parseObject(s);
|
|
|
+ System.out.println("【创建或更新】表单object对象:" + jsonObjectYD);
|
|
|
+ if (jsonObjectYD != null && !jsonObjectYD.isEmpty()) {
|
|
|
+ //新增或更新的表单实例id
|
|
|
+ slidFalg = jsonObjectYD.getJSONArray("result").getString(0);
|
|
|
+ }
|
|
|
+ } catch (TeaException err) {
|
|
|
+ if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
|
|
|
+ // err 中含有 code 和 message 属性,可帮助开发定位问题
|
|
|
+ }
|
|
|
+ } catch (Exception _err) {
|
|
|
+ TeaException err = new TeaException(_err.getMessage(), _err);
|
|
|
+ if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
|
|
|
+ // err 中含有 code 和 message 属性,可帮助开发定位问题
|
|
|
+ }
|
|
|
+ }
|
|
|
+ System.out.println("新增或更新的表单实例id:" + slidFalg);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从指定URL下载文件到本地路径
|
|
|
+ */
|
|
|
+ public static File downloadFile(String urlPath, String downloadDir, String fileName) {
|
|
|
+ File file = null;
|
|
|
+ try {
|
|
|
+
|
|
|
+ URL url = new URL(urlPath);
|
|
|
+
|
|
|
+ URLConnection urlConnection = url.openConnection();
|
|
|
+
|
|
|
+ HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;// http的连接类
|
|
|
+
|
|
|
+ //String contentType = httpURLConnection.getContentType();//请求类型,可用来过滤请求,
|
|
|
+
|
|
|
+ httpURLConnection.setConnectTimeout(1000*5);//设置超时
|
|
|
+
|
|
|
+ httpURLConnection.setRequestMethod("GET");//设置请求方式,默认是GET
|
|
|
+
|
|
|
+ httpURLConnection.setRequestProperty("Charset", "UTF-8");// 设置字符编码
|
|
|
+
|
|
|
+
|
|
|
+ httpURLConnection.connect();// 打开连接
|
|
|
+
|
|
|
+ BufferedInputStream bin = new BufferedInputStream(httpURLConnection.getInputStream());
|
|
|
+
|
|
|
+ //TODO 修改成动态文件名称:当前审批单的文件名称
|
|
|
+// String fileName = "ceshi.pdf";// 指定文件名称
|
|
|
+
|
|
|
+ String path = "file" + File.separatorChar + downloadDir + File.separatorChar + fileName;// 指定存放位置,此处使用的是全局变量的文件名称
|
|
|
+ file = new File(path);
|
|
|
+ // 校验文件夹目录是否存在,不存在就创建一个目录
|
|
|
+ if (!file.getParentFile().exists()) {
|
|
|
+ file.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+ OutputStream out = new FileOutputStream(file);
|
|
|
+ int size = 0;
|
|
|
+
|
|
|
+ byte[] b = new byte[2048];
|
|
|
+ //把输入流的文件读取到字节数据b中,然后输出到指定目录的文件
|
|
|
+ while ((size = bin.read(b)) != -1) {
|
|
|
+ out.write(b, 0, size);
|
|
|
+ }
|
|
|
+ // 关闭资源
|
|
|
+ bin.close();
|
|
|
+ out.close();
|
|
|
+ System.out.println("文件下载成功!");
|
|
|
+ } catch (MalformedURLException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ } catch (IOException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ System.out.println("文件下载失败!");
|
|
|
+ return null;
|
|
|
+ } finally {
|
|
|
+ return file;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isBetween0And5() {
|
|
|
+ LocalTime now = LocalTime.now(); // 获取当前本地时间
|
|
|
+ LocalTime start = LocalTime.of(0, 0); // 00:00
|
|
|
+ LocalTime end = LocalTime.of(6, 0); // 06:00(不包含)
|
|
|
+
|
|
|
+ return !now.isBefore(start) && now.isBefore(end);
|
|
|
+ // 等价于:now >= 00:00 且 now < 06:00
|
|
|
+ }
|
|
|
+
|
|
|
+ //【启用】智能合同推送同步DMS销售合同
|
|
|
+ @Override
|
|
|
+ public R syncDmsXSHT(JSONObject obj){
|
|
|
+ log.info("接收传参「智能合同单据上的钉钉OA审批单实例id」:" + obj);
|
|
|
+
|
|
|
+ log.info("传参解析中......");
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断是否为每天的0点~5点,如果属于该天的该时段,就不执行该接口
|
|
|
+ * 原因:因为智能合同中,会在合同到期日的凌晨批量推送到期合同信息,避免执行该批数据,导致推送b2b异常
|
|
|
+ * */
|
|
|
+ if (isBetween0And5()) {
|
|
|
+ System.out.println("当前时间在 0:00 ~ 5:59 之间,不执行该接口推送!");
|
|
|
+ } else {
|
|
|
+ System.out.println("当前时间属于正常工作时段,正常执行该接口===");
|
|
|
+
|
|
|
+ //合同id(智能合同)
|
|
|
+ String contractId = null;
|
|
|
+ //合同编号(钉钉审批)
|
|
|
+ String contractNo = null;
|
|
|
+ //合同名称
|
|
|
+ String contractName = null;
|
|
|
+
|
|
|
+ //合同金额
|
|
|
+ String htje = "0";
|
|
|
+ //合同开始时间
|
|
|
+ String contractStartDateText = null;
|
|
|
+ //合同结束时间
|
|
|
+ String contractEndDateText = null;
|
|
|
+
|
|
|
+ //审批实例id
|
|
|
+ String processInstanceId = null;
|
|
|
+ //合同状态
|
|
|
+ String contractStatus = null;
|
|
|
+ //金额类型
|
|
|
+ String amountType = null;
|
|
|
+ //合同所属年份
|
|
|
+ String year = null;
|
|
|
+
|
|
|
+ //客户编号
|
|
|
+ String khbh = null;
|
|
|
+ //客户名称
|
|
|
+ String khmc = null;
|
|
|
+ //渠道
|
|
|
+ String qd = null;
|
|
|
+ //品牌
|
|
|
+ String pp = null;
|
|
|
+ //dms合同状态
|
|
|
+ String dmshtzt = "3";
|
|
|
+
|
|
|
+ //合同附件
|
|
|
+ String file = "";
|
|
|
+
|
|
|
+ //【百雀羚服务器】合同归档文件下载地址
|
|
|
+ String fileNewUrl = null;
|
|
|
+
|
|
|
+ //查询传参标记(手动同步、系统同步)
|
|
|
+ String flag = obj.getString("flag");
|
|
|
+
|
|
|
+ if (flag.equals("手动同步")){
|
|
|
+ processInstanceId = obj.getString("processInstanceId");
|
|
|
+ System.out.println("钉钉审批实例id:" + processInstanceId);
|
|
|
+ contractStatus = obj.getString("contractStatus");
|
|
|
+ System.out.println("智能合同状态:" + contractStatus);
|
|
|
+ amountType = obj.getString("amountType");
|
|
|
+ System.out.println("智能合同类型:" + amountType);
|
|
|
+ year = obj.getString("year");
|
|
|
+ System.out.println("合同所属年份:" + year);
|
|
|
+
|
|
|
+ khbh = obj.getString("khbh");
|
|
|
+ System.out.println("客户编号:" + khbh);
|
|
|
+ khmc = obj.getString("khmc");
|
|
|
+ System.out.println("客户名称:" + khmc);
|
|
|
+ contractNo = obj.getString("htbh");
|
|
|
+ System.out.println("钉钉合同编号:" + contractNo);
|
|
|
+ contractName = obj.getString("htmc");
|
|
|
+ System.out.println("钉钉合同名称:" + contractName);
|
|
|
+ contractStartDateText = obj.getString("htksrq");
|
|
|
+ System.out.println("合同开始日期:" + contractStartDateText);
|
|
|
+ contractEndDateText = obj.getString("htjsrq");
|
|
|
+ System.out.println("合同结束日期:" + contractEndDateText);
|
|
|
+ htje = obj.getString("htje");
|
|
|
+ System.out.println("合同金额:" + htje);
|
|
|
+ qd = obj.getString("qd");
|
|
|
+ System.out.println("分销渠道:" + qd);
|
|
|
+ pp = obj.getString("pp");
|
|
|
+ System.out.println("品牌:" + pp);
|
|
|
+ dmshtzt = obj.getString("dmshtzt");
|
|
|
+ System.out.println("dms合同状态:" + dmshtzt);
|
|
|
+ fileNewUrl = obj.getString("file");
|
|
|
+ System.out.println("合同附件:" + file);
|
|
|
+ }else {
|
|
|
+ //【推送数据:object】
|
|
|
+ JSONObject data = obj.getJSONObject("data");
|
|
|
+
|
|
|
+ //合同id
|
|
|
+ contractId = data.getString("contractId");
|
|
|
+ //合同编号(合同编号,企业根据需要设置的合同编号)
|
|
|
+ contractNo = data.getString("contractNo");
|
|
|
+ System.out.println("合同编号(合同编号,企业根据需要设置的合同编号):" + contractNo);
|
|
|
+ System.out.println("合同id:" + contractId);
|
|
|
+ //合同名称
|
|
|
+ contractName = data.getString("contractName");
|
|
|
+ System.out.println("合同名称:" + contractName);
|
|
|
+
|
|
|
+ //审批实例id(审批实例 id,仅通过审批发起的合同有值)
|
|
|
+ processInstanceId = data.getString("processInstanceId");
|
|
|
+ System.out.println("审批实例id(审批实例 id,仅通过审批发起的合同有值):" + processInstanceId);
|
|
|
+
|
|
|
+ //合同状态(合同状态: approving: 审批中, signing: 签署中, canceled: 已作废, withdraw: 已撤销, refused: 已拒绝, not-archive: 待归档, archive-confirming: 归档确认中, archived: 已归档)
|
|
|
+ contractStatus = data.getString("contractStatus");
|
|
|
+ System.out.println("合同状态: approving: 审批中, signing: 签署中, canceled: 已作废, withdraw: 已撤销, refused: 已拒绝, not-archive: 待归档, archive-confirming: 归档确认中, archived: 已归档:" + contractStatus);
|
|
|
+ //金额类型(payment_party_other: 收入, payment_party_our: 支出, none: 无金额)
|
|
|
+ amountType = data.getString("amountType");
|
|
|
+ System.out.println("金额类型(payment_party_other: 收入, payment_party_our: 支出, none: 无金额):" + amountType);
|
|
|
+
|
|
|
+ //金额数量(浮点型)
|
|
|
+ htje = data.getString("contractAmount");
|
|
|
+ System.out.println("金额数量(浮点型):" + htje);
|
|
|
+
|
|
|
+ //合同开始时间(Unix时间戳,单位毫秒)
|
|
|
+ long contractStartDate = 0;
|
|
|
+ long contractEndDate = 0;
|
|
|
+ try {
|
|
|
+ contractStartDate = data.getLong("contractStartDate") != null ? data.getLong("contractStartDate") : 0;
|
|
|
+ System.out.println("合同开始时间(Unix时间戳,单位毫秒):" + contractStartDate);
|
|
|
+ //将合同开始时间的毫秒级时间戳转成年月日文本格式
|
|
|
+ if (contractStartDate != 0){
|
|
|
+ contractStartDateText = DateUtil.format(new Date(contractStartDate), "yyyy-MM-dd");
|
|
|
+ System.out.println("合同开始时间(文本):" + contractStartDateText);
|
|
|
+ }
|
|
|
+ //合同结束时间(Unix时间戳,单位毫秒)
|
|
|
+ contractEndDate = data.getLong("contractEndDate");
|
|
|
+ System.out.println("合同结束时间(Unix时间戳,单位毫秒):" + contractEndDate);
|
|
|
+ if (contractEndDate != 0){
|
|
|
+ //将合同结束时间的毫秒级时间戳转成年月日文本格式
|
|
|
+ contractEndDateText = DateUtil.format(new Date(contractEndDate), "yyyy-MM-dd");
|
|
|
+ System.out.println("合同结束时间(文本):" + contractEndDateText);
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ if (contractStartDate != 0){
|
|
|
+ int yearInt = DateUtil.year(new Date(contractStartDate));
|
|
|
+ year = String.valueOf(yearInt);
|
|
|
+ System.out.println("获取合同开始日期的时间戳年份:" + year);
|
|
|
+ }else {
|
|
|
+ //获取当前年份
|
|
|
+ year = DateUtil.format(new Date(), "yyyy");
|
|
|
+ System.out.println("获取当前年份:" + year);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (contractStatus.equals("archived") && amountType.equals("payment_party_other")) {
|
|
|
+ //TODO 合同附件,需要将智能合同的附件下载到客户服务器,并将服务器文件的完整地址传入dms接口
|
|
|
+ //【合同归档文件:array】
|
|
|
+ JSONArray contractArchiveFiles = data.getJSONArray("contractArchiveFiles");
|
|
|
+ if (contractArchiveFiles.size() > 0) {
|
|
|
+ JSONObject contractArchiveItems = contractArchiveFiles.getJSONObject(0);
|
|
|
+ //文件名称
|
|
|
+ String contractArchiveFileName = contractArchiveItems.getString("fileName");
|
|
|
+ System.out.println("合同归档文件名称:" + contractArchiveFileName);
|
|
|
+ //文件类型
|
|
|
+ String contractArchiveFileType = contractArchiveItems.getString("fileType");
|
|
|
+ System.out.println("合同归档文件类型:" + contractArchiveFileType);
|
|
|
+ //文件大小
|
|
|
+ String contractArchiveFileSize = contractArchiveItems.getString("fileSize");
|
|
|
+ System.out.println("合同归档文件大小:" + contractArchiveFileSize);
|
|
|
+ //文件下载地址
|
|
|
+ String contractArchiveFileDownloadUrl = contractArchiveItems.getString("fileDownloadUrl");
|
|
|
+ System.out.println("合同归档文件下载地址:" + contractArchiveFileDownloadUrl);
|
|
|
+
|
|
|
+ try {
|
|
|
+ String destinationDir = "/file"; // Linux 路径
|
|
|
+// String destinationDir = "/Users/zhangkan/Documents/静态资源/图片/生成的新图片";
|
|
|
+ String fileName = contractArchiveFileName;
|
|
|
+
|
|
|
+ //获取当前日期,用于创建文件保存目录中增加当前日期的文件夹作区分
|
|
|
+ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式 yyyy-MM-dd HH:mm:ss
|
|
|
+ String format = df.format(new Date());
|
|
|
+ System.out.println(format);// new Date()为获取当前系统时间
|
|
|
+
|
|
|
+ File file1 = downloadFile(contractArchiveFileDownloadUrl, format, fileName);
|
|
|
+
|
|
|
+ if (file1 != null){
|
|
|
+ fileNewUrl = "http://192.168.200.156/file/" + format + File.separator + fileName;
|
|
|
+ }else {
|
|
|
+// fileNewUrl = "【转存失败】" + "http://192.168.200.156/file/" + format + File.separator + fileName;
|
|
|
+ log.info("【百雀羚服务器】合同归档文件下载转存失败,服务器转存新地址为空!");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.err.println("下载失败: " + e.getMessage());
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ System.out.println("【百雀羚服务器】合同归档文件下载地址:" + fileNewUrl);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (contractStatus.equals("archived") && amountType.equals("payment_party_other")) {
|
|
|
+ log.info("智能合同已归档、并且该合同类型属于【收入】,开始同步销售合同指标到DMS!");
|
|
|
+
|
|
|
+ //TODO 测试实例id
|
|
|
+// String processInstanceId = "wHmd-0t0TZKpU2N_2QKP7w00451753239482";
|
|
|
+// String year = "2025";
|
|
|
+
|
|
|
+ /**在执行方法前,将参数记录到宜搭日志表中*/
|
|
|
+ createYiDaoLog("DMS","参数已记录",null,contractId,contractNo,contractName,processInstanceId,contractStatus,amountType,year,null,
|
|
|
+ null, contractStartDateText, contractEndDateText, htje, null, null, "3", fileNewUrl);
|
|
|
+
|
|
|
+ /**查询钉钉OA审批详情「合同审批信息」,主要是获取客户编码、品牌、各项指标*/
|
|
|
+ JSONObject dingHetongXS = getDingHetongXS(processInstanceId);
|
|
|
+ JSONArray formComponentValues = dingHetongXS.getJSONArray("formComponentValues");
|
|
|
+ if (formComponentValues.size() > 0 && formComponentValues != null && !formComponentValues.isEmpty()){
|
|
|
+ //审批单实例标题
|
|
|
+ String title = dingHetongXS.getString("title");
|
|
|
+
|
|
|
+ //判断标题中是否包含指定字符
|
|
|
+ if (title.contains("草本")){
|
|
|
+ log.info("当前审批单标题包含:{草本}",title);
|
|
|
+ /**调用创建DMS销售合同方法*/
|
|
|
+ createDmsXSHT(processInstanceId, year, dingHetongXS.getJSONArray("formComponentValues"), contractNo, contractName, contractStartDateText, contractEndDateText, htje, fileNewUrl);
|
|
|
+ }else if (title.contains("经典")){
|
|
|
+ log.info("当前审批单标题包含:{经典}",title);
|
|
|
+ /**调用创建DMS销售合同方法*/
|
|
|
+ createDmsXSHT(processInstanceId, year, dingHetongXS.getJSONArray("formComponentValues"), contractNo, contractName, contractStartDateText, contractEndDateText, htje, fileNewUrl);
|
|
|
+ }else if (title.contains("气韵")){
|
|
|
+ log.info("当前审批单标题包含:{气韵}",title);
|
|
|
+ /**调用创建DMS销售合同方法*/
|
|
|
+ createDmsXSHT(processInstanceId, year, dingHetongXS.getJSONArray("formComponentValues"), contractNo, contractName, contractStartDateText, contractEndDateText, htje, fileNewUrl);
|
|
|
+ }else if (title.contains("销售合同申请(三生花)") || title.contains("销售合同申请(施美,三生花、新零售、生物医药、拼多多)")){//remark 2026-02-03新增:销售合同申请(施美,三生花、新零售、生物医药、拼多多)
|
|
|
+ log.info("当前审批单标题包含:{销售合同申请(三生花)}",title);
|
|
|
+ /**调用创建DMS销售合同方法*/
|
|
|
+ createDmsXSHT(processInstanceId, year, dingHetongXS.getJSONArray("formComponentValues"), contractNo, contractName, contractStartDateText, contractEndDateText, htje, fileNewUrl);
|
|
|
+ }else if (title.contains("销售合同申请(施美,三生花洗护)")){
|
|
|
+ log.info("当前审批单标题包含:{销售合同申请(施美,三生花洗护)}",title);
|
|
|
+ /**调用创建DMS销售合同方法*/
|
|
|
+ createDmsXSHT(processInstanceId, year, dingHetongXS.getJSONArray("formComponentValues"), contractNo, contractName, contractStartDateText, contractEndDateText, htje, fileNewUrl);
|
|
|
+ }else if (title.contains("销售合同申请(施美)")){
|
|
|
+ log.info("当前审批单标题包含:{销售合同申请(施美)}",title);
|
|
|
+ /**调用创建DMS销售合同方法*/
|
|
|
+ createDmsXSHT(processInstanceId, year, dingHetongXS.getJSONArray("formComponentValues"), contractNo, contractName, contractStartDateText, contractEndDateText, htje, fileNewUrl);
|
|
|
+ }else if (title.contains("销售合同申请(施美补充协议)")){
|
|
|
+ log.info("当前审批单标题包含:{销售合同申请(施美补充协议)}",title);
|
|
|
+ /**调用创建DMS销售合同方法*/
|
|
|
+ createDmsXSHT(processInstanceId, year, dingHetongXS.getJSONArray("formComponentValues"), contractNo, contractName, contractStartDateText, contractEndDateText, htje, fileNewUrl);
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ log.info("当前审批单未获取到表单信息,审批实例id【" + processInstanceId + "】");
|
|
|
+ /**更新宜搭日志记录表*/
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("智能合同推送同步推送dms「销售合同」-->执行完成,成功与否请查看日志记录");
|
|
|
+ return R.ok().message("智能合同推送同步推送dms「销售合同」-->执行完成,成功与否请查看日志记录");
|
|
|
+ }
|
|
|
+}
|