package com.huatek.busi.task.transformer;

import java.nio.file.Files;
import java.nio.file.Path;
import java.text.DecimalFormat;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import com.huatek.busi.entity.TransformerEvaluationIntermediateValue;
import com.huatek.busi.service.impl.dataoutput.helper.DataOutputUtil;

public class TransfromerCreateUtil {

	public static void createFile(TransformerEvaluationIntermediateValue enetity, boolean flag,
			String basePath, String filePath, String fileName,String date) {
		DecimalFormat df = new DecimalFormat("0.00%"); 
		
		try {
			 Path path = DataOutputUtil.createFile(basePath, filePath, fileName);
			// 创建document对象
			Document document = DocumentHelper.createDocument();
			// 创建根节点evaluate
			Element evaluate = document.addElement("evaluate");
			// 节点添加属性
			
			if(flag){
				evaluate.addAttribute("name", "result");
				evaluate.addAttribute("value", "true");
				evaluate.addAttribute("desc", "评测成功");

				Element transformer = evaluate.addElement("transformer");
				transformer.setText(enetity.getTransformerName());
				Element evaluatetime = evaluate.addElement("evaluatetime");
				evaluatetime.setText(date);

				Element healthy = evaluate.addElement("healthy");
				healthy.addAttribute("name", "finalIndex");
				healthy.addAttribute("value", enetity.getFinalHealthLevelIndex());
				healthy.addAttribute("desc", "最终健康水平指数");

				Element atrrIndex1 = healthy.addElement("atrr");
				atrrIndex1.addAttribute("name", "mainInsulationIndex");
				atrrIndex1.addAttribute("value", enetity.getAgingHealthStatusOfMainInsulation());
				atrrIndex1.addAttribute("desc", "主绝缘老化健康状态");

				Element atrrIndex2 = healthy.addElement("atrrs");
				atrrIndex2.addAttribute("name", "runInsulationIndex");
				atrrIndex2.addAttribute("value", enetity.getInsulationOperationHealthIndex());
				atrrIndex2.addAttribute("desc", "绝缘运行状态健康指数");

				createAttr(atrrIndex2, "oilChromatogramTest", enetity.getOilChromatogramTest(), "油色谱试验");
				createAttr(atrrIndex2, "furfural", enetity.getFurfural(), "糠醛");
				createAttr(atrrIndex2, "oilQualityTest", enetity.getOilQualityTest(), "油质试验");
				createAttr(atrrIndex2, "insulatingPaperTest", enetity.getInsulatingPaper(), "绝缘纸试验");
				createAttr(atrrIndex2, "routineTest",enetity.getRoutineTest(), "例行试验");
				createAttr(atrrIndex2, "diagnosticTest", enetity.getDiagnosticTest(), "诊断性试验");

				Element atrrIndex3 = healthy.addElement("atrr");
				atrrIndex3.addAttribute("name", "componentDefectIndex");
				atrrIndex3.addAttribute("value", enetity.getComponentDefectRiskHealthIndex());
				atrrIndex3.addAttribute("desc", "部件缺陷风险健康指数");

				Element result = evaluate.addElement("result");
				createAttr(result, "designLife", enetity.getDesignLife(), "设计寿命(年)");
				createAttr(result, "surplusLife", enetity.getSurplusLife(), "变压器预期寿命(年)");
				createAttr(result, "score", enetity.getScore(), "健康评分");
				
				if(StringUtils.isNotBlank(enetity.getFutureFailureRate())){
					createAttr(result, "agingRate",  enetity.getAgingRate(), "变压器老化程度");
//					createAttr(result, "agingRate",  df.format(Double.valueOf(enetity.getAgingRate())), "变压器老化程度(%)");
				}else{
					createAttr(result, "agingRate",  "", "变压器老化程度");
				}
				createAttr(result, "advice", enetity.getAdvice(), "检修策略");
				createAttr(result, "healthIndex", enetity.getHealthIndex(), "变压器健康评估状态");
				if(StringUtils.isNotBlank(enetity.getFutureFailureRate())){
					createAttr(result, "futureFailureRate", df.format(Double.valueOf(enetity.getFutureFailureRate())), "今后故障发生概率(%)");
				}else{
					createAttr(result, "futureFailureRate", "", "今后故障发生概率(%)");
				}
				
			}else{
				evaluate.addAttribute("name", "result");
				evaluate.addAttribute("value", "false");
				evaluate.addAttribute("desc", enetity.getFailureRreasons());

				Element transformer = evaluate.addElement("transformer");
				transformer.setText(enetity.getTransformerName());
				Element evaluatetime = evaluate.addElement("evaluatetime");
				evaluatetime.setText(date);

				Element healthy = evaluate.addElement("healthy");
				healthy.addAttribute("name", "finalIndex");
				healthy.addAttribute("value", "");
				healthy.addAttribute("desc", "最终健康水平指数");

				Element atrrIndex1 = healthy.addElement("atrr");
				atrrIndex1.addAttribute("name", "mainInsulationIndex");
				atrrIndex1.addAttribute("value", "");
				atrrIndex1.addAttribute("desc", "主绝缘老化健康状态");

				Element atrrIndex2 = healthy.addElement("atrrs");
				atrrIndex2.addAttribute("name", "runInsulationIndex");
				atrrIndex2.addAttribute("value", "");
				atrrIndex2.addAttribute("desc", "绝缘运行状态健康指数");

				createAttr(atrrIndex2, "oilChromatogramTest", "", "油色谱试验");
				createAttr(atrrIndex2, "furfural", "", "糠醛");
				createAttr(atrrIndex2, "oilQualityTest", "", "油质试验");
				createAttr(atrrIndex2, "insulatingPaperTest", "", "绝缘纸试验");
				createAttr(atrrIndex2, "routineTest", "", "例行试验");
				createAttr(atrrIndex2, "diagnosticTest", "", "诊断性试验");

				Element atrrIndex3 = healthy.addElement("atrr");
				atrrIndex3.addAttribute("name", "componentDefectIndex");
				atrrIndex3.addAttribute("value", "");
				atrrIndex3.addAttribute("desc", "部件缺陷风险健康指数");

				Element result = evaluate.addElement("result");
				createAttr(result, "designLife", "", "设计寿命(年)");
				createAttr(result, "surplusLife", "", "变压器预期寿命(年)");
				createAttr(result, "score", "", "健康评分");
				createAttr(result, "agingRate", "", "变压器老化程度");
				createAttr(result, "advice", "", "检修策略");
				createAttr(result, "healthIndex", "", "变压器健康评估状态");
				createAttr(result, "futureFailureRate", "", "今后故障发生概率(%)");
			}
			// 设置生成xml的格式
			OutputFormat format = OutputFormat.createPrettyPrint();
			// 设置编码格式
			format.setEncoding("UTF-8");
			format.setNewLineAfterDeclaration(false);//解决声明下空行问题  
			format.setEncoding("UTF-8");  
			format.setIndent(true);    // 设置是否缩进 
			format.setIndent("   ");   // 以空格方式实现缩进 
			format.setNewlines(true);  
			format.setTrimText(true);
			
			// 生成xml文件
			//File file = new File("d:\xmlDemo.xml");
			XMLWriter writer = new XMLWriter(Files.newOutputStream(path), format);
			// 设置是否转义，默认使用转义字符
			writer.setEscapeText(false);
			writer.write(document);
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void createAttr(Element parenet, String name, String value, String desc) {
		Element attr = parenet.addElement("attr");
		attr.addAttribute("name", name);
		attr.addAttribute("value", value);
		attr.addAttribute("desc", desc);
	}
	
	
}
