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.WindTurbineInfoDayMapper;
import com.gyee.frame.model.auto.WindTurbineInfoDay;
import com.gyee.frame.model.auto.WindTurbineInfoDayExample;
import com.gyee.frame.model.auto.Windpowerstation;
import com.gyee.frame.model.auto.Windturbine;
import com.gyee.frame.model.custom.SingleAnalysisVo;
import com.gyee.frame.model.custom.StatisticalAnalysisTopVo;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.util.DateUtils;
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.*;

/**
 * 风机信息表 WindTurbineInfoDayService
 * @Title: WindTurbineInfoDayService.java 
 * @Package com.gyee.frame.service 
 **/
@Service
public class WindturbineinfodayService implements BaseService<WindTurbineInfoDay, WindTurbineInfoDayExample> {
	@Resource
	private WindTurbineInfoDayMapper windTurbineInfoDayMapper;


	/**
	 * 分页查询
	 * @param tablepar
	 * @param name
	 * @return
	 */
	 public PageInfo<WindTurbineInfoDay> list(Tablepar tablepar, String name){
	        WindTurbineInfoDayExample testExample=new WindTurbineInfoDayExample();
	        testExample.setOrderByClause("id ASC");
	        if(name!=null&&!"".equals(name)){
//	        	testExample.createCriteria().and.andCodeLike("%"+name+"%");
	        }

	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        List<WindTurbineInfoDay> list= windTurbineInfoDayMapper.selectByExample(testExample);
	        PageInfo<WindTurbineInfoDay> pageInfo = new PageInfo<WindTurbineInfoDay>(list);
	        return  pageInfo;
	 }

	@Override
	public int deleteByPrimaryKey(String ids) {

			List<Integer> lista=Convert.toIntArrayList(ids);
			WindTurbineInfoDayExample example=new WindTurbineInfoDayExample();
			example.createCriteria().andIdIn(lista);
			return windTurbineInfoDayMapper.deleteByExample(example);


	}


	@Override
	public WindTurbineInfoDay selectByPrimaryKey(String id) {

	 	if(StringUtils.isNotEmpty(id))
		{
			return windTurbineInfoDayMapper.selectByPrimaryKey(Integer.valueOf(id));

		}
	 	return new WindTurbineInfoDay();

	}


	@Override
	public int updateByPrimaryKeySelective(WindTurbineInfoDay record) {
		return windTurbineInfoDayMapper.updateByPrimaryKeySelective(record);
	}


	/**
	 * 添加
	 */
	@Override
	public int insertSelective(WindTurbineInfoDay record) {

//		//添加雪花主键id
//		record.setId(SnowflakeIdWorker.getUUID());


		return windTurbineInfoDayMapper.insertSelective(record);
	}


	@Override
	public int updateByExampleSelective(WindTurbineInfoDay record, WindTurbineInfoDayExample example) {

		return windTurbineInfoDayMapper.updateByExampleSelective(record, example);
	}


	@Override
	public int updateByExample(WindTurbineInfoDay record, WindTurbineInfoDayExample example) {

		return windTurbineInfoDayMapper.updateByExample(record, example);
	}

	@Override
	public List<WindTurbineInfoDay> selectByExample(WindTurbineInfoDayExample example) {

		return windTurbineInfoDayMapper.selectByExample(example);
	}


	@Override
	public long countByExample(WindTurbineInfoDayExample example) {

		return windTurbineInfoDayMapper.countByExample(example);
	}


	@Override
	public int deleteByExample(WindTurbineInfoDayExample example) {

		return windTurbineInfoDayMapper.deleteByExample(example);
	}

	public List<WindTurbineInfoDay> getWindturbineinfo(Date beginDate, Date endDate, String wtId) {


		List<WindTurbineInfoDay> list=null;

		if (StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate)) {

			WindTurbineInfoDayExample example=new WindTurbineInfoDayExample();
			example.setOrderByClause("recorddate DESC");

			if(StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate)){


				example.createCriteria().andWindpowerstationidLike("%FDC").andWindturbineidEqualTo(wtId).andRecorddateGreaterThanOrEqualTo(beginDate).andRecorddateLessThan(endDate);
				list= windTurbineInfoDayMapper.selectByExample(example);
			}else
			{
				list=new ArrayList<>();
			}

		}
		return list;

	}


	public List<StatisticalAnalysisTopVo> getWindturbineinfo(List<String> wpIds, Date beginDate, Date endDate)  {

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

		WindTurbineInfoDayExample example=new WindTurbineInfoDayExample();
		example.setOrderByClause("windturbineid ASC");


		WindTurbineInfoDayExample.Criteria criteria =example.createCriteria();

		if(!wpIds.isEmpty())
		{
			criteria.andWindpowerstationidIn(wpIds);
		}


		criteria.andRecorddateGreaterThanOrEqualTo(beginDate).andRecorddateLessThan(endDate);

		List<WindTurbineInfoDay> wils=windTurbineInfoDayMapper.selectByExample(example);

		Map<String, List<WindTurbineInfoDay>> lsMap = new HashMap<String, List<WindTurbineInfoDay>>();
		if(!wils.isEmpty())
		{
			for(WindTurbineInfoDay ap:wils)
			{
				List<WindTurbineInfoDay> items=null;
				if (lsMap.containsKey(ap.getWindturbineid())) {
					items = lsMap.get(ap.getWindturbineid());
					items.add(ap);
					lsMap.put(ap.getWindturbineid(), items);
				} else {
					items = new ArrayList<>();
					items.add(ap);
					lsMap.put(ap.getWindturbineid(), items);
				}

			}


			Set<Map.Entry<String, List<WindTurbineInfoDay>>> set = lsMap.entrySet();
			for (Iterator<Map.Entry<String, List<WindTurbineInfoDay>>> it = set.iterator(); it.hasNext();) {
				Map.Entry<String, List<WindTurbineInfoDay>> entry = (Map.Entry<String, List<WindTurbineInfoDay>>) it.next();
				System.out.println(entry.getKey() + "--->" + entry.getValue());

				List<WindTurbineInfoDay> ls=entry.getValue();

				Windturbine wt= InitialRunner.wtmap.get(entry.getKey());
				Windpowerstation wp=InitialRunner.wpmap.get(wt.getWindpowerstationid());
				StatisticalAnalysisTopVo item = new StatisticalAnalysisTopVo();
				item.setWindfarm(wp.getName());
				item.setWindturbine(wt.getId());
				item.setBeginDate(beginDate);
				item.setEndDate(endDate);
				double power=ls.stream().mapToDouble(WindTurbineInfoDay::getPower).average().getAsDouble();//平均值
				double speed=ls.stream().mapToDouble(WindTurbineInfoDay::getSpeed).average().getAsDouble();//平均值
				double generatingcapacity=ls.stream().mapToDouble(WindTurbineInfoDay::getGeneratingcapacity).sum();//合计

				generatingcapacity = new BigDecimal(generatingcapacity).divide(new BigDecimal(10000), 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue();
				item.setGeneratingCapacity(generatingcapacity);
				item.setSpeed(speed);
				item.setPower(power);

				list.add(item);
			}

		}
		return list;

	}


	public List<WindTurbineInfoDay> getWindturbineinfo(String wtId,Date recorddate) {


		List<com.gyee.frame.model.auto.WindTurbineInfoDay> list=null;

		if (StringUtils.notEmp(wtId) ) {

			WindTurbineInfoDayExample example=new WindTurbineInfoDayExample();
			example.setOrderByClause("recorddate DESC");

			if(StringUtils.notEmp(recorddate)){

				WindTurbineInfoDayExample.Criteria criteria =example.createCriteria();

				Date endDate= DateUtils.addDays(recorddate,1);
				criteria.andRecorddateGreaterThanOrEqualTo(recorddate).andRecorddateLessThan(endDate);

				criteria.andWindturbineidEqualTo(wtId);
				list= windTurbineInfoDayMapper.selectByExample(example);
			}else
			{
				list=new ArrayList<>();
			}

		}
		return list;

	}


	public List<WindTurbineInfoDay> getWindturbineinfodayList(String wpId,Date beginDate,Date endDate) {


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

		if (StringUtils.notEmp(wpId) && StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate)) {

			WindTurbineInfoDayExample example=new WindTurbineInfoDayExample();
			example.setOrderByClause("recorddate DESC");

			WindTurbineInfoDayExample.Criteria criteria =example.createCriteria();

			criteria.andRecorddateGreaterThanOrEqualTo(beginDate).andRecorddateLessThan(endDate);

			criteria.andWindpowerstationidEqualTo(wpId);
			list= windTurbineInfoDayMapper.selectByExample(example);

		}
		return list;

	}


	public List<WindTurbineInfoDay> getWindturbineinfodayListByWtId(String wtId,Date beginDate,Date endDate) {


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

		if (StringUtils.notEmp(wtId) && StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate)) {

			WindTurbineInfoDayExample example=new WindTurbineInfoDayExample();
			example.setOrderByClause("recorddate asc");

			WindTurbineInfoDayExample.Criteria criteria =example.createCriteria();

			criteria.andRecorddateGreaterThanOrEqualTo(beginDate).andRecorddateLessThan(endDate);

			criteria.andWindturbineidEqualTo(wtId);
			list= windTurbineInfoDayMapper.selectByExample(example);

		}
		return list;

	}

	public List<SingleAnalysisVo>  getWindTurbineInfoDayMapper(String wpId,Date beginDate,Date endDate) {

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


		if (StringUtils.notEmp(wpId) && StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate)) {


			list = windTurbineInfoDayMapper.getWindTurbineInfoDay(wpId,beginDate,endDate);


		}
		return list;

	}


	public List<SingleAnalysisVo>  getWindTurbineInfoDayMapperByWtId(String wtId, Date beginDate, Date endDate) {

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


		if (StringUtils.notEmp(wtId) && StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate)) {


			list = windTurbineInfoDayMapper.getWindTurbineInfoDayByWtId(wtId,beginDate,endDate);


		}
		return list;

	}
}
