This commit is contained in:
syngna_qiao 2024-12-24 16:42:32 +08:00
commit b8172fc3a6
9 changed files with 220 additions and 28 deletions

View File

@ -4,6 +4,7 @@ import com.foreverwin.mom.common.core.util.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
@ -14,4 +15,7 @@ public interface RemoteEsbFileDealService {
@PostMapping(value = "/esbFileDeal/api/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
R<Map<String, String>> uploadFileDoExecute(@RequestPart(value = "file") MultipartFile file);
@PostMapping("/esbFileDeal/api/downLoadFile")
R<Map<String, String>> downLoadFileDoExecute(@RequestBody String objectPath);
}

View File

@ -14,10 +14,7 @@ import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.time.LocalDateTime;
@ -52,7 +49,7 @@ public class EsbFileDealController {
InvokeRequest invokeRequest = new InvokeRequest();
String site = TenantContextHolder.getTenantId();
try {
// 发送给ERP执行 ZMM_MES_POST_DN 接口
// 发送给ERP执行 ESB_FILE_DEAL 接口
LocalDateTime nowLocalDateTime = LocalDateTime.now();
String nowTime = nowLocalDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
invokeRequest.setTransSid(SapUtil.generateReqId());
@ -86,16 +83,46 @@ public class EsbFileDealController {
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();
@ApiOperation(value = "销售出货-附件打印获取附件", notes = "销售出货-附件打印获取附件")
@SysLog("销售出货-附件打印获取附件")
@PostMapping("/api/downLoadFile")
public R downLoadFileDoExecute(@RequestBody String objectPath) {
Map<String, String> params = new HashMap<>();
InvokeResponse invokeResponse = new InvokeResponse();
InvokeRequest invokeRequest = new InvokeRequest();
String site = TenantContextHolder.getTenantId();
try {
// 发送给ERP执行 ESB_FILE_DEAL 接口
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("ESB");
invokeRequest.setSite(site);
invokeRequest.setMessageId(MESSAGE_ID);
JSONObject contentObject = new JSONObject();
// 组装抛送的报文
contentObject.put("OBJECT_PATH", objectPath);
invokeRequest.setMessageContent(contentObject.toJSONString());
invokeRequest.setStart(nowTime);
invokeRequest.setKeyValue(objectPath);
invokeRequest.setRetry(false);
// 呼叫SAP
invokeResponse = esbFileDealService.call(invokeRequest,objectPath);
params.put("RESULT", invokeResponse.getResult());
params.put("MSG", invokeResponse.getMessageContent());
} 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);
}
}

View File

@ -28,8 +28,11 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.UUID;
import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;
@RestController
@RequiredArgsConstructor
@Api(value = "invoke", tags = "整合服務接口")
@ -39,9 +42,16 @@ public class InvokeController {
private final IntInDataService intInDataService;
/**
* 头部信息
*/
private final HttpServletRequest servletRequest;
@PostMapping(value = {"/heartBeateInvoke"})
public R heartBeateInvoke(@RequestBody JSONObject jsonBody) {
String token = jsonBody.getString("access_token");
// String ipAddress = SapUtil.getIpAddr(servletRequest);
// String message = "OK,您的IP地址是"+ipAddress;
return R.ok("OK");
}

View File

@ -290,8 +290,13 @@ public class ShopOrderStatisticTransController {
jsonObject.put("EBELP", ""); // 采购凭证的项目编号
jsonObject.put("MENGE", consumptionTransaction.getMainQty()); // 数量
jsonObject.put("MEINS", consumptionTransaction.getMainUom()); // 基本计量单位
jsonObject.put("ERFMG", consumptionTransaction.getSubQty()); // 以录入项单位表示的数量
jsonObject.put("ERFME", consumptionTransaction.getSubUom()); // 录入单位
if(consumptionTransaction.getSubQty()!=null){
jsonObject.put("ERFMG", consumptionTransaction.getSubQty()); // 以录入项单位表示的数量
jsonObject.put("ERFME", consumptionTransaction.getSubUom()); // 录入单位
}else{
jsonObject.put("ERFMG", null); // 以录入项单位表示的数量
jsonObject.put("ERFME", ""); // 录入单位
}
jsonObject.put("MAT_KDAUF", ""); // 评估销售定单库存的销售定单号
jsonObject.put("MAT_KDPOS", ""); // 评估销售订单库存的销售订单项目
jsonObject.put("UMWRK", ""); // 收货工厂/发货工厂
@ -424,8 +429,13 @@ public class ShopOrderStatisticTransController {
jsonObject.put("EBELP", ""); // 采购凭证的项目编号
jsonObject.put("MENGE", completeTransaction.getMainQty()); // 数量
jsonObject.put("MEINS", completeTransaction.getMainUom()); // 基本计量单位
jsonObject.put("ERFMG", completeTransaction.getSubQty()); // 以录入项单位表示的数量
jsonObject.put("ERFME", completeTransaction.getSubUom()); // 录入单位
if(completeTransaction.getSubQty()!=null){
jsonObject.put("ERFMG", completeTransaction.getSubQty()); // 以录入项单位表示的数量
jsonObject.put("ERFME", completeTransaction.getSubUom()); // 录入单位
}else{
jsonObject.put("ERFMG", null); // 以录入项单位表示的数量
jsonObject.put("ERFME", ""); // 录入单位
}
jsonObject.put("MAT_KDAUF", completeTransaction.getSoNo()); // 评估销售定单库存的销售定单号
if(StringUtils.isNotEmpty(completeTransaction.getSoLineNo())){
jsonObject.put("MAT_KDPOS", Integer.valueOf(completeTransaction.getSoLineNo())); // 评估销售订单库存的销售订单项目

View File

@ -17,6 +17,10 @@ public class OAUtil {
private static final String WbpUrl = "https://agw.esb.nit.pub/oa/pip";
private static final String apikey = "hWS3L96a0h4NKCAjN9hG1OmOeEIsxCTz";
private static final String env = "DEV";
public static String appendHead(String jsonStr) {
JSONObject allJson = new JSONObject();
allJson.put("API_METHOD", "POST");
@ -46,6 +50,8 @@ public class OAUtil {
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json"); // 设置内容类型
connection.setRequestProperty("x-esb-apikey", apikey);
connection.setRequestProperty("x-esb-env", env);
// 发送请求
try (OutputStream os = connection.getOutputStream()) {

View File

@ -0,0 +1,48 @@
package com.foreverwin.iep.integration.handler.def.oa;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.foreverwin.iep.integration.constant.Msg;
import com.foreverwin.iep.integration.domain.InvokeRequest;
import com.foreverwin.iep.integration.domain.InvokeResponse;
import com.foreverwin.iep.integration.service.HandlerService;
import com.foreverwin.iep.integration.utils.JS;
import com.foreverwin.iep.qms.dto.QmsInspecJobDTO;
import com.google.gson.Gson;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
@AllArgsConstructor
@Service("OA_TBWG_INSPEC")
public class OaTbwgInspec implements HandlerService {
@Override
public InvokeResponse call(InvokeRequest invokeRequest) {
JSONObject messageContentjsonObject = JS.parseObject(invokeRequest.getMessageContent());
String jsonInputString = messageContentjsonObject.get(Msg.MESSAGE_CONTENT).toString();
try {
// 统一调用 OA 接口
InvokeResponse invokeResponse = OAUtil.sendOA(jsonInputString);
if(!"E".equals(invokeResponse.getResult())){
// 请求处理成功
JSONArray jsonArray = JS.parseArray(invokeResponse.getMessageContent());
invokeResponse = new InvokeResponse();
invokeResponse.setTransSid(invokeRequest.getTransSid());
invokeResponse.setResult(Msg.S);
invokeResponse.setMessageContent(Msg.SUCCESS);
JSONObject jsonObject = jsonArray.getJSONObject(0);
if (!jsonObject.getBoolean("status")) {
invokeResponse.setResult(Msg.E);
invokeResponse.setMessageContent(jsonObject.getString("msg"));
}
}
return invokeResponse;
} catch (Exception e) {
return getDefaultInvoke().fail(invokeRequest.getTransSid(), e.getMessage());
}
}
@Override
public String validation(InvokeRequest invokeRequest) {
return "";
}
}

View File

@ -65,6 +65,38 @@ public class EsbFileDealService {
return invokeResponse;
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public InvokeResponse call(InvokeRequest invokeRequest,String objectFilePath ) {
String reqId = invokeRequest.getTransSid();
InvokeResponse invokeResponse = new InvokeResponse(invokeRequest);
String errorMsg = "成功";
String status = "S";
try {
// 处理响应
invokeResponse = SapUtil.getFileFormEsb(objectFilePath);
invokeResponse.setTransSid(invokeRequest.getTransSid());
invokeResponse.setTransTime(Local.now());
if (!"E".equals(invokeResponse.getResult())) {
invokeResponse.setResult("S");
// 请求处理成功
JSONObject jsonObject = invokeResponse.getObjectResponse();
invokeResponse.setMessageContent(jsonObject.toJSONString());
}
} 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;
}
private void saveLogData(InvokeRequest invokeRequest, InvokeResponse invokeResponse) {
if (StringUtils.isNotEmpty(invokeRequest.getTransSid())) {
// 存储日志

View File

@ -48,7 +48,7 @@ public class MesPostDnService implements HandlerService {
// 请求处理成功
JSONObject jsonObject = invokeResponse.getObjectResponse();
JSONObject bodyJson = jsonObject.getJSONObject("RESPONSE_BODY");
JSONObject outJson = bodyJson.getJSONObject("ES_OUTPUT");
JSONObject outJson = bodyJson.getJSONObject("ES_RETURN");
String result = outJson.getString("MSGTY");
status = !"S".equals(result) ? "E" : result;
String message = outJson.getString("MSGTX");

View File

@ -6,6 +6,7 @@ import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.foreverwin.iep.integration.domain.InvokeResponse;
import com.foreverwin.iep.integration.utils.SSLUtil;
import com.foreverwin.mom.wms.util.StringUtil;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
@ -24,10 +25,12 @@ import org.springframework.web.multipart.MultipartFile;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
@ -35,6 +38,7 @@ import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.concurrent.ThreadLocalRandom;
import java.net.UnknownHostException;
public class SapUtil {
@ -47,7 +51,8 @@ public class SapUtil {
private static final String apikey = "hWS3L96a0h4NKCAjN9hG1OmOeEIsxCTz";
private static final String env = "DEV";
private static final String EsbFileServerUrl = "https://agw.esb.nit.pub/oss/upload";
private static final String EsbUploadFileServerUrl = "https://agw.esb.nit.pub/oss/upload";
private static final String EsbGetFileServerUrl = "https://agw.esb.nit.pub/oss/file";
public static String appendHead(String jsonStr) {
@ -67,26 +72,41 @@ public class SapUtil {
return allJson;
}
/*
发送照片上传到ESB
*/
public static InvokeResponse sendFileToEsb(String fileDirValue, MultipartFile multipartFile) {
String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(multipartFile.getOriginalFilename());
return callEsb("FILE_DIR",fileDirValue,fileName, multipartFile,EsbUploadFileServerUrl);
}
/*
从ESB获取文件、获取销售订单的客户发货附件等
*/
public static InvokeResponse getFileFormEsb(String objectFilePath){
return callEsb("OBJECT_PATH",objectFilePath,null, null,EsbGetFileServerUrl);
}
public static InvokeResponse callEsb(String jsonKey,String jsonValue,String fileName, MultipartFile multipartFile,String serverUrl){
InvokeResponse invokeResponse = new InvokeResponse();
invokeResponse.setResult("S");
try {
String fileDir = "FILE_DIR";
String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(multipartFile.getOriginalFilename());
// 禁用 SSL 验证
SSLUtil.disableCertificateValidation();
CloseableHttpClient httpClient = HttpClients.createDefault();
httpClient = (CloseableHttpClient) wrapClient(httpClient);
HttpPost httpPost = new HttpPost();
httpPost.setURI(new URI(EsbFileServerUrl));
httpPost.setURI(new URI(serverUrl));
httpPost.setHeader("x-esb-apikey", apikey);
httpPost.setHeader("x-esb-env", env);
HttpEntity reqEntity = MultipartEntityBuilder.create().setCharset(StandardCharsets.UTF_8).setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.addTextBody(fileDir, fileDirValue)
.addBinaryBody("file", multipartFile.getInputStream(), ContentType.APPLICATION_OCTET_STREAM, fileName)
.build();
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().setCharset(StandardCharsets.UTF_8).setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.addTextBody(jsonKey, jsonValue);
if(StringUtil.notEmpty(fileName)){
multipartEntityBuilder.addBinaryBody("file", multipartFile.getInputStream(), ContentType.APPLICATION_OCTET_STREAM, fileName);
}
HttpEntity reqEntity = multipartEntityBuilder.build();
httpPost.setEntity(reqEntity);
httpPost.setHeader("Content-Type", reqEntity.getContentType().getValue()); // 设置内容类型
@ -259,4 +279,39 @@ public class SapUtil {
// 生成时间戳部分
return dateFormat.format(new Date());
}
public static String getIpAddr(HttpServletRequest request) {
String ipAddress = null;
try {
ipAddress = request.getHeader("x-forwarded-for");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
if (ipAddress.equals("127.0.0.1")) {
// 根据网卡取本机配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress = inet.getHostAddress();
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
if (ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
} catch (Exception e) {
ipAddress = "";
}
return ipAddress;
}
}