package cn.com.taiji.mqcrj.utils;

import cn.hutool.json.JSONObject;
import cn.hutool.json.XML;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import javax.xml.soap.*;
import java.io.ByteArrayInputStream;
import java.util.Iterator;

/**
 * @author chen mh
 * @date 2023/7/18 10:08
 */
public class ResultHandle {


    public JSONArray handle(String response){
        try {
            SOAPMessage msg = formatSoapString(response);
            SOAPBody body = msg.getSOAPBody();
            Iterator<SOAPElement> iterator = body.getChildElements();
            JSONObject jsonObject = PrintBody(iterator, null);
            System.out.println(jsonObject);
            DataJson t = JSON.parseObject(jsonObject.toString(), DataJson.class);
            String result = t.getRoot().getResults().getResult();
            JSONArray jsonArray = JSON.parseArray(result);

            jsonArray.stream().forEach(array->{
                com.alibaba.fastjson.JSONObject object = JSON.parseObject(array.toString());
                com.alibaba.fastjson.JSONObject param = new com.alibaba.fastjson.JSONObject();
                param.put("data",object.getString("JDJK_SEND.DATA"));
                param.put("dataType",object.getString("JDJK_SEND.DATETYPE"));
                param.put("type",object.getString("JDJK_SEND.TYPE"));
                param.put("operateOrg",object.getString("JDJK_SEND.OPERATEORG"));
                param.put("operator",object.getString("JDJK_SEND.OPERATOR"));
                param.put("photoName",object.getString("JDJK_SEND.PHOTONAME"));
                param.put("photo",object.getString("JDJK_SEND.PHOTO"));
                System.out.println(param);
            });
            return  jsonArray;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null;
    }

    /**
     * 把soap字符串格式化为SOAPMessage
     *
     * @param soapString
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static SOAPMessage formatSoapString(String soapString) {
        MessageFactory msgFactory;
        try {
            msgFactory = MessageFactory.newInstance();
            SOAPMessage reqMsg = msgFactory.createMessage(new MimeHeaders(),
                    new ByteArrayInputStream(soapString.getBytes("UTF-8")));
            reqMsg.saveChanges();
            return reqMsg;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static JSONObject PrintBody(Iterator<SOAPElement> iterator, String side) {
        JSONObject result = null;
        JSONObject xmlJSONObj = null;
        while (iterator.hasNext()) {
            Object o=iterator.next();
            if(o!=null)  {
                SOAPElement element=null;
                try{
                    element = (SOAPElement) o;
                    System.out.println("Node Name:" + element.getNodeName());
                    System.out.println("Value:" + element.getValue());

                    if(element.getValue()!=null){
                        xmlJSONObj = XML.toJSONObject(element.getValue());
                        if (xmlJSONObj != null){
                            return xmlJSONObj;
                        }
                    }

                }catch(Exception e){}
                if ( element !=null) {
                    result = PrintBody(element.getChildElements(), side + "-----");
                }

            }
        }
        return result;
    }

}
