package com.hh.holyolm.util;

import static android.content.ContentValues.TAG;

import android.util.Log;

import com.hh.holyolm.model.CheckItemModel;
import com.hh.holyolm.model.CheckItemModel2;
import com.hh.holyolm.model.CommandCardModel;
import com.hh.holyolm.model.InputModel;
import com.hh.holyolm.model.OptCardModel;
import com.hh.holyolm.model.PhotoGroupModel;
import com.hh.holyolm.model.PhotoItemModel;
import com.hh.holyolm.model.TaskModel;
import com.hh.holyolm.model.WorkRecordModel;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

/*import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;*/

public class XmlUtil {

    public static void InstanceTaskXml(File xmlFile,String taskNum,String model,String planeNum){
        if(!xmlFile.exists()){
            Log.e(TAG, "XML文件未找到："+xmlFile.getAbsolutePath());
            throw new RuntimeException("XML文件未找到："+xmlFile.getAbsolutePath());
        }
        SAXReader reader=new SAXReader();
        reader.setEncoding("UTF-8");

        try{
            Document xmlDoc = reader.read(xmlFile);
            Element docEle = xmlDoc.getRootElement();
            List elementList = docEle.selectNodes("/数据模块/指令卡");
            if (elementList != null && elementList.size() > 0) {
                Element element = (Element) elementList.get(0);
                element.setAttributeValue("机型",model);
                element.setAttributeValue("任务号",taskNum);
                element.setAttributeValue("机号",planeNum);
                element.setAttributeValue("状态","待执行");
                element.setAttributeValue("创建时间",Utils.GenTimeStr());
                element.setAttributeValue("创建人","");
                element.setAttributeValue("完成时间","");
                element.setAttributeValue("创建单位","");

                Iterator<Element> ite = element.elementIterator();
                while(ite.hasNext()){
                    Element childEle=ite.next();
                    if(childEle.getName().equals("任务执行")){
                        childEle.setAttributeValue("编号", UUID.randomUUID().toString().replaceAll("-", ""));
                    }
                }
            }
            Writer writer = new FileWriter(xmlFile);
            OutputFormat format= OutputFormat.createPrettyPrint();//格式化
            XMLWriter xmlWriter = new XMLWriter(writer,format);
            xmlWriter.write(xmlDoc);
            xmlWriter.close();

        } catch (DocumentException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static List<CommandCardModel> ReadCommandCardLib(String commdLibPath){
        String xmlPath=commdLibPath+File.separator+"CommandCardLib.xml";
        ArrayList<CommandCardModel> list=new ArrayList<CommandCardModel>();
        File xmlFile=new File(xmlPath);
        if(!xmlFile.exists()){
            Log.e(TAG, "XML文件未找到："+xmlFile.getAbsolutePath());
            return null;
            //throw new RuntimeException("XML文件未找到："+xmlFile.getAbsolutePath());
        }
        SAXReader reader=new SAXReader();
        reader.setEncoding("UTF-8");
        try {
            Document xmlDoc= reader.read(xmlFile);
            Element docEle=xmlDoc.getRootElement();
            List elementList=docEle.selectNodes("/数据模块/指令卡库/指令卡");
            if(!elementList.isEmpty()){
                for(int i=0;i<elementList.size();i++){
                    Element element=(Element)elementList.get(i);
                    CommandCardModel model=new CommandCardModel();
                    model.setType(element.attributeValue("类别"));
                    model.setWindow(element.attributeValue("时机"));
                    model.setSubject(element.attributeValue("专业"));
                    model.setNumber(element.attributeValue("卡号"));
                    model.setVersion(element.attributeValue("版本"));
                    model.setXmlFile(element.attributeValue("xml"));
                    model.setPackagePath(commdLibPath+File.separator+element.attributeValue("package"));
                    model.setName(element.getText());
                    list.add(model);
                }
            }

        } catch (DocumentException e) {
            Log.e(TAG, Log.getStackTraceString(e));
            throw new RuntimeException(e);
        }

        return  list;
    }

    public static List<OptCardModel> ReadOptCardLib(String xmlPath){
        ArrayList<OptCardModel> list=new ArrayList<OptCardModel>();
        File xmlFile=new File(xmlPath);
        if(!xmlFile.exists()){
            Log.e(TAG, "XML文件未找到："+xmlFile.getAbsolutePath());
            //throw new RuntimeException("XML文件未找到："+xmlFile.getAbsolutePath());
            return null;
        }
        SAXReader reader=new SAXReader();
        reader.setEncoding("UTF-8");
        try {
            Document xmlDoc= reader.read(xmlFile);
            Element docEle=xmlDoc.getRootElement();
            List elementList=docEle.selectNodes("/数据模块/操作卡");
            if(!elementList.isEmpty()){
                for(int i=0;i<elementList.size();i++){
                    Element element=(Element)elementList.get(i);
                    OptCardModel model=new OptCardModel();
                    model.setNumber(element.attributeValue("编号"));
                    model.setVersion(element.attributeValue("版本"));
                    model.setPdfFile(element.attributeValue("pdf"));
                    model.setDir(element.attributeValue("package"));
                    model.setName(element.getText());
                    list.add(model);
                }
            }

        } catch (DocumentException e) {
            Log.e(TAG, Log.getStackTraceString(e));
            throw new RuntimeException(e);
        }

        return  list;
    }

    public static OptCardModel FindOptCard(String xmlPath,String cardNum){
        File xmlFile=new File(xmlPath);
        if(!xmlFile.exists()){
            Log.e(TAG, "XML文件未找到："+xmlFile.getAbsolutePath());
            return null;
        }
        SAXReader reader=new SAXReader();
        reader.setEncoding("UTF-8");
        try {
            Document xmlDoc= reader.read(xmlFile);
            Element docEle=xmlDoc.getRootElement();
            Node node=docEle.selectSingleNode("/数据模块/操作卡[@编号='"+cardNum+"']");
            if(node!=null){
                Element element=(Element) node;
                OptCardModel model=new OptCardModel();
                model.setNumber(element.attributeValue("编号"));
                model.setVersion(element.attributeValue("版本"));
                model.setPdfFile(element.attributeValue("pdf"));
                model.setDir(element.attributeValue("package"));
                model.setName(element.getText());
                return model;
            }
        } catch (DocumentException e) {
            Log.e(TAG, Log.getStackTraceString(e));
            throw new RuntimeException(e);
        }

        return  null;
    }

    public static List<TaskModel> ReadTaskList(File taskDir){
        SAXReader reader=new SAXReader();
        reader.setEncoding("UTF-8");

        ArrayList<TaskModel> list=new ArrayList<TaskModel>();
        File[] childDirs=taskDir.listFiles();
        for(int i=0;i<childDirs.length;i++){
            File childDir=childDirs[i];
            if(childDir.isDirectory()){
                File[] files=childDir.listFiles();
                for (int j = 0; j < files.length; j++) {
                    File file=files[j];
                    if(file.getName().toLowerCase().equals("task.xml")){
                        try{
                            Document xmlDoc= reader.read(file);
                            Element docEle=xmlDoc.getRootElement();
                            TaskModel taskModel=new TaskModel();

                            List elementList=docEle.selectNodes("/数据模块/指令卡");
                            if(elementList!=null && elementList.size()>0){
                                Element element=(Element) elementList.get(0);
                                taskModel.setTaskNum(element.attributeValue("任务号"));
                                taskModel.setCardNum(element.attributeValue("编号"));
                                taskModel.setCardName(element.attributeValue("名称"));
                                taskModel.setPlaneModel(element.attributeValue("机型"));
                                taskModel.setPlaneNum(element.attributeValue("机号"));
                                taskModel.setStatus(element.attributeValue("状态"));
                                taskModel.setIntime(element.attributeValue("创建时间"));
                                taskModel.setPackagePath(childDir.getAbsolutePath());
                                taskModel.setXmlPath(file.getAbsolutePath());
                                list.add(taskModel);
                            }
                        }catch (DocumentException e) {
                            Log.e(TAG, Log.getStackTraceString(e));
                            throw new RuntimeException(e);
                        }
                    }else{
                        continue;
                    }
                }
            }
        }

        return  list;
    }

    public static TaskModel ReadTaskInfo(String xmlPath,String pkgPath) {
        SAXReader reader=new SAXReader();
        reader.setEncoding("UTF-8");
        File xmlFile=new File(xmlPath);

        try {
            Document xmlDoc = reader.read(xmlFile);
            Element docEle = xmlDoc.getRootElement();
            TaskModel taskModel = new TaskModel();

            List elementList = docEle.selectNodes("/数据模块/指令卡");
            if (elementList != null && elementList.size() > 0) {
                Element element = (Element) elementList.get(0);
                taskModel.setTaskNum(element.attributeValue("任务号"));
                taskModel.setCardNum(element.attributeValue("编号"));
                taskModel.setCardName(element.attributeValue("名称"));
                taskModel.setPlaneModel(element.attributeValue("机型"));
                taskModel.setPlaneNum(element.attributeValue("机号"));
                taskModel.setStatus(element.attributeValue("状态"));
                taskModel.setIntime(element.attributeValue("接收时间"));
                taskModel.setPackagePath(pkgPath);
                taskModel.setXmlPath(xmlFile.getAbsolutePath());
            }
            elementList = docEle.selectNodes("/数据模块/指令卡/任务执行");
            if (elementList != null && elementList.size() > 0) {
                //ArrayList<CheckItemModel> checkItemList = new ArrayList<CheckItemModel>();
                //taskModel.setCheckItemList(checkItemList);
                List<CheckItemModel> checkItemList =taskModel.getCheckItemList(); ////维修指令
                List<CheckItemModel2> checkItem2List =taskModel.getCheckItem2List(); //维护措施
                for (int i = 0; i < elementList.size(); i++) {
                    Element itemEle = (Element) elementList.get(i);
                    if(itemEle.attributeValue("类型")!=null && itemEle.attributeValue("类型").equals("维修指令") ){
                        CheckItemModel checkItemModel = new CheckItemModel();
                        ArrayList<WorkRecordModel> recordList = new ArrayList<WorkRecordModel>();
                        checkItemModel.setRecordList(recordList);
                        ArrayList<OptCardModel> cardList = new ArrayList<OptCardModel>();
                        checkItemModel.setCardList(cardList);

                        checkItemModel.setSeq(itemEle.attributeValue("序号"));
                        Iterator<Element> ite = itemEle.elementIterator();
                        while (ite.hasNext()) {
                            Element child = ite.next();
                            if (child.getName().equals("工作内容"))
                                checkItemModel.setCheckContent(child.getText());
                            if (child.getName().equals("完成情况"))
                                checkItemModel.setIsComplete(child.getText());
                            if (child.getName().equals("工作者"))
                                checkItemModel.setWoker(child.getText());
                            if (child.getName().equals("复查者"))
                                checkItemModel.setChecker(child.getText());

                            if (child.getName().equals("检查时机序号"))
                                checkItemModel.setCheckOppSeq(child.getText());
                            if (child.getName().equals("检查时机"))
                                checkItemModel.setCheckOpp(child.getText());
                            if (child.getName().equals("检查项目部位序号"))
                                checkItemModel.setCheckRegionSeq(child.getText());
                            if (child.getName().equals("检查项目部位"))
                                checkItemModel.setCheckRegion(child.getText());
                            if (child.getName().equals("检查顺序序号"))
                                checkItemModel.setCheckSeqSeq(child.getText());
                            if (child.getName().equals("检查顺序"))
                                checkItemModel.setCheckSeq(child.getText());
                            if (child.getName().equals("检查方法和标准主序号"))
                                checkItemModel.setMainSeq(child.getText());
                            if (child.getName().equals("检查方法和标准次序号"))
                                checkItemModel.setSecondarySeq(child.getText());
                            if (child.getName().equals("检查方法和标准次名称"))
                                checkItemModel.setSecondaryName(child.getText());
                            if (child.getName().equals("检查方法和标准"))
                                checkItemModel.setCheckMethod(child.getText());
                            if (child.getName().equals("涉及的特定任务"))
                                checkItemModel.setSpecialTask(child.getText());
                            if (child.getName().equals("针对典型环境"))
                                checkItemModel.setSpecialCond(child.getText());
                            if (child.getName().equals("典型环境下的措施"))
                                checkItemModel.setSpecialMethod(child.getText());
                            if (child.getName().equals("注意内容"))
                                checkItemModel.setAttention(child.getText());
                            if (child.getName().equals("警告内容"))
                                checkItemModel.setWarning(child.getText());

                            if (child.getName().equals("操作卡")){
                                OptCardModel optCardModel=new OptCardModel();
                                optCardModel.setNumber(child.attributeValue("编号"));
                                optCardModel.setName(child.attributeValue("名称"));
                                cardList.add(optCardModel);
                            }

                            if (child.getName().equals("执行记录")){
                                WorkRecordModel recordModel=new WorkRecordModel();
                                recordList.add(recordModel);
                                if(child.attributeValue("类型")!=null){
                                    if(child.attributeValue("类型").equals("填写项")){
                                        recordModel.setType("Input");
                                        recordModel.setInputEles(new ArrayList<Object>());
                                        Iterator<Element> it2 = child.elementIterator();
                                        while (it2.hasNext()){
                                            Element ele2=it2.next();
                                            if(ele2.getName().equals("普通文本")) recordModel.getInputEles().add(ele2.getText());
                                            if(ele2.getName().equals("填写项")){
                                                InputModel inputModel=new InputModel();
                                                inputModel.setId(ele2.attributeValue("id"));
                                                inputModel.setValue(ele2.getText());
                                                recordModel.getInputEles().add(inputModel);
                                            }
                                        }
                                    }
                                }

                            }
                        /*if(child.getName().equals("选择项")){
                            OptionModel option=new OptionModel();
                            option.setId(child.attributeValue("id"));
                            option.setText(child.getText());
                            //option.setDefault(child.attributeValue("默认值"));
                            if(child.attributeValue("checked")!=null && child.attributeValue("checked").equals("1")){
                                option.setChecked(true);
                                userChecked=true;
                            }else{
                                option.setChecked(false);
                            }
                            if(child.attributeValue("默认值")!=null && child.attributeValue("默认值").equals("1")){
                                defaultOpt=option;
                            }
                            optionList.add(option);
                        }*/
                            if (child.getName().equals("拍照项")) {
                                PhotoGroupModel photoGroupModel = new PhotoGroupModel();
                                photoGroupModel.setId(child.attributeValue("编号"));
                                photoGroupModel.setPicNum(Integer.valueOf(child.attributeValue("照片数量")));
                                List<PhotoItemModel> photoItemModelList = new ArrayList<PhotoItemModel>();
                                photoGroupModel.setItemList(photoItemModelList);
                                Iterator<Element> l2ite = child.elementIterator();
                                while (l2ite.hasNext()) {
                                    Element l2ele = l2ite.next();
                                    if (l2ele.getName().equals("拍照要求")) {
                                        PhotoItemModel photoItemModel = new PhotoItemModel();
                                        photoItemModelList.add(photoItemModel);
                                        photoItemModel.setSeq(l2ele.attributeValue("序号"));
                                        Iterator<Element> l3ite = l2ele.elementIterator();
                                        while (l3ite.hasNext()) {
                                            Element l3ele = l3ite.next();
                                            if (l3ele.getName().equals("描述"))
                                                photoItemModel.setDesc(l3ele.getText());
                                            if (l3ele.getName().equals("样例照片"))
                                                photoItemModel.setSamplePic(l3ele.getText());
                                            if (l3ele.getName().equals("实际照片")) {
                                                photoItemModel.setActualPic(l3ele.getText());
                                                photoItemModel.setActualPicSrc(l3ele.attributeValue("来源"));
                                            }
                                        }
                                    }
                                }
                                checkItemModel.setPhotoGroup(photoGroupModel);
                            }
                        }
                        checkItemList.add(checkItemModel);

                    }else if(itemEle.attributeValue("类型")!=null && itemEle.attributeValue("类型").equals("维护措施") ){

                        CheckItemModel2 checkItemModel2 = new CheckItemModel2();
                        ArrayList<WorkRecordModel> recordList = new ArrayList<WorkRecordModel>();
                        checkItemModel2.setRecordList(recordList);
                        ArrayList<OptCardModel> cardList = new ArrayList<OptCardModel>();
                        checkItemModel2.setCardList(cardList);

                        checkItemModel2.setSeq(itemEle.attributeValue("序号"));
                        Iterator<Element> ite = itemEle.elementIterator();
                        while (ite.hasNext()) {
                            Element child = ite.next();
                            if (child.getName().equals("工作内容"))
                                checkItemModel2.setCheckContent(child.getText());
                            if (child.getName().equals("完成情况"))
                                checkItemModel2.setIsComplete(child.getText());
                            if (child.getName().equals("工作者"))
                                checkItemModel2.setWoker(child.getText());
                            if (child.getName().equals("复查者"))
                                checkItemModel2.setChecker(child.getText());

                            if (child.getName().equals("分类"))
                                checkItemModel2.setItemType(child.getText());
                            if (child.getName().equals("来源"))
                                checkItemModel2.setItemSrc(child.getText());
                            if (child.getName().equals("发布单位"))
                                checkItemModel2.setReleaseDept(child.getText());
                            if (child.getName().equals("故障类别"))
                                checkItemModel2.setFailureType(child.getText());
                            if (child.getName().equals("系统"))
                                checkItemModel2.setFailureSys(child.getText());
                            if (child.getName().equals("故障现象"))
                                checkItemModel2.setFailure(child.getText());
                            if (child.getName().equals("落实时机"))
                                checkItemModel2.setWorkOpp(child.getText());

                            if (child.getName().equals("操作卡")){
                                OptCardModel optCardModel=new OptCardModel();
                                optCardModel.setNumber(child.attributeValue("编号"));
                                optCardModel.setName(child.attributeValue("名称"));
                                cardList.add(optCardModel);
                            }

                            if (child.getName().equals("执行记录")){
                                WorkRecordModel recordModel=new WorkRecordModel();
                                recordList.add(recordModel);
                                if(child.attributeValue("类型")!=null){
                                    if(child.attributeValue("类型").equals("填写项")){
                                        recordModel.setType("Input");
                                        recordModel.setInputEles(new ArrayList<Object>());
                                        Iterator<Element> it2 = child.elementIterator();
                                        while (it2.hasNext()){
                                            Element ele2=it2.next();
                                            if(ele2.getName().equals("普通文本")) recordModel.getInputEles().add(ele2.getText());
                                            if(ele2.getName().equals("填写项")){
                                                InputModel inputModel=new InputModel();
                                                inputModel.setId(ele2.attributeValue("id"));
                                                inputModel.setValue(ele2.getText());
                                                recordModel.getInputEles().add(inputModel);
                                            }
                                        }
                                    }
                                }

                            }
                        /*if(child.getName().equals("选择项")){
                            OptionModel option=new OptionModel();
                            option.setId(child.attributeValue("id"));
                            option.setText(child.getText());
                            //option.setDefault(child.attributeValue("默认值"));
                            if(child.attributeValue("checked")!=null && child.attributeValue("checked").equals("1")){
                                option.setChecked(true);
                                userChecked=true;
                            }else{
                                option.setChecked(false);
                            }
                            if(child.attributeValue("默认值")!=null && child.attributeValue("默认值").equals("1")){
                                defaultOpt=option;
                            }
                            optionList.add(option);
                        }*/
                            if (child.getName().equals("拍照项")) {
                                PhotoGroupModel photoGroupModel = new PhotoGroupModel();
                                photoGroupModel.setId(child.attributeValue("编号"));
                                photoGroupModel.setPicNum(Integer.valueOf(child.attributeValue("照片数量")));
                                List<PhotoItemModel> photoItemModelList = new ArrayList<PhotoItemModel>();
                                photoGroupModel.setItemList(photoItemModelList);
                                Iterator<Element> l2ite = child.elementIterator();
                                while (l2ite.hasNext()) {
                                    Element l2ele = l2ite.next();
                                    if (l2ele.getName().equals("拍照要求")) {
                                        PhotoItemModel photoItemModel = new PhotoItemModel();
                                        photoItemModelList.add(photoItemModel);
                                        photoItemModel.setSeq(l2ele.attributeValue("序号"));
                                        Iterator<Element> l3ite = l2ele.elementIterator();
                                        while (l3ite.hasNext()) {
                                            Element l3ele = l3ite.next();
                                            if (l3ele.getName().equals("描述"))
                                                photoItemModel.setDesc(l3ele.getText());
                                            if (l3ele.getName().equals("样例照片"))
                                                photoItemModel.setSamplePic(l3ele.getText());
                                            if (l3ele.getName().equals("实际照片")) {
                                                photoItemModel.setActualPic(l3ele.getText());
                                                photoItemModel.setActualPicSrc(l3ele.attributeValue("来源"));
                                            }
                                        }
                                    }
                                }
                                checkItemModel2.setPhotoGroup(photoGroupModel);
                            }
                        }
                        checkItem2List.add(checkItemModel2);
                    }

                    /*if(!userChecked && defaultOpt!=null){
                        defaultOpt.setChecked(true);
                    }*/
                }
            }

            return taskModel;
        }catch (DocumentException e) {
            Log.e(TAG, Log.getStackTraceString(e));
            throw new RuntimeException(e);
        }
    }


    public static void SaveTaskXML(String pkgPath,TaskModel taskModel,List<CheckItemModel> listProd){
        File xmlFile=new File(pkgPath+File.separator+"task.xml");
        if(!xmlFile.exists()){
            Log.e(TAG, "XML文件未找到："+xmlFile.getAbsolutePath());
            throw new RuntimeException("XML文件未找到："+xmlFile.getAbsolutePath());
        }

        SAXReader reader=new SAXReader();
        reader.setEncoding("UTF-8");
        Document xmlDoc= null;
        try {
            xmlDoc = reader.read(xmlFile);
            Element docEle=xmlDoc.getRootElement();
            for (int i = 0; i < listProd.size(); i++) {
                CheckItemModel item=listProd.get(i);
                /*List<OptionModel> optionList=item.getOptionList();
                for (int j = 0; j < optionList.size(); j++) {
                    OptionModel option=optionList.get(j);
                    String optionId=option.getId();
                    boolean optionChecked=option.isChecked();
                    UpdateOptionCheck(docEle,optionId,optionChecked);
                }*/
                UpdateIsComplete(docEle,item.getSeq(),item.getIsComplete());
                UpdateWorkRecord(docEle,item.getSeq(),item.getRecordList());
                UpdateWorker(docEle,item.getSeq(),item.getWoker(),item.getChecker());

                PhotoGroupModel photoGroupModel=item.getPhotoGroup();
                if(photoGroupModel!=null){
                    UpdatePhotoElement(docEle,photoGroupModel);
                }
            }

            Writer writer = new FileWriter(new File(pkgPath+File.separator+"task.xml"));
            OutputFormat format= OutputFormat.createPrettyPrint();//格式化
            XMLWriter xmlWriter = new XMLWriter(writer,format);
            xmlWriter.write(xmlDoc);
            xmlWriter.close();
        } catch (DocumentException e) {
            Log.e(TAG, Log.getStackTraceString(e));
            throw new RuntimeException(e);
        } catch (IOException e) {
            Log.e(TAG, Log.getStackTraceString(e));
            throw new RuntimeException(e);
        }
    }

    private static void UpdateWorkRecord(Element docEle, String seq, List<WorkRecordModel> recordList) {
        for (WorkRecordModel recordModel:recordList) {
            List<Object> objectList=recordModel.getInputEles();
            for (Object obj:objectList) {
                if(obj instanceof InputModel){
                    InputModel inputModel=(InputModel)obj;
                    String id=inputModel.getId();
                    String val=inputModel.getValue();
                    Node node=docEle.selectSingleNode("/数据模块/指令卡/任务执行[@序号='"+seq+"']/执行记录/填写项[@id='"+id+"']");
                    if(node!=null){
                        node.setText(val);
                    }
                }
            }
        }

    }

    private static void UpdateWorker(Element docEle, String seq, String woker, String checker) {
        Node node=docEle.selectSingleNode("/数据模块/指令卡/任务执行[@序号='"+seq+"']/工作者");
        if(node!=null){
            node.setText(woker);
        }
        node=docEle.selectSingleNode("/数据模块/指令卡/任务执行[@序号='"+seq+"']/复查者");
        if(node!=null){
            node.setText(checker);
        }
    }


    static void UpdatePhotoElement(Element docEle,PhotoGroupModel groupModel){
        List groupList=docEle.selectNodes("/数据模块/指令卡/任务执行/拍照项[@编号='"+groupModel.getId()+"']");
        if(groupList.size()>0){
            Element groupEle=(Element) groupList.get(0);
            Iterator<Element> iterator=groupEle.elementIterator();
            while (iterator.hasNext()){
                Element photoEle=iterator.next();
                String eleSeq=photoEle.attributeValue("序号");
                List<PhotoItemModel> photoList=groupModel.getItemList();
                for (PhotoItemModel photoModel: photoList) {
                    if(photoModel.getSeq()!=null && photoModel.getSeq().equals(eleSeq)){
                        Iterator<Element> childs=photoEle.elementIterator();
                        while (childs.hasNext()){
                            Element element=childs.next();
                            if(element.getName().equals("实际照片")){
                                element.setText(photoModel.getActualPic()==null?"":photoModel.getActualPic());
                                element.setAttributeValue("来源",photoModel.getActualPicSrc()==null?"":photoModel.getActualPicSrc());
                            }
                        }
                    }
                }
            }
        }

    }

    static void UpdateIsComplete(Element docEle,String seq,String val){
        List elementList=docEle.selectNodes("/数据模块/指令卡/任务执行[@序号='"+seq+"']/完成情况");
        if(elementList.size()>0){
            for (int i = 0; i < elementList.size(); i++) {
                Element element=(Element)elementList.get(i);
                element.setText(val);
                /*if(checked){
                    //element.setAttributeValue("checked","1");
                    element.setText("以");
                }else{
                    //element.setAttributeValue("checked","0");
                }*/
            }
        }
    }
    static String findNodeValue(Element root,String xpath){
        if(xpath!=null){
            try{
                List elementList=root.selectNodes(xpath);
                if(!elementList.isEmpty()){
                    Element element=(Element) elementList.get(0);
                    return  element.getText();
                }
            }catch (Exception e){
                Log.e(TAG, Log.getStackTraceString(e));
                throw new RuntimeException(e);
            }

        }
        return  null;
    }

}
