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 PackageTask {

    /**
     * @Method      : getNewPackageRequestXML
     * @Description : 构造请求文档
     * @Params      : [applyId]
     * @Return      : java.lang.String
     *
     * @Author      : ZhuoNing
     * @Date        : 2021/10/29
     * @Time        : 15:51
     */
    public String getNewPackageRequestXML(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" +
                "    <GetNewMissionPackage xmlns=\"http://tempuri.org/\">\n" +
                "      <applyID>"+applyId+"</applyID>\n" +
                "    </GetNewMissionPackage>\n" +
                "  </soap:Body>\n" +
                "</soap:Envelope>";
    }

    /**
     * @Method      : parseAnswerXML
     * @Description :
     * @Params      : [answerXML]
     * @Return      : com.alibaba.fastjson.JSONArray
     *
     * @Author      : ZhuoNing
     * @Date        : 2021/10/29
     * @Time        : 16:02
     */
    public JSONArray parseNewPackageAnswerXML(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("GetNewMissionPackageResponse");
            Element recordEle2 = (Element) iter2.next();

            Iterator GetNewMissionEarMarkResultIter = recordEle2.elementIterator("GetNewMissionPackageResult");
            Element GetNewMissionEarMarkResultIterEle = (Element) GetNewMissionEarMarkResultIter.next();

//            Iterator schemaIter = GetNewMissionEarMarkResultIterEle.elementIterator("schema");
//            Element schemaEle = (Element) schemaIter.next();
            Iterator diffgramIter = GetNewMissionEarMarkResultIterEle.elementIterator("diffgram");
            Element diffgramEle = (Element) diffgramIter.next();

            Iterator newDataSetIter = diffgramEle.elementIterator("NewDataSet");
            Element newDataSetChildEle = (Element) newDataSetIter.next();

            Iterator packageIter = newDataSetChildEle.elementIterator("Package");
            // 所有数据
            JSONArray allDataJa = new JSONArray();
            while (packageIter.hasNext()) {
                Element packageIterEle = (Element) packageIter.next();
                JSONObject packageJo = new JSONObject();
                allDataJa.add(packageJo);
                packageJo.put("packageId",packageIterEle.elementTextTrim("PackageID"));
                packageJo.put("packageNumber",packageIterEle.elementTextTrim("PackageNumber"));
                packageJo.put("applyID",packageIterEle.elementTextTrim("ApplyID"));
                packageJo.put("packageData",packageIterEle.elementTextTrim("PackageData"));
            }
            return allDataJa;
        } catch (DocumentException e) {
            e.printStackTrace();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // 构造请求XML
    public String getReturnPackageHasDownloadXML(String[] packageIdArray) {
        StringBuilder dataSb = new StringBuilder();
        for(int a=0;a<packageIdArray.length;a++){
            dataSb.append("<int>"+packageIdArray[a]+"</int>");
        }
        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" +
                "    <SetPackagesHasDownload xmlns=\"http://tempuri.org/\">\n" +
                "      <packageIDs>\n" +
                dataSb.toString() +
                "      </packageIDs>\n" +
                "    </SetPackagesHasDownload>\n" +
                "  </soap:Body>\n" +
                "</soap:Envelope>";
    }

    // 解析返回报文
    public String parseAnswerXML(String answerXML) {
        // test
//        answerXML="<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" +
//                "    <SetPackagesHasDownloadResponse xmlns=\"http://tempuri.org/\">\n" +
//                "      <SetPackagesHasDownloadResult>true</SetPackagesHasDownloadResult>\n" +
//                "    </SetPackagesHasDownloadResponse>\n" +
//                "  </soap:Body>\n" +
//                "</soap:Envelope>";

        //解析生成数据
        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("SetPackagesHasDownloadResponse");
            Element recordEle2 = (Element) iter2.next();
            String updateReturnState = recordEle2.elementTextTrim("SetPackagesHasDownloadResult");
            System.out.println("updateReturnState>>"+updateReturnState);
            return updateReturnState;
        } catch (DocumentException e) {
            e.printStackTrace();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
