dbs
2024-12-12 83e0034ab5f729afeb2c25e328d63fda31ff01e1
新增MES处理,分发系统名字特定---》MES系统
2个文件已修改
86 ■■■■■ 已修改文件
src/main/java/com/itstyle/mdm/utils/MdmApiUtils.java 73 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/itstyle/mdm/utils/MdmUtils.java 13 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/itstyle/mdm/utils/MdmApiUtils.java
@@ -1,5 +1,7 @@
package com.itstyle.mdm.utils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itstyle.mdm.entity.HttpClientResult;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
@@ -154,6 +156,77 @@
        }
    }
    public static String getMesJson(String json){
        JSONObject mesResJson=new JSONObject();
        JSONObject jsonobj=JSONObject.parseObject(json);
        JSONArray jsonArray = jsonobj.getJSONArray("records");
        JSONArray mesResDetail=new JSONArray();
        for(int i=0;i<jsonArray.size();i++){
            JSONObject rowObject = jsonArray.getJSONObject(i);
            JSONObject tmp1=new JSONObject();
            tmp1.put("Tgcoriginalcode",rowObject.getString("tgctwoclasscode"));
            JSONObject tmp2=new JSONObject();
            tmp2.put("Code",rowObject.getString("code"));
            JSONObject tmp3=new JSONObject();
            tmp3.put("Name",rowObject.getString("name"));
            JSONObject tmp4=new JSONObject();
            tmp4.put("Specification",rowObject.getString("specification"));
            JSONObject tmp5=new JSONObject();
            tmp5.put("UnitName",rowObject.getString("unitName"));
            JSONObject tmp6=new JSONObject();
            tmp6.put("TypeCode",rowObject.getString("typeCode"));
            JSONObject tmp7=new JSONObject();
            tmp7.put("SpecificationDisplayName",rowObject.getString("specificationDisplayName"));
            JSONObject tmp8=new JSONObject();
            tmp8.put("SupplierName",rowObject.getString("supplierName"));
            mesResDetail.add(tmp1);
            mesResDetail.add(tmp2);
            mesResDetail.add(tmp3);
            mesResDetail.add(tmp4);
            mesResDetail.add(tmp5);
            mesResDetail.add(tmp6);
            mesResDetail.add(tmp7);
            mesResDetail.add(tmp8);
        }
        mesResJson.put("Records",mesResDetail);
        return mesResJson.toJSONString();
    }
    public static String getMesSoapBody(String json){
        return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
                "  <soap12:Body>\n" +
                "    <ReceiveTCG xmlns=\"http://www.hitachi.com/HITPHAMS\">\n" +
                "      <JsonStr><![CDATA["+json+"]]></JsonStr>\n" +
                "    </ReceiveTCG>\n" +
                "  </soap12:Body>\n" +
                "</soap12:Envelope>";
    }
    public static HttpClientResult doPostXmlJson(String url, String xmlStr) throws Exception{
        // 创建httpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //第二步:创建httpPost对象
        HttpPost httpPost = new HttpPost(url);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
        httpPost.setConfig(requestConfig);
        //第三步:给httpPost设置JSON格式的参数
        StringEntity requestEntity = new StringEntity(xmlStr,"utf-8");
        requestEntity.setContentEncoding("UTF-8");
        httpPost.setHeader("Content-type", "application/soap+xml; charset=utf-8");
        httpPost.setEntity(requestEntity);
        // 创建httpResponse对象
        CloseableHttpResponse httpResponse = null;
        try {
            // 执行请求并获得响应结果
            return getHttpClientResult(httpResponse, httpClient, httpPost);
        } finally {
            // 释放资源
            release(httpResponse, httpClient);
        }
    }
    /**
     * Description: 获得响应结果
     *
src/main/java/com/itstyle/mdm/utils/MdmUtils.java
@@ -50,6 +50,7 @@
                String records = httpClientResult.getContent();
                records = records.replaceAll("\\p{Z}", "");
                JSONObject jsonObj = JSON.parseObject(records);
                records = jsonObj.getString("records");
                records = records.replaceAll("attri2","unitName");
                records = records.replaceAll("brand","supplierName");
@@ -144,7 +145,17 @@
                String uuid = UUID.randomUUID().toString();
                try {
                    //数据分发
                    httpClientResult = MdmApiUtils.doPostJson(address, jsonParam);
                    if("MES系统".equals(sysName)){
                        //处理json为MES系统想要的格式
                        jsonParam=MdmApiUtils.getMesJson(jsonParam);
                        //拼接webservice
                        String xmlStr=MdmApiUtils.getMesSoapBody(jsonParam);
                        //请求webservice
                        httpClientResult = MdmApiUtils.doPostXmlJson(address, xmlStr);
                    }else{
                        httpClientResult = MdmApiUtils.doPostJson(address, jsonParam);
                    }
                    //响应结果
                    String content = httpClientResult.getContent();
                    int code = httpClientResult.getCode();