diff --git a/magic-boot/src/main/java/org/ssssssss/magicboot/controller/KDTokenController.java b/magic-boot/src/main/java/org/ssssssss/magicboot/controller/KDTokenController.java index 9efedae..2dbb8ba 100644 --- a/magic-boot/src/main/java/org/ssssssss/magicboot/controller/KDTokenController.java +++ b/magic-boot/src/main/java/org/ssssssss/magicboot/controller/KDTokenController.java @@ -150,13 +150,13 @@ public class KDTokenController { //开始封装 //接口地址 - String appTokenUrl = "http://192.168.31.158:8022/ierp/api/getAppToken.do"; + String appTokenUrl = "http://192.168.31.233:8022/ierp/api/getAppToken.do"; //请求参数 LinkedHashMap appTokenParams = new LinkedHashMap<>(); - appTokenParams.put("appId", "apitest"); - appTokenParams.put("appSecret", "ZHYkdERPapi@2025"); + appTokenParams.put("appId", "FXXKCRM"); + appTokenParams.put("appSecret", "FXXKCRM20251234a!"); appTokenParams.put("tenantid", "zyierp"); - appTokenParams.put("accountId", "2096108315279888384"); + appTokenParams.put("accountId", "2095345516136108032"); //开始发起请求 String appTokenRes = ""; @@ -205,7 +205,7 @@ public class KDTokenController { //不存在,将执行获取accessToken的逻辑 //开始封装参数 //URL - String accessTokenUrl = "http://192.168.31.158:8022/ierp/api/login.do"; + String accessTokenUrl = "http://192.168.31.233:8022/ierp/api/login.do"; //请求参数 LinkedHashMap accessTokenParams = new LinkedHashMap<>(); @@ -213,7 +213,7 @@ public class KDTokenController { accessTokenParams.put("usertype", "Mobile"); accessTokenParams.put("apptoken", getKDAppTokenTest()); accessTokenParams.put("tenantid", "zyierp"); - accessTokenParams.put("accountId", "2096108315279888384"); + accessTokenParams.put("accountId", "2095345516136108032"); accessTokenParams.put("language", "zh_CN"); //开始向金蝶发起请求 diff --git a/magic-boot/src/main/java/org/ssssssss/magicboot/controller/PreContractController.java b/magic-boot/src/main/java/org/ssssssss/magicboot/controller/PreContractController.java new file mode 100644 index 0000000..502a399 --- /dev/null +++ b/magic-boot/src/main/java/org/ssssssss/magicboot/controller/PreContractController.java @@ -0,0 +1,301 @@ +package org.ssssssss.magicboot.controller; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +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; +import org.ssssssss.magicboot.constant.URLConstant; +import org.ssssssss.magicboot.entity.Filter; +import org.ssssssss.magicboot.utils.CrmRequestUtil; + +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) { + new PreContractController().syncContract(); + } + + + /** + * 智洋同步预签合同 + * + * @return void + * @Author weiloong_zhang + */ + @PostMapping("/sync_pre_contract") + public void syncContract() { + CrmRequestUtil crmRequestUtil = new CrmRequestUtil(); + + JSONObject preContractReq = crmRequestUtil.getCRMList(Arrays.asList( + new Filter("EQ", "name", Arrays.asList("YQHT-20250427-0001")), + 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(); + } + + //判断查询是否成功 + if (!"success".equals(preContractRes.getString("errorDescription")) && preContractRes.getJSONObject("data").getJSONArray("records").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"); + + //根据唯一id和创建时间查询是否已经执行当前数据 + JSONObject preContractIsExist = new JSONObject(); + + try { + preContractIsExist = restTemplate.getForObject("http://localhost:18088/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唯一性编码 + erpContractReq.put("project_number", preContractData.getString("opportunity_id__c"));//项目号 + //erpContractReq.put("project_number", "SJ20250424-0033");//项目号 + erpContractReq.put("billtype_number", "conm_salcontract_BT_YQ");//单据类型 + erpContractReq.put("billname", preContractData.getString("contract_name__c"));//合同名称 + erpContractReq.put("type_number", "XSHT-YQ01");//合同类型 + erpContractReq.put("contparties_number", "BU-0002");//合同主体 + erpContractReq.put("createorg_number", "zyierp");//销售组织编码 + erpContractReq.put("org_number", "zyierp");//销售组织编码 + 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:分谈分签 + + //部门编码 + 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", deptCode);//归属部门 + + //获取负责人编码 + String ownerCode = ""; + + List ownerIdList = preContractData.getJSONArray("owner"); + + JSONObject ownerReq = crmRequestUtil.getCRMList(Arrays.asList( + new Filter("EQ", "_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", "ZHY12025");//todo 负责人 + + //合同单位 + 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("customer_number", "Cus-000007");//合同单位名称 + + //获取产品线 + + 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 = "http://192.168.31.233:8022/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", "同步成功"); + } 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:18088/Log/insert/log_data", logMap, JSONObject.class); + } catch (RestClientException e) { + e.printStackTrace(); + } + + System.out.println("日志插入结果为:" + logRes); + + } + } +} diff --git a/magic-boot/src/main/java/org/ssssssss/magicboot/controller/XSHTController.java b/magic-boot/src/main/java/org/ssssssss/magicboot/controller/XSHTController.java new file mode 100644 index 0000000..6b8c490 --- /dev/null +++ b/magic-boot/src/main/java/org/ssssssss/magicboot/controller/XSHTController.java @@ -0,0 +1,422 @@ +package org.ssssssss.magicboot.controller; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +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; +import org.ssssssss.magicboot.constant.URLConstant; +import org.ssssssss.magicboot.entity.Filter; +import org.ssssssss.magicboot.utils.CrmRequestUtil; + +import java.util.*; + +/** + * 销售合同同步 + * + * @return null + * @Author weiloong_zhang + */ +@RestController +@RequestMapping("/api/sign/conract") +@Slf4j +public class XSHTController { + + private RestTemplate restTemplate = new RestTemplate(); + + public static void main(String[] args) { + new XSHTController().syncXSHT(); + } + + /** + * 销售合同同步 + * + * @return void + * @Author weiloong_zhang + */ + @PostMapping("/sync_xsht") + public void syncXSHT() { + CrmRequestUtil crmRequestUtil = new CrmRequestUtil(); + + JSONObject preContractReq = crmRequestUtil.getCRMList(Arrays.asList( + new Filter("EQ", "name", Arrays.asList("ZHY250506-0002")), + new Filter("EQ", "life_status", Arrays.asList("normal")) + ), "SaleContractObj"); + + JSONObject ContractRes = new JSONObject(); + + try { + ContractRes = restTemplate.postForObject(URLConstant.GET_CUSTOMIZE_LIST_URL, preContractReq, JSONObject.class); + } catch (RestClientException e) { + e.printStackTrace(); + } + + //判断查询是否成功 + if (!"success".equals(ContractRes.getString("errorDescription")) && ContractRes.getJSONObject("data").getJSONArray("records").isEmpty()) { + log.info("查询失败或者没有符合条件的单据"); + return; + } + + for (Object ContractObj : ContractRes.getJSONObject("data").getJSONArray("dataList")) { + //销售合同数据 + JSONObject contractData = JSON.parseObject(JSON.toJSONString(ContractObj)); + + System.out.println("当前正在处理的数据为:" + contractData); + + //合同唯一id + String contractId = contractData.getString("_id"); + //合同名称 + String contractName = contractData.getString("name"); + //创建时间 + String createTime = contractData.getString("create_time"); + + //查看数据库表中是否已执行过当前数据 + JSONObject isLogRes = new JSONObject(); + + try { + isLogRes = restTemplate.getForObject("http://localhost:18088/Log/query/log_data?table=send_log_htxx&log_type=XSHT&dataId=" + contractId + "&mark=" + createTime, JSONObject.class); + } catch (RestClientException e) { + e.printStackTrace(); + } + + System.out.println(isLogRes); + + if (!isLogRes.getJSONArray("data").isEmpty()) { + log.info("当前数据已经执行过了,将不再向下执行"); + continue; + } + + log.info("没有集成过,将继续向下执行"); + + //签订日期 + long documentDate = contractData.getLong("contract_time"); + + Date date = new Date(documentDate); + + //开始查询项目号(商机) + JSONObject projectReq = crmRequestUtil.getCRMList(Arrays.asList( + new Filter("EQ", "_id", Arrays.asList(contractData.getString("opportunity_name__c"))) + ), "NewOpportunityObj"); + + JSONObject projectRes = new JSONObject(); + + try { + projectRes = restTemplate.postForObject(URLConstant.GET_CUSTOMIZE_LIST_URL, projectReq, JSONObject.class); + } catch (RestClientException e) { + e.printStackTrace(); + } + + if (!"success".equals(projectRes.getString("errorDescription")) || projectRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) { + log.info("项目号不存在,将不再向下执行"); + continue; + } + + String projectCode = projectRes.getJSONObject("data").getJSONArray("dataList").getJSONObject(0).getString("opportunity_id__c"); + + //开始封装传输至ERP的请求 + Map erpContractReq = new HashMap<>(); + erpContractReq.put("billno", contractData.getString("name"));//预签合同编号 + erpContractReq.put("crmid", contractData.getString("_id"));//CRM唯一性编码 + erpContractReq.put("project_number", projectCode);//项目号 + erpContractReq.put("zhybilltype", contractData.getString("contract_type__c"));//合同类型 + //erpContractReq.put("project_number", "SJ20250424-0033");//项目号 + erpContractReq.put("billtype_number", "conm_salcontract_BT_QD");//单据类型 + erpContractReq.put("billname", contractData.getString("contract_name__c"));//合同名称 + erpContractReq.put("type_number", "XSHT-QD01");//合同类型 + erpContractReq.put("contparties_number", "BU-0002");//合同主体 + erpContractReq.put("createorg_number", "zyierp");//销售组织编码 + erpContractReq.put("org_number", "zyierp");//销售组织编码 + 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", contractData.getString("account_id__r"));//甲方 + erpContractReq.put("party2nd", contractData.getString("our_company_name__c__r"));//乙方 + erpContractReq.put("bizmode", "C");//业务模式 A:统谈统签, B:统谈分签,C:分谈分签 + erpContractReq.put("totalallamount", 0);//价税合计 + erpContractReq.put("deviceqty", 0);//价税合计 + + //部门编码 + String deptCode = ""; + + List deptIdList = contractData.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", deptCode);//归属部门 + + //获取负责人编码 + String ownerCode = ""; + + List ownerIdList = contractData.getJSONArray("owner"); + + JSONObject ownerReq = crmRequestUtil.getCRMList(Arrays.asList( + new Filter("EQ", "_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", "ZHY12025");//todo 负责人 + + //合同单位 + String customerId = contractData.getString("account_id"); + + //根据客户唯一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("customer_number", "Cus-000007");//合同单位名称 + + //获取产品线 + erpContractReq.put("productline_number", contractData.getString("product_line__c"));//产品线 + + //获取具体安装方式 + erpContractReq.put("installationmethod", contractData.getString("installation_method__c"));//安装方式 + + //行业类型 + erpContractReq.put("industrytype_number", contractData.getString("industry_type__c"));//行业类型 + erpContractReq.put("province", contractData.getString("field_9IuQE__c__r"));//省 + erpContractReq.put("city", contractData.getString("field_9IuQE__c__r"));//市 + erpContractReq.put("county", contractData.getString("field_e9XrV__c"));//县 + erpContractReq.put("warranty", contractData.getBigDecimal("warranty_period_months__c") != null ? contractData.getBigDecimal("warranty_period_months__c") : 0);//质保期 + + //是否招投标 + Boolean isBidding = null; + + switch (contractData.getString("is_bidding__c")) { + case "yes": + isBidding = true; + break; + case "no": + isBidding = false; + break; + default: + isBidding = null; + break; + } + erpContractReq.put("isbidding", isBidding); + + erpContractReq.put("customerpropert", contractData.getString("customer_attribute__c"));//客户属性 + erpContractReq.put("station", contractData.getString("bureau_level__c"));//局级 + + //款到发货 + Boolean delivery = null; + + switch (contractData.getString("ship_on_payment_received__c")) { + case "yes": + delivery = true; + break; + case "no": + delivery = false; + break; + default: + delivery = null; + break; + } + erpContractReq.put("delivery", delivery); + + //项目来源 + erpContractReq.put("projectsource", contractData.getString("project_source__c")); + + //销售方式 + erpContractReq.put("salesmethod", contractData.getString("sales_method__c")); + + //根据合同号查询合同明细 + JSONObject contractDetailReq = crmRequestUtil.getCRMList(Arrays.asList( + new Filter("EQ", "sale_contract_id", Arrays.asList(contractId)) + ), "SaleContractLineObj"); + + JSONObject contractDetailRes = new JSONObject(); + + try { + contractDetailRes = restTemplate.postForObject(URLConstant.GET_CRM_LIST_URL, contractDetailReq, JSONObject.class); + } catch (RestClientException e) { + e.printStackTrace(); + } + + if (!"success".equals(contractDetailRes.getString("errorDescription")) || contractDetailRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) { + log.info("合同明细查询失败或者不存在,直接跳过"); + continue; + } + + //合同明细存在,继续向下执行 + List contractDetailList = new ArrayList<>(); + + for (Object contractDetailObj : contractDetailRes.getJSONObject("data").getJSONArray("dataList")) { + JSONObject contractDetailData = JSON.parseObject(JSON.toJSONString(contractDetailObj)); + + System.out.println("当前正在处理的明细为:" + contractDetailData); + + //开始封装销售合同明细 + Map contractDetailMap = new HashMap(); + contractDetailMap.put("materialname", contractDetailData.getString("product_name__c"));//产品名称 + //contractDetailMap.put("material_number", contractDetailData.getString("product_id__r"));//物料编号 + contractDetailMap.put("material_number", "TEST0001");//物料编号 + contractDetailMap.put("specifications", contractDetailData.getString("specification__c") != null ? contractDetailData.getString("specification__c") : "");//规格 + contractDetailMap.put("baseunit", contractDetailData.getString("unit__c__r") != null ? contractDetailData.getString("unit__c__r") : "");//单位 + //contractDetailMap.put("baseunit", "TEST0001");//todo 单位 + contractDetailMap.put("unit_number", "pcs");//单位编码 + contractDetailMap.put("baseqty", contractDetailData.getBigDecimal("quantity") != null ? contractDetailData.getBigDecimal("quantity") : 0);//数量 + contractDetailMap.put("qty", contractDetailData.getBigDecimal("quantity") != null ? contractDetailData.getBigDecimal("quantity") : 0);//数量 + contractDetailMap.put("priceandtax", contractDetailData.getBigDecimal("quantity") != null ? contractDetailData.getBigDecimal("quantity") : 0);//含税单价 + //contractDetailMap.put("taxrateid_number", contractDetailData.getBigDecimal("tax_rate__c") != null ? contractDetailData.getBigDecimal("tax_rate__c") : 0);//税率 + contractDetailMap.put("taxrateid_number", "V13");//税率 + contractDetailMap.put("curamountandtax", contractDetailData.getBigDecimal("subtotal") != null ? contractDetailData.getBigDecimal("subtotal") : 0);//价税合计 + contractDetailMap.put("curtaxamount", contractDetailData.getBigDecimal("tax_amount__c") != null ? contractDetailData.getBigDecimal("tax_amount__c") : 0);//金额 + contractDetailMap.put("crmentryid", contractDetailData.getString("_id"));//明细id + + contractDetailList.add(contractDetailMap); + + } + + //开始封装明细 + erpContractReq.put("billentry", contractDetailList); + + System.out.println("erp请求为:" + JSON.parseObject(JSON.toJSONString(erpContractReq))); + + 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 = "http://192.168.31.233:8022/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(); + } + + System.out.println(JSON.parseObject(JSON.toJSONString(contractRes))); + + //开始封装回写程序 + Map backReqMap = new HashMap(); + backReqMap.put("_id", contractId); + backReqMap.put("dataObjectApiName", "SaleContractObj"); + + //开始封装日志 + Map logMap = new HashMap<>(); + logMap.put("log_id", UUID.randomUUID().toString().replace("-", "")); + logMap.put("log_type", "XSHT"); + logMap.put("syn_type", "0"); + logMap.put("data_name", contractName); + logMap.put("data_id", contractId); + 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", "同步成功"); + } else { + //同步失败 + logMap.put("log_status", "1"); + logMap.put("res_body", "同步失败:" + contractRes.getString("message")); + backReqMap.put("sync_status__c", "k2JhS69IK"); + backReqMap.put("response_info__c", contractRes.getString("message")); + } + + //开始回写 + JSONObject backReq = crmRequestUtil.updateCRM(backReqMap); + + System.out.println("回写请求为:" + JSON.parseObject(JSON.toJSONString(backReq))); + + JSONObject backRes = new JSONObject(); + + try { + backRes = restTemplate.postForObject(URLConstant.UPDATE_CRM_MAIN, backReq, JSONObject.class); + } catch (RestClientException e) { + e.printStackTrace(); + } + + System.out.println("回写结果为:" + backRes); + + //开始记录日志 + JSONObject logRes = new JSONObject(); + try { + logRes = restTemplate.postForObject("http://localhost:18088/Log/insert/log_data", logMap, JSONObject.class); + } catch (RestClientException e) { + e.printStackTrace(); + } + + System.out.println("日志插入结果为:" + logRes); + + } + + } +}