package com.huimv.manage.webservice.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Component;

import java.util.Iterator;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
@Component
public class EarmarkTask {

    /**
     * @Method      : getRequestEarmarkXML
     * @Description : 构造请求内容
     * @Params      : [applyId]
     * @Return      : java.lang.String
     * 
     * @Author      : ZhuoNing
     * @Date        : 2021/10/29       
     * @Time        : 21:54
     */
    public String getRequestEarmarkXML(int applyId) {
        return "<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/\">\n" +
                "  <soap:Body>\n" +
                "    <GetNewMissionEarMark xmlns=\"http://tempuri.org/\">\n" +
                "      <applyID>"+applyId+"</applyID>\n" +
                "    </GetNewMissionEarMark>\n" +
                "  </soap:Body>\n" +
                "</soap:Envelope>";
    }

    /**
     * @Method      : parseearMarkReponseXML
     * @Description : 解析返回结果
     * @Params      : [earMarkReponseXML]
     * @Return      : com.alibaba.fastjson.JSONArray
     * 
     * @Author      : ZhuoNing
     * @Date        : 2021/10/29       
     * @Time        : 21:59
     */
    public JSONArray parseEarMarkReponseXML(String earMarkReponseXML) {
        Document doc = null;
        try {
            doc = DocumentHelper.parseText(earMarkReponseXML); // 将字符串转为XML
            Element rootElt = doc.getRootElement(); // 获取根节点
//            System.out.println("根节点：" + rootElt.getName()); // 拿到根节点的名称

            Iterator iter = rootElt.elementIterator("Body"); // 获取根节点下的子节点 Body
//            System.out.println("iter.hasNext:"+iter.hasNext());
            Element recordEle = (Element) iter.next();
//            System.out.println("name="+recordEle.getName());

            Iterator iter2 = recordEle.elementIterator("GetNewMissionEarMarkResponse");
//            System.out.println("iter2.hasNext:"+iter2.hasNext());
            Element recordEle2 = (Element) iter2.next();
//            System.out.println("name="+recordEle2.getName());

            Iterator GetNewMissionEarMarkResultIter = recordEle2.elementIterator("GetNewMissionEarMarkResult");
//            System.out.println("GetNewMissionEarMarkResultIter.hasNext:"+GetNewMissionEarMarkResultIter.hasNext());
            Element GetNewMissionEarMarkResultIterEle = (Element) GetNewMissionEarMarkResultIter.next();
//            System.out.println("name="+GetNewMissionEarMarkResultIterEle.getName());

            Iterator schemaIter = GetNewMissionEarMarkResultIterEle.elementIterator("schema");
//            System.out.println("schemaIter.hasNext:"+schemaIter.hasNext());
            Element schemaEle = (Element) schemaIter.next();
//            System.out.println("name="+schemaEle.getName());
            Iterator diffgramIter = GetNewMissionEarMarkResultIterEle.elementIterator("diffgram");
//            System.out.println("diffgramIter.hasNext:"+diffgramIter.hasNext());
            Element diffgramEle = (Element) diffgramIter.next();
//            System.out.println("name="+diffgramEle.getName());

            Iterator newDataSetIter = diffgramEle.elementIterator("NewDataSet");
//            System.out.println("newDataSetIter.hasNext:"+newDataSetIter.hasNext());
            Element newDataSetChildEle = (Element) newDataSetIter.next();
//            System.out.println("name="+newDataSetChildEle.getName());

            Iterator earMarkIter = newDataSetChildEle.elementIterator("EarMark");
//            System.out.println("newDataSetIter.hasNext:"+newDataSetIter.hasNext());
            // 所有数据
            JSONArray allDataJa = new JSONArray();

            while (earMarkIter.hasNext()) {
                JSONObject earMarkJo = new JSONObject();
                allDataJa.add(earMarkJo);
                Element earMarkIterEle = (Element) earMarkIter.next();
                earMarkJo.put("earmarkId",earMarkIterEle.elementTextTrim("EarMarkID"));
                earMarkJo.put("packageId",earMarkIterEle.elementTextTrim("PackageID"));
                earMarkJo.put("earmarkNumber",earMarkIterEle.elementTextTrim("EarMarkNumber"));
                earMarkJo.put("applyId",earMarkIterEle.elementTextTrim("ApplyID"));
                earMarkJo.put("earmarkData",earMarkIterEle.elementTextTrim("EarMarkData"));
            }
            return allDataJa;
        } catch (DocumentException e) {
            e.printStackTrace();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //构造请求内容
    public String getUpdateDownStateRequestXML(String earmarkIDs) {
        String[] earmarkIdArray = earmarkIDs.split(",");
        StringBuilder idSb = new StringBuilder();
        for(int a=0;a<earmarkIdArray.length;a++){
            idSb.append("<long>"+earmarkIdArray[a]+"</long>");
        }
        return "<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/\">\n" +
                "  <soap:Body>\n" +
                "    <SetEarMarksHasDownload xmlns=\"http://tempuri.org/\">\n" +
                "      <earMarkIDs>\n" +
                idSb.toString()+
                "      </earMarkIDs>\n" +
                "    </SetEarMarksHasDownload>\n" +
                "  </soap:Body>\n" +
                "</soap:Envelope>";
    }

    //解析设置耳标已下载状态
    public String parseUpdateDownStateXML(String answerXML) {
        //解析生成数据
        Document doc = null;
        try {
            doc = DocumentHelper.parseText(answerXML); // 将字符串转为XML
            Element rootElt = doc.getRootElement(); // 获取根节点
//            System.out.println("根节点：" + rootElt.getName()); // 拿到根节点的名称

            Iterator iter = rootElt.elementIterator("Body"); // 获取根节点下的子节点 Body
            Element recordEle = (Element) iter.next();

            Iterator iter2 = recordEle.elementIterator("SetEarMarksHasDownloadResponse");
            Element recordEle2 = (Element) iter2.next();
            return recordEle2.elementTextTrim("SetEarMarksHasDownloadResult");
        } catch (DocumentException e) {
            e.printStackTrace();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //test
    public String TestParseUpdateDownStateXML() {
        return "<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/\">\n" +
                        "  <soap:Body>\n" +
                        "    <SetEarMarksHasDownloadResponse xmlns=\"http://tempuri.org/\">\n" +
                        "      <SetEarMarksHasDownloadResult>true</SetEarMarksHasDownloadResult>\n" +
                        "    </SetEarMarksHasDownloadResponse>\n" +
                        "  </soap:Body>\n" +
                        "</soap:Envelope>";
    }
}
