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.Shutdownevent;
import com.gyee.frame.model.auto.ShutdowneventExample;
import com.gyee.frame.model.auto.Stoptype;
import com.gyee.frame.model.custom.ShutdowneventVo;
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.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;

	}

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

		Map<String,Object> map=new HashMap<>();
		List<Shutdownevent> list=new ArrayList<>();


		Calendar c = Calendar.getInstance();
		// c.add(Calendar.MONTH, 0);
		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();
		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);
		}
		criteria.andStoptimeGreaterThanOrEqualTo(beginDate).andStoptimeLessThanOrEqualTo(endDate);
		criteria.andStophoursIsNotNull();
		criteria.andStoptypeidNotEqualTo("wh");

		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()), 2, RoundingMode.HALF_UP).doubleValue();
			map.put("mttr",mttr);
			int dayhours=daynum*24*list.size();
			double stophoursall=dayhours-stophours;
			double mtbf = new BigDecimal(stophoursall).divide(new BigDecimal(list.size()), 2, RoundingMode.HALF_UP).doubleValue();;// 平均间隔时间
			map.put("mtbf",mtbf);
		}

		return map;

	}
}
