diff --git a/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/domain/SAPPortDto.java b/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/domain/sap/SAPPortDto.java similarity index 96% rename from ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/domain/SAPPortDto.java rename to ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/domain/sap/SAPPortDto.java index dfe51e12..fc623bd2 100644 --- a/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/domain/SAPPortDto.java +++ b/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/domain/sap/SAPPortDto.java @@ -1,4 +1,4 @@ -package org.dromara.api.domain; +package org.dromara.api.domain.sap; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/domain/WERKSDto.java b/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/domain/sap/WERKSDto.java similarity index 93% rename from ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/domain/WERKSDto.java rename to ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/domain/sap/WERKSDto.java index 5e064695..03946c15 100644 --- a/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/domain/WERKSDto.java +++ b/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/domain/sap/WERKSDto.java @@ -1,4 +1,4 @@ -package org.dromara.api.domain; +package org.dromara.api.domain.sap; import java.util.HashMap; import java.util.List; diff --git a/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/domain/vo/SAPResultVo.java b/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/domain/sap/vo/SAPResultVo.java similarity index 93% rename from ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/domain/vo/SAPResultVo.java rename to ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/domain/sap/vo/SAPResultVo.java index 11bea006..bdcf0c21 100644 --- a/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/domain/vo/SAPResultVo.java +++ b/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/domain/sap/vo/SAPResultVo.java @@ -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; diff --git a/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/service/ICommonApiService.java b/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/service/ICommonApiService.java new file mode 100644 index 00000000..c5a3950c --- /dev/null +++ b/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/service/ICommonApiService.java @@ -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 convertSAPToMaterialEntity(String json); + + +} diff --git a/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/service/impl/BaseMaterialInfoApiServiceImpl.java b/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/service/impl/BaseMaterialInfoApiServiceImpl.java index d46c9f45..6052085a 100644 --- a/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/service/impl/BaseMaterialInfoApiServiceImpl.java +++ b/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/service/impl/BaseMaterialInfoApiServiceImpl.java @@ -2,17 +2,23 @@ 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.apache.dubbo.config.annotation.DubboReference; -import org.dromara.api.domain.SAPPortDto; -import org.dromara.api.domain.vo.SAPResultVo; +import org.dromara.api.domain.sap.SAPPortDto; +import org.dromara.api.domain.sap.vo.SAPResultVo; import org.dromara.api.service.IBaseMaterialInfoApiService; import lombok.RequiredArgsConstructor; +import org.dromara.api.service.ICommonApiService; +import org.dromara.api.utils.SAPApiUtils; +import org.dromara.api.utils.SAPConstants; import org.dromara.mes.api.RemoteMaterialInfoService; import org.dromara.mes.api.model.bo.BaseMaterialInfoBo; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; /** @@ -25,6 +31,9 @@ import java.util.List; @Service public class BaseMaterialInfoApiServiceImpl implements IBaseMaterialInfoApiService { + @Autowired + private final ICommonApiService commonApiService; + @DubboReference private final RemoteMaterialInfoService remoteMaterialInfoService; @@ -32,34 +41,13 @@ public class BaseMaterialInfoApiServiceImpl implements IBaseMaterialInfoApiServi public String insertApi(String requestBody) { SAPPortDto sapPortDto = new SAPPortDto(); //请求API接口,获取数据 - List list = getMaterialData(sapPortDto); + String json = commonApiService.getSAPData(sapPortDto); //解析数据,存储实体类 - + List list = commonApiService.convertSAPToMaterialEntity(json); remoteMaterialInfoService.remoteInsertBaseMaterialInfo(list); return "success"; } - public List getMaterialData(SAPPortDto sapPortDto) { - List materialVos = new ArrayList<>(); - String requestParam = null; - String result = null; - // 创建ObjectMapper实例 对象转JSON字符串 - ObjectMapper objectMapper = new ObjectMapper(); - // 格式化输出 - // objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true); -// requestParam = objectMapper.writeValueAsString(sapPortDto); -// result = ApiUtils.sendSAPHttpPost(SAPConstants.SAP_PREFIX_URL + SAPConstants.MATERIAL_URL, requestParam); - try { - // 处理接口返回消息 - ObjectMapper resultMapper = new ObjectMapper(); - // 将 JSON 字符串 转换为 Java 对象 - SAPResultVo resultVo = resultMapper.readValue(result, SAPResultVo.class); - //转物料实体类 保存数据 -// materialVos = storageService.insertSAPMaterialInfo(resultVo); - } catch (JsonProcessingException e) { - e.printStackTrace(); - } - return materialVos; - } + } diff --git a/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/service/impl/CommonApiServiceImpl.java b/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/service/impl/CommonApiServiceImpl.java new file mode 100644 index 00000000..157dab5f --- /dev/null +++ b/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/service/impl/CommonApiServiceImpl.java @@ -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 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 materialInfoArrayList = new ArrayList<>(); + assert resultVo != null; + List> itemList = resultVo.getO_TAB().get("item"); + for (HashMap 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; + } + + + + + +} diff --git a/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/utils/SAPApiUtils.java b/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/utils/SAPApiUtils.java new file mode 100644 index 00000000..b5f88881 --- /dev/null +++ b/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/utils/SAPApiUtils.java @@ -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); + } + + } +} diff --git a/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/utils/SAPConstants.java b/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/utils/SAPConstants.java new file mode 100644 index 00000000..8dd0c71a --- /dev/null +++ b/ruoyi-modules/hwmom-api/src/main/java/org/dromara/api/utils/SAPConstants.java @@ -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; + } + +} diff --git a/ruoyi-modules/hwmom-mes/src/main/java/org/dromara/mes/dubbo/RemoteMaterialInfoServiceImpl.java b/ruoyi-modules/hwmom-mes/src/main/java/org/dromara/mes/dubbo/RemoteMaterialInfoServiceImpl.java index 811b818b..b5d57401 100644 --- a/ruoyi-modules/hwmom-mes/src/main/java/org/dromara/mes/dubbo/RemoteMaterialInfoServiceImpl.java +++ b/ruoyi-modules/hwmom-mes/src/main/java/org/dromara/mes/dubbo/RemoteMaterialInfoServiceImpl.java @@ -34,6 +34,7 @@ public class RemoteMaterialInfoServiceImpl implements RemoteMaterialInfoService public Boolean remoteInsertBaseMaterialInfo(List bos) { List list = new ArrayList<>(); for (BaseMaterialInfoBo bo : bos) { + //校验物料是否已存在,若存在则更新数据 BaseMaterialInfo add = MapstructUtils.convert(bo, BaseMaterialInfo.class); list.add(add); }