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