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.MainBrownouts2Mapper;
import com.gyee.frame.model.auto.Brownoutsevent2;
import com.gyee.frame.model.auto.MainBrownouts2;
import com.gyee.frame.model.auto.MainBrownouts2Example;
import com.gyee.frame.model.custom.MainBrownoutsVo;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.util.SnowflakeIdWorker;
import com.gyee.frame.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class MainBrownouts2Service implements BaseService<MainBrownouts2, MainBrownouts2Example> {
	@Resource
	private MainBrownouts2Mapper mainBrownouts2Mapper;
	@Resource
	private Brownoutsevent2Service brownoutsevent2Service;

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

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

	
	@Override
	public int updateByExample(MainBrownouts2 record, MainBrownouts2Example example) {
		
		return mainBrownouts2Mapper.updateByExample(record, example);
	}

	@Override
	public List<MainBrownouts2> selectByExample(MainBrownouts2Example example) {
		
		return mainBrownouts2Mapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(MainBrownouts2Example example) {
		
		return mainBrownouts2Mapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(MainBrownouts2Example example) {
		
		return mainBrownouts2Mapper.deleteByExample(example);
	}



	public PageInfo<MainBrownoutsVo> getMainBrownouts2(Tablepar tablepar, String wpId, Date beginDate, Date endDate)  {

		PageInfo<MainBrownoutsVo> pageInfo=new PageInfo<>();
		List<MainBrownouts2> list =new ArrayList<>();
		MainBrownouts2Example example=new MainBrownouts2Example();
		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";
		}

		MainBrownouts2Example.Criteria criteria =example.createCriteria();

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

		criteria.andStoptimeGreaterThanOrEqualTo(beginDate).andStoptimeLessThanOrEqualTo(endDate);


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


		if(null!=list && !list.isEmpty())
		{
			for(MainBrownouts2 sde:list)
			{
				MainBrownoutsVo event = new MainBrownoutsVo();
				event.setId(sde.getId());
				event.setWindPowerStationId(sde.getWindpowerstationid());
				event.setWpName(InitialRunner.wpmap.get(sde.getWindpowerstationid()).getName());
				event.setStartTime(sde.getStarttime());
				event.setStopTime(sde.getStoptime());
				event.setStopHours(sde.getStophours());
				event.setStopTypeId(sde.getStoptypeid());
				event.setDescription(sde.getDescription());
				event.setLossPower(sde.getLosspower());
				List<Brownoutsevent2>  bwls=brownoutsevent2Service.getBrownoutsevent2List(sde.getId());
				if(!bwls.isEmpty())
				{
					StringBuilder sb=new StringBuilder();
					for(Brownoutsevent2 bw:bwls)
					{
						sb.append(bw.getWindturbineid()).append(",");
					}

					String wts=String.valueOf(sb);

					event.setWts(wts.substring(0,wts.length()-1));
				}

				sdelist.add(event);
			}

			pageInfo = new PageInfo<MainBrownoutsVo>(sdelist);

		}
		return pageInfo;

	}




}
