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