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.Shutdownevent2Mapper;
import com.gyee.frame.model.auto.*;
import com.gyee.frame.model.custom.ShutdowneventVo;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.util.SnowflakeIdWorker;
import com.gyee.frame.util.StringUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

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


@Service
public class Shutdownevent2Service implements BaseService<Shutdownevent2, Shutdownevent2Example> {
	@Resource
	private Shutdownevent2Mapper shutdownevent2Mapper;
	

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

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

	
	@Override
	public int updateByExample(Shutdownevent2 record, Shutdownevent2Example example) {
		
		return shutdownevent2Mapper.updateByExample(record, example);
	}

	@Override
	public List<Shutdownevent2> selectByExample(Shutdownevent2Example example) {
		
		return shutdownevent2Mapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(Shutdownevent2Example example) {
		
		return shutdownevent2Mapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(Shutdownevent2Example example) {
		
		return shutdownevent2Mapper.deleteByExample(example);
	}

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

		Shutdownevent2Example example = new Shutdownevent2Example();

		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 ");
			}
			example.setOrderByClause(String.valueOf(sb));
		}else {
			example.setOrderByClause("windturbineid ASC");
		}


		PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());

		Shutdownevent2Example.Criteria criteria = example.createCriteria();
		if (StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate)) {
			criteria.andStoptimeGreaterThanOrEqualTo(beginDate).andStoptimeLessThan(endDate);
		}

		if (StringUtils.notEmp(wtId)) {
			criteria.andWindturbineidEqualTo(wtId);
		}

		List<Shutdownevent2> list = shutdownevent2Mapper.selectByExample(example);

		List<ShutdowneventVo> ls=new ArrayList<>();
		if(!list.isEmpty())
		{
			Windturbine wt=InitialRunner.wtmap.get(wtId);
			Windpowerstation wp=InitialRunner.wpmap.get(wt.getWindpowerstationid());
			Project pj=InitialRunner.pjmap.get(wt.getProjectid());
			for(Shutdownevent2 wr:list)
			{
				ShutdowneventVo event=new ShutdowneventVo();

				event.setId(wr.getId());
				event.setWindPowerStationId(wr.getWindpowerstationid());
				event.setWpName(wp.getName());
				event.setWindTurbineId(wtId);

				event.setWarningId(wr.getWarningid());
				if(StringUtils.notEmp(wr.getWarningid()) && InitialRunner.warningmap.containsKey(wr.getWarningid()))
				{
					Warning warning2=InitialRunner.warningmap.get(wr.getWarningid());
					event.setWarnDesc(warning2.getChinesetext());
				}
				event.setStartTime(wr.getStarttime());
				event.setStopTime(wr.getStoptime());
				event.setStopHours(wr.getStophours());
				event.setStopTypeId(wr.getStoptypeid());
				event.setStatusCode(wr.getStatuscode());
//				event.setStopTypeName(CacheContext.stmap.get(rs.getString("stopTypeId")) == null ? "" : CacheContext.stmap.get(rs.getString("stopTypeId")).getName());
				event.setIsHandle(wr.getIshandle());
				event.setProjectid(wr.getProjectid());
				event.setLossPower(wr.getLosspower());

				event.setProjectName(pj.getName());
				event.setFaultView(wr.getFaultview());
				event.setHandleWay(wr.getHandleway());
				event.setModelId(wt.getModelid());
//				event.setModelCode(CacheContext.mlmap.get(rs.getString("modelId")).getCode());

				ls.add(event);
			}
		}

		PageInfo<ShutdowneventVo> pageInfo = new PageInfo<ShutdowneventVo>(ls);

		return pageInfo;
	}

}
