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.ActivePowerDataMapper;
import com.gyee.frame.model.auto.ActivePowerData;
import com.gyee.frame.model.auto.ActivePowerDataExample;
import com.gyee.frame.model.auto.Windturbine;
import com.gyee.frame.model.custom.StatisticalAnalysisRnewTopVo;
import com.gyee.frame.util.StringUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class ActivePowerDataService implements BaseService<ActivePowerData, ActivePowerDataExample> {
	@Resource
	private ActivePowerDataMapper activePowerDataMapper;
	

	@Override
	public int deleteByPrimaryKey(String ids) {
				
			List<Integer> lista=Convert.toIntArrayList(ids);
			ActivePowerDataExample example=new ActivePowerDataExample();
			example.createCriteria().andIdIn(lista);
			return activePowerDataMapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public ActivePowerData selectByPrimaryKey(String id) {
				
			return activePowerDataMapper.selectByPrimaryKey(Integer.valueOf(id));
				
	}

	
	@Override
	public int updateByPrimaryKeySelective(ActivePowerData record) {
		return activePowerDataMapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(ActivePowerData record) {
				

				
		return activePowerDataMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(ActivePowerData record, ActivePowerDataExample example) {
		
		return activePowerDataMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(ActivePowerData record, ActivePowerDataExample example) {
		
		return activePowerDataMapper.updateByExample(record, example);
	}

	@Override
	public List<ActivePowerData> selectByExample(ActivePowerDataExample example) {
		
		return activePowerDataMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(ActivePowerDataExample example) {
		
		return activePowerDataMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(ActivePowerDataExample example) {
		
		return activePowerDataMapper.deleteByExample(example);
	}

	public List<StatisticalAnalysisRnewTopVo> getStatisticalAnalysisRnewTopVoList(String wpIds, Date beginDate, Date endDate)  {


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

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


		ActivePowerDataExample.Criteria criteria =example.createCriteria();

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


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

		List<ActivePowerData> apls= activePowerDataMapper.selectByExample(example);

		if(!apls.isEmpty())
		{
			int num=1;
			for(ActivePowerData ap:apls)
			{
				StatisticalAnalysisRnewTopVo item = new StatisticalAnalysisRnewTopVo();
				item.setNum(num);
				item.setWindPowerStationId(ap.getWindpowerstationid());
				item.setWindTurbineId(ap.getWindturbineid());
				item.setWindTurbineCode(ap.getWindturbinecode());
				item.setModelId(ap.getModelid());
				item.setModelName(ap.getModelname());
				item.setSpeed(ap.getSpeed());
				item.setAvgpower(ap.getPower());


				double spend=item.getSpeed();
				int c = (int) spend;
				if(c == spend)
				{
					list.add(item);
				}

				num++;
			}
		}
		return list;

	}


	public Map<String, List>  getActivePowerDataChart(List<String> wtIds, Date beginDate, Date endDate)  {

		Map<String, List> retValue = new HashMap<String, List>();
		List<StatisticalAnalysisRnewTopVo> list =new ArrayList<>();

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


		ActivePowerDataExample.Criteria criteria =example.createCriteria();

		criteria.andWindturbineidIn(wtIds);

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

		List<ActivePowerData> apls= activePowerDataMapper.selectByExample(example);

		Map<String, Map<String, Double>> listMap = new HashMap<String, Map<String, Double>>();
		Map<String, Map<Double, List<Double>>> lsMap = new HashMap<String, Map<Double, List<Double>>>();
		if(!apls.isEmpty())
		{
			for(ActivePowerData ap:apls)
			{
				Map<Double, List<Double>> items=null;
				if (lsMap.containsKey(ap.getWindturbineid())) {
					items = lsMap.get(ap.getWindturbineid());
					if(items.containsKey(String.valueOf(ap.getSpeed())))
					{
						List<Double> doubleList=items.get(String.valueOf(ap.getSpeed()));
									doubleList.add(ap.getPower());
						items.put(ap.getSpeed(), doubleList);
					}else
					{
						List<Double> doubleList=new ArrayList<>();
						doubleList.add(ap.getPower());
						items.put(ap.getSpeed(), doubleList);
					}
				} else {
					items = new HashMap<Double, List<Double>>();
					lsMap.put(ap.getWindturbineid(), items);
				}

			}
			List<String> sers = new ArrayList<String>();

			retValue.put("sers", sers);
			for (String wtid : wtIds) {
				Windturbine wt = InitialRunner.wtmap.get(wtid);

				Map<Double,List<Double>> sjMap = lsMap.get(wtid);
				List<Double> sj = new ArrayList<Double>();
				for (Double i = 3.0; i <= 25; i += .5) {
					if (sjMap != null && sjMap.containsKey(i)) {
						List<Double> ls=sjMap.get(i);

						double power=ls.stream().mapToDouble(Double::doubleValue).average().getAsDouble();//平均值
						sj.add(StringUtils.round(power, 2));
					}
				}
				retValue.put(String.format("%s(%s)实际功率", wt.getCode(), wt.getModelid()), sj);
				sers.add(String.format("%s(%s)实际功率", wt.getCode(), wt.getModelid()));
			}



		}
		return retValue;

	}


}
