package com.gyee.frame.service;

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.custom.ShutdowneventVo;
import com.gyee.frame.util.SnowflakeIdWorker;
import com.gyee.frame.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class ShutdowneventService implements BaseService<Shutdownevent, ShutdowneventExample> {
	@Autowired
	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;

	}
}
