package com.gyee.alarm.task.thread;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.alarm.init.CacheContext;
import com.gyee.alarm.model.auto.*;
import com.gyee.alarm.service.auto.IProEconInterruptionService;
import com.gyee.alarm.service.auto.IProEconShutdownEvent2Service;
import com.gyee.alarm.service.auto.IProEconShutdownEventService;
import com.gyee.alarm.service.auto.IProEconWindturbineStatusService;
import com.gyee.alarm.util.DateUtils;
import com.gyee.alarm.util.StringUtils;
import com.gyee.alarm.util.realtimesource.IEdosUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.*;
import java.util.concurrent.Callable;

public class ShutdownnewThreadPool implements Callable<String>, Serializable {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	private static final long serialVersionUID = 0;

	// 保存任务所需要的数据

	private Object threadPoolTaskData;
	private Map<String, ProEconWindturbineStatus> oldStatusMap;
	private  Map<String, List<PointData>> statusMap;

	private  String windturbineId;
	private Map<Integer, String> statusDescriptionMap;

	private Map<String, ProEconShutdownEvent> oldShutdownEvent;
	private Map<String, ProEconInterruption> oldInterrup;
	private Map<String, ProEconShutdownEvent2> oldShutdownEvent2;
	private  Date begin;
	private  Date end;

	private IEdosUtil edosUtil;
	private IProEconWindturbineStatusService proEconWindturbineStatusService;
	private IProEconShutdownEventService proEconShutdownEventService;
	private IProEconShutdownEvent2Service proEconShutdownEvent2Service;
	private IProEconInterruptionService proEconInterruptionService;

	public ShutdownnewThreadPool(Object tasks,Map<String, ProEconWindturbineStatus> oldStatusMap,Map<String, List<PointData>> statusMap,String windturbineId,
								   Map<Integer, String> statusDescriptionMap,Map<String, ProEconShutdownEvent> oldShutdownEvent,
								   Map<String, ProEconInterruption> oldInterrup,
								 Map<String, ProEconShutdownEvent2> oldShutdownEvent2, Date begin, Date end ,
								 IEdosUtil edosUtil,IProEconWindturbineStatusService proEconWindturbineStatusService,
								 IProEconShutdownEventService proEconShutdownEventService,IProEconShutdownEvent2Service proEconShutdownEvent2Service,
								 IProEconInterruptionService proEconInterruptionService
	) {

		this.threadPoolTaskData = tasks;
		this.oldStatusMap = oldStatusMap;
		this.statusMap = statusMap;
		this.windturbineId = windturbineId;
		this.statusDescriptionMap = statusDescriptionMap;
		this.oldShutdownEvent = oldShutdownEvent;
		this.oldInterrup = oldInterrup;
		this.oldShutdownEvent2 = oldShutdownEvent2;
		this.begin = begin;
		this.end = end;

		this.edosUtil = edosUtil;
		this.proEconWindturbineStatusService = proEconWindturbineStatusService;
		this.proEconShutdownEventService = proEconShutdownEventService;
		this.proEconShutdownEvent2Service = proEconShutdownEvent2Service;
		this.proEconInterruptionService = proEconInterruptionService;
	}

	public synchronized String call() throws Exception {


		// 处理一个任务，这里的处理方式太简单了，仅仅是一个打印语句

		//System.out.println("开始执行任务：" + threadPoolTaskData);

		String result = "";

		// //便于观察，等待一段时间



		try {


			Map<String, ProBasicEquipmentPoint> windturbinetestingpointnewMap = CacheContext.wtpAimap.get(windturbineId);

			//取实时状态数据 赋值给status

			if(windturbinetestingpointnewMap.containsKey(ContantXk.MXZT))
			{
				ProBasicEquipmentPoint mxztPoint = windturbinetestingpointnewMap.get(ContantXk.MXZT);
				//获取5分钟没个间隔1秒钟，保证状态与时序库保持一致
				List<PointData> statusls = edosUtil.getHistoryDatasSnap(mxztPoint.getNemCode(), begin.getTime() / 1000, end.getTime() / 1000,null,1L);

				statusMap.put(windturbineId, statusls);
			}

			ProEconWindturbineStatus item = oldStatusMap.get(windturbineId);
			//获取5分钟的状态值
			List<PointData> statusls=statusMap.get(windturbineId);

			ProEconShutdownEvent shutdwonEvent=null;
			ProEconInterruption interrup=null;
			ProEconShutdownEvent2 shutdwonEvent2=null;
			//记录上次判定的状态值
			int lastStatus =(int)statusls.get(0).getPointValueInDouble();

			List<ProEconShutdownEvent> shutdwonEventls=new ArrayList<>();
			List<ProEconInterruption> interrupls=new ArrayList<>();
			List<ProEconShutdownEvent2> shutdwonEvent2ls=new ArrayList<>();
			List<ProEconWindturbineStatus> windturbineStatusls=new ArrayList<>();

			for(PointData stpo:statusls)
			{
				//获取当前状态
				Integer currentStatus = (int)stpo.getPointValueInDouble();
				//获取当前时间
				Date currentTime=new Date(stpo.getPointTime());
				//如果数据库中不存在该风机未完成的故障记录，进入
				if (item == null)
				{

					item = new ProEconWindturbineStatus();

					initalWindturbineStatus(windturbineId, item, currentStatus, currentTime,stpo);

					if (currentStatus == 4 || currentStatus == 6)
					{
						shutdwonEvent=new ProEconShutdownEvent();
						initalShutdownEvent(windturbineId, item, currentStatus, currentTime, shutdwonEvent);


						shutdwonEvent2=new ProEconShutdownEvent2();
						initalShutdownEvent2(windturbineId, item, currentStatus, currentTime, shutdwonEvent2);


						item.setShutdownEventId(shutdwonEvent.getId());
					}
					if (currentStatus == 12)
					{
						interrup=new ProEconInterruption();
						initalInterruption(windturbineId, item, currentTime, interrup);
					}

					//将状态保存到MAP对象中
					oldStatusMap.put(item.getWindturbineId(),item);


					if (shutdwonEvent != null )
					{
						oldShutdownEvent.put(item.getWindturbineId(),shutdwonEvent);

					}
					if (interrup != null)
					{
						oldInterrup.put(item.getWindturbineId(),interrup);

					}
					if (shutdwonEvent2 != null)
					{
						oldShutdownEvent2.put(item.getWindturbineId(), shutdwonEvent2);
					}
					//将当前状态赋值给过去状态
					lastStatus=currentStatus;
				}else  if(lastStatus!=currentStatus)
				{

					//如果添加了状态值，状态值变化存储，并清空
					if(oldStatusMap.containsKey(windturbineId))
					{

						//获取数据库中的状态值，状态值变化存储，并清空
						item=oldStatusMap.get(windturbineId);
						if(item==null)
						{
							item=new ProEconWindturbineStatus();
							initalWindturbineStatus(windturbineId, item, currentStatus, currentTime,stpo);
							oldStatusMap.put(windturbineId,item);
						}else
						{
							finishWindturbineStatus(windturbineStatusls,windturbineId, item, currentStatus, currentTime);
							item =null;
							oldStatusMap.put(windturbineId,null);
						}



					}


					if(oldShutdownEvent.containsKey(windturbineId))
					{
						shutdwonEvent=oldShutdownEvent.get(windturbineId);

						if(shutdwonEvent==null)
						{
							shutdwonEvent=new ProEconShutdownEvent();
							initalShutdownEvent(windturbineId, item, currentStatus, currentTime, shutdwonEvent);
							oldShutdownEvent.put(windturbineId,shutdwonEvent);
						}else
						{
							shutdwonEvent = finishShutdownEvent(shutdwonEventls,windturbineId, shutdwonEvent, currentStatus, currentTime);
							oldShutdownEvent.put(windturbineId,null);
						}


					}
					if(oldInterrup.containsKey(windturbineId))
					{
						interrup=oldInterrup.get(windturbineId);
						if(interrup==null)
						{
							interrup=new ProEconInterruption();
							initalInterruption(windturbineId, item, currentTime, interrup);
							oldInterrup.put(windturbineId,interrup);
						}else
						{
							interrup = finishInterruption(interrupls,windturbineId, interrup, currentStatus, currentTime);
							oldInterrup.put(windturbineId,null);
						}


					}

					if(oldShutdownEvent2.containsKey(windturbineId))
					{
						shutdwonEvent2=oldShutdownEvent2.get(windturbineId);
						if(shutdwonEvent2==null)
						{
							shutdwonEvent2=new ProEconShutdownEvent2();
							initalShutdownEvent2(windturbineId, item, currentStatus, currentTime, shutdwonEvent2);
							oldShutdownEvent2.put(windturbineId,shutdwonEvent2);
						}else
						{
							shutdwonEvent2 = finishShutdownEvent2(shutdwonEvent2ls,windturbineId, shutdwonEvent2, currentStatus, currentTime);
							oldShutdownEvent2.put(windturbineId,null);
						}

					}

					lastStatus=currentStatus;
				}



			}
			//循环结束落盘，并清空
			if(oldStatusMap.containsKey(windturbineId))
			{
				//获取数据库查询是否有风机对应时间的记录，如果没有写入数据库
				item=oldStatusMap.get(windturbineId);
				if(item!=null)
				{
					ProEconWindturbineStatus finalItem = item;
					QueryWrapper<ProEconWindturbineStatus> queryWrapper9 = new QueryWrapper<>();
					queryWrapper9.eq("stop_Time",finalItem.getStopTime()).eq("windturbine_id",finalItem.getWindturbineId());
					List<ProEconWindturbineStatus> ls= proEconWindturbineStatusService.list(queryWrapper9);
//                            .stream().filter(i->i.getWindturbineId().equals(finalItem.getWindturbineId())
//                                    && i.getStopTime().compareTo(finalItem.getStopTime())==0).collect(Collectors.toList());

					if(ls.isEmpty())
					{
						//proEconWindturbineStatusService.save(item);

						windturbineStatusls.add(item);

						oldStatusMap.put(windturbineId,null);
					}
				}


			}


			if(oldShutdownEvent.containsKey(windturbineId))
			{

				shutdwonEvent=oldShutdownEvent.get(windturbineId);

				if(shutdwonEvent!=null)
				{
					ProEconShutdownEvent finalShutdwonEvent = shutdwonEvent;
					QueryWrapper<ProEconShutdownEvent> queryWrapper5 = new QueryWrapper<>();
					queryWrapper5.eq("stop_Time",finalShutdwonEvent.getStopTime()).eq("windturbine_id",finalShutdwonEvent.getWindturbineId());
					List<ProEconShutdownEvent> ls= proEconShutdownEventService.list(queryWrapper5);
//                            .stream().filter(i->i.getWindturbineId().equals(finalShutdwonEvent.getWindturbineId())
//                                    && i.getStopTime().compareTo(finalShutdwonEvent.getStopTime())==0)
//                            .collect(Collectors.toList());

					if(ls.isEmpty())
					{
						//proEconShutdownEventService.save(shutdwonEvent);
						shutdwonEventls.add(shutdwonEvent);
						oldShutdownEvent.put(windturbineId,null);
					}
				}

			}
			if(oldInterrup.containsKey(windturbineId))
			{

				interrup=oldInterrup.get(windturbineId);

				if(interrup!=null)
				{
					ProEconInterruption finalInterrup = interrup;
					QueryWrapper<ProEconInterruption> queryWrapper5 = new QueryWrapper<>();
					queryWrapper5.eq("stop_Time",finalInterrup.getStopTime()).eq("windturbine_id",finalInterrup.getWindturbineId());
					List<ProEconInterruption> ls= proEconInterruptionService.list(queryWrapper5);
//                            .stream().filter(i->i.getWindturbineId().equals(finalInterrup.getWindturbineId())
//                                    && i.getStopTime().compareTo(finalInterrup.getStopTime())==0).collect(Collectors.toList());

					if(ls.isEmpty())
					{
						//proEconInterruptionService.save(interrup);
						interrupls.add(interrup);
						oldInterrup.put(windturbineId,null);
					}
				}


			}

			if(oldShutdownEvent2.containsKey(windturbineId))
			{
				shutdwonEvent2=oldShutdownEvent2.get(windturbineId);

				if(shutdwonEvent2!=null)
				{
					ProEconShutdownEvent2 finalShutdwonEvent1 = shutdwonEvent2;
					QueryWrapper<ProEconShutdownEvent2> queryWrapper5 = new QueryWrapper<>();
					queryWrapper5.eq("stop_Time",finalShutdwonEvent1.getStopTime()).eq("windturbine_id",finalShutdwonEvent1.getWindturbineId());
					List<ProEconShutdownEvent2> ls= proEconShutdownEvent2Service.list(queryWrapper5);
//                            .stream().filter(i->i.getWindturbineId().equals(finalShutdwonEvent1.getWindturbineId())
//                                    && i.getStopTime().compareTo(finalShutdwonEvent1.getStopTime())==0).collect(Collectors.toList());

					if(ls.isEmpty())
					{
						//proEconShutdownEvent2Service.save(shutdwonEvent2);
						shutdwonEvent2ls.add(shutdwonEvent2);
						oldShutdownEvent2.put(windturbineId,null);
					}
				}

			}

			if(!windturbineStatusls.isEmpty())
			{
				List<ProEconWindturbineStatus> templs=new ArrayList<>();
				for(ProEconWindturbineStatus vo:windturbineStatusls)
				{
					templs.add(vo);
					if(templs.size()==100)
					{
						proEconWindturbineStatusService.saveOrUpdateBatch(templs);
						templs=new ArrayList<>();
					}
				}

				if(!templs.isEmpty())
				{
					proEconWindturbineStatusService.saveOrUpdateBatch(templs);
				}
			}
			if(!shutdwonEventls.isEmpty())
			{

				List<ProEconShutdownEvent> templs=new ArrayList<>();
				for(ProEconShutdownEvent vo:shutdwonEventls)
				{
					templs.add(vo);
					if(templs.size()==100)
					{
						proEconShutdownEventService.saveOrUpdateBatch(templs);
						templs=new ArrayList<>();
					}
				}

				if(!templs.isEmpty())
				{
					proEconShutdownEventService.saveOrUpdateBatch(templs);
				}

			}
			if(!shutdwonEvent2ls.isEmpty())
			{

				List<ProEconShutdownEvent2> templs=new ArrayList<>();
				for(ProEconShutdownEvent2 vo:shutdwonEvent2ls)
				{
					templs.add(vo);
					if(templs.size()==100)
					{
						proEconShutdownEvent2Service.saveOrUpdateBatch(templs);
						templs=new ArrayList<>();
					}
				}

				if(!templs.isEmpty())
				{
					proEconShutdownEvent2Service.saveOrUpdateBatch(templs);
				}

			}
			if(!interrupls.isEmpty())
			{

				List<ProEconInterruption> templs=new ArrayList<>();
				for(ProEconInterruption vo:interrupls)
				{
					templs.add(vo);
					if(templs.size()==100)
					{
						proEconInterruptionService.saveOrUpdateBatch(templs);
						templs=new ArrayList<>();
					}
				}

				if(!templs.isEmpty())
				{
					proEconInterruptionService.saveOrUpdateBatch(templs);
				}

			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		threadPoolTaskData = null;

		return result;

	}



	private ProEconShutdownEvent finishShutdownEvent(List<ProEconShutdownEvent> shutdwonEventls,String windturbineId, ProEconShutdownEvent shutdwonEvent, Integer currentStatus, Date currentTime) throws Exception {
//        if(currentStatus != 4 && currentStatus != 6)
//        {
		shutdwonEvent.setStartTime(currentTime);

		double timehour=DateUtils.hoursDiff2(shutdwonEvent.getStartTime(), shutdwonEvent.getStopTime());
		shutdwonEvent.setStopHours(timehour);
		double losspower=calLossPower(windturbineId, shutdwonEvent.getStartTime(), shutdwonEvent.getStopTime());
		shutdwonEvent.setLossPower(losspower);

		shutdwonEventls.add(shutdwonEvent);
//		proEconShutdownEventService.saveOrUpdate(shutdwonEvent);

		oldShutdownEvent.put(windturbineId,null);
		shutdwonEvent =null;
//        }
		return shutdwonEvent;
	}

	private ProEconInterruption finishInterruption(List<ProEconInterruption> interrupls,String windturbineId, ProEconInterruption interrup, Integer currentStatus, Date currentTime) {
//        if(currentStatus != 12)
//        {
		interrup.setStartTime(currentTime);
		double timehour=DateUtils.hoursDiff2(interrup.getStartTime(), interrup.getStopTime());
		interrup.setStopHours(timehour);

		interrupls.add(interrup);
		//proEconInterruptionService.saveOrUpdate(interrup);
		oldInterrup.put(windturbineId,null);
		interrup =null;
//        }
		return interrup;
	}

	private ProEconShutdownEvent2 finishShutdownEvent2(List<ProEconShutdownEvent2> shutdwonEvent2ls,String windturbineId, ProEconShutdownEvent2 shutdwonEvent2, Integer currentStatus, Date currentTime) throws Exception {
//        if(currentStatus != 4 && currentStatus != 6)
//        {
		shutdwonEvent2.setStartTime(currentTime);

		double timehour=DateUtils.hoursDiff2(shutdwonEvent2.getStartTime(), shutdwonEvent2.getStopTime());
		shutdwonEvent2.setStopHours(timehour);
		double losspower=calLossPower(windturbineId, shutdwonEvent2.getStartTime(), shutdwonEvent2.getStopTime());
		shutdwonEvent2.setLossPower(losspower);

		if(timehour<=24)
		{
			//proEconShutdownEvent2Service.saveOrUpdate(shutdwonEvent2);
			shutdwonEvent2ls.add(shutdwonEvent2);
		}else
		{
			int days=DateUtils.daysDiff1(shutdwonEvent2.getStartTime(), shutdwonEvent2.getStopTime());
			Calendar c=Calendar.getInstance();
			Date startTime=shutdwonEvent2.getStartTime();
			c.setTime(shutdwonEvent2.getStopTime());
			c.set(Calendar.HOUR_OF_DAY,23);
			c.set(Calendar.MINUTE,59);
			c.set(Calendar.SECOND,59);
			for(int i=0;i<days;i++)
			{
				if(i==0)
				{
					shutdwonEvent2.setStartTime(c.getTime());
					losspower=calLossPower(windturbineId, shutdwonEvent2.getStartTime(), shutdwonEvent2.getStopTime());

				}
				else {
					shutdwonEvent2.setStopTime(DateUtils.truncate(c.getTime()));
					if(i==days-1)
					{
						shutdwonEvent2.setStartTime(startTime);

					}else
					{
						shutdwonEvent2.setStartTime(c.getTime());

					}
					losspower=calLossPower(windturbineId, shutdwonEvent2.getStartTime(), shutdwonEvent2.getStopTime());
				}
				shutdwonEvent2.setLossPower(losspower);
//				proEconShutdownEvent2Service.saveOrUpdate(shutdwonEvent2);
				shutdwonEvent2ls.add(shutdwonEvent2);
				c.add(Calendar.DAY_OF_MONTH,1);
			}
		}


		shutdwonEvent2 =null;
		oldShutdownEvent2.put(windturbineId,null);
//        }
		return shutdwonEvent2;
	}

	private void finishWindturbineStatus(List<ProEconWindturbineStatus> windturbineStatusls,String windturbineId, ProEconWindturbineStatus item, Integer currentStatus, Date currentTime) throws Exception {
		if(!Objects.equals(item.getSatusCode(), currentStatus))
		{
			item.setStartTime(currentTime);
			double timehour=DateUtils.hoursDiff2(item.getStartTime(), item.getStopTime());
			item.setStopHours(timehour);
			double losspower=calLossPower(windturbineId, item.getStartTime(), item.getStopTime());
			item.setLossPower(losspower);
			windturbineStatusls.add(item);
			//proEconWindturbineStatusService.saveOrUpdate(item);

		}
	}

	private static void initalInterruption(String windturbineId, ProEconWindturbineStatus item, Date stopTime, ProEconInterruption interrup) {
		interrup.setProjectId(CacheContext.wtmap.get(windturbineId).getProjectId());
		interrup.setStopTime(stopTime);
		interrup.setWindpowerstationId(CacheContext.wtmap.get(windturbineId).getWindpowerstationId());
		interrup.setWindturbineId(windturbineId);
	}

	private static void initalShutdownEvent2(String windturbineId, ProEconWindturbineStatus item, Integer currentStatus, Date stopTime, ProEconShutdownEvent2 shutdwonEvent2) {
		shutdwonEvent2.setId(StringUtils.getUUID());
		shutdwonEvent2.setProjectId(CacheContext.wtmap.get(windturbineId).getProjectId());
		shutdwonEvent2.setStatusCode(currentStatus);
		shutdwonEvent2.setStopTime(stopTime);
		shutdwonEvent2.setWinpowerstationId(CacheContext.wtmap.get(windturbineId).getWindpowerstationId());
		shutdwonEvent2.setWindturbineId(windturbineId);
		if (currentStatus == 4)
		{

			shutdwonEvent2.setStopTypeId("gzbmq");
			//读取报警记录表前后五分钟数据，选取第一个报警报警类型

			// String stopTypeId =newMethod( windturbineId, begin, end);
			String stopTypeId=null;
			if (StringUtils.notEmp(stopTypeId))
			{
				shutdwonEvent2.setStopTypeId(stopTypeId);

			}
		}
		else
		{
			shutdwonEvent2.setStopTypeId("wh");

		}
	}

	private static void initalShutdownEvent(String windturbineId, ProEconWindturbineStatus item, Integer currentStatus, Date stopTime, ProEconShutdownEvent shutdwonEvent) {

		shutdwonEvent.setId(StringUtils.getUUID());
		shutdwonEvent.setProjectId(CacheContext.wtmap.get(windturbineId).getProjectId());
		shutdwonEvent.setStatusCode(currentStatus);
		shutdwonEvent.setStopTime(stopTime);
		shutdwonEvent.setWindpowerstationId(CacheContext.wtmap.get(windturbineId).getWindpowerstationId());
		shutdwonEvent.setWindturbineId(windturbineId);
		if (currentStatus == 4)
		{

			shutdwonEvent.setStoptypeId("gzbmq");

			//读取报警记录表前后五分钟数据，选取第一个报警报警类型

			// String stopTypeId =newMethod( windturbineId, begin, end);
			String stopTypeId=null;
			if (StringUtils.notEmp(stopTypeId))
			{
				shutdwonEvent.setStoptypeId(stopTypeId);

			}
		}
		else
		{
			shutdwonEvent.setStoptypeId("wh");

		}
	}

	private void initalWindturbineStatus(String windturbineId, ProEconWindturbineStatus item, Integer currentStatus, Date stopTime,PointData stpo) {
		item.setProjectId(CacheContext.wtmap.get(windturbineId).getProjectId());
		item.setSatusCode(currentStatus);
		item.setStatusDesc(statusDescriptionMap.get((int)stpo.getPointValueInDouble()));
		item.setStopTime(stopTime);
		item.setWindpowerstationId(CacheContext.wtmap.get(windturbineId).getWindpowerstationId());
		item.setWindturbineId(windturbineId);
	}


	private  String newMethod( String windturbineId, Date begin, Date end)
	{
		String result = "";

		return result;
	}


	private Double calLossPower(String windturbineId, Date startDate, Date endDate) throws Exception {
		double result = 0.0;

		Map<String, Map<String, ProBasicEquipmentPoint>> wtpAimap = CacheContext.wtpAimap;
		Map<String, ProBasicEquipmentPoint> wtpointmap=wtpAimap.get(windturbineId);

		Calendar c1=Calendar.getInstance();
		c1.setTime(startDate);
		Calendar c2=Calendar.getInstance();
		c2.setTime(endDate);
		//判定限电是否跨年
		if(endDate.after(startDate) && c1.get(Calendar.YEAR)==c2.get(Calendar.YEAR))
		{
			//年故障损失电量
			if(wtpointmap.containsKey(ContantXk.NGZSSDL) )
			{
				double beginvalue;
				double endvalue;

				List<String> pointid=new ArrayList<>();
				pointid.add(wtpointmap.get(ContantXk.NGZSSDL).getNemCode());
				List<PointData> xdbegin=edosUtil.getHistMatrix(pointid,startDate.getTime()/1000);
				List<PointData> xdend=edosUtil.getHistMatrix(pointid,endDate.getTime()/1000);


				if(xdbegin.size()==1 && xdend.size()==1)
				{
					beginvalue=xdbegin.get(0).getPointValueInDouble();
					endvalue=xdend.get(0).getPointValueInDouble();

					if (endvalue > beginvalue) {
						result =result+(endvalue - beginvalue);
					}
				}
			}


		}else
		{
			Calendar c=Calendar.getInstance();
			c.setTime(DateUtils.truncate(startDate));
			c.add(Calendar.DAY_OF_MONTH,1);
			//获得1月1日零点值
			Date endtime=c.getTime();
			//年故障损失电量
			if(wtpointmap.containsKey(ContantXk.NGZSSDL) )
			{
				double beginvalue;
				double endvalue;



				List<String> pointid=new ArrayList<>();
				pointid.add(wtpointmap.get(ContantXk.NGZSSDL).getNemCode());
				List<PointData> xdbegin=edosUtil.getHistMatrix(pointid,startDate.getTime()/1000);
				List<PointData> xdend=edosUtil.getHistMatrix(pointid,endtime.getTime()/1000);


				if(xdbegin.size()==1 && xdend.size()==1)
				{
					beginvalue=xdbegin.get(0).getPointValueInDouble();
					endvalue=xdend.get(0).getPointValueInDouble();

					if (endvalue > beginvalue) {
						result =result+(endvalue - beginvalue);
					}
				}
			}

			//日故障损失电量
			if(wtpointmap.containsKey(ContantXk.NGZSSDL) )
			{

				double endvalue;

				List<String> pointid=new ArrayList<>();
				pointid.add(wtpointmap.get(ContantXk.NGZSSDL).getNemCode());
				List<PointData> xdend=edosUtil.getHistMatrix(pointid,startDate.getTime()/1000);

				if(xdend.size()==1)
				{

					endvalue=xdend.get(0).getPointValueInDouble();

					if (endvalue > 0) {
						result =result+endvalue;
					}
				}
			}

		}

		return result;

	}
}