2025-06-26 20:52:12 +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 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 java.util.*;
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("/api/vendor")
|
|
|
|
@Slf4j
|
|
|
|
public class VendorController {
|
|
|
|
|
|
|
|
private RestTemplate restTemplate = new RestTemplate();
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
new VendorController().syncVendor();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 供应商信息同步
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @Author weiloong_zhang
|
|
|
|
*/
|
|
|
|
@PostMapping("/sync/create_vendor")
|
|
|
|
public void syncVendor() {
|
|
|
|
CrmRequestUtil crmRequestUtil = new CrmRequestUtil();
|
|
|
|
|
|
|
|
JSONObject getVendor = crmRequestUtil.getCRMList(Arrays.asList(
|
|
|
|
new Filter("EQ", "life_status", Arrays.asList("normal")),
|
2025-06-28 08:55:13 +08:00
|
|
|
new Filter("EQ", "record_type", Arrays.asList("record_ev1gl__c")),
|
2025-06-27 22:50:21 +08:00
|
|
|
//new Filter("EQ", "name", Arrays.asList("山东研了究的僧教育科技有限公司")),
|
|
|
|
new Filter("EQ", "sync_status__c", Arrays.asList("option_pending_sync__c")),
|
|
|
|
new Filter("GT", "create_time", Arrays.asList("1751035349267"))
|
2025-06-26 20:52:12 +08:00
|
|
|
), "SupplierObj");
|
|
|
|
|
|
|
|
JSONObject vendorRes = new JSONObject();
|
|
|
|
|
|
|
|
try {
|
|
|
|
vendorRes = restTemplate.postForObject(URLConstant.GET_CUSTOMIZE_LIST_URL, getVendor, JSONObject.class);
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!"success".equals(vendorRes.getString("errorDescription")) || vendorRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
|
|
|
|
log.info("供应商不存在");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info("供应商存在,将继续向下执行");
|
|
|
|
|
|
|
|
for (Object vendorObj : vendorRes.getJSONObject("data").getJSONArray("dataList")) {
|
|
|
|
JSONObject vendorData = JSON.parseObject(JSON.toJSONString(vendorObj));
|
|
|
|
|
|
|
|
System.out.println("当前正在处理的供应商数据为:" + vendorData);
|
|
|
|
|
|
|
|
//开始截取关键数据
|
|
|
|
String vendorId = vendorData.getString("_id");
|
|
|
|
String vendorName = vendorData.getString("name");
|
|
|
|
String createTime = vendorData.getString("last_modified_time");
|
|
|
|
|
|
|
|
//开始查询日子是否存在当前单据
|
|
|
|
JSONObject isLog = new JSONObject();
|
|
|
|
|
|
|
|
try {
|
|
|
|
isLog = restTemplate.getForObject("http://localhost:18085/Log/query/log_data?table=send_log_vendor&log_type=供应商&dataId=" + vendorId + "&mark=" + createTime, JSONObject.class);
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isLog.getJSONArray("data").isEmpty()) {
|
|
|
|
log.info("当前单据已经集成过了,将不再向下执行");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info("当前单据没有集成过,将开始向下执行");
|
|
|
|
|
|
|
|
Map vendorMap = new HashMap<>();
|
|
|
|
vendorMap.put("number", vendorData.getString("construction_unit_id__c"));
|
|
|
|
vendorMap.put("name", vendorName);
|
|
|
|
//简称
|
|
|
|
vendorMap.put("simplename", vendorData.getString("supplier_id") != null ? vendorData.getString("supplier_id") : vendorName);
|
|
|
|
//统一社会信用代码
|
|
|
|
vendorMap.put("societycreditcode", vendorData.getString("tax_identification_number") != null ? vendorData.getString("tax_identification_number") : "");
|
|
|
|
//纳税人识别号
|
|
|
|
vendorMap.put("tx_register_no", vendorData.getString("tax_identification_number") != null ? vendorData.getString("tax_identification_number") : "");
|
2025-06-27 17:12:16 +08:00
|
|
|
//U8C编码
|
|
|
|
vendorMap.put("f9w5_u8cnumber", vendorData.getString("historical_code__c") != null ? vendorData.getString("historical_code__c") : "");
|
2025-06-26 20:52:12 +08:00
|
|
|
|
|
|
|
// //联系人封装
|
|
|
|
// Map contactMap = new HashMap<>();
|
|
|
|
// //联系人名称
|
|
|
|
// contactMap.put("contactperson", "");
|
|
|
|
// //联系人手机
|
|
|
|
// contactMap.put("phone", vendorData.getString("mobile") != null ? vendorData.getString("mobile") : "");
|
|
|
|
|
|
|
|
//银行分录
|
|
|
|
Map bankEntryMap = new HashMap<>();
|
|
|
|
//银行账户
|
|
|
|
bankEntryMap.put("bankaccount", vendorData.getString("bank") != null ? vendorData.getString("bank") : "");
|
|
|
|
//账户名称
|
|
|
|
bankEntryMap.put("accountname", vendorData.getString("bank_account") != null ? vendorData.getString("bank_account") : "");
|
|
|
|
//银行信息分录默认
|
|
|
|
bankEntryMap.put("isdefault_bank", "1");
|
|
|
|
//货币代码
|
|
|
|
bankEntryMap.put("currency_number", "CNY");
|
|
|
|
//开户银行
|
|
|
|
bankEntryMap.put("bank_number", vendorData.getString("bank_name__c__r") != null ? vendorData.getString("bank_name__c__r") : "");
|
|
|
|
|
|
|
|
vendorMap.put("entry_bank", Arrays.asList(bankEntryMap));
|
|
|
|
|
|
|
|
//分类标准
|
|
|
|
Map standardMap = new HashMap<>();
|
|
|
|
//分类编码
|
|
|
|
standardMap.put("groupid_number", vendorData.getString("find_association__c__r") != null ? vendorData.getString("find_association__c__r") : "");
|
|
|
|
//分类标准编码
|
|
|
|
standardMap.put("standardid_number", "JBFLBZ");
|
|
|
|
|
|
|
|
vendorMap.put("standard", Arrays.asList(standardMap));
|
|
|
|
|
|
|
|
//交易币
|
|
|
|
vendorMap.put("settlementcyid_number", "CNY");
|
|
|
|
|
|
|
|
//创建组织
|
|
|
|
//vendorMap.put("createorg_number", "zyierp");//归属部门
|
|
|
|
vendorMap.put("createorg_number", "ZHY");//归属部门
|
|
|
|
|
|
|
|
vendorMap.put("status", "C");//数据状态
|
|
|
|
vendorMap.put("enable", "1");//使用状态
|
|
|
|
|
|
|
|
//供应商最终请求
|
|
|
|
Map vendorReq = new HashMap<>();
|
|
|
|
vendorReq.put("data", Arrays.asList(vendorMap));
|
|
|
|
|
|
|
|
//开始获取token
|
2025-06-27 22:50:21 +08:00
|
|
|
String accessToken = new KDTokenController().getKDAccessToken();
|
2025-06-26 20:52:12 +08:00
|
|
|
|
|
|
|
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 vendorReqEntity = new HttpEntity(vendorReq, headers);
|
|
|
|
|
|
|
|
System.out.println(JSON.parseObject(JSON.toJSONString(vendorReqEntity)));
|
|
|
|
|
2025-06-27 22:50:21 +08:00
|
|
|
//String vendorUrl = ERPURLConstant.ERP_URL + "/ierp/kapi/v2/f9w5/basedata/bd_supplier/bdSupplierSave";
|
|
|
|
String vendorUrl = "https://lxr2.zhydsp.cn:40448/ierp/kapi/v2/f9w5/basedata/bd_supplier/bdSupplierSave";
|
2025-06-26 20:52:12 +08:00
|
|
|
|
|
|
|
JSONObject vendorERPRes = new JSONObject();
|
|
|
|
|
|
|
|
try {
|
|
|
|
vendorERPRes = restTemplate.postForObject(vendorUrl, vendorReqEntity, JSONObject.class);
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println(vendorERPRes);
|
|
|
|
|
|
|
|
//开始封装回写程序
|
|
|
|
Map backReqMap = new HashMap();
|
|
|
|
backReqMap.put("_id", vendorId);
|
|
|
|
backReqMap.put("dataObjectApiName", "SupplierObj");
|
|
|
|
|
|
|
|
//开始封装日志
|
|
|
|
Map logMap = new HashMap<>();
|
|
|
|
logMap.put("log_id", UUID.randomUUID().toString().replace("-", ""));
|
|
|
|
logMap.put("log_type", "供应商");
|
|
|
|
logMap.put("syn_type", "0");
|
|
|
|
logMap.put("data_name", vendorName);
|
|
|
|
logMap.put("data_id", vendorId);
|
|
|
|
logMap.put("mark", createTime);
|
|
|
|
logMap.put("send_body", JSON.toJSONString(vendorReqEntity));
|
|
|
|
logMap.put("send_res", JSON.toJSONString(vendorERPRes));
|
|
|
|
logMap.put("tableName", "send_log_vendor");
|
|
|
|
|
|
|
|
//判断是否执行成功
|
|
|
|
if ("0".equals(vendorERPRes.getString("errorCode"))) {
|
|
|
|
//同步成功
|
|
|
|
logMap.put("log_status", "0");
|
|
|
|
logMap.put("res_body", "同步成功");
|
|
|
|
backReqMap.put("erp_id__c", vendorERPRes.getJSONObject("data").getJSONArray("result").getJSONObject(0).getString("id"));
|
|
|
|
backReqMap.put("sync_status__c", "option_sync_success__c");
|
|
|
|
backReqMap.put("response_status__c", "同步成功");
|
|
|
|
} else {
|
|
|
|
//同步失败
|
|
|
|
logMap.put("log_status", "1");
|
|
|
|
logMap.put("res_body", "同步失败:" + vendorERPRes.getString("message"));
|
|
|
|
backReqMap.put("sync_status__c", "option_sync_failure__c");
|
|
|
|
backReqMap.put("response_status__c", vendorERPRes.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);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|