2025-06-11 13:58:36 +08:00
|
|
|
package com.management.controller;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.management.constant.ERPURLConstant;
|
|
|
|
import com.management.constant.URLConstant;
|
|
|
|
import com.management.entity.Filter;
|
|
|
|
import com.management.utils.CrmRequestUtil;
|
|
|
|
import com.management.utils.KingDeeUtils;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.springframework.http.HttpEntity;
|
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import org.springframework.web.client.RestClientException;
|
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
import java.math.BigDecimal;
|
2025-06-24 16:28:45 +08:00
|
|
|
import java.time.LocalDateTime;
|
|
|
|
import java.time.ZoneId;
|
2025-06-11 13:58:36 +08:00
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 预签合同
|
|
|
|
*
|
|
|
|
* @param
|
|
|
|
* @return null
|
|
|
|
* @Author weiloong_zhang
|
|
|
|
*/
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("/api/pre/contract")
|
|
|
|
@Slf4j
|
|
|
|
public class PreContractController {
|
|
|
|
|
|
|
|
private RestTemplate restTemplate = new RestTemplate();
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
2025-06-12 13:46:17 +08:00
|
|
|
//new PreContractController().syncContract();
|
|
|
|
new PreContractController().bgPreContract();
|
2025-06-11 13:58:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 智洋同步预签合同
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @Author weiloong_zhang
|
|
|
|
*/
|
|
|
|
@PostMapping("/sync_pre_contract")
|
|
|
|
public void syncContract() {
|
|
|
|
CrmRequestUtil crmRequestUtil = new CrmRequestUtil();
|
|
|
|
|
|
|
|
String[] syncStatus = {"option_pending_sync__c", "option_resync__c"};
|
|
|
|
|
|
|
|
JSONObject preContractReq = crmRequestUtil.getCRMList(Arrays.asList(
|
|
|
|
//new Filter("IS", "erp_id__c", Arrays.asList("")),
|
|
|
|
new Filter("IN", "sync_status__c", Arrays.asList(syncStatus)),
|
|
|
|
new Filter("GT", "create_time", Arrays.asList("1748923200000")),
|
|
|
|
//new Filter("EQ", "name", Arrays.asList("YQ2505220005")),
|
|
|
|
new Filter("EQ", "life_status", Arrays.asList("normal"))
|
|
|
|
), "pre_signed_contract__c");
|
|
|
|
|
|
|
|
JSONObject preContractRes = new JSONObject();
|
|
|
|
|
|
|
|
try {
|
|
|
|
preContractRes = restTemplate.postForObject(URLConstant.GET_CUSTOMIZE_LIST_URL, preContractReq, JSONObject.class);
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.println("查询结果为:" + preContractRes);
|
|
|
|
|
|
|
|
//判断查询是否成功
|
|
|
|
if (!"success".equals(preContractRes.getString("errorDescription")) || preContractRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
|
|
|
|
log.info("查询失败或者没有符合条件的单据");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//数据存在
|
|
|
|
for (Object preContractObj : preContractRes.getJSONObject("data").getJSONArray("dataList")) {
|
|
|
|
JSONObject preContractData = JSON.parseObject(JSON.toJSONString(preContractObj));
|
|
|
|
|
|
|
|
System.out.println("当前正在处理的数据为:" + preContractData);
|
|
|
|
|
|
|
|
String preContractId = preContractData.getString("_id");
|
|
|
|
//String createTime = preContractData.getString("create_time");
|
|
|
|
String createTime = preContractData.getString("last_modified_time");
|
|
|
|
|
|
|
|
//根据唯一id和创建时间查询是否已经执行当前数据
|
|
|
|
JSONObject preContractIsExist = new JSONObject();
|
|
|
|
|
|
|
|
try {
|
|
|
|
preContractIsExist = restTemplate.getForObject("http://localhost:18085/Log/query/log_data?table=send_log_htxx&log_type=YQHT&dataId=" + preContractId + "&mark=" + createTime, JSONObject.class);
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.println(preContractIsExist);
|
|
|
|
|
|
|
|
if (!preContractIsExist.getJSONArray("data").isEmpty()) {
|
|
|
|
log.info("当前数据已经执行过了,将不再向下执行");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info("没有集成过,将继续向下执行");
|
|
|
|
|
|
|
|
long documentDate = preContractData.getLong("document_date__c");
|
|
|
|
|
|
|
|
Date date = new Date(documentDate);
|
|
|
|
|
|
|
|
//开始封装传输至ERP的请求
|
|
|
|
Map erpContractReq = new HashMap<>();
|
|
|
|
erpContractReq.put("billno", preContractData.getString("name"));//预签合同编号
|
|
|
|
erpContractReq.put("crmid", preContractData.getString("_id"));//CRM唯一性编码
|
2025-06-12 13:46:17 +08:00
|
|
|
erpContractReq.put("project_number", preContractData.getString("opportunity_id__c"));//项目号
|
|
|
|
//erpContractReq.put("project_number", "SJ20250424-0033");//项目号
|
2025-06-11 13:58:36 +08:00
|
|
|
erpContractReq.put("billtype_number", "conm_salcontract_BT_YQ");//单据类型
|
|
|
|
erpContractReq.put("billname", preContractData.getString("project_name__c") != null ? preContractData.getString("project_name__c") : "预签合同");//合同名称
|
|
|
|
erpContractReq.put("type_number", "XSHT-YQ01");//合同类型
|
|
|
|
erpContractReq.put("contparties_number", preContractData.getString("our_company_name__c") != null ? preContractData.getString("our_company_name__c") : "");//合同主体
|
|
|
|
erpContractReq.put("createorg_number", preContractData.getString("our_company_name__c") != null ? preContractData.getString("our_company_name__c") : "");//销售组织编码
|
|
|
|
erpContractReq.put("org_number", preContractData.getString("our_company_name__c") != null ? preContractData.getString("our_company_name__c") : "");//销售组织编码
|
|
|
|
erpContractReq.put("currency_number", "CNY");//本位币.货币代码
|
|
|
|
erpContractReq.put("settlecurrency_number", "CNY");//结算币别.货币代码
|
|
|
|
erpContractReq.put("biztime", date);//签订日期,单据日期
|
|
|
|
erpContractReq.put("biztimebegin", date);//起始日期
|
|
|
|
erpContractReq.put("biztimeend", date);//截止日期
|
|
|
|
erpContractReq.put("party1st", preContractData.getString("contract_unit_name__c__r"));//甲方
|
|
|
|
erpContractReq.put("party2nd", preContractData.getString("our_company_name__c"));//乙方
|
|
|
|
erpContractReq.put("bizmode", "C");//业务模式 A:统谈统签, B:统谈分签,C:分谈分签
|
|
|
|
erpContractReq.put("isentrysumamt", false);//isentrysumamt
|
|
|
|
|
|
|
|
//部门编码
|
|
|
|
String deptCode = "";
|
|
|
|
|
|
|
|
List deptIdList = preContractData.getJSONArray("data_own_department");
|
|
|
|
|
|
|
|
//开始查询部门编码
|
|
|
|
JSONObject deptReq = crmRequestUtil.getCRMList(Arrays.asList(
|
|
|
|
new Filter("EQ", "_id", deptIdList)
|
|
|
|
), "DepartmentObj");
|
|
|
|
|
|
|
|
JSONObject deptRes = new JSONObject();
|
|
|
|
|
|
|
|
try {
|
|
|
|
deptRes = restTemplate.postForObject(URLConstant.GET_CRM_LIST_URL, deptReq, JSONObject.class);
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ("success".equals(deptRes.getString("errorDescription")) && !deptRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
|
|
|
|
JSONObject deptData = deptRes.getJSONObject("data").getJSONArray("dataList").getJSONObject(0);
|
|
|
|
deptCode = deptData.getString("field_Vj6sf__c") != null ? deptData.getString("field_Vj6sf__c") : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
//erpContractReq.put("dept", "ZHY");//归属部门
|
|
|
|
erpContractReq.put("dept", deptCode);//归属部门
|
|
|
|
//erpContractReq.put("dept_number", "004");//归属部门
|
|
|
|
erpContractReq.put("dept_number", deptCode);//归属部门
|
|
|
|
|
|
|
|
//获取负责人编码
|
|
|
|
String ownerCode = "";
|
|
|
|
|
|
|
|
List ownerIdList = preContractData.getJSONArray("owner");
|
|
|
|
|
|
|
|
JSONObject ownerReq = crmRequestUtil.getCRMList(Arrays.asList(
|
|
|
|
new Filter("EQ", "user_id", ownerIdList)
|
|
|
|
), "PersonnelObj");
|
|
|
|
|
|
|
|
JSONObject ownerRes = new JSONObject();
|
|
|
|
|
|
|
|
try {
|
|
|
|
ownerRes = restTemplate.postForObject(URLConstant.GET_CRM_LIST_URL, ownerReq, JSONObject.class);
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ("success".equals(ownerRes.getString("errorDescription")) && !ownerRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
|
|
|
|
JSONObject ownerData = ownerRes.getJSONObject("data").getJSONArray("dataList").getJSONObject(0);
|
|
|
|
ownerCode = ownerData.getString("field_T1xid__c") != null ? ownerData.getString("field_T1xid__c") : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
//erpContractReq.put("operator", "ID-000016");//todo 负责人
|
|
|
|
erpContractReq.put("operator", ownerCode);//负责人
|
|
|
|
|
|
|
|
//合同单位
|
|
|
|
String customerId = preContractData.getString("contract_unit_name__c");
|
|
|
|
|
|
|
|
//根据客户唯一id查询客户名称
|
|
|
|
JSONObject customerReq = crmRequestUtil.getCRMList(Arrays.asList(
|
|
|
|
new Filter("EQ", "_id", Arrays.asList(customerId))
|
|
|
|
), "AccountObj");
|
|
|
|
|
|
|
|
JSONObject customerRes = new JSONObject();
|
|
|
|
|
|
|
|
try {
|
|
|
|
customerRes = restTemplate.postForObject(URLConstant.GET_CRM_LIST_URL, customerReq, JSONObject.class);
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!"success".equals(customerRes.getString("errorDescription")) || customerRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
|
|
|
|
log.info("客户名称查询失败或者不存在,直接跳过更新");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONObject customerData = customerRes.getJSONObject("data").getJSONArray("dataList").getJSONObject(0);
|
|
|
|
|
|
|
|
erpContractReq.put("customer_number", customerData.getString("account_no"));//合同单位名称
|
|
|
|
erpContractReq.put("reccustomer_number", customerData.getString("account_no"));//收获客户单位名称
|
|
|
|
//erpContractReq.put("customer_number", "Cus-000007");//todo 合同单位名称
|
|
|
|
erpContractReq.put("settlecustomer_number", preContractData.getString("billing_customer_code__c") != null ? preContractData.getString("billing_customer_code__c") : "");//结算客户
|
|
|
|
erpContractReq.put("payingcustomer_number", preContractData.getString("payment_customer_code__c") != null ? preContractData.getString("payment_customer_code__c") : "");//付款客户
|
|
|
|
|
|
|
|
//获取产品线
|
|
|
|
|
|
|
|
erpContractReq.put("productline_number", preContractData.getString("product_line__c"));//产品线
|
|
|
|
|
|
|
|
//获取具体安装方式
|
|
|
|
|
|
|
|
erpContractReq.put("installationmethod", preContractData.getString("installation_method__c"));//安装方式
|
|
|
|
|
|
|
|
//行业类型
|
|
|
|
|
|
|
|
erpContractReq.put("industrytype_number", preContractData.getString("industry_type__c"));//行业类型
|
|
|
|
erpContractReq.put("province", preContractData.getString("field_t3fgo__c__r"));//省
|
|
|
|
erpContractReq.put("city", preContractData.getString("field_ngwth__c__r"));//市
|
|
|
|
erpContractReq.put("county", preContractData.getString("field_K5p2C__c__r"));//县
|
|
|
|
erpContractReq.put("deviceqty", preContractData.getBigDecimal("device_quantity__c") != null ? preContractData.getBigDecimal("device_quantity__c") : 0);//设备数量
|
|
|
|
erpContractReq.put("totalallamount", preContractData.getBigDecimal("pre_signed_amount__c") != null ? preContractData.getBigDecimal("pre_signed_amount__c") : 0);//预签金额
|
|
|
|
erpContractReq.put("warranty", preContractData.getBigDecimal("warranty_period_months__c") != null ? preContractData.getBigDecimal("warranty_period_months__c") : 0);//质保期
|
|
|
|
|
|
|
|
Map erpContractData = new HashMap<>();
|
|
|
|
erpContractData.put("data", Arrays.asList(erpContractReq));//请求体
|
|
|
|
|
|
|
|
String accessToken = new KDTokenController().getKDAccessTokenTest();
|
|
|
|
|
|
|
|
if (accessToken == null || accessToken.equals("")) {
|
|
|
|
log.info("金蝶token为空或不存在");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
String uuid = UUID.randomUUID().toString().replace("-", "");
|
|
|
|
//开始封装请求头
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
headers.set("accessToken", accessToken);
|
|
|
|
headers.set("Idempotency-Key", uuid);
|
|
|
|
|
|
|
|
HttpEntity contractReqEntity = new HttpEntity(erpContractData, headers);
|
|
|
|
|
|
|
|
System.out.println(JSON.parseObject(JSON.toJSONString(contractReqEntity)));
|
|
|
|
|
|
|
|
//开始向金蝶发起请求
|
|
|
|
String contractUrl = ERPURLConstant.ERP_URL + "/ierp/kapi/v2/f9w5/conm/conm_salcontract/savecontract";
|
|
|
|
|
|
|
|
JSONObject contractRes = new JSONObject();
|
|
|
|
|
|
|
|
try {
|
|
|
|
contractRes = restTemplate.postForObject(contractUrl, contractReqEntity, JSONObject.class);
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
//开始封装回写程序
|
|
|
|
Map backReqMap = new HashMap();
|
|
|
|
backReqMap.put("_id", preContractId);
|
|
|
|
backReqMap.put("dataObjectApiName", "pre_signed_contract__c");
|
|
|
|
|
|
|
|
//开始封装日志
|
|
|
|
Map logMap = new HashMap<>();
|
|
|
|
logMap.put("log_id", UUID.randomUUID().toString().replace("-", ""));
|
|
|
|
logMap.put("log_type", "YQHT");
|
|
|
|
logMap.put("syn_type", "0");
|
|
|
|
logMap.put("data_name", preContractData.getString("name"));
|
|
|
|
logMap.put("data_id", preContractId);
|
|
|
|
logMap.put("mark", createTime);
|
|
|
|
logMap.put("send_body", JSON.toJSONString(contractReqEntity));
|
|
|
|
logMap.put("send_res", JSON.toJSONString(contractRes));
|
|
|
|
logMap.put("tableName", "send_log_htxx");
|
|
|
|
|
|
|
|
//判断是否执行成功
|
|
|
|
if ("0".equals(contractRes.getString("errorCode"))) {
|
|
|
|
//同步成功
|
|
|
|
logMap.put("log_status", "0");
|
|
|
|
logMap.put("res_body", "同步成功");
|
|
|
|
backReqMap.put("erp_id__c", contractRes.getJSONObject("data").getJSONArray("result").getJSONObject(0).getString("id"));
|
|
|
|
backReqMap.put("sync_status__c", "option_sync_success__c");
|
|
|
|
backReqMap.put("response_info__c", "同步成功");
|
|
|
|
|
|
|
|
//开始审核
|
|
|
|
KingDeeUtils kingDeeUtils = new KingDeeUtils();
|
|
|
|
kingDeeUtils.audit(contractRes.getJSONObject("data").getJSONArray("result").getJSONObject(0).getString("id"), "conm_salcontract");
|
|
|
|
} else {
|
|
|
|
//同步失败
|
|
|
|
logMap.put("log_status", "1");
|
|
|
|
logMap.put("res_body", "同步失败:" + contractRes.getString("message"));
|
|
|
|
backReqMap.put("sync_status__c", "option_sync_failure__c");
|
|
|
|
backReqMap.put("response_info__c", contractRes.getString("message"));
|
|
|
|
}
|
|
|
|
|
|
|
|
//开始回写
|
|
|
|
JSONObject backReq = crmRequestUtil.updateCRM(backReqMap);
|
|
|
|
|
|
|
|
JSONObject backRes = new JSONObject();
|
|
|
|
|
|
|
|
try {
|
|
|
|
backRes = restTemplate.postForObject(URLConstant.UPDATE_CRM_CUSTOMIZE, backReq, JSONObject.class);
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.println("回写结果为:" + backRes);
|
|
|
|
|
|
|
|
//开始记录日志
|
|
|
|
JSONObject logRes = new JSONObject();
|
|
|
|
try {
|
|
|
|
logRes = restTemplate.postForObject("http://localhost:18085/Log/insert/log_data", logMap, JSONObject.class);
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.println("日志插入结果为:" + logRes);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 预签合同变更
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @Author weiloong_zhang
|
|
|
|
*/
|
|
|
|
@PostMapping("/bg_pre_contract")
|
|
|
|
public void bgPreContract() {
|
|
|
|
CrmRequestUtil crmRequestUtil = new CrmRequestUtil();
|
|
|
|
|
2025-06-24 16:28:45 +08:00
|
|
|
long twoDaysAgo = LocalDateTime.now()
|
|
|
|
.minusDays(1)
|
|
|
|
.atZone(ZoneId.systemDefault())
|
|
|
|
.toInstant()
|
|
|
|
.toEpochMilli();
|
|
|
|
|
2025-06-11 13:58:36 +08:00
|
|
|
JSONObject preContractBGReq = crmRequestUtil.getCRMList(Arrays.asList(
|
2025-06-24 16:28:45 +08:00
|
|
|
//new Filter("EQ", "name", Arrays.asList("2025-06-13-00004")),
|
2025-06-25 13:54:57 +08:00
|
|
|
new Filter("GT", "create_time", Arrays.asList(String.valueOf(twoDaysAgo))),
|
|
|
|
new Filter("EQ", "life_status", Arrays.asList("normal"))
|
2025-06-12 13:46:17 +08:00
|
|
|
), "pre_signed_contract__c__changeObj__c");
|
2025-06-11 13:58:36 +08:00
|
|
|
|
|
|
|
JSONObject preContractBGRes = new JSONObject();
|
|
|
|
|
|
|
|
try {
|
|
|
|
preContractBGRes = restTemplate.postForObject(URLConstant.GET_CUSTOMIZE_LIST_URL, preContractBGReq, JSONObject.class);
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!"success".equals(preContractBGRes.getString("errorDescription")) || preContractBGRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
|
|
|
|
log.info("不存在要变更的预签合同");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Object preContractBGObj : preContractBGRes.getJSONObject("data").getJSONArray("dataList")) {
|
|
|
|
JSONObject preContractBGData = JSON.parseObject(JSON.toJSONString(preContractBGObj));
|
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
System.out.println("当前预签合同变更数据为:" + preContractBGData);
|
2025-06-11 13:58:36 +08:00
|
|
|
|
|
|
|
//开始截取关键数据
|
|
|
|
String preContractId = preContractBGData.getString("_id");
|
2025-06-12 17:46:41 +08:00
|
|
|
String createTime = preContractBGData.getString("create_time");
|
2025-06-12 13:46:17 +08:00
|
|
|
String YQHTName = preContractBGData.getString("name");
|
|
|
|
String originalName = preContractBGData.getString("original_data__r");
|
2025-06-11 13:58:36 +08:00
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
//开始判断是否已经执行过
|
2025-06-11 13:58:36 +08:00
|
|
|
JSONObject isLogRes = new JSONObject();
|
|
|
|
|
|
|
|
try {
|
2025-06-12 17:46:41 +08:00
|
|
|
isLogRes = restTemplate.getForObject("http://localhost:18085/Log/query/log_data?table=send_log_htxx&log_type=YQHTBG&dataId=" + preContractId + "&mark=" + createTime, JSONObject.class);
|
2025-06-11 13:58:36 +08:00
|
|
|
} catch (RestClientException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isLogRes.getJSONArray("data").isEmpty()) {
|
|
|
|
log.info("当前数据已经执行过了,将不再向下执行");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info("日志不存在,将继续向下执行");
|
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
//开始封装请求数据
|
|
|
|
Map YQBGMap = new HashMap<>();
|
|
|
|
YQBGMap.put("billno", YQHTName);//合同编号
|
2025-06-11 13:58:36 +08:00
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
//开始获取商机编号
|
|
|
|
String businessId = preContractBGData.getString("change_business_opportunity__c") != null ? preContractBGData.getString("change_business_opportunity__c") : "";
|
2025-06-11 13:58:36 +08:00
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
//根据商机唯一性ID获取商机编号
|
|
|
|
JSONObject getBusiness = crmRequestUtil.getCRMList(Arrays.asList(
|
|
|
|
new Filter("EQ", "_id", Arrays.asList(businessId))
|
2025-06-11 13:58:36 +08:00
|
|
|
), "NewOpportunityObj");
|
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
JSONObject businessRes = new JSONObject();
|
2025-06-11 13:58:36 +08:00
|
|
|
|
|
|
|
try {
|
2025-06-12 13:46:17 +08:00
|
|
|
businessRes = restTemplate.postForObject(URLConstant.GET_CUSTOMIZE_LIST_URL, getBusiness, JSONObject.class);
|
2025-06-11 13:58:36 +08:00
|
|
|
} catch (RestClientException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
if (!"success".equals(businessRes.getString("errorDescription")) || businessRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
|
|
|
|
log.info("不存在该商机,将不再向下执行");
|
2025-06-11 13:58:36 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
String projectCode = businessRes.getJSONObject("data").getJSONArray("dataList").getJSONObject(0).getString("opportunity_id__c");
|
2025-06-11 13:58:36 +08:00
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
YQBGMap.put("project", projectCode);//项目号
|
|
|
|
YQBGMap.put("billname", preContractBGData.getString("change_project_name__c") != null ? preContractBGData.getString("change_project_name__c") : "");//合同名称
|
|
|
|
YQBGMap.put("srcbillnumber", originalName);//来源单单号
|
|
|
|
YQBGMap.put("srcsysbillid", preContractBGData.getString("original_data"));//来源单ID
|
|
|
|
YQBGMap.put("crmid", preContractBGData.getString("original_data"));//CRMID
|
2025-06-11 13:58:36 +08:00
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
//获取部门
|
|
|
|
String ownerDept = preContractBGData.getString("owner_department_id") != null ? preContractBGData.getString("owner_department_id") : "";
|
|
|
|
|
|
|
|
JSONObject getDept = crmRequestUtil.getCRMList(Arrays.asList(
|
|
|
|
new Filter("EQ", "_id", Arrays.asList(ownerDept))
|
2025-06-11 13:58:36 +08:00
|
|
|
), "DepartmentObj");
|
|
|
|
|
|
|
|
JSONObject deptRes = new JSONObject();
|
|
|
|
|
|
|
|
try {
|
2025-06-12 13:46:17 +08:00
|
|
|
deptRes = restTemplate.postForObject(URLConstant.GET_CRM_LIST_URL, getDept, JSONObject.class);
|
2025-06-11 13:58:36 +08:00
|
|
|
} catch (RestClientException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
if (!"success".equals(deptRes.getString("errorDescription")) || deptRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
|
|
|
|
log.info("不存在该部门,将不再向下执行");
|
|
|
|
continue;
|
2025-06-11 13:58:36 +08:00
|
|
|
}
|
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
String deptERPCode = deptRes.getJSONObject("data").getJSONArray("dataList").getJSONObject(0).getString("field_Vj6sf__c");
|
2025-06-29 09:22:15 +08:00
|
|
|
YQBGMap.put("dept", deptERPCode);//部门编码
|
|
|
|
//YQBGMap.put("dept", "ZHY");//部门编码
|
2025-06-11 13:58:36 +08:00
|
|
|
|
2025-06-12 17:46:41 +08:00
|
|
|
List ownerId = preContractBGData.getJSONArray("change_owner") != null ? preContractBGData.getJSONArray("change_owner") : new ArrayList<>();
|
2025-06-11 13:58:36 +08:00
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
JSONObject getOwner = crmRequestUtil.getCRMList(Arrays.asList(
|
|
|
|
new Filter("EQ", "user_id", ownerId)
|
2025-06-11 13:58:36 +08:00
|
|
|
), "PersonnelObj");
|
|
|
|
|
|
|
|
JSONObject ownerRes = new JSONObject();
|
|
|
|
|
|
|
|
try {
|
2025-06-12 13:46:17 +08:00
|
|
|
ownerRes = restTemplate.postForObject(URLConstant.GET_CRM_LIST_URL, getOwner, JSONObject.class);
|
2025-06-11 13:58:36 +08:00
|
|
|
} catch (RestClientException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
if (!"success".equals(ownerRes.getString("errorDescription")) || ownerRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
|
|
|
|
log.info("负责人不存在或者请求失败");
|
2025-06-11 13:58:36 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
System.out.println("负责人数据为:" + ownerRes);
|
2025-06-11 13:58:36 +08:00
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
String ownerUserId = ownerRes.getJSONObject("data").getJSONArray("dataList").getJSONObject(0).getString("field_T1xid__c");
|
2025-06-29 09:22:15 +08:00
|
|
|
YQBGMap.put("operator", ownerUserId);//负责人
|
|
|
|
//YQBGMap.put("operator", "ID-000016");//负责人
|
2025-06-11 13:58:36 +08:00
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
//质保期
|
|
|
|
YQBGMap.put("warranty", preContractBGData.getBigDecimal("change_warranty_period_months__c") != null ? preContractBGData.getBigDecimal("warranty_period") : BigDecimal.ZERO);
|
|
|
|
//订货客户
|
|
|
|
YQBGMap.put("customer", preContractBGData.getString("change_billing_customer_code__c") != null ? preContractBGData.getString("change_billing_customer_code__c") : "");
|
2025-06-11 13:58:36 +08:00
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
//产品线
|
|
|
|
YQBGMap.put("productLine", preContractBGData.getString("change_product_line__c") != null ? preContractBGData.getString("change_product_line__c") : "");
|
|
|
|
//安装方式
|
|
|
|
YQBGMap.put("installationmethod", preContractBGData.getString("change_installation_method__c") != null ? preContractBGData.getString("change_installation_method__c") : "");
|
2025-06-11 13:58:36 +08:00
|
|
|
//行业类型
|
2025-06-12 13:46:17 +08:00
|
|
|
YQBGMap.put("industrytype", preContractBGData.getString("change_industry_type__c") != null ? preContractBGData.getString("change_industry_type__c") : "");
|
|
|
|
//省
|
|
|
|
YQBGMap.put("province", preContractBGData.getString("change_field_t3fgo__c__r") != null ? preContractBGData.getString("change_field_t3fgo__c__r") : "");
|
|
|
|
//市
|
|
|
|
YQBGMap.put("city", preContractBGData.getString("change_field_ngwth__c__r") != null ? preContractBGData.getString("change_field_ngwth__c__r") : "");
|
|
|
|
//区
|
|
|
|
YQBGMap.put("county", preContractBGData.getString("change_field_K5p2C__c__r") != null ? preContractBGData.getString("change_field_K5p2C__c__r") : "");
|
|
|
|
|
|
|
|
//开始获取token
|
2025-06-11 13:58:36 +08:00
|
|
|
String accessToken = new KDTokenController().getKDAccessTokenTest();
|
|
|
|
|
|
|
|
if (accessToken == null || accessToken.equals("")) {
|
|
|
|
log.info("金蝶token为空或不存在");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
String uuid = UUID.randomUUID().toString().replace("-", "");
|
|
|
|
//开始封装请求头
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
headers.set("accessToken", accessToken);
|
|
|
|
headers.set("Idempotency-Key", uuid);
|
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
HttpEntity contractReqEntity = new HttpEntity(YQBGMap, headers);
|
2025-06-11 13:58:36 +08:00
|
|
|
|
|
|
|
System.out.println(JSON.parseObject(JSON.toJSONString(contractReqEntity)));
|
|
|
|
|
|
|
|
String contractUrl = ERPURLConstant.ERP_URL + "/ierp/kapi/v2/f9w5/conm/salcontract/change";
|
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
JSONObject contractRes = new JSONObject();
|
2025-06-11 13:58:36 +08:00
|
|
|
|
|
|
|
try {
|
2025-06-12 13:46:17 +08:00
|
|
|
contractRes = restTemplate.postForObject(contractUrl, contractReqEntity, JSONObject.class);
|
2025-06-11 13:58:36 +08:00
|
|
|
} catch (RestClientException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
System.out.println(JSON.parseObject(JSON.toJSONString(contractRes)));
|
2025-06-11 13:58:36 +08:00
|
|
|
|
|
|
|
//开始封装回写程序
|
|
|
|
Map backReqMap = new HashMap();
|
2025-06-12 13:46:17 +08:00
|
|
|
backReqMap.put("_id", preContractBGData.getString("original_data"));
|
|
|
|
backReqMap.put("dataObjectApiName", "pre_signed_contract__c");
|
2025-06-11 13:58:36 +08:00
|
|
|
|
|
|
|
//开始封装日志
|
|
|
|
Map logMap = new HashMap<>();
|
|
|
|
logMap.put("log_id", UUID.randomUUID().toString().replace("-", ""));
|
|
|
|
logMap.put("log_type", "YQHTBG");
|
|
|
|
logMap.put("syn_type", "0");
|
2025-06-12 13:46:17 +08:00
|
|
|
logMap.put("data_name", YQHTName);
|
2025-06-11 13:58:36 +08:00
|
|
|
logMap.put("data_id", preContractId);
|
2025-06-12 17:46:41 +08:00
|
|
|
logMap.put("mark", createTime);
|
2025-06-11 13:58:36 +08:00
|
|
|
logMap.put("send_body", JSON.toJSONString(contractReqEntity));
|
2025-06-12 13:46:17 +08:00
|
|
|
logMap.put("send_res", JSON.toJSONString(contractRes));
|
2025-06-11 13:58:36 +08:00
|
|
|
logMap.put("tableName", "send_log_htxx");
|
|
|
|
|
|
|
|
//判断是否执行成功
|
2025-06-12 13:46:17 +08:00
|
|
|
if ("0".equals(contractRes.getString("errorCode"))) {
|
2025-06-11 13:58:36 +08:00
|
|
|
//同步成功
|
|
|
|
logMap.put("log_status", "0");
|
|
|
|
logMap.put("res_body", "同步成功");
|
2025-06-12 17:46:41 +08:00
|
|
|
//backReqMap.put("updated_erp_id__c", contractRes.getJSONObject("data").getJSONArray("result").getJSONObject(0).getString("id"));
|
2025-06-11 13:58:36 +08:00
|
|
|
backReqMap.put("sync_status__c", "option_sync_success__c");
|
2025-06-12 13:46:17 +08:00
|
|
|
backReqMap.put("response_info__c", "变更同步成功");
|
2025-06-11 13:58:36 +08:00
|
|
|
|
2025-06-12 13:46:17 +08:00
|
|
|
//开始审核
|
|
|
|
// KingDeeUtils kingDeeUtils = new KingDeeUtils();
|
|
|
|
// kingDeeUtils.audit(contractRes.getJSONObject("data").getJSONArray("result").getJSONObject(0).getString("id"), "conm_salcontract");
|
2025-06-11 13:58:36 +08:00
|
|
|
} else {
|
|
|
|
//同步失败
|
|
|
|
logMap.put("log_status", "1");
|
2025-06-12 13:46:17 +08:00
|
|
|
logMap.put("res_body", "同步失败:" + contractRes.getString("message"));
|
2025-06-11 13:58:36 +08:00
|
|
|
backReqMap.put("sync_status__c", "option_sync_failure__c");
|
2025-06-12 13:46:17 +08:00
|
|
|
backReqMap.put("response_info__c", "变更失败:" + contractRes.getString("message"));
|
2025-06-11 13:58:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//开始回写
|
|
|
|
JSONObject backReq = crmRequestUtil.updateCRM(backReqMap);
|
|
|
|
|
|
|
|
JSONObject backRes = new JSONObject();
|
|
|
|
|
|
|
|
try {
|
|
|
|
backRes = restTemplate.postForObject(URLConstant.UPDATE_CRM_CUSTOMIZE, backReq, JSONObject.class);
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.println("回写结果为:" + backRes);
|
|
|
|
|
|
|
|
//开始记录日志
|
|
|
|
JSONObject logRes = new JSONObject();
|
|
|
|
try {
|
|
|
|
logRes = restTemplate.postForObject("http://localhost:18085/Log/insert/log_data", logMap, JSONObject.class);
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.println("日志插入结果为:" + logRes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|