package com.gyee.frame.service;

import com.gyee.frame.common.base.BaseService;
import com.gyee.frame.common.conf.V2Config;
import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.common.support.Convert;
import com.gyee.frame.mapper.auto.CurvefittingsubMapper;
import com.gyee.frame.model.auto.Curvefittingsub;
import com.gyee.frame.model.auto.CurvefittingsubExample;
import com.gyee.frame.model.auto.Windturbine;
import com.gyee.frame.model.custom.QxpcVo;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.util.DateUtils;
import com.gyee.frame.util.SnowflakeIdWorker;
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.text.SimpleDateFormat;
import java.util.*;


@Service
public class CurvefittingsubService implements BaseService<Curvefittingsub, CurvefittingsubExample> {

	private final int DIGIT = 2;
	@Resource
	private CurvefittingsubMapper curvefittingsubMapper;
	private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");


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

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

	
	@Override
	public int updateByExample(Curvefittingsub record, CurvefittingsubExample example) {
		
		return curvefittingsubMapper.updateByExample(record, example);
	}

	@Override
	public List<Curvefittingsub> selectByExample(CurvefittingsubExample example) {
		
		return curvefittingsubMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(CurvefittingsubExample example) {
		
		return curvefittingsubMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(CurvefittingsubExample example) {
		
		return curvefittingsubMapper.deleteByExample(example);
	}


	public Map<String, List<Curvefittingsub>> findCurvefittingmapByDate(Tablepar tablepar,List<String> wtIds, Date recorddate,Double min,Double max)  {

		Map<String, List<Curvefittingsub>> cfvmap=new HashMap<>();

		CurvefittingsubExample example=new CurvefittingsubExample();

		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("deviationrate2 ASC");
		}

		CurvefittingsubExample.Criteria criteria =example.createCriteria();

		criteria.andWindturbineidIn(wtIds);

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

		criteria.andSpeedGreaterThanOrEqualTo(min);
		criteria.andSpeedLessThanOrEqualTo(max);
		List<Curvefittingsub>  cfvls=curvefittingsubMapper.selectByExample(example);

		if (!cfvls.isEmpty()) {

			for (Curvefittingsub cfv : cfvls) {

				if (cfvmap.containsKey(cfv.getWindturbineid())) {
					List<Curvefittingsub> ls = cfvmap.get(cfv.getWindturbineid());
					ls.add(cfv);
					cfvmap.put(cfv.getWindturbineid(), ls);
				} else {
					List<Curvefittingsub> ls = new ArrayList<Curvefittingsub>();
					ls.add(cfv);
					cfvmap.put(cfv.getWindturbineid(), ls);
				}
			}
		}
		return cfvmap;

	}


	public List<QxpcVo> getCurvefittingsubList(Tablepar tablepar,String wpId, Date recorddate)
	{
		List<QxpcVo> vos=new ArrayList<>();
		if(StringUtils.notEmp(wpId) && StringUtils.notEmp(wpId) && InitialRunner.wp_wtmap.containsKey(wpId))
		{

			List<String> wtIds=new ArrayList<>();
			for (Windturbine wt : InitialRunner.wp_wtmap.get(wpId)) {
				wtIds.add(wt.getId());
			}

			Map<String, List<Curvefittingsub>> cfmap1 = findCurvefittingmapByDate(tablepar,wtIds,recorddate, 3.0, 5.0);
			Map<String, List<Curvefittingsub>> cfmap2 = findCurvefittingmapByDate(tablepar,wtIds,recorddate, 5.0, 11.0);
			Map<String, List<Curvefittingsub>> cfmap3 = findCurvefittingmapByDate(tablepar,wtIds,recorddate, 11.0, 25.0);

			for (Windturbine wt : InitialRunner.wp_wtmap.get(wpId)) {
				QxpcVo vo = new QxpcVo();

				vo.setWtId(wt.getId());
				vo.setRd(df.format(recorddate));
				if (cfmap1.containsKey(wt.getId())) {
					double qxpc1 = getNuberValue(cfmap1, wt);
					vo.setQxpc1(qxpc1);
				} else {
					vo.setQxpc1(0.0);
				}

				if (cfmap2.containsKey(wt.getId())) {
					double qxpc2 = getNuberValue(cfmap2, wt);
					vo.setQxpc2(qxpc2);
				} else {
					vo.setQxpc2(0.0);
				}

				if (cfmap3.containsKey(wt.getId())) {
					double qxpc3 = getNuberValue(cfmap3, wt);
					vo.setQxpc3(qxpc3);
				} else {
					vo.setQxpc3(0.0);
				}
				vos.add(vo);
			}
		}


		return vos;
	}

	public double getNuberValue(Map<String, List<Curvefittingsub>> daymap, Windturbine wt) {

		if (daymap.containsKey(wt.getId())) {

			List<Curvefittingsub> cfsls = daymap.get(wt.getId());
			if (!cfsls.isEmpty()) {

				double result = 0.0;
				for (Curvefittingsub sub : cfsls) {
					double value = null != sub.getDeviationrate2() ? sub.getDeviationrate2() : 0;
					result = result + value;
				}

				result = new BigDecimal(result).divide(new BigDecimal(cfsls.size()), V2Config.getDigit(), RoundingMode.HALF_EVEN).doubleValue();
				return result;
			}

		}
		return 0.0;
	}


	public Map<String, List<Curvefittingsub>> findCurvefittingmapByDate(Date recorddate,String wtId, Double min,Double max)  {

		Map<String, List<Curvefittingsub>> cfvmap=new HashMap<>();

		CurvefittingsubExample example=new CurvefittingsubExample();



		CurvefittingsubExample.Criteria criteria =example.createCriteria();

		criteria.andWindturbineidEqualTo(wtId);

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

		criteria.andSpeedGreaterThanOrEqualTo(min);
		criteria.andSpeedLessThanOrEqualTo(max);
		List<Curvefittingsub>  cfvls=curvefittingsubMapper.selectByExample(example);

		if (!cfvls.isEmpty()) {

			for (Curvefittingsub cfv : cfvls) {

				if (cfvmap.containsKey(cfv.getWindturbineid())) {
					List<Curvefittingsub> ls = cfvmap.get(cfv.getWindturbineid());
					ls.add(cfv);
					cfvmap.put(cfv.getWindturbineid(), ls);
				} else {
					List<Curvefittingsub> ls = new ArrayList<Curvefittingsub>();
					ls.add(cfv);
					cfvmap.put(cfv.getWindturbineid(), ls);
				}
			}
		}
		return cfvmap;

	}


}
