diff --git a/int-api/src/main/java/com/foreverwin/iep/integration/feign/RemoteMesPostDnService.java b/int-api/src/main/java/com/foreverwin/iep/integration/feign/RemoteMesPostDnService.java new file mode 100644 index 0000000..b01fe06 --- /dev/null +++ b/int-api/src/main/java/com/foreverwin/iep/integration/feign/RemoteMesPostDnService.java @@ -0,0 +1,23 @@ +package com.foreverwin.iep.integration.feign; + +import com.alibaba.fastjson.JSONObject; +import com.foreverwin.mom.common.core.util.R; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +import java.util.Map; + +/** + * @program: mom-wms-biz + * @description: 公司间交货过账入库 + * @author: Snow + * @create: 2024-11-26 09:24:08 + */ +@FeignClient(contextId = "remoteMesPostDnService", value = "iep-int") +public interface RemoteMesPostDnService { + + @PostMapping("/mesPostDn/api/doExecute") + R> doExecute(@RequestBody JSONObject contentObject); + +} \ No newline at end of file diff --git a/int-core/src/main/java/com/foreverwin/iep/integration/controller/MesPostDnController.java b/int-core/src/main/java/com/foreverwin/iep/integration/controller/MesPostDnController.java new file mode 100644 index 0000000..0447717 --- /dev/null +++ b/int-core/src/main/java/com/foreverwin/iep/integration/controller/MesPostDnController.java @@ -0,0 +1,96 @@ +package com.foreverwin.iep.integration.controller; + +import com.alibaba.fastjson.JSONObject; +import com.foreverwin.iep.integration.domain.InvokeRequest; +import com.foreverwin.iep.integration.domain.InvokeResponse; +import com.foreverwin.iep.integration.handler.def.sap.MesPostDnService; +import com.foreverwin.iep.integration.handler.def.sap.SapUtil; +import com.foreverwin.mom.common.core.util.R; +import com.foreverwin.mom.common.data.tenant.TenantContextHolder; +import com.foreverwin.mom.common.log.annotation.SysLog; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.HashMap; +import java.util.Map; + +/** + * 公司间交货过账入库 + * + * @author Snow + * @date 2024-12-17 09:59:18 + */ +@RestController +@RequiredArgsConstructor +@RequestMapping("/mesPostDn") +@Api(value = "mesPostDn", tags = "公司间交货过账入库") +public class MesPostDnController { + + @Autowired + @Qualifier("ZMM_MES_POST_DN") + private final MesPostDnService mesPostDnService; + + private final String MESSAGE_ID = "ZMM_MES_POST_DN"; + + @ApiOperation(value = "公司间交货过账入库", notes = "公司间交货过账入库") + @SysLog("公司间交货过账入库") + @PostMapping("/api/doExecute") + public R doExecute(@RequestBody JSONObject contentObject) { + Map params = new HashMap<>(); + InvokeResponse invokeResponse = new InvokeResponse(); + InvokeRequest invokeRequest = new InvokeRequest(); + String site = TenantContextHolder.getTenantId(); + try { + // 发送给ERP执行 ZMM_MES_POST_DN 接口 + LocalDateTime nowLocalDateTime = LocalDateTime.now(); + String nowTime = nowLocalDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); + invokeRequest.setTransSid(SapUtil.generateReqId()); + invokeRequest.setTransTime(nowTime); // 设定过账日期 + invokeRequest.setFromSystem("IEP"); + invokeRequest.setToSystem("ERP"); + invokeRequest.setSite(site); + invokeRequest.setMessageId(MESSAGE_ID); + + // 组装抛送的报完呢 + String srcJsonBody = assembleMesPostDn(contentObject); + String jsonObjectStr = SapUtil.appendHead(srcJsonBody); + invokeRequest.setMessageContent(jsonObjectStr); + invokeRequest.setStart(nowTime); + invokeRequest.setKeyValue(contentObject.getString("IV_VBELN")); + invokeRequest.setRetry(false); + // 呼叫SAP + invokeResponse = mesPostDnService.call(invokeRequest); + } catch (Exception e) { + params.put("RESULT", "E"); + params.put("MSG", e.getMessage()); + } + // 如果处理失败,抛出异常,让业务逻辑回滚,但日志还是记录 + if ("E".equals(invokeResponse.getResult())) { + params.put("RESULT", "E"); + params.put("MSG", invokeResponse.getMessageContent()); + } + return R.ok(params); + } + + private String assembleMesPostDn(JSONObject requestBody) { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("RFC_FUNCTION", MESSAGE_ID); + JSONObject headRe = new JSONObject(); + headRe.put("EXCHANGE_SYSTEM", "MES"); + headRe.put("EXCHANGE_ID", SapUtil.generateReqId()); + headRe.put("EXCHANGE_DOC", "iMES_Call_SAP"); + jsonObject.put("REQUEST_CONTEXT", headRe); + jsonObject.put("REQUEST_BODY", requestBody); + return jsonObject.toJSONString(); + } + +} diff --git a/int-core/src/main/java/com/foreverwin/iep/integration/handler/def/sap/MesPostDnService.java b/int-core/src/main/java/com/foreverwin/iep/integration/handler/def/sap/MesPostDnService.java new file mode 100644 index 0000000..e2e03c6 --- /dev/null +++ b/int-core/src/main/java/com/foreverwin/iep/integration/handler/def/sap/MesPostDnService.java @@ -0,0 +1,106 @@ +package com.foreverwin.iep.integration.handler.def.sap; + +import com.alibaba.fastjson.JSONObject; +import com.foreverwin.iep.common.faas.common.util.StringUtils; +import com.foreverwin.iep.integration.domain.InvokeRequest; +import com.foreverwin.iep.integration.domain.InvokeResponse; +import com.foreverwin.iep.integration.entity.IntInData; +import com.foreverwin.iep.integration.entity.IntInDataLog; +import com.foreverwin.iep.integration.service.HandlerService; +import com.foreverwin.iep.integration.service.IntInDataLogService; +import com.foreverwin.iep.integration.service.IntInDataService; +import com.foreverwin.iep.integration.utils.JS; +import com.foreverwin.iep.integration.utils.Local; +import com.foreverwin.mom.common.security.util.SecurityUtils; +import lombok.AllArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.UUID; + +/** + * 公司间交货过账入库 + */ +@AllArgsConstructor +@Service("ZMM_MES_POST_DN") +public class MesPostDnService implements HandlerService { + + private final IntInDataService intInDataService; + private final IntInDataLogService intInDataLogService; + + @Override + @Transactional(propagation = Propagation.REQUIRES_NEW) + public InvokeResponse call(InvokeRequest invokeRequest) { + String reqId = invokeRequest.getTransSid(); + InvokeResponse invokeResponse = new InvokeResponse(invokeRequest); + String errorMsg = "成功"; + String status = "S"; + try { + // 处理响应 + invokeResponse = SapUtil.sendSap(invokeRequest.getMessageContent()); + invokeResponse.setTransSid(invokeRequest.getTransSid()); + invokeResponse.setTransTime(Local.now()); + if (!"E".equals(invokeResponse.getResult())) { + invokeResponse.setResult("S"); + // 请求处理成功 + JSONObject jsonObject = invokeResponse.getObjectResponse(); + JSONObject bodyJson = jsonObject.getJSONObject("RESPONSE_BODY"); + JSONObject outJson = bodyJson.getJSONObject("ES_OUTPUT"); + String result = outJson.getString("MSGTY"); + status = !"S".equals(result) ? "E" : result; + String message = outJson.getString("MSGTX"); + invokeResponse.setMessageContent(StringUtils.isEmpty(message) ? "成功" : message); + } + } catch (Exception e) { + status = "E"; + errorMsg = e.getMessage(); + } finally { + invokeResponse.setResult(status); + if (StringUtils.isEmpty(invokeResponse.getMessageContent())) { + invokeResponse.setMessageContent(errorMsg); + } + invokeResponse.setTransTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); + invokeResponse.setTransSid(reqId); + saveLogData(invokeRequest, invokeResponse); + } + return invokeResponse; + } + + @Override + public String validation(InvokeRequest invokeRequest) { + JSONObject jsonObject = JS.parseObject(invokeRequest.getMessageContent()); + return null; + } + + private void saveLogData(InvokeRequest invokeRequest, InvokeResponse invokeResponse) { + if (StringUtils.isNotEmpty(invokeRequest.getTransSid())) { + // 存储日志 + IntInData intInData = new IntInData(); + intInData.setSid(invokeRequest.getTransSid()); + intInData.setRequest(invokeRequest.getMessageContent()); + intInData.setFunctionName(invokeRequest.getMessageId()); + intInData.setKeyValue(invokeRequest.getKeyValue()); + intInData.setFromSystem(invokeRequest.getFromSystem()); + intInData.setToSystem(invokeRequest.getToSystem()); + intInData.setRetryCount(0); + intInData.setSite(invokeRequest.getSite()); + intInData.setCreator(SecurityUtils.getUserName()); + intInData.setDateCreate(LocalDateTime.now()); + intInDataService.save(intInData); + IntInDataLog log = new IntInDataLog(); + log.setSid(UUID.randomUUID().toString()); + log.setIntInDataSid(intInData.getSid()); + log.setCreator(SecurityUtils.getUserName()); + log.setDateCreate(LocalDateTime.now()); + log.setSeq(0); + log.setMessage(StringUtils.isEmpty(invokeResponse.getMessageContent()) ? "N/A" : invokeResponse.getMessageContent()); + log.setResponse(invokeResponse.getObjectResponse().toJSONString()); + log.setState(invokeResponse.getResult()); + intInDataLogService.save(log); + } + } + +} diff --git a/int-core/src/main/java/com/foreverwin/iep/integration/handler/def/sap/SapUtil.java b/int-core/src/main/java/com/foreverwin/iep/integration/handler/def/sap/SapUtil.java index 7039dfe..e5d24b2 100644 --- a/int-core/src/main/java/com/foreverwin/iep/integration/handler/def/sap/SapUtil.java +++ b/int-core/src/main/java/com/foreverwin/iep/integration/handler/def/sap/SapUtil.java @@ -26,13 +26,8 @@ public class SapUtil { private static final String sapUser = "RFC"; private static final String sapPassword = "Admin@1234567890"; - private static final JSONObject headJson; - - static { - headJson = new JSONObject(); - headJson.put("x-esb-apikey","hWS3L96a0h4NKCAjN9hG1OmOeEIsxCTz"); - headJson.put("x-esb-env","DEV"); - } + private static final String apikey = "hWS3L96a0h4NKCAjN9hG1OmOeEIsxCTz"; + private static final String env = "DEV"; public static String appendHead(String jsonStr) { @@ -41,7 +36,6 @@ public class SapUtil { JSONObject json = JSONObject.parseObject(jsonStr); allJson.put("API_DATA", json); allJson.put("API_PATH", sapUrl); - allJson.put("headers", headJson); return allJson.toJSONString(); } @@ -50,7 +44,6 @@ public class SapUtil { allJson.put("API_METHOD", "POST"); allJson.put("API_DATA", jsonObj); allJson.put("API_PATH", sapUrl); - allJson.put("headers", headJson); return allJson; } @@ -66,6 +59,8 @@ public class SapUtil { connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/json"); // 设置内容类型 + connection.setRequestProperty("x-esb-apikey", apikey); + connection.setRequestProperty("x-esb-env", env); // 设置 Basic Auth String auth = sapUser + ":" + sapPassword;