春风项目四线(合箱线、总装线)
jiang
2024-01-30 d82e4465e5e3c9e9f93a8ce7ce3b8c0f73fdf991
提交 | 用户 | 时间
df1f2b 1 package com.jcdm.main.webservice.service;
2
3
4 import cn.hutool.json.JSONObject;
55b0cb 5 import com.fasterxml.jackson.core.JsonProcessingException;
6 import com.fasterxml.jackson.databind.JsonNode;
7 import com.fasterxml.jackson.databind.ObjectMapper;
df1f2b 8 import com.google.gson.Gson;
9 import com.google.gson.JsonObject;
10 import com.jcdm.main.da.collectionParamConf.service.IDaCollectionParamConfService;
11 import com.jcdm.main.da.opcuaconfig.init.BeanUtils;
12 import com.jcdm.main.om.productionOrde.domain.OmProductionOrdeInfo;
13 import com.jcdm.main.om.productionOrde.service.IOmProductionOrdeInfoService;
14 import com.jcdm.main.om.workReport.service.IOmWorkReportService;
15 import com.jcdm.main.webservice.domain.Item;
16 import com.jcdm.main.webservice.domain.ItemList;
17 import org.apache.commons.httpclient.HttpClient;
18 import org.apache.commons.httpclient.UsernamePasswordCredentials;
19 import org.apache.commons.httpclient.auth.AuthScope;
20 import org.apache.commons.httpclient.methods.PostMethod;
21 import org.apache.commons.httpclient.methods.RequestEntity;
22 import org.apache.commons.httpclient.methods.StringRequestEntity;
23 import org.apache.commons.io.IOUtils;
24 import com.jcdm.main.om.productionOrde.*;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.web.bind.annotation.RestController;
27
28 import javax.swing.border.Border;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.UnsupportedEncodingException;
32 import java.util.List;
33
34 public class ReceivingServices {
35     @Autowired
36 //    private static IOmProductionOrdeInfoService omProductionOrdeInfoService;
37     private static IOmProductionOrdeInfoService omProductionOrdeInfoService = BeanUtils.getBean(IOmProductionOrdeInfoService.class);
38
39     public static final String ProductionPlanInformationUrl = "http://podqapp.cfmoto.com.cn:50200/XISOAPAdapter/MessageServlet?senderParty=&senderService=BC_MES&receiverParty=&receiverService=&interface=SI_ZPP_CF_MES_005_SYN_OUT&interfaceNamespace=http://cfmoto.com/xi/MES";
40
41     public static void main(String[] args) throws Exception {
42 //        getInterfaceInformationXml(ProductionPlanInformationUrl,getProductionPlanInformationXml("A0055577"));
43 //        test1();
44
45 //        String serviceUrl = "http://podqapp.cfmoto.com.cn:50200/XISOAPAdapter/MessageServlet?senderParty=&senderService=BC_MES&receiverParty=&receiverService=&interface=SI_ZPP_CF_MES_005_SYN_OUT&interfaceNamespace=http://cfmoto.com/xi/MES";
55b0cb 46 //        String serviceUrl = "http://podqapp.cfmoto.com.cn:50200/XISOAPAdapter/MessageServlet?senderParty=&senderService=BC_MES&receiverParty=&receiverService=&interface=SI_ZPP_CF_BC_001_SYN_OUT&interfaceNamespace=http://cfmoto.com/xi/MES";
47 ////        String content = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:sap-com:document:sap:rfc:functions\">\n" +
48 ////                "   <soapenv:Header/>\n" +
49 ////                "   <soapenv:Body>\n" +
50 ////                "      <urn:ZPP_CF_MES_005>\n" +
51 ////                "         <!--Optional:-->\n" +
52 ////                "         <IV_WERKS>1000</IV_WERKS>\n" +
53 ////                "         <!--Optional:-->\n" +
54 ////                "         <IV_ZSCTZD>A0055577</IV_ZSCTZD>\n" +
55 ////                "      </urn:ZPP_CF_MES_005>\n" +
56 ////                "   </soapenv:Body>\n" +
57 ////                "</soapenv:Envelope>";
df1f2b 58 //        String content = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:sap-com:document:sap:rfc:functions\">\n" +
59 //                "   <soapenv:Header/>\n" +
60 //                "   <soapenv:Body>\n" +
55b0cb 61 //                "      <urn:ZPP_BC_001>\n" +
62 //                "         <IV_AUFNR>000010569212</IV_AUFNR>\n" +
63 //                "          <IV_VORNR>0010</IV_VORNR>\n" +
64 //                "           <IV_LMNGA>1</IV_LMNGA>\n" +
65 //                "           <IV_XMNGA>0</IV_XMNGA>   \n" +
66 //                "         <IV_STATU>1</IV_STATU>  \n" +
67 //                "      </urn:ZPP_BC_001>\n" +
df1f2b 68 //                "   </soapenv:Body>\n" +
69 //                "</soapenv:Envelope>";
70 //        // HttpClient发送SOAP请求
71 //        int timeout = 10000;
72 //        HttpClient client = new HttpClient();
73 //        //如果需要用户名密码验证;不需要验证登录则不需要以下4行
74 //        String username = "POMESUSER";
75 //        String password = "12345tgb";
76 //        UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
77 //        client.getState().setCredentials(AuthScope.ANY, creds);
78 //
79 //        PostMethod postMethod = new PostMethod(serviceUrl);
80 //        // 设置连接超时
81 //        client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
82 //        // 设置读取时间超时
83 //        client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
84 //        // 然后把Soap请求数据添加到PostMethod中
85 //        RequestEntity requestEntity = new StringRequestEntity(content, "text/xml", "UTF-8");
86 //
87 //        // 设置请求头部,否则可能会报 “no SOAPAction header” 的错误
88 //        postMethod.setRequestHeader("SOAPAction", "");
89 //        // 设置请求体
90 //        postMethod.setRequestEntity(requestEntity);
91 //        int status = client.executeMethod(postMethod);
92 //
93 //        if (status == 200) {// 成功
94 //            InputStream is = postMethod.getResponseBodyAsStream();
95 //            // 获取请求结果字符串
96 //            String result = IOUtils.toString(is);
55b0cb 97 ////            String jsonStr = xmlToJSON2(result);
98 ////            Gson gson = new Gson();
99 ////            // 将json字符串转换成对象
100 ////            ItemList itemList = gson.fromJson(jsonStr, ItemList.class);
df1f2b 101 //            System.out.println("返回结果:" + result);
102 //        } else {
103 //            System.out.println("错误代码:" + status + ":" + postMethod.getResponseBodyAsString());
104 //        }
105     }
106     
107     public static void insertWebserviceData(String str) {
108         String result = getInterfaceInformationXml(ProductionPlanInformationUrl, getProductionPlanInformationXml(str));
109         String jsonStr = xmlToJSON2(result);
110         Gson gson = new Gson();
111         // 将json字符串转换成对象
112         ItemList itemList = gson.fromJson(jsonStr, ItemList.class);
113         for (Item item : itemList.getItem()) {
114             OmProductionOrdeInfo ordeInfo = new OmProductionOrdeInfo();
f11989 115             ordeInfo.setProductCode(item.getPLNBEZ());
df1f2b 116             ordeInfo.setProductName(item.getMAKTX());
117             ordeInfo.setLineCode(item.getFEVOR());
118             ordeInfo.setWorkOrderNo(item.getAUFNR());
119             ordeInfo.setPlanQty((long) Double.parseDouble(item.getPSMNG()));
f11989 120             ordeInfo.setProductionNotice(item.getZSCTZD());
df1f2b 121             ordeInfo.setTypeZ(item.getFERTH());
122             ordeInfo.setCreateUser(item.getZUSER());
123             ordeInfo.setCreateTime(item.getZDATE());
124             omProductionOrdeInfoService.insertOmProductionOrdeInfo(ordeInfo);
125         }
126     }
127
128     public static String getInterfaceInformationXml(String url,String xml){
129         int timeout = 10000;
130         HttpClient client = new HttpClient();
131         //如果需要用户名密码验证;不需要验证登录则不需要以下4行
132         String username = "POMESUSER";
133         String password = "12345tgb";
134         UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
135         client.getState().setCredentials(AuthScope.ANY, creds);
136
137         PostMethod postMethod = new PostMethod(url);
138         // 设置连接超时
139         client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
140         // 设置读取时间超时
141         client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
142         // 然后把Soap请求数据添加到PostMethod中
143         RequestEntity requestEntity = null;
144         try {
145             requestEntity = new StringRequestEntity(xml, "text/xml", "UTF-8");
146         } catch (UnsupportedEncodingException e) {
147             throw new RuntimeException(e);
148         }
149
150         // 设置请求头部,否则可能会报 “no SOAPAction header” 的错误
151         postMethod.setRequestHeader("SOAPAction", "");
152         // 设置请求体
153         postMethod.setRequestEntity(requestEntity);
154         int status = 0;
155         try {
156             status = client.executeMethod(postMethod);
157         } catch (IOException e) {
158             throw new RuntimeException(e);
159         }
160         if (status == 200) {// 成功
161             InputStream is = null;
162             try {
163                 is = postMethod.getResponseBodyAsStream();
164             } catch (IOException e) {
165                 throw new RuntimeException(e);
166             }
167             // 获取请求结果字符串
168             String result = null;
169             try {
170                 result = IOUtils.toString(is);
171                 return result;
172             } catch (IOException e) {
173                 throw new RuntimeException(e);
174             }
175         }
176         return "接口调用错误";
177     }
178
179     public static String getProductionPlanInformationXml(String str){
180         String content = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:sap-com:document:sap:rfc:functions\">\n" +
181                 "   <soapenv:Header/>\n" +
182                 "   <soapenv:Body>\n" +
183                 "      <urn:ZPP_CF_MES_005>\n" +
184                 "         <!--Optional:-->\n" +
185                 "         <IV_WERKS>1000</IV_WERKS>\n" +
186                 "         <!--Optional:-->\n" +
187                 "         <IV_ZSCTZD>"+str+"</IV_ZSCTZD>\n" +
188                 "      </urn:ZPP_CF_MES_005>\n" +
189                 "   </soapenv:Body>\n" +
190                 "</soapenv:Envelope>";
191         return content;
192     }
193
194     /**
195      * 方式--贰
196      * 使用hutool工具包中的工具转化
197      * @param xmlStr
198      * @return
199      */
200     public static String xmlToJSON2(String xmlStr){
201         JSONObject jsonObject1 = cn.hutool.json.XML.toJSONObject(xmlStr, true);
202
203         Gson gson = new Gson();
204         JsonObject jsonObject2 = gson.fromJson(jsonObject1.toString(), JsonObject.class);
205         JsonObject etData = jsonObject2
206                 .getAsJsonObject("SOAP:Envelope")
207                 .getAsJsonObject("SOAP:Body")
208                 .getAsJsonObject("n0:ZPP_CF_MES_005.Response")
209                 .getAsJsonObject("ET_DATA");
210         return etData.toString();
211     }
212
55b0cb 213     /**
214      * 方式--贰
215      * 使用hutool工具包中的工具转化
216      * @param xmlStr
217      * @return
218      */
219     public static String xmlToJSON3(String xmlStr){
220         String status = "";
221         if(xmlStr.contains("EV_STATUS")){
222             JSONObject jsonObject1 = cn.hutool.json.XML.toJSONObject(xmlStr, true);
223
224             ObjectMapper objectMapper = new ObjectMapper();
225             JsonNode rootNode = null;
226             try {
227                 rootNode = objectMapper.readTree(String.valueOf(jsonObject1));
228             } catch (JsonProcessingException e) {
229                 throw new RuntimeException(e);
230             }
231             String evStatus = rootNode
232                     .path("SOAP:Envelope")
233                     .path("SOAP:Body")
234                     .path("n0:ZPP_BC_001.Response")
235                     .path("EV_STATUS")
236                     .asText();
237             status = evStatus;
238         }
239         return status;
240     }
241
df1f2b 242 }