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