春风项目四线(合箱线、总装线)
yyt
2024-06-03 5030f3d30ccc1bd16db371c6970a48103aff9191
提交 | 用户 | 时间
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
282556 39     public static final String ProductionPlanInformationUrl = "http://podqapp.cfmoto.com.cn:50100/XISOAPAdapter/MessageServlet?senderParty=&senderService=BC_MES&receiverParty=&receiverService=&interface=SI_ZPP_CF_MES_005_SYN_OUT&interfaceNamespace=http://cfmoto.com/xi/MES";
df1f2b 40
41     public static void main(String[] args) throws Exception {
42 //        getInterfaceInformationXml(ProductionPlanInformationUrl,getProductionPlanInformationXml("A0055577"));
43 //        test1();
44
282556 45 //        String serviceUrl = "http://podqapp.cfmoto.com.cn:50100/XISOAPAdapter/MessageServlet?senderParty=&senderService=BC_MES&receiverParty=&receiverService=&interface=SI_ZPP_CF_MES_005_SYN_OUT&interfaceNamespace=http://cfmoto.com/xi/MES";
Y 46 //        String serviceUrl = "http://podqapp.cfmoto.com.cn:50100/XISOAPAdapter/MessageServlet?senderParty=&senderService=BC_MES&receiverParty=&receiverService=&interface=SI_ZPP_CF_BC_001_SYN_OUT&interfaceNamespace=http://cfmoto.com/xi/MES";
55b0cb 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());
e0a2b8 123             //ordeInfo.setCreateTime(item.getZDATE());
C 124             ordeInfo.setDemandDate(item.getGSTRP());//开工日期
df1f2b 125             omProductionOrdeInfoService.insertOmProductionOrdeInfo(ordeInfo);
126         }
127     }
128
129     public static String getInterfaceInformationXml(String url,String xml){
130         int timeout = 10000;
131         HttpClient client = new HttpClient();
132         //如果需要用户名密码验证;不需要验证登录则不需要以下4行
133         String username = "POMESUSER";
134         String password = "12345tgb";
135         UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
136         client.getState().setCredentials(AuthScope.ANY, creds);
137
138         PostMethod postMethod = new PostMethod(url);
139         // 设置连接超时
140         client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
141         // 设置读取时间超时
142         client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
143         // 然后把Soap请求数据添加到PostMethod中
144         RequestEntity requestEntity = null;
145         try {
146             requestEntity = new StringRequestEntity(xml, "text/xml", "UTF-8");
147         } catch (UnsupportedEncodingException e) {
148             throw new RuntimeException(e);
149         }
150
151         // 设置请求头部,否则可能会报 “no SOAPAction header” 的错误
152         postMethod.setRequestHeader("SOAPAction", "");
153         // 设置请求体
154         postMethod.setRequestEntity(requestEntity);
155         int status = 0;
156         try {
157             status = client.executeMethod(postMethod);
158         } catch (IOException e) {
159             throw new RuntimeException(e);
160         }
161         if (status == 200) {// 成功
162             InputStream is = null;
163             try {
164                 is = postMethod.getResponseBodyAsStream();
165             } catch (IOException e) {
166                 throw new RuntimeException(e);
167             }
168             // 获取请求结果字符串
169             String result = null;
170             try {
171                 result = IOUtils.toString(is);
172                 return result;
173             } catch (IOException e) {
174                 throw new RuntimeException(e);
175             }
176         }
177         return "接口调用错误";
178     }
179
180     public static String getProductionPlanInformationXml(String str){
181         String content = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:sap-com:document:sap:rfc:functions\">\n" +
182                 "   <soapenv:Header/>\n" +
183                 "   <soapenv:Body>\n" +
184                 "      <urn:ZPP_CF_MES_005>\n" +
185                 "         <!--Optional:-->\n" +
186                 "         <IV_WERKS>1000</IV_WERKS>\n" +
187                 "         <!--Optional:-->\n" +
188                 "         <IV_ZSCTZD>"+str+"</IV_ZSCTZD>\n" +
189                 "      </urn:ZPP_CF_MES_005>\n" +
190                 "   </soapenv:Body>\n" +
191                 "</soapenv:Envelope>";
192         return content;
193     }
194
195     /**
196      * 方式--贰
197      * 使用hutool工具包中的工具转化
198      * @param xmlStr
199      * @return
200      */
201     public static String xmlToJSON2(String xmlStr){
202         JSONObject jsonObject1 = cn.hutool.json.XML.toJSONObject(xmlStr, true);
203
204         Gson gson = new Gson();
205         JsonObject jsonObject2 = gson.fromJson(jsonObject1.toString(), JsonObject.class);
206         JsonObject etData = jsonObject2
207                 .getAsJsonObject("SOAP:Envelope")
208                 .getAsJsonObject("SOAP:Body")
209                 .getAsJsonObject("n0:ZPP_CF_MES_005.Response")
210                 .getAsJsonObject("ET_DATA");
211         return etData.toString();
212     }
5030f3 213     /**
Y 214      * 方式--贰
215      * 使用hutool工具包中的工具转化
216      * @param xmlStr
217      * @return
218      */
219     public static String xmlToJSON1(String xmlStr){
220         String status = "";
221         if(xmlStr.contains("EV_MESSAGE")){
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_MESSAGE")
236                     .asText();
237             status = evStatus;
238         }
239         return status;
240     }
df1f2b 241
55b0cb 242     /**
243      * 方式--贰
244      * 使用hutool工具包中的工具转化
245      * @param xmlStr
246      * @return
247      */
248     public static String xmlToJSON3(String xmlStr){
249         String status = "";
250         if(xmlStr.contains("EV_STATUS")){
251             JSONObject jsonObject1 = cn.hutool.json.XML.toJSONObject(xmlStr, true);
252
253             ObjectMapper objectMapper = new ObjectMapper();
254             JsonNode rootNode = null;
255             try {
256                 rootNode = objectMapper.readTree(String.valueOf(jsonObject1));
257             } catch (JsonProcessingException e) {
258                 throw new RuntimeException(e);
259             }
260             String evStatus = rootNode
261                     .path("SOAP:Envelope")
262                     .path("SOAP:Body")
263                     .path("n0:ZPP_BC_001.Response")
264                     .path("EV_STATUS")
265                     .asText();
266             status = evStatus;
267         }
268         return status;
269     }
270
df1f2b 271 }