243 lines
10 KiB
Java
243 lines
10 KiB
Java
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.Arrays;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/tendering")
|
|
@Slf4j
|
|
public class TenderingController {
|
|
|
|
private RestTemplate restTemplate = new RestTemplate();
|
|
|
|
public static void main(String[] args) {
|
|
new TenderingController().syncTendering();
|
|
}
|
|
|
|
/**
|
|
* 投标费用
|
|
*
|
|
* @return void
|
|
* @Author weiloong_zhang
|
|
*/
|
|
@PostMapping("/sync/tendering")
|
|
public void syncTendering() {
|
|
CrmRequestUtil crmRequestUtil = new CrmRequestUtil();
|
|
|
|
String[] syncStatus = {"option_pending_sync__c", "option_resync__c"};
|
|
|
|
JSONObject getTendering = crmRequestUtil.getCRMList(Arrays.asList(
|
|
//new Filter("IS", "erp_id__c", Arrays.asList("")),
|
|
new Filter("IN", "sync_status__c", Arrays.asList(syncStatus)),
|
|
new Filter("EQ", "life_status", Arrays.asList("normal")),
|
|
//new Filter("EQ", "name", Arrays.asList("BSGM250509")),
|
|
new Filter("GT", "create_time", Arrays.asList("1748923200000"))
|
|
), "bid_application__c");
|
|
|
|
JSONObject tenderingRes = new JSONObject();
|
|
|
|
//开始查询
|
|
try {
|
|
tenderingRes = restTemplate.postForObject(URLConstant.GET_CUSTOMIZE_LIST_URL, getTendering, JSONObject.class);
|
|
} catch (RestClientException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
if (!"success".equals(tenderingRes.getString("errorDescription")) || tenderingRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
|
|
log.info("投标费用请求失败或者没有符合条件的数据");
|
|
return;
|
|
}
|
|
|
|
log.info("投标费用请求成功,继续向下执行");
|
|
|
|
for (Object tenderingObj : tenderingRes.getJSONObject("data").getJSONArray("dataList")) {
|
|
JSONObject tenderingData = JSON.parseObject(JSON.toJSONString(tenderingObj));
|
|
|
|
System.out.println("当前正在处理数据为:" + tenderingData);
|
|
|
|
//开始截取关键数据
|
|
//唯一性ID
|
|
String tenderingId = tenderingData.getString("_id");
|
|
//投标单号
|
|
String tenderingName = tenderingData.getString("name");
|
|
//投标单创建时间
|
|
//String createTime = tenderingData.getString("create_time");
|
|
String createTime = tenderingData.getString("last_modified_time");
|
|
|
|
//开始判断是否集成过该单据了
|
|
JSONObject isLogRes = new JSONObject();
|
|
|
|
try {
|
|
isLogRes = restTemplate.getForObject("http://localhost:18085/Log/query/log_data?table=send_log_tendering&log_type=TBFY&dataId=" + tenderingId + "&mark=" + createTime, JSONObject.class);
|
|
} catch (RestClientException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
if (!isLogRes.getJSONArray("data").isEmpty()) {
|
|
log.info("当前投标单存在日志,将不再向下执行");
|
|
continue;
|
|
}
|
|
|
|
log.info("不存在日志,继续向下执行");
|
|
|
|
//开始封装请求
|
|
Map tenderingMapReq = new HashMap<>();
|
|
tenderingMapReq.put("number", tenderingData.getString("field_31pd4__c"));//标书购买申请流水号
|
|
tenderingMapReq.put("f9w5_crmid", tenderingId);//标书购买申请ID
|
|
tenderingMapReq.put("name", tenderingData.getString("opportunity_name__c__r") != null ? tenderingData.getString("opportunity_name__c__r") : "");//商机名称
|
|
|
|
//申请类型
|
|
String applyType = tenderingData.getString("application_type__c") != null ? tenderingData.getString("application_type__c") : "";
|
|
|
|
String applyTypeStr = "";
|
|
|
|
switch (applyType) {
|
|
case "cNKcx460m":
|
|
applyTypeStr = "A";
|
|
break;
|
|
case "option1":
|
|
applyTypeStr = "B";
|
|
break;
|
|
default:
|
|
applyTypeStr = "";
|
|
}
|
|
|
|
tenderingMapReq.put("f9w5_applytype", applyTypeStr);//申请类型
|
|
tenderingMapReq.put("f9w5_lot", tenderingData.getString("batch_name__c") != null ? tenderingData.getString("batch_name__c") : "");//批次名称
|
|
tenderingMapReq.put("createorg_number", tenderingData.getString("erp_organization__c__v") != null ? tenderingData.getString("erp_organization__c__v") : "");//组织
|
|
|
|
//供应商ID
|
|
String vendorId = tenderingData.getString("bidding_agency__c") != null ? tenderingData.getString("bidding_agency__c") : "";
|
|
|
|
String vendorErpCode = "";
|
|
if (vendorId != null && !vendorId.equals("")) {
|
|
JSONObject getVendor = crmRequestUtil.getCRMList(Arrays.asList(
|
|
new Filter("EQ", "_id", Arrays.asList(vendorId))
|
|
), "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()) {
|
|
vendorErpCode = vendorRes.getJSONObject("data").getJSONArray("dataList").getJSONObject(0).getString("erp_number__c");
|
|
}
|
|
}
|
|
|
|
tenderingMapReq.put("f9w5_institution_number", vendorErpCode);//招投标代理机构
|
|
|
|
Map data = new HashMap<>();
|
|
data.put("data", tenderingMapReq);
|
|
|
|
//开始获取ERP的Token
|
|
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 tenderingReqEntity = new HttpEntity(data, headers);
|
|
|
|
System.out.println(JSON.parseObject(JSON.toJSONString(tenderingReqEntity)));
|
|
|
|
String tenderingUrl = ERPURLConstant.ERP_URL + "/ierp/kapi/v2/f9w5/em/f9w5_tenderbuyapply/save";
|
|
|
|
JSONObject tenderingERPRes = new JSONObject();
|
|
|
|
try {
|
|
tenderingERPRes = restTemplate.postForObject(tenderingUrl, tenderingReqEntity, JSONObject.class);
|
|
} catch (RestClientException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
System.out.println(tenderingERPRes);
|
|
|
|
//开始封装回写程序
|
|
Map backReqMap = new HashMap();
|
|
backReqMap.put("_id", tenderingId);
|
|
backReqMap.put("dataObjectApiName", "bid_application__c");
|
|
|
|
//开始封装日志
|
|
Map logMap = new HashMap<>();
|
|
logMap.put("log_id", UUID.randomUUID().toString().replace("-", ""));
|
|
logMap.put("log_type", "TBFY");
|
|
logMap.put("syn_type", "0");
|
|
logMap.put("data_name", tenderingName);
|
|
logMap.put("data_id", tenderingId);
|
|
logMap.put("mark", createTime);
|
|
logMap.put("send_body", JSON.toJSONString(tenderingReqEntity));
|
|
logMap.put("send_res", JSON.toJSONString(tenderingERPRes));
|
|
logMap.put("tableName", "send_log_tendering");
|
|
|
|
//判断是否执行成功
|
|
if ("0".equals(tenderingERPRes.getString("errorCode"))) {
|
|
//同步成功
|
|
logMap.put("log_status", "0");
|
|
logMap.put("res_body", "同步成功");
|
|
backReqMap.put("erp_id__c", tenderingERPRes.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", "同步失败:" + tenderingERPRes.getString("message"));
|
|
backReqMap.put("sync_status__c", "option_sync_failure__c");
|
|
backReqMap.put("response_status__c", tenderingERPRes.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);
|
|
|
|
}
|
|
}
|
|
}
|