package com.huatek.busi.util;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import com.alibaba.fastjson.JSONArray;
import com.huatek.busi.entity.GisAssessmentResultData;
import com.huatek.busi.service.impl.dataoutput.helper.DataOutputUtil;
import com.huatek.busi.vo.GisEvaVo;

public class GisCreateUtil {

	/**
	 * 开断磨损
	 */
	public static String BREAKING_WEAR = "breakingWear";

	/**
	 * 运行数据
	 */
	public static String OPERATIONAL_DATA = "operationalData";

	/**
	 * 运行参数
	 */
	public static String OPERATION_PARAMETERS = "operationParameters";

	/**
	 * 运行参数-电气试验
	 */
	public static String OPERATION_PARAMETERS_DQSY = "operationParametersDQSY";

	public static String JYDZ = "jydz";
	public static String Dielectric_Loss = "dielectricLoss";
	public static String Electric_Capacity = "electricCapacity";
	public static String Galvanic_Circle = "galvanicCircle";
	public static String Closing_Coil_Resistance = "closingCoilResistance";
	public static String Opening_Coil_Resistance = "openingCoilResistance";

	/**
	 * 绝缘介质
	 */
	public static String INSULATING_MEDIUM = "insulatingMedium";

	/**
	 * DOM节点
	 */
	public static String EVALUATE_ELE = "evaluate";

	/**
	 * DOM节点
	 */
	public static String HEALTHY_ELE = "healthy";

	/**
	 * DOM节点
	 */
	public static String RESULT_ELE = "result";

	/**
	 * DOM节点
	 */
	public static String EVALUATETIME_ELE = "evaluatetime";

	/**
	 * DOM节点
	 */
	public static String GIS_ELE = "dev/gis";

	/**
	 * DOM节点属性
	 */
	public static String NAME_ATTR = "name";

	/**
	 * DOM节点属性
	 */
	public static String VALUE_ATTR = "value";

	/**
	 * DOM节点属性
	 */
	public static String DESC_ATTR = "desc";

	/**
	 * 使用年限
	 */
	public static String SERVICE_LIFE = "service_life";

	/**
	 * 相对磨损程度
	 */
	public static String RELATIVE_WEAR_DEGREE = "relative_wear_degree";

	/**
	 * 累计动作次数
	 */
	public static String CUMULATIVE_ACTION_TIMES = "cumulative_action_times";

	/**
	 * 100%负荷的断开次数
	 */
	public static String CUMULATIVE_ACTION_TIMES_VALUE = "cumulative_action_times_value";

	/**
	 * 触头累计磨损率(7天)
	 */
	public static String RELATIVE_WEAR_DEGREE_VALUE = "relative_wear_degree_value";

	/**
	 * 时间参数
	 */
	public static String TIME_PARAMETER ="time_parameter";

	/**
	 * 速度特性
	 */
	public static String VELOCITY_CHARACTERISTICS ="Velocity_characteristics";

	/**
	 * 导电回路电阻
	 */
	public static String CONDUCTING_CIRCUIT_RESISTANCE ="Conducting_circuit_resistance";

	/**
	 * 气体压力
	 */
	public static String GAS_PRESSURE ="gas_pressure";

	/**
	 * 微水含量
	 */
	public static String MICRO_WATER_CONTENT ="micro_water_content";

	/**
	 * 局放量
	 */
	public static String PARTIAL_DISCHARGE="partial_discharge";

	/**
	 * 局放次数
	 */
	public static String PARTIAL_DISCHARGE_COUNT ="partial_discharge_count";

	/**
	 * 局放次数
	 */
	public static String JYJZ_WS_MIEHUSHI ="jyjz_ws_miehushi";

	/**
	 * 工作环境
	 */
	public static String WORK_ENVIRONMENT ="work_environment";

	/**
	 * 外观情况
	 */
	public static String APPEARANCE = "appearance";

	/**
	 * 维修记录
	 */
	public static String  MAINTENANCE_RECORD="maintenance_record";

	/**
	 * 生产厂家
	 */
	public static String Manufacturer = "manufacturer";

	/**
	 * 报告路径
	 */
	public static final String FILE_PATH = "dev/gis";

	/**
	 * path
	 */
	public static final String PATH = "/script/formula_gis.js";

	/**
	 * 804
	 */
	public static final String GIS_804 = "5eab181e80bc4a8f82b904cab4c9a963";

	/**
	 * 局放类型:cac类型编码
	 */
	public static final String PD_TYPE = "024001";

	public static void createFile(GisAssessmentResultData enetity, boolean flag,
			String basePath, String filePath, String fileName,String date) {
		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(GIS_ELE);
				transformer.setText(enetity.getGisName());
				Element evaluatetime = evaluate.addElement("evaluatetime");
				evaluatetime.setText(date);

				Element healthy = evaluate.addElement("healthy");

				Element index1 = healthy.addElement("atrrs");
				index1.addAttribute("name", BREAKING_WEAR);
				index1.addAttribute("value", enetity.getBreakingWear());
				index1.addAttribute("desc", "开断磨损");
				List<GisEvaVo> list1 = JSONArray.parseArray(enetity.getBreakingWearDetails(), GisEvaVo.class);
				List<GisEvaVo> tmpList = new ArrayList<GisEvaVo>();
				tmpList.add(list1.get(1));
				tmpList.add(list1.get(2));
				createAttr(index1,tmpList);
				
				Element index2 = healthy.addElement("atrrs");
				index2.addAttribute("name", OPERATIONAL_DATA);
				index2.addAttribute("value", enetity.getOperationalData());
				index2.addAttribute("desc", "运行数据");
				List<GisEvaVo> list2 = JSONArray.parseArray(enetity.getOperationalDataDetails(), GisEvaVo.class);
				List<GisEvaVo> tmpList2 = new ArrayList<GisEvaVo>();
				tmpList2.add(list2.get(0));
				tmpList2.add(list2.get(1));
				createAttr(index2,list2);
				
				Element index3= healthy.addElement("atrrs");
				index3.addAttribute("name", INSULATING_MEDIUM);
				index3.addAttribute("value", enetity.getInsulatingMedium());
				index3.addAttribute("desc", "绝缘介质");
				List<GisEvaVo> list3 = JSONArray.parseArray(enetity.getInsulatingMediumDetails(), GisEvaVo.class);
				createAttr(index3,list3);
				
				Element index4 = healthy.addElement("atrrs");
				index4.addAttribute("name", OPERATION_PARAMETERS);
				index4.addAttribute("value", enetity.getOperationParameters());
				index4.addAttribute("desc", "运行参数");
				List<GisEvaVo> list4 = JSONArray.parseArray(enetity.getOperationParametersDetails(), GisEvaVo.class);
				createAttr(index4,list4);

				Element result = evaluate.addElement("result");
				createAttr(result, "future_failure_rate", enetity.getFutureFailureRate()==null?"": enetity.getFutureFailureRate(), "开关恶化程度");
				createAttr(result, "comprehensive_assessment", enetity.getHealthLevel(), "开关综合评估");
				createAttr(result, "remaining_disconnects", enetity.getRemainingUsageCount(), "剩余断开次数");
				createAttr(result, "expected_number_of_disconnects", enetity.getExpectedUsageCount(), "预期断开次数");
				
			}
			// 设置生成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);
	}
	
	
	public static void createAttr(Element parenet, List<GisEvaVo> list) {
		list.forEach(vo ->{
			Element attr = parenet.addElement("attr");
			attr.addAttribute("name", vo.getName());
			attr.addAttribute("value", vo.getValue());
			attr.addAttribute("desc", vo.getDesc());
		});
	}
}
