李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
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
package com.chinaztt.mes.mould.util;
 
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.chinaztt.mes.mould.config.MouldConfig;
import com.chinaztt.mes.mould.dto.ZttMouldDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.helpers.IOUtils;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import javax.persistence.Column;
import java.io.*;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
 
/**
 * @Author: Zero
 * @Date: 2022/12/2 09:14
 * @Description:
 */
@Component
@Slf4j
public class MouldUtil {
    @Resource
    private MouldConfig mouldConfig;
 
    public JSONObject readTempCollectionRecord(String param) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success", false);
        try {
            String result = getRes("Read_temp_collection_record", JSONObject.parseObject(param, ZttMouldDTO.class));
            if (StringUtils.equals("no data", result)) {
                jsonObject.put("message", result);
            } else {
                jsonObject.put("success", true);
                jsonObject.put("data", result);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            jsonObject.put("data", StringUtils.EMPTY);
        }
        return jsonObject;
    }
 
    public Boolean insertTempLifeRecord(String param) {
        try {
            SerializeConfig config = SerializeConfig.getGlobalInstance();
            config.put(BigDecimal.class, ToStringSerializer.instance);
            String result = getRes("Insert_temp_life_record", JSONObject.parseObject(param, ZttMouldDTO.class));
            if (StringUtils.equalsIgnoreCase("insert success", result)) {
                return Boolean.TRUE;
            } else {
                return Boolean.FALSE;
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Boolean.FALSE;
            //throw new RuntimeException(e.getMessage(), e);
        }
    }
 
 
    private String getRes(String methodKey, Object object) throws Exception {
        String result = getResByXml(getXml(methodKey, object), mouldConfig.getMouldUrl());
        switch (methodKey) {
            case "Read_temp_collection_record":
                result = SoapUtil.parseReadTempCollectionRecordResult(result);
                break;
            case "Insert_temp_life_record":
                result = SoapUtil.parseInsertTempLifeRecordResult(result);
                break;
            default:
                return result;
        }
        return result;
    }
 
 
    public static String getResByXml(String soapXML, String url1) {
        StringBuilder sb = new StringBuilder();
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        OutputStream os = null;
        HttpURLConnection connection = null;
        String result = StringUtils.EMPTY;
        try {
            //第一步:创建服务地址
            URL url = new URL(url1);
            //打开链接
            connection = (HttpURLConnection) url.openConnection();
            //第三步:设置参数
            //3.1发送方式设置:POST必须大写
            connection.setRequestMethod("POST");
            //3.2设置数据格式:content-type
            connection.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
            //3.3设置输入输出,因为默认新创建的connection没有读写权限,
            connection.setDoInput(true);
            connection.setDoOutput(true);
            //第四步:组织SOAP数据,发送请求
            os = connection.getOutputStream();
            os.write(soapXML.getBytes("UTF-8"));
            //第五步:接收服务端响应,打印
            int responseCode = connection.getResponseCode();
            if (responseCode == 200) {
                is = connection.getInputStream();
                result = IOUtils.toString(is, "utf-8");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            try {
                if (null != is) {
                    is.close();
                }
                if (null != isr) {
                    isr.close();
                }
                if (null != br) {
                    br.close();
                }
                if (null != os) {
                    os.close();
                }
                if (null != connection) {
                    connection.disconnect();
                }
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
        return result;
    }
 
    private String getXml(String sign, Object object) throws Exception {
        String xml = "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www" +
                ".w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><" + sign + " xmlns=\"http://tempuri.org/\">";
        switch (sign) {
            case "Insert_collection_record":
                xml += xmlMaker(new String[]{"MouldCode", "operatorMan", "TeamName", "ShiftsName", "MachineName",
                        "GroupName"}, object);
                break;
            case "Insert_temp_life_record":
                xml += xmlMaker(new String[]{"MouldCode", "SerialNumber", "ProcedureNumber", "MachineNumber",
                        "ProductSpec", "PlateNumber", "LifeValue", "ProductionDate", "BuildMaker"}, object);
                break;
            case "Query_group_mould_account":
                xml += xmlMaker(new String[]{"MouldCode", "MachineName", "GroupName", "MouldSpec", "MouldDrawingNo"},
                        object);
                break;
            case "Query_mould_check_record":
                xml += xmlMaker(new String[]{"MouldCode", "GroupName"}, object);
                break;
            case "Read_temp_collection_record":
                String[] cols = new String[]{"MachineNum", "MouldName"};
                xml += xmlMaker(cols, object);
                break;
            case "Insert_Process_Order":
                xml += xmlMaker(new String[]{"OrderNo", "GroupName", "MouldList"}, object);
                break;
            case "Insert_mould_exception_record":
                xml += xmlMaker(new String[]{"MouldCode", "ExceptioInfo", "OrderNo", "CommitterName", "OccurrenceTime", "GroupName"}, object);
                break;
        }
        xml += "</" + sign + "></soap:Body></soap:Envelope>";
        return xml;
    }
 
    private String xmlMaker(String[] strings, Object object) throws Exception {
        String xml = "";
        Method[] methods = object.getClass().getDeclaredMethods();
        for (Method method : methods) {
            if (method.isAnnotationPresent(Column.class)) {
                //获取注解
                Column column = method.getAnnotation(Column.class);
                //获取注解的name 属性 name中存放的 字段名
                String para = column.name();
                if (Arrays.asList(strings).contains(para)) {
                    Object value = method.invoke(object);
                    if (value != null) {
                        xml += "<" + para + ">" + value + "</" + para + ">";
                    } else {
                        xml += "<" + para + "></" + para + ">";
                    }
                }
            }
        }
        return xml;
    }
}