package com.gyee.frame.service;

import com.gyee.frame.common.base.BaseService;
import com.gyee.frame.common.support.Convert;
import com.gyee.frame.mapper.auto.CurvefittingmainMapper;
import com.gyee.frame.model.auto.Curvefittingmain;
import com.gyee.frame.model.auto.CurvefittingmainExample;
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.util.*;


@Service
public class CurvefittingmainService implements BaseService<Curvefittingmain, CurvefittingmainExample> {
	@Resource
	private CurvefittingmainMapper curvefittingmainMapper;
	

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

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

	
	@Override
	public int updateByExample(Curvefittingmain record, CurvefittingmainExample example) {
		
		return curvefittingmainMapper.updateByExample(record, example);
	}

	@Override
	public List<Curvefittingmain> selectByExample(CurvefittingmainExample example) {
		
		return curvefittingmainMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(CurvefittingmainExample example) {
		
		return curvefittingmainMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(CurvefittingmainExample example) {
		
		return curvefittingmainMapper.deleteByExample(example);
	}


	public List<Curvefittingmain> getCurvefittingmainList(Tablepar tablepar, List<String> wtIds, Date recorddate)  {

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

		CurvefittingmainExample example=new CurvefittingmainExample();
		example.setOrderByClause(" deviationrate2 DESC");


		CurvefittingmainExample.Criteria criteria =example.createCriteria();

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

		list=curvefittingmainMapper.selectByExample(example);

		list=sortCurvefittingmainList(tablepar, list);

		return list;

	}

	public  List<Curvefittingmain> sortCurvefittingmainList(Tablepar tablepar,List<Curvefittingmain> ls)
	{

		if (null !=tablepar && StringUtils.notEmp(tablepar.getOrderByColumn())) {

			switch (tablepar.getOrderByColumn()) {
				case "windturbineid":
					if (tablepar.getIsAsc().equals("asc")) {

						Collections.sort(ls, new Comparator<Curvefittingmain>() {
							// 升序排序
							public int compare(Curvefittingmain o1, Curvefittingmain o2) {
								return o1.getWindturbineid().compareTo(o2.getWindturbineid());
							}

						});
					} else   {
						Collections.sort(ls, new Comparator<Curvefittingmain>() {
							// 升序排序
							public int compare(Curvefittingmain o1, Curvefittingmain o2) {
								return o2.getWindturbineid().compareTo(o1.getWindturbineid());
							}

						});
					}
					break;
				case "monthdeviationrate":
					if (tablepar.getIsAsc().equals("asc")) {

						Collections.sort(ls, new Comparator<Curvefittingmain>() {
							// 升序排序
							public int compare(Curvefittingmain o1, Curvefittingmain o2) {
								return o1.getMonthdeviationrate().compareTo(o2.getMonthdeviationrate());
							}

						});
					} else   {
						Collections.sort(ls, new Comparator<Curvefittingmain>() {
							// 升序排序
							public int compare(Curvefittingmain o1, Curvefittingmain o2) {
								return o2.getMonthdeviationrate().compareTo(o1.getMonthdeviationrate());
							}

						});
					}
					break;
				case "yeardeviationrate":
					if (tablepar.getIsAsc().equals("asc")) {

						Collections.sort(ls, new Comparator<Curvefittingmain>() {
							// 升序排序
							public int compare(Curvefittingmain o1, Curvefittingmain o2) {
								return o1.getYeardeviationrate().compareTo(o2.getYeardeviationrate());
							}

						});
					} else   {
						Collections.sort(ls, new Comparator<Curvefittingmain>() {
							// 升序排序
							public int compare(Curvefittingmain o1, Curvefittingmain o2) {
								return o2.getYeardeviationrate().compareTo(o1.getYeardeviationrate());
							}

						});
					}
					break;
				case "standarddeviationrate":
					if (tablepar.getIsAsc().equals("asc")) {

						Collections.sort(ls, new Comparator<Curvefittingmain>() {
							// 升序排序
							public int compare(Curvefittingmain o1, Curvefittingmain o2) {
								return o1.getStandarddeviationrate().compareTo(o2.getStandarddeviationrate());
							}

						});
					} else   {
						Collections.sort(ls, new Comparator<Curvefittingmain>() {
							// 升序排序
							public int compare(Curvefittingmain o1, Curvefittingmain o2) {
								return o2.getStandarddeviationrate().compareTo(o1.getStandarddeviationrate());
							}

						});
					}
					break;
				case "deviationrate1":
					if (tablepar.getIsAsc().equals("asc")) {

						Collections.sort(ls, new Comparator<Curvefittingmain>() {
							// 升序排序
							public int compare(Curvefittingmain o1, Curvefittingmain o2) {
								return o1.getDeviationrate1().compareTo(o2.getDeviationrate1());
							}

						});
					} else   {
						Collections.sort(ls, new Comparator<Curvefittingmain>() {
							// 升序排序
							public int compare(Curvefittingmain o1, Curvefittingmain o2) {
								return o2.getDeviationrate1().compareTo(o1.getDeviationrate1());
							}

						});
					}
					break;
				case "deviationrate2":
					if (tablepar.getIsAsc().equals("asc")) {

						Collections.sort(ls, new Comparator<Curvefittingmain>() {
							// 升序排序
							public int compare(Curvefittingmain o1, Curvefittingmain o2) {
								return o1.getDeviationrate2().compareTo(o2.getDeviationrate2());
							}

						});
					} else   {
						Collections.sort(ls, new Comparator<Curvefittingmain>() {
							// 升序排序
							public int compare(Curvefittingmain o1, Curvefittingmain o2) {
								return o2.getDeviationrate2().compareTo(o1.getDeviationrate2());
							}

						});
					}
					break;
				case "deviationrate3":
					if (tablepar.getIsAsc().equals("asc")) {

						Collections.sort(ls, new Comparator<Curvefittingmain>() {
							// 升序排序
							public int compare(Curvefittingmain o1, Curvefittingmain o2) {
								return o1.getDeviationrate3().compareTo(o2.getDeviationrate3());
							}

						});
					} else   {
						Collections.sort(ls, new Comparator<Curvefittingmain>() {
							// 升序排序
							public int compare(Curvefittingmain o1, Curvefittingmain o2) {
								return o2.getDeviationrate3().compareTo(o1.getDeviationrate3());
							}

						});
					}
					break;
				default:

					break;
			}

		}
		return ls;
	}
}
