|
|
|
@ -187,9 +187,6 @@ public class ErpInterfaceServiceImpl implements ErpInterfaceService { |
|
|
|
} else if ("GetSaveroductionIn".equals(interfaceName)) { |
|
|
|
// 生产入库 |
|
|
|
requestBody = buildProductionInRequestBody(transactionLogs, interfaceName); |
|
|
|
} else { |
|
|
|
// 默认格式 |
|
|
|
//requestBody = buildDefaultRequestBody(transactionLogs, interfaceName); |
|
|
|
} |
|
|
|
|
|
|
|
return requestBody; |
|
|
|
@ -203,7 +200,7 @@ public class ErpInterfaceServiceImpl implements ErpInterfaceService { |
|
|
|
Map<String, Object> requestBody = new LinkedHashMap<>(); |
|
|
|
|
|
|
|
// 主表字段 |
|
|
|
requestBody.put("MESCCode", nullToEmpty(firstLog.getDocumentNo())); |
|
|
|
requestBody.put("MESCCode", nullToEmpty(firstLog.getTransactionId())); |
|
|
|
requestBody.put("KdType", getKdType(interfaceName)); |
|
|
|
requestBody.put("DDate", formatDate(firstLog.getTransactionDate())); |
|
|
|
requestBody.put("CRdCode", nullToEmpty(firstLog.getDocumentNoType())); |
|
|
|
@ -248,7 +245,7 @@ public class ErpInterfaceServiceImpl implements ErpInterfaceService { |
|
|
|
Map<String, Object> requestBody = new LinkedHashMap<>(); |
|
|
|
|
|
|
|
// 主表字段 |
|
|
|
requestBody.put("MESCCode", nullToEmpty(firstLog.getDocumentNo())); |
|
|
|
requestBody.put("MESCCode", nullToEmpty(firstLog.getTransactionId())); |
|
|
|
requestBody.put("KdType", getKdType(interfaceName)); |
|
|
|
requestBody.put("DDate", formatDate(firstLog.getTransactionDate())); |
|
|
|
requestBody.put("CBusType", ""); |
|
|
|
@ -327,7 +324,7 @@ public class ErpInterfaceServiceImpl implements ErpInterfaceService { |
|
|
|
Map<String, Object> requestBody = new LinkedHashMap<>(); |
|
|
|
|
|
|
|
// 主表字段 |
|
|
|
requestBody.put("MESCCode", nullToEmpty(firstLog.getDocumentNo())); |
|
|
|
requestBody.put("MESCCode", nullToEmpty(firstLog.getTransactionId())); |
|
|
|
requestBody.put("KdType", getKdType(interfaceName)); |
|
|
|
requestBody.put("DDate", formatDate(firstLog.getTransactionDate())); |
|
|
|
requestBody.put("CWhCode", nullToEmpty(firstLog.getWarehouseId())); |
|
|
|
@ -363,7 +360,7 @@ public class ErpInterfaceServiceImpl implements ErpInterfaceService { |
|
|
|
Map<String, Object> requestBody = new LinkedHashMap<>(); |
|
|
|
|
|
|
|
// 主表字段 |
|
|
|
requestBody.put("MESCCode", nullToEmpty(firstLog.getDocumentNo())); |
|
|
|
requestBody.put("MESCCode", nullToEmpty(firstLog.getTransactionId())); |
|
|
|
requestBody.put("DDate", formatDate(firstLog.getTransactionDate())); |
|
|
|
requestBody.put("CDepCode", nullToEmpty(firstLog.getDepartmentId())); |
|
|
|
requestBody.put("CRdCode", nullToEmpty(firstLog.getDocumentNoType())); |
|
|
|
@ -392,45 +389,6 @@ public class ErpInterfaceServiceImpl implements ErpInterfaceService { |
|
|
|
return requestBody; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 默认请求参数格式 |
|
|
|
*/ |
|
|
|
private Map<String, Object> buildDefaultRequestBody(List<StockTransactionLogEntity> transactionLogs, String interfaceName) { |
|
|
|
StockTransactionLogEntity firstLog = transactionLogs.get(0); |
|
|
|
Map<String, Object> requestBody = new LinkedHashMap<>(); |
|
|
|
|
|
|
|
// 主表字段 |
|
|
|
requestBody.put("MESCCode", nullToEmpty(firstLog.getDocumentNo())); |
|
|
|
requestBody.put("KdType", getKdType(interfaceName)); |
|
|
|
requestBody.put("DDate", formatDate(firstLog.getTransactionDate())); |
|
|
|
requestBody.put("CRdCode", nullToEmpty(firstLog.getDocumentNoType())); |
|
|
|
requestBody.put("CMemo", ""); |
|
|
|
requestBody.put("CWhCode", nullToEmpty(firstLog.getWarehouseId())); |
|
|
|
requestBody.put("CCusOAddress", ""); |
|
|
|
requestBody.put("CDepCode", nullToEmpty(firstLog.getDepartmentId())); |
|
|
|
|
|
|
|
// 明细列表 |
|
|
|
List<Map<String, Object>> detailList = new ArrayList<>(); |
|
|
|
for (StockTransactionLogEntity log : transactionLogs) { |
|
|
|
Map<String, Object> detail = new LinkedHashMap<>(); |
|
|
|
detail.put("MESIrowNo", String.valueOf(log.getRowNo())); |
|
|
|
detail.put("OutCode", nullToEmpty(log.getOrderNo())); |
|
|
|
detail.put("OutIrowNo", nullToEmpty(log.getOrderLineNo())); |
|
|
|
detail.put("CInvCode", nullToEmpty(log.getPartNo())); |
|
|
|
detail.put("IQuantity", log.getRollQty() != null ? log.getRollQty().toString() : "0"); |
|
|
|
detail.put("CBatch", nullToEmpty(log.getBatchFlag())); |
|
|
|
detail.put("CbMemo", ""); |
|
|
|
detail.put("CWhCode", nullToEmpty(log.getWarehouseId())); |
|
|
|
detail.put("OutMocode", nullToEmpty(log.getOrderNo())); |
|
|
|
detail.put("OutIrowNo_zj", nullToEmpty(log.getBomItemNo())); |
|
|
|
detail.put("NumberOfCases", ""); |
|
|
|
detailList.add(detail); |
|
|
|
} |
|
|
|
requestBody.put("DetailList", detailList); |
|
|
|
|
|
|
|
return requestBody; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 将null转换为空字符串 |
|
|
|
*/ |
|
|
|
@ -684,6 +642,127 @@ public class ErpInterfaceServiceImpl implements ErpInterfaceService { |
|
|
|
return detailEntity; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 按顺序异步调用ERP接口(用于跨区调拨等需要保证顺序的场景) |
|
|
|
* 先调用出库接口,等待完成后再调用入库接口 |
|
|
|
*/ |
|
|
|
@Async("erpInterfaceExecutor") |
|
|
|
@Override |
|
|
|
public void asyncCallErpInterfaceSequentially(String site, String buNo, String outboundTransactionId, String inboundTransactionId) { |
|
|
|
logger.info("开始按顺序异步调用ERP接口 - site: {}, buNo: {}, 出库transactionId: {}, 入库transactionId: {}", |
|
|
|
site, buNo, outboundTransactionId, inboundTransactionId); |
|
|
|
|
|
|
|
try { |
|
|
|
// 1. 先调用出库接口(如果需要) |
|
|
|
if (outboundTransactionId != null && !outboundTransactionId.trim().isEmpty()) { |
|
|
|
logger.info("【顺序调用-步骤1】开始调用出库ERP接口 - transactionId: {}", outboundTransactionId); |
|
|
|
callErpInterfaceSync(site, buNo, outboundTransactionId); |
|
|
|
logger.info("【顺序调用-步骤1】出库ERP接口调用完成 - transactionId: {}", outboundTransactionId); |
|
|
|
} else { |
|
|
|
logger.info("【顺序调用-步骤1】跳过出库ERP接口调用(transactionId为空)"); |
|
|
|
} |
|
|
|
|
|
|
|
// 2. 再调用入库接口(如果需要) |
|
|
|
if (inboundTransactionId != null && !inboundTransactionId.trim().isEmpty()) { |
|
|
|
logger.info("【顺序调用-步骤2】开始调用入库ERP接口 - transactionId: {}", inboundTransactionId); |
|
|
|
callErpInterfaceSync(site, buNo, inboundTransactionId); |
|
|
|
logger.info("【顺序调用-步骤2】入库ERP接口调用完成 - transactionId: {}", inboundTransactionId); |
|
|
|
} else { |
|
|
|
logger.info("【顺序调用-步骤2】跳过入库ERP接口调用(transactionId为空)"); |
|
|
|
} |
|
|
|
|
|
|
|
logger.info("按顺序异步调用ERP接口全部完成 - site: {}, buNo: {}", site, buNo); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
logger.error("按顺序异步调用ERP接口失败 - site: {}, buNo: {}, error: {}", site, buNo, e.getMessage(), e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 同步调用ERP接口(内部方法,用于顺序调用) |
|
|
|
* 这个方法不使用@Async注解,确保在调用线程中同步执行 |
|
|
|
*/ |
|
|
|
private void callErpInterfaceSync(String site, String buNo, String transactionId) { |
|
|
|
logger.info("同步调用ERP接口 - site: {}, buNo: {}, transactionId: {}", site, buNo, transactionId); |
|
|
|
|
|
|
|
try { |
|
|
|
// 1. 查询StockTransactionLog数据 |
|
|
|
List<StockTransactionLogEntity> transactionLogs = stockTransactionLogDao.queryByTransactionId(site, buNo, transactionId); |
|
|
|
|
|
|
|
if (transactionLogs == null || transactionLogs.isEmpty()) { |
|
|
|
logger.warn("未找到事务记录 - transactionId: {}", transactionId); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// 取第一条记录作为主表数据 |
|
|
|
StockTransactionLogEntity firstLog = transactionLogs.get(0); |
|
|
|
String interfaceName = firstLog.getInterfaceName(); |
|
|
|
|
|
|
|
if (interfaceName == null || interfaceName.trim().isEmpty()) { |
|
|
|
logger.warn("接口名称为空 - transactionId: {}", transactionId); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// 2. 获取接口地址 |
|
|
|
ApiInterfaceEntity apiInterface = apiInterfaceDao.getByInterfaceName(site, buNo, interfaceName); |
|
|
|
if (apiInterface == null) { |
|
|
|
logger.error("未找到接口配置 - interfaceName: {}", interfaceName); |
|
|
|
saveFailureLog(site, buNo, transactionId, interfaceName, transactionLogs, "未找到接口配置"); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
String interfaceUrl = "http://" + apiInterface.getInterfaceIp() + apiInterface.getInterfaceValue(); |
|
|
|
logger.info("接口地址: {}", interfaceUrl); |
|
|
|
|
|
|
|
// 3. 组装接口参数 |
|
|
|
Map<String, Object> requestBody = buildRequestBody(transactionLogs); |
|
|
|
String requestJson = JSON.toJSONString(requestBody); |
|
|
|
logger.info("请求参数: {}", requestJson); |
|
|
|
|
|
|
|
// 4. 调用ERP接口 |
|
|
|
HttpHeaders headers = new HttpHeaders(); |
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON); |
|
|
|
HttpEntity<String> requestEntity = new HttpEntity<>(requestJson, headers); |
|
|
|
|
|
|
|
try { |
|
|
|
ResponseEntity<String> response = restTemplate.exchange( |
|
|
|
interfaceUrl, |
|
|
|
HttpMethod.POST, |
|
|
|
requestEntity, |
|
|
|
String.class |
|
|
|
); |
|
|
|
|
|
|
|
String responseBody = response.getBody(); |
|
|
|
logger.info("接口返回: {}", responseBody); |
|
|
|
|
|
|
|
// 5. 解析返回结果 |
|
|
|
JSONObject jsonResponse = JSON.parseObject(responseBody); |
|
|
|
String flag = jsonResponse.getString("Flag"); |
|
|
|
|
|
|
|
if ("success".equalsIgnoreCase(flag)) { |
|
|
|
// 成功:更新StockTransactionLog |
|
|
|
String u8CCode = jsonResponse.getString("U8CCode"); |
|
|
|
logger.info("ERP接口调用成功 - U8单号: {}", u8CCode); |
|
|
|
|
|
|
|
stockTransactionLogDao.updateSyncedSuccess(site, buNo, transactionId); |
|
|
|
} else { |
|
|
|
// 失败:记录到api_log |
|
|
|
String errMsg = jsonResponse.getString("ErrMsg"); |
|
|
|
logger.error("ERP接口调用失败 - 错误: {}", errMsg); |
|
|
|
|
|
|
|
saveFailureLog(site, buNo, transactionId, interfaceName, transactionLogs, errMsg); |
|
|
|
} |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
logger.error("调用ERP接口异常 - transactionId: {}, error: {}", transactionId, e.getMessage(), e); |
|
|
|
saveFailureLog(site, buNo, transactionId, interfaceName, transactionLogs, "接口调用异常: " + e.getMessage()); |
|
|
|
} |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
logger.error("同步调用ERP接口失败 - transactionId: {}, error: {}", transactionId, e.getMessage(), e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 格式化日期 |
|
|
|
*/ |
|
|
|
|