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

    /**
     * @Method : getNewMission
     * @Description :
     * @Params : [producerId]
     * @Return : java.lang.String
     * @Author : ZhuoNing
     * @Date : 2021/10/28
     * @Time : 16:32
     */
    public String getNewMissionXML(String producerId) {
        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" +
                "    <GetNewProduceMission xmlns=\"http://tempuri.org/\">\n" +
                "      <produceKey>"+producerId+"</produceKey>\n" +
                "    </GetNewProduceMission>\n" +
                "  </soap:Body>\n" +
                "</soap:Envelope>";
    }

    public String getNewMessionResult_test(){
        return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap:Envelope\n" +
                "    xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
                "    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                "    xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n" +
                "    <soap:Body>\n" +
                "        <GetNewProduceMissionResponse\n" +
                "            xmlns=\"http://tempuri.org/\">\n" +
                "            <GetNewProduceMissionResult>\n" +
                "                <xs:schema id=\"NewDataSet\"\n" +
                "                    xmlns=\"\"\n" +
                "                    xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"\n" +
                "                    xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\">\n" +
                "                    <xs:element name=\"NewDataSet\" msdata:IsDataSet=\"true\" msdata:UseCurrentLocale=\"true\">\n" +
                "                        <xs:complexType>\n" +
                "                            <xs:choice minOccurs=\"0\" maxOccurs=\"unbounded\">\n" +
                "                                <xs:element name=\"Mission\">\n" +
                "                                    <xs:complexType>\n" +
                "                                        <xs:sequence>\n" +
                "                                            <xs:element name=\"org_id\" type=\"xs:int\" minOccurs=\"0\" />\n" +
                "                                            <xs:element name=\"org_name\" type=\"xs:string\" minOccurs=\"0\" />\n" +
                "                                            <xs:element name=\"org_address\" type=\"xs:string\" minOccurs=\"0\" />\n" +
                "                                            <xs:element name=\"org_linkman\" type=\"xs:string\" minOccurs=\"0\" />\n" +
                "                                            <xs:element name=\"postcode\" type=\"xs:string\" minOccurs=\"0\" />\n" +
                "                                            <xs:element name=\"org_tel\" type=\"xs:string\" minOccurs=\"0\" />\n" +
                "                                            <xs:element name=\"ApplyID\" type=\"xs:int\" minOccurs=\"0\" />\n" +
                "                                            <xs:element name=\"AssignedDate\" type=\"xs:dateTime\" minOccurs=\"0\" />\n" +
                "                                            <xs:element name=\"EarMarkAmount\" type=\"xs:long\" minOccurs=\"0\" />\n" +
                "                                            <xs:element name=\"RealEarMarkAmount\" type=\"xs:long\" minOccurs=\"0\" />\n" +
                "                                            <xs:element name=\"MissionProcessor\" type=\"xs:int\" minOccurs=\"0\" />\n" +
                "                                            <xs:element name=\"ConsignmentDate\" type=\"xs:dateTime\" minOccurs=\"0\" />\n" +
                "                                            <xs:element name=\"ApplyRegionName\" type=\"xs:string\" minOccurs=\"0\" />\n" +
                "                                            <xs:element name=\"AnimalType\" type=\"xs:int\" minOccurs=\"0\" />\n" +
                "                                            <xs:element name=\"AnimalTypeName\" type=\"xs:string\" minOccurs=\"0\" />\n" +
                "                                        </xs:sequence>\n" +
                "                                    </xs:complexType>\n" +
                "                                </xs:element>\n" +
                "                            </xs:choice>\n" +
                "                        </xs:complexType>\n" +
                "                    </xs:element>\n" +
                "                </xs:schema>\n" +
                "                <diffgr:diffgram\n" +
                "                    xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\"\n" +
                "                    xmlns:diffgr=\"urn:schemas-microsoft-com:xml-diffgram-v1\">\n" +
                "                    <NewDataSet\n" +
                "                        xmlns=\"\">\n" +
                "                        <Mission diffgr:id=\"Mission1\" msdata:rowOrder=\"0\">\n" +
                "                            <org_id>1936</org_id>\n" +
                "                            <org_name>大兴区动物疾病控制中心</org_name>\n" +
                "                            <org_address>大兴区动物疾病控制中心</org_address>\n" +
                "                            <org_linkman>大兴区动物疾病控制中心</org_linkman>\n" +
                "                            <postcode>102600</postcode>\n" +
                "                            <org_tel>01069207882                                       </org_tel>\n" +
                "                            <ApplyID>111111</ApplyID>\n" +
                "                            <AssignedDate>2021-10-19T09:43:52+08:00</AssignedDate>\n" +
                "                            <EarMarkAmount>140</EarMarkAmount>\n" +
                "                            <RealEarMarkAmount>140</RealEarMarkAmount>\n" +
                "                            <MissionProcessor>1936</MissionProcessor>\n" +
                "                            <ConsignmentDate>2022-01-17T09:53:18.621132+08:00</ConsignmentDate>\n" +
                "                            <ApplyRegionName>北京市大兴区</ApplyRegionName>\n" +
                "                            <AnimalType>3</AnimalType>\n" +
                "                            <AnimalTypeName>羊</AnimalTypeName>\n" +
                "                        </Mission>\n" +
                "                        <Mission diffgr:id=\"Mission2\" msdata:rowOrder=\"1\">\n" +
                "                            <org_id>1936</org_id>\n" +
                "                            <org_name>大兴区动物疾病控制中心</org_name>\n" +
                "                            <org_address>大兴区动物疾病控制中心</org_address>\n" +
                "                            <org_linkman>大兴区动物疾病控制中心</org_linkman>\n" +
                "                            <postcode>102600</postcode>\n" +
                "                            <org_tel>01069207882                                       </org_tel>\n" +
                "                            <ApplyID>216764</ApplyID>\n" +
                "                            <AssignedDate>2021-10-19T09:43:52+08:00</AssignedDate>\n" +
                "                            <EarMarkAmount>140</EarMarkAmount>\n" +
                "                            <RealEarMarkAmount>140</RealEarMarkAmount>\n" +
                "                            <MissionProcessor>1936</MissionProcessor>\n" +
                "                            <ConsignmentDate>2022-01-17T09:53:18.621132+08:00</ConsignmentDate>\n" +
                "                            <ApplyRegionName>北京市大兴区</ApplyRegionName>\n" +
                "                            <AnimalType>3</AnimalType>\n" +
                "                            <AnimalTypeName>羊</AnimalTypeName>\n" +
                "                        </Mission>\n" +
                "                    </NewDataSet>\n" +
                "                </diffgr:diffgram>\n" +
                "            </GetNewProduceMissionResult>\n" +
                "        </GetNewProduceMissionResponse>\n" +
                "    </soap:Body>\n" +
                "</soap:Envelope>\n";
    }

    /**
     * @Method : parseApplyXML
     * @Description :
     * @Params : [xml]
     * @Return : com.alibaba.fastjson.JSONArray
     * @Author : ZhuoNing
     * @Date : 2021/10/28
     * @Time : 16:32
     */
    public JSONArray parseReturnNewMissionXML(String xml) {
        Document doc = null;
        try {
            doc = DocumentHelper.parseText(xml); // 将字符串转为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("GetNewProduceMissionResponse");
            Element recordEle2 = (Element) iter2.next();

            Iterator GetNewMissionEarMarkResultIter = recordEle2.elementIterator("GetNewProduceMissionResult");
            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 missionIter = newDataSetChildEle.elementIterator("Mission");
//            System.out.println("missionIter.hasNext()>>" + missionIter.hasNext());
            // 所有数据
            JSONArray allDataJa = new JSONArray();

            while (missionIter.hasNext()) {
                JSONObject earMarkJo = new JSONObject();
                allDataJa.add(earMarkJo);
                Element earMarkIterEle = (Element) missionIter.next();
                earMarkJo.put("applyID", earMarkIterEle.elementTextTrim("ApplyID"));
                earMarkJo.put("orgId", earMarkIterEle.elementTextTrim("org_id"));
                earMarkJo.put("orgName", earMarkIterEle.elementTextTrim("org_name"));
                earMarkJo.put("orgAddress", earMarkIterEle.elementTextTrim("org_address"));
                earMarkJo.put("orgLinkman", earMarkIterEle.elementTextTrim("org_linkman"));
                earMarkJo.put("postCode", earMarkIterEle.elementTextTrim("postcode"));
                earMarkJo.put("orgTel", earMarkIterEle.elementTextTrim("org_tel"));
                earMarkJo.put("applyId", earMarkIterEle.elementTextTrim("ApplyID"));
                earMarkJo.put("assignedDate", earMarkIterEle.elementTextTrim("AssignedDate"));
                earMarkJo.put("earmarkAmount", earMarkIterEle.elementTextTrim("EarMarkAmount"));
                earMarkJo.put("realEarmarkAmount", earMarkIterEle.elementTextTrim("RealEarMarkAmount"));
                earMarkJo.put("missionProcessor", earMarkIterEle.elementTextTrim("MissionProcessor"));
                earMarkJo.put("consignmentDate", earMarkIterEle.elementTextTrim("ConsignmentDate"));
                earMarkJo.put("applyRegionName", earMarkIterEle.elementTextTrim("ApplyRegionName"));
                earMarkJo.put("animalType", earMarkIterEle.elementTextTrim("AnimalType"));
                earMarkJo.put("animalTypeName", earMarkIterEle.elementTextTrim("AnimalTypeName"));
            }
            return allDataJa;
        } catch (DocumentException e) {
            e.printStackTrace();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @Method      : getSetMissionHasDownloadXML
     * @Description : 设置任务已下载状态信息
     * @Params      : [applyId]
     * @Return      : java.lang.String
     *
     * @Author      : ZhuoNing
     * @Date        : 2021/10/29
     * @Time        : 11:05
     */
    public String getSetMissionHasDownloadXML(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" +
                "    <SetMissionHasDownload xmlns=\"http://tempuri.org/\">\n" +
                "      <applyID>"+applyId+"</applyID>\n" +
                "    </SetMissionHasDownload>\n" +
                "  </soap:Body>\n" +
                "</soap:Envelope>";
    }

    /**
     * @Method      : parseReturnSetMissionHasDownStateXML
     * @Description : 
     * @Params      : [callSoap]
     * @Return      : java.lang.String
     * 
     * @Author      : ZhuoNing
     * @Date        : 2021/10/29       
     * @Time        : 11:21
     */
    public String parseReturnSetMissionHasDownStateXML(String xml) {
        Document doc = null;
        try {
            doc = DocumentHelper.parseText(xml); // 将字符串转为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("SetMissionHasDownloadResponse");
            Element recordEle2 = (Element) iter2.next();
            return recordEle2.elementTextTrim("SetMissionHasDownloadResult");
        } catch (DocumentException e) {
            e.printStackTrace();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
