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 = "<" + 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 += ""; 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 + ""; } else { xml += "<" + para + ">"; } } } } return xml; } }