package com.gyee.frame.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gyee.frame.common.base.BaseService;
import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.common.support.Convert;
import com.gyee.frame.mapper.auto.ShutdowneventMapper;
import com.gyee.frame.model.auto.*;
import com.gyee.frame.model.custom.ShutdowneventVo;
import com.gyee.frame.model.custom.SimpleVo;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.util.MathUtil;
import com.gyee.frame.util.SnowflakeIdWorker;
import com.gyee.frame.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;


@Service
public class ShutdowneventService implements BaseService<Shutdownevent, ShutdowneventExample> {
	@Resource
	private ShutdowneventMapper shutdowneventMapper;
	

	@Override
	public int deleteByPrimaryKey(String ids) {
				
			List<String> lista=Convert.toListStrArray(ids);
			ShutdowneventExample example=new ShutdowneventExample();
			example.createCriteria().andIdIn(lista);
			return shutdowneventMapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public Shutdownevent selectByPrimaryKey(String id) {
				
			return shutdowneventMapper.selectByPrimaryKey(id);
				
	}

	
	@Override
	public int updateByPrimaryKeySelective(Shutdownevent record) {
		return shutdowneventMapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(Shutdownevent record) {
				
		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());
			
				
		return shutdowneventMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(Shutdownevent record, ShutdowneventExample example) {
		
		return shutdowneventMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(Shutdownevent record, ShutdowneventExample example) {
		
		return shutdowneventMapper.updateByExample(record, example);
	}

	@Override
	public List<Shutdownevent> selectByExample(ShutdowneventExample example) {
		
		return shutdowneventMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(ShutdowneventExample example) {
		
		return shutdowneventMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(ShutdowneventExample example) {
		
		return shutdowneventMapper.deleteByExample(example);
	}

	public List<ShutdowneventVo> queryEvents(String wpId, String wtId, Date beginDate, Date endDate, String type) {

		List<Shutdownevent> list=new ArrayList<>();

		ShutdowneventExample example=new ShutdowneventExample();
		example.setOrderByClause("windPowerStationId ASC");

		ShutdowneventExample.Criteria criteria =example.createCriteria();

		if(StringUtils.isNotEmpty(wpId))
		{
			criteria.andWindpowerstationidEqualTo(wpId);
		}
		if(StringUtils.isNotEmpty(wtId))
		{
			criteria.andWindturbineidEqualTo(wtId);
		}

		if(StringUtils.isNotEmpty(type))
		{
			criteria.andStatuscodeEqualTo(Integer.valueOf(type));
		}
		if(beginDate!=null && endDate!=null ){
			criteria.andStoptimeGreaterThanOrEqualTo(beginDate).andStoptimeLessThanOrEqualTo(endDate);
		}

		list= shutdowneventMapper.selectByExample(example);
		List<ShutdowneventVo> sdelist=new ArrayList<>();

		if(null!=list && !list.isEmpty())
		{
			for(Shutdownevent sde:list)
			{
				ShutdowneventVo event = new ShutdowneventVo();
				event.setId(sde.getId());

				event.setWindPowerStationId(sde.getWindpowerstationid());
				event.setWpName(InitialRunner.wpmap.get(sde.getWindpowerstationid()).getName());
				event.setWindTurbineId(sde.getWindturbineid());
				event.setWtName(InitialRunner.wtmap.get(sde.getWindturbineid()).getCode());
				event.setWarningId(sde.getWarningid());

				event.setStartTime(sde.getStarttime());
				event.setStopTime(sde.getStoptime());
				event.setStopHours(sde.getStophours());
				event.setStopTypeId(sde.getStoptypeid());
				event.setStatusCode(sde.getStatuscode());
				sdelist.add(event);
			}
		}

		return sdelist;

	}

	public List<ShutdowneventVo> queryStopTop10(String wtId) {

		List<Shutdownevent> list=new ArrayList<>();

		ShutdowneventExample example=new ShutdowneventExample();
		example.setOrderByClause(" StopTime ASC");

		ShutdowneventExample.Criteria criteria =example.createCriteria();

		if(StringUtils.isNotEmpty(wtId))
		{
			criteria.andWindturbineidEqualTo(wtId);
		}
		criteria.andStophoursIsNotNull();
		criteria.andStoptypeidNotEqualTo("wh");
		criteria.andStoptypeidNotEqualTo("gzbmq");
		criteria.andStoptypeidNotEqualTo("other");
		PageHelper.startPage(0, 10);


		list= shutdowneventMapper.selectByExample(example);

		PageInfo<Shutdownevent> pageInfo = new PageInfo<Shutdownevent>(list);

		List<ShutdowneventVo> sdelist=new ArrayList<>();

		if(null!=list && !list.isEmpty())
		{
			for(Shutdownevent sde:list)
			{
				ShutdowneventVo event = new ShutdowneventVo();
				event.setId(sde.getId());

				event.setWindPowerStationId(sde.getWindpowerstationid());
				event.setWpName(InitialRunner.wpmap.get(sde.getWindpowerstationid()).getName());
				event.setWindTurbineId(sde.getWindturbineid());
				event.setWtName(InitialRunner.wtmap.get(sde.getWindturbineid()).getCode());
				event.setWarningId(sde.getWarningid());

				if(InitialRunner.stoptypemap.containsKey(sde.getStoptypeid()))
				{
					Stoptype stoptype=InitialRunner.stoptypemap.get(sde.getStoptypeid());
					event.setWarnDesc(stoptype.getName());
				}
				event.setStartTime(sde.getStarttime());
				event.setStopTime(sde.getStoptime());
				event.setStopHours(sde.getStophours());
				event.setStopTypeId(sde.getStoptypeid());
				event.setStatusCode(sde.getStatuscode());
				sdelist.add(event);
			}
		}

		return sdelist;

	}


	public List<ShutdowneventVo> queryWpStopTop5(String wpId) {

		List<Shutdownevent> list=new ArrayList<>();

		ShutdowneventExample example=new ShutdowneventExample();
		example.setOrderByClause(" StopTime deSC");

		ShutdowneventExample.Criteria criteria =example.createCriteria();

		if(StringUtils.isNotEmpty(wpId))
		{
			criteria.andWindpowerstationidEqualTo(wpId);
		}
		criteria.andStophoursIsNotNull();
		criteria.andStoptypeidNotEqualTo("wh");
		criteria.andStoptypeidNotEqualTo("gzbmq");
		criteria.andStoptypeidNotEqualTo("other");
		criteria.andStophoursLessThan(Double.valueOf(24*7));
		PageHelper.startPage(0, 5);


		list= shutdowneventMapper.selectByExample(example);

		PageInfo<Shutdownevent> pageInfo = new PageInfo<Shutdownevent>(list);

		List<ShutdowneventVo> sdelist=new ArrayList<>();

		if(null!=list && !list.isEmpty())
		{
			for(Shutdownevent sde:list)
			{
				ShutdowneventVo event = new ShutdowneventVo();
				event.setId(sde.getId());

				event.setWindPowerStationId(sde.getWindpowerstationid());
				event.setWpName(InitialRunner.wpmap.get(sde.getWindpowerstationid()).getName());
				event.setWindTurbineId(sde.getWindturbineid());
				event.setWtName(InitialRunner.wtmap.get(sde.getWindturbineid()).getCode());
				event.setWarningId(sde.getWarningid());

				if(InitialRunner.stoptypemap.containsKey(sde.getStoptypeid()))
				{
					Stoptype stoptype=InitialRunner.stoptypemap.get(sde.getStoptypeid());
					event.setWarnDesc(stoptype.getName());
				}
				event.setStartTime(sde.getStarttime());
				event.setStopTime(sde.getStoptime());
				event.setStopHours(sde.getStophours());
				event.setStopTypeId(sde.getStoptypeid());
				event.setStatusCode(sde.getStatuscode());
				sdelist.add(event);
			}
		}

		return sdelist;

	}
	/**
	 * 获取MTTR和MTBF
	 * @param wtIds
	 * @return
	 */
	public Map<String,Object> queryMttrAndMtbf(List<String> wtIds,Date recodedate) throws Exception {

		Map<String,Object> map=new HashMap<>();


		if(StringUtils.notEmp(recodedate) && StringUtils.notEmp(wtIds) && !wtIds.isEmpty())
		{
			List<Shutdownevent> list=new ArrayList<>();


			Calendar c = Calendar.getInstance();

			int year=c.get(Calendar.YEAR);
			int month=c.get(Calendar.MONTH);
			c.setTime(recodedate);
			c.set(Calendar.DAY_OF_MONTH, 1);
			c.set(Calendar.HOUR_OF_DAY, 0);
			c.set(Calendar.MINUTE, 0);
			c.set(Calendar.SECOND, 0);
			c.set(Calendar.MILLISECOND, 0);
			Date beginDate=c.getTime();
			if(c.get(Calendar.YEAR) == year && c.get(Calendar.MONTH)==month)
			{
				c = Calendar.getInstance();
				c.add(Calendar.DAY_OF_MONTH,1);
				c.set(Calendar.HOUR_OF_DAY, 0);
				c.set(Calendar.MINUTE, 0);
				c.set(Calendar.SECOND, 0);
				c.set(Calendar.MILLISECOND, 0);

			}else
			{
				c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
			}


			Date endDate=c.getTime();
			ShutdowneventExample example=new ShutdowneventExample();
			example.setOrderByClause(" StopTime ASC");

			int daynum=c.get(Calendar.DAY_OF_MONTH);
			ShutdowneventExample.Criteria criteria =example.createCriteria();

			if(StringUtils.isNotEmpty(wtIds))
			{
				criteria.andWindturbineidIn(wtIds);
			}
			SimpleDateFormat formatter  = new SimpleDateFormat("yyyy-MM-dd");
			String  bString = formatter.format(beginDate);
			String  eString = formatter.format(endDate);
			criteria.andStoptimeGreaterThanOrEqualTo2(bString).andStoptimeLessThanOrEqualTo2(eString);
			criteria.andStophoursIsNotNull();
		//	criteria.andStoptypeidNotEqualTo("wh");
		//	criteria.andStoptypeidNotEqualTo("other");
		//	criteria.andStoptypeidNotEqualTo("gzbmq");
			criteria.andStophoursLessThan(Double.valueOf(24*7));
			list= shutdowneventMapper.selectByExample(example);


			double stophours=0.0;

			if(!list.isEmpty())
			{
				for(Shutdownevent sde:list)
				{
					stophours=stophours+sde.getStophours();
				}


				double mttr= new BigDecimal(stophours).divide(new BigDecimal(list.size()), 0, RoundingMode.HALF_UP).doubleValue();
				map.put("mttr",mttr);
				int dayhours=daynum*24*wtIds.size();
				double stophoursall=dayhours-stophours;
				double mtbf = new BigDecimal(stophoursall).divide(new BigDecimal(list.size()), 0, RoundingMode.HALF_UP).doubleValue();;// 平均间隔时间
				map.put("mtbf",mtbf);
				map.put("mttf", MathUtil.twoBit(mtbf-mttr));
			}
		}

		return map;

	}



	/**
	 * 获取七天内各个风场停机统计
	 * @return
	 */
	public List<SimpleVo> countStopByWp() {

		List<SimpleVo> vos=new ArrayList<>();
		List<SimpleVo> ls= shutdowneventMapper.countStopByWp();
		if(!ls.isEmpty())
		{
			Map<String,SimpleVo> map=new HashMap<>();
			for(SimpleVo vo:ls)
			{
				map.put(vo.getId(),vo);
			}

			for(Windpowerstation wp:InitialRunner.wpls)
			{
				SimpleVo vo=new SimpleVo();
				vo.setId(wp.getId());
				vo.setName(wp.getName());
				vo.setValue(0);
				if(map.containsKey(wp.getId()))
				{
					SimpleVo votemp=map.get(wp.getId());
					vo.setValue(votemp.getValue());
				}
				vos.add(vo);
			}
		}
		return vos;

	}

	/**
	 * 获取七天内各个项目停机统计
	 * @return
	 */
	public List<SimpleVo> countStopByPj() {

		List<SimpleVo> vos=new ArrayList<>();
		List<SimpleVo> ls= shutdowneventMapper.countStopByPj();
		if(!ls.isEmpty())
		{
			Map<String,SimpleVo> map=new HashMap<>();
			for(SimpleVo vo:ls)
			{
				map.put(vo.getId(),vo);
			}

			for(Project pj:InitialRunner.pjls)
			{
				SimpleVo vo=new SimpleVo();
				vo.setId(pj.getId());
				vo.setName(pj.getName());
				vo.setValue(0);
				if(map.containsKey(pj.getId()))
				{
					SimpleVo votemp=map.get(pj.getId());
					vo.setValue(votemp.getValue());
				}
				vos.add(vo);
			}
		}
		return vos;

	}


	public List<Shutdownevent> queryStopWtByPart(String wtId) {

		List<Shutdownevent> list=new ArrayList<>();

		ShutdowneventExample example=new ShutdowneventExample();
		example.setOrderByClause(" StopTime ASC");

		ShutdowneventExample.Criteria criteria =example.createCriteria();

		if(StringUtils.isNotEmpty(wtId))
		{
			criteria.andWindturbineidEqualTo(wtId);
			criteria.andStoptypeidIsNotNull();
			criteria.andStarttimeIsNotNull();
			criteria.andStoptypeidNotEqualTo("wh");
			criteria.andStoptypeidNotEqualTo("gzbmq");
			criteria.andStoptypeidNotEqualTo("other");
			criteria.andStoptypeidNotEqualTo("其它");

			Calendar cal = Calendar.getInstance();
			cal.set(Calendar.MONTH,0);
			cal.set(Calendar.DAY_OF_MONTH,1);
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			cal.set(Calendar.MILLISECOND, 0);
			Date beginDate=cal.getTime();
			cal.add(Calendar.YEAR,1);
			Date endDate=cal.getTime();
			criteria.andStoptimeGreaterThanOrEqualTo(beginDate).andStoptimeLessThanOrEqualTo(endDate);

			list= shutdowneventMapper.selectByExample(example);
		}

		return list;

	}

	public PageInfo<ShutdowneventVo> getShutdownevent(Tablepar tablepar,String wpId,String wtId, Date beginDate, Date endDate,String type)  {

		PageInfo<ShutdowneventVo> pageInfo=new PageInfo<>();
		List<Shutdownevent> list =new ArrayList<>();
		ShutdowneventExample example=new ShutdowneventExample();
		String order =null;
		if(StringUtils.isNotEmpty(tablepar.getOrderByColumn()))
		{
			StringBuilder sb=new StringBuilder();
			sb.append(" ").append(tablepar.getOrderByColumn());
			if(StringUtils.isNotEmpty(tablepar.getIsAsc()))
			{
				sb.append(" ").append(tablepar.getIsAsc());
			}else
			{
				sb.append(" asc ");
			}
			order=String.valueOf(sb);
		}else {
			order=" stoptime desc";
		}

		ShutdowneventExample.Criteria criteria =example.createCriteria();

		if(StringUtils.isNotEmpty(wpId))
		{
			criteria.andWindpowerstationidEqualTo(wpId);
		}
		if(StringUtils.isNotEmpty(wtId))
		{
			criteria.andWindturbineidEqualTo(wtId);
		}

		if(StringUtils.isNotEmpty(type))
		{
			criteria.andStatuscodeEqualTo(Integer.valueOf(type));
		}
		criteria.andStoptimeGreaterThanOrEqualTo(beginDate).andStoptimeLessThanOrEqualTo(endDate);


		PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
		list= shutdowneventMapper.selectByExample(example);
		List<ShutdowneventVo> sdelist=new ArrayList<>();


		if(null!=list && !list.isEmpty())
		{
			for(Shutdownevent sde:list)
			{
				ShutdowneventVo event = new ShutdowneventVo();
				event.setId(sde.getId());

				event.setWindPowerStationId(sde.getWindpowerstationid());
				event.setWpName(InitialRunner.wpmap.get(sde.getWindpowerstationid()).getName());
				event.setWindTurbineId(sde.getWindturbineid());
				event.setWtName(InitialRunner.wtmap.get(sde.getWindturbineid()).getCode());
				event.setWarningId(sde.getWarningid());

				if(InitialRunner.stoptypemap.containsKey(sde.getStoptypeid()))
				{
					Stoptype stoptype=InitialRunner.stoptypemap.get(sde.getStoptypeid());
					event.setWarnDesc(stoptype.getName());
				}
				event.setStartTime(sde.getStarttime());
				event.setStopTime(sde.getStoptime());
				event.setStopHours(sde.getStophours());
				event.setStopTypeId(sde.getStoptypeid());
				event.setStatusCode(sde.getStatuscode());
				sdelist.add(event);
			}

			pageInfo = new PageInfo<ShutdowneventVo>(sdelist);

		}
		return pageInfo;

	}

}
