update 添加通用API接口、对接SAP物料接口
parent
3ce2e846c9
commit
0cb9f6d654
@ -1,4 +1,4 @@
|
||||
package org.dromara.api.domain;
|
||||
package org.dromara.api.domain.sap;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package org.dromara.api.domain;
|
||||
package org.dromara.api.domain.sap;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
@ -1,7 +1,7 @@
|
||||
package org.dromara.api.domain.vo;
|
||||
package org.dromara.api.domain.sap.vo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import org.dromara.api.domain.WERKSDto;
|
||||
import org.dromara.api.domain.sap.WERKSDto;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
@ -0,0 +1,23 @@
|
||||
package org.dromara.api.service;
|
||||
|
||||
import org.dromara.api.domain.sap.SAPPortDto;
|
||||
import org.dromara.mes.api.model.bo.BaseMaterialInfoBo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 通用APIService接口
|
||||
*
|
||||
* @author Yinq
|
||||
* @date 2025-01-02
|
||||
*/
|
||||
public interface ICommonApiService {
|
||||
|
||||
|
||||
String getSAPData(SAPPortDto sapPortDto);
|
||||
|
||||
|
||||
List<BaseMaterialInfoBo> convertSAPToMaterialEntity(String json);
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
package org.dromara.api.service.impl;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import org.dromara.api.domain.sap.SAPPortDto;
|
||||
import org.dromara.api.domain.sap.vo.SAPResultVo;
|
||||
import org.dromara.api.service.ICommonApiService;
|
||||
import org.dromara.api.utils.SAPApiUtils;
|
||||
import org.dromara.api.utils.SAPConstants;
|
||||
import org.dromara.mes.api.model.bo.BaseMaterialInfoBo;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* 通用APIService接口
|
||||
*
|
||||
* @author Yinq
|
||||
* @date 2025-01-02
|
||||
*/
|
||||
@Service
|
||||
public class CommonApiServiceImpl implements ICommonApiService {
|
||||
|
||||
|
||||
@Override
|
||||
public String getSAPData(SAPPortDto sapPortDto) {
|
||||
String requestParam = null;
|
||||
String result = null;
|
||||
try {
|
||||
//获取物料API
|
||||
|
||||
// 创建ObjectMapper实例 对象转JSON字符串
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
// 格式化输出
|
||||
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
|
||||
requestParam = objectMapper.writeValueAsString(sapPortDto);
|
||||
result = SAPApiUtils.sendSAPHttpPost(SAPConstants.SAP_PREFIX_URL + SAPConstants.MATERIAL_URL, requestParam);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BaseMaterialInfoBo> convertSAPToMaterialEntity(String json) {
|
||||
SAPResultVo resultVo = null;
|
||||
try {
|
||||
// 处理接口返回消息
|
||||
ObjectMapper resultMapper = new ObjectMapper();
|
||||
// 将 JSON 字符串 转换为 Java 对象
|
||||
resultVo = resultMapper.readValue(json, SAPResultVo.class);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
List<BaseMaterialInfoBo> materialInfoArrayList = new ArrayList<>();
|
||||
assert resultVo != null;
|
||||
List<HashMap<String, String>> itemList = resultVo.getO_TAB().get("item");
|
||||
for (HashMap<String, String> map : itemList) {
|
||||
BaseMaterialInfoBo materialInfo = new BaseMaterialInfoBo();
|
||||
materialInfo.setMaterialCode(map.get("MATNR"));
|
||||
materialInfo.setMaterialName(map.get("MAKTX"));
|
||||
materialInfo.setMaterialUnit(map.get("MEINS"));
|
||||
materialInfo.setMaterialMatkl(map.get("MATKL"));
|
||||
materialInfoArrayList.add(materialInfo);
|
||||
}
|
||||
return materialInfoArrayList;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package org.dromara.api.utils;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* @Author YinQ
|
||||
* @create 2023-10-12 10:52
|
||||
*/
|
||||
public class SAPApiUtils {
|
||||
|
||||
|
||||
/**
|
||||
* POST请求json格式
|
||||
* @param apiUrl
|
||||
* @param jsonData
|
||||
* @return
|
||||
*/
|
||||
public static String sendSAPHttpPost(String apiUrl, String jsonData) {
|
||||
try {
|
||||
// 构建POST请求
|
||||
HttpRequest request = HttpRequest.post(apiUrl)
|
||||
.header("Authorization", "Basic " + Base64.getUrlEncoder().
|
||||
encodeToString((SAPConstants.USER_NAME + ":" + SAPConstants.PASS_WORD).getBytes()))
|
||||
.body(jsonData) // 设置JSON格式的请求体
|
||||
.contentType("application/json") // 设置Content-Type为application/json
|
||||
.setConnectionTimeout(1000 * 10) // 设置请求连接超时时间
|
||||
;
|
||||
|
||||
// 发送POST请求
|
||||
HttpResponse httpResponse = request.execute();
|
||||
|
||||
// 获取响应内容
|
||||
return httpResponse.body();
|
||||
} catch (Exception e) {
|
||||
// 捕获异常并重新抛出自定义异常
|
||||
throw new RuntimeException("Failed to send SAP HTTP POST request: " + e.getMessage(), e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
package org.dromara.api.utils;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @Author YinQ
|
||||
* @create 2023-09-26 15:39
|
||||
*/
|
||||
@Component
|
||||
public class SAPConstants {
|
||||
|
||||
|
||||
/**
|
||||
* SAP-URL前缀
|
||||
*/
|
||||
public static String SAP_PREFIX_URL;
|
||||
|
||||
/**
|
||||
* SAP-用户名
|
||||
*/
|
||||
public static String USER_NAME;
|
||||
|
||||
/**
|
||||
* SAP-密码
|
||||
*/
|
||||
public static String PASS_WORD;
|
||||
|
||||
/**
|
||||
* 获取物料主数据(MES2019)
|
||||
*/
|
||||
public static final String MATERIAL_URL = "/SdGetSB/SdSapGetMaterialToMesSvcRSProxy/merge";
|
||||
|
||||
/**
|
||||
* 获取生产工单(MES2019)
|
||||
*/
|
||||
public static final String PRODUCTION_ORDERS_URL = "/SdGetSB/SdSapGetPOToMesSvcRSProxy/merge";
|
||||
|
||||
/**
|
||||
* 获取设备模具信息(MES2019)
|
||||
*/
|
||||
public static final String EQUIPMENT_URL = "/SdGetSB/SdSapGetEQToMesSvcRSProxy/merge";
|
||||
|
||||
/**
|
||||
* 获取产品BOM信息(MES2019)
|
||||
*/
|
||||
public static final String PRODUCTS_BOM_URL = "/SdGetSB/SdSapGetBomToMesSvcRSProxy/merge";
|
||||
|
||||
/**
|
||||
* 工厂编号
|
||||
*/
|
||||
public static final String FACTORY_CODE = "1301";
|
||||
|
||||
|
||||
// @Value("${sap.prefix}")
|
||||
public void setPrefix(String value) {
|
||||
SAP_PREFIX_URL = value;
|
||||
}
|
||||
|
||||
// @Value("${sap.username}")
|
||||
public void setUserName(String value) {
|
||||
USER_NAME = value;
|
||||
}
|
||||
|
||||
// @Value("${sap.password}")
|
||||
public void setPassWord(String value) {
|
||||
PASS_WORD = value;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue