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.WarningInfoDayMapper;
import com.gyee.frame.model.auto.*;
import com.gyee.frame.model.custom.StatisticalAnalysisWarningTopVo;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;


@Service
public class WarningInfoDayService implements BaseService<WarningInfoDay, WarningInfoDayExample> {
	@Autowired
	private WarningInfoDayMapper warningInfoDayMapper;
	

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

	
	@Override
	public int updateByPrimaryKeySelective(WarningInfoDay record) {
		return warningInfoDayMapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(WarningInfoDay record) {
				
			
				
		return warningInfoDayMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(WarningInfoDay record, WarningInfoDayExample example) {
		
		return warningInfoDayMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(WarningInfoDay record, WarningInfoDayExample example) {
		
		return warningInfoDayMapper.updateByExample(record, example);
	}

	@Override
	public List<WarningInfoDay> selectByExample(WarningInfoDayExample example) {
		
		return warningInfoDayMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(WarningInfoDayExample example) {
		
		return warningInfoDayMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(WarningInfoDayExample example) {
		
		return warningInfoDayMapper.deleteByExample(example);
	}


	public List<StatisticalAnalysisWarningTopVo> getWarningInfoDayByWarningid(Tablepar tablepar,Date beginDate, Date endDate)  {

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

		WarningInfoDayExample example=new WarningInfoDayExample();
		example.setOrderByClause(" frequencyday desc");


		WarningInfoDayExample.Criteria criteria =example.createCriteria();

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

		List<WarningInfoDay> wils=warningInfoDayMapper.selectByExample(example);

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

			}


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

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

				Warning2 warning= InitialRunner.warningmap.get(entry.getKey());

				StatisticalAnalysisWarningTopVo item = new StatisticalAnalysisWarningTopVo();
				item.setNum(i++);
				item.setName(warning.getChinesetext());
				int frequencyday=ls.stream().mapToInt(WarningInfoDay::getFrequencyday).sum();//合计
				double totalhoursday=ls.stream().mapToDouble(WarningInfoDay::getTotalhoursday).sum();//合计
				item.setFrequency(frequencyday);
				item.setFrequencyday(frequencyday);
				item.setTotalhours(totalhoursday);

				if(i<10)
				{
					list.add(item);
				}else
				{
					break;
				}
			}

			list=sortStatisticalAnalysisWarningTopVoList(tablepar,list);
		}
		return list;

	}


	public List<StatisticalAnalysisWarningTopVo> getWarningInfoDayByWarningClassify(Tablepar tablepar,Date beginDate, Date endDate)  {

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

		WarningInfoDayExample example=new WarningInfoDayExample();
		example.setOrderByClause("frequencyday desc");


		WarningInfoDayExample.Criteria criteria =example.createCriteria();

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

		List<WarningInfoDay> wils=warningInfoDayMapper.selectByExample(example);

		Map<String, List<WarningInfoDay>> lsMap = new LinkedHashMap<String, List<WarningInfoDay>>();
		if(!wils.isEmpty())
		{
			for(WarningInfoDay ap:wils)
			{
				Warning2 warning=InitialRunner.warningmap.get(ap.getWarningid());
				WarningClassify warningClassify=InitialRunner.warningClassifymap.get(warning.getWarningclassifyid());
				List<WarningInfoDay> items=null;

				if (lsMap.containsKey(warningClassify.getName())) {
					items = lsMap.get(warningClassify.getName());
					items.add(ap);
					lsMap.put(warningClassify.getName(), items);
				} else {
					items = new ArrayList<>();
					items.add(ap);
					lsMap.put(warningClassify.getName(), items);
				}

			}


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

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

				Warning2 warning= InitialRunner.warningmap.get(entry.getKey());

				StatisticalAnalysisWarningTopVo item = new StatisticalAnalysisWarningTopVo();
				item.setNum(i++);
				item.setName(entry.getKey());
				int frequencyday=ls.stream().mapToInt(WarningInfoDay::getFrequencyday).sum();//合计
				double totalhoursday=ls.stream().mapToDouble(WarningInfoDay::getTotalhoursday).sum();//合计
				item.setFrequency(frequencyday);
				item.setFrequencyday(frequencyday);
				item.setTotalhours(totalhoursday);

				if(i<10)
				{
					list.add(item);
				}else
				{
					break;
				}
			}
			list=sortStatisticalAnalysisWarningTopVoList(tablepar,list);
		}
		return list;

	}


	public  List<StatisticalAnalysisWarningTopVo> sortStatisticalAnalysisWarningTopVoList(Tablepar tablepar,List<StatisticalAnalysisWarningTopVo> ls)
	{

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

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

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

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

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

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

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

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

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

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

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

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

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

						});
					}
					break;
				default:

					break;
			}

		}
		return ls;
	}


	public List<StatisticalAnalysisWarningTopVo> getWarningInfoDayByManufacturer(Tablepar tablepar,Date beginDate, Date endDate)  {

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

		WarningInfoDayExample example=new WarningInfoDayExample();
		example.setOrderByClause("frequencyday desc");


		WarningInfoDayExample.Criteria criteria =example.createCriteria();

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

		List<WarningInfoDay> wils=warningInfoDayMapper.selectByExample(example);

		Map<String, List<WarningInfoDay>> lsMap = new LinkedHashMap<String, List<WarningInfoDay>>();
		if(!wils.isEmpty())
		{
			for(WarningInfoDay ap:wils)
			{
				Manufacturer manufacturer=InitialRunner.manufacturermap.get(ap.getManufacturerid());

				List<WarningInfoDay> items=null;

				if (lsMap.containsKey(manufacturer.getName())) {
					items = lsMap.get(manufacturer.getName());
					items.add(ap);
					lsMap.put(manufacturer.getName(), items);
				} else {
					items = new ArrayList<>();
					items.add(ap);
					lsMap.put(manufacturer.getName(), items);
				}

			}


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

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

				Warning2 warning= InitialRunner.warningmap.get(entry.getKey());

				StatisticalAnalysisWarningTopVo item = new StatisticalAnalysisWarningTopVo();
				item.setNum(i++);
				item.setName(entry.getKey());
				int frequencyday=ls.stream().mapToInt(WarningInfoDay::getFrequencyday).sum();//合计
				double totalhoursday=ls.stream().mapToDouble(WarningInfoDay::getTotalhoursday).sum();//合计
				item.setFrequency(frequencyday);
				item.setFrequencyday(frequencyday);
				item.setTotalhours(totalhoursday);

				if(i<10)
				{
					list.add(item);
				}else
				{
					break;
				}
			}

			list=sortStatisticalAnalysisWarningTopVoList(tablepar,list);
		}
		return list;

	}

	/*****************************************************************************************************/



	public List<StatisticalAnalysisWarningTopVo> getWarningInfoDayByWarningidPercentage(Date beginDate, Date endDate)  {

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

		WarningInfoDayExample example=new WarningInfoDayExample();
		example.setOrderByClause("frequencyday desc");


		WarningInfoDayExample.Criteria criteria =example.createCriteria();

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

		List<WarningInfoDay> wils=warningInfoDayMapper.selectByExample(example);

		Map<String, List<WarningInfoDay>> lsMap = new LinkedHashMap<String, List<WarningInfoDay>>();
		if(!wils.isEmpty())
		{

			int total=wils.stream().mapToInt(WarningInfoDay::getFrequencyday).sum();//总合计
			for(WarningInfoDay ap:wils)
			{
				List<WarningInfoDay> items=null;
				if (lsMap.containsKey(ap.getWarningid())) {
					items = lsMap.get(ap.getWarningid());
					items.add(ap);
					lsMap.put(ap.getWarningid(), items);
				} else {
					items = new ArrayList<>();
					items.add(ap);
					lsMap.put(ap.getWarningid(), items);
				}

			}


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

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

				Warning2 warning= InitialRunner.warningmap.get(entry.getKey());

				StatisticalAnalysisWarningTopVo item = new StatisticalAnalysisWarningTopVo();
				item.setNum(i++);
				item.setName(warning.getChinesetext());
				int frequencyday=ls.stream().mapToInt(WarningInfoDay::getFrequencyday).sum();//合计
				double totalhoursday=ls.stream().mapToDouble(WarningInfoDay::getTotalhoursday).sum();//合计
				item.setFrequency(frequencyday);
				item.setFrequencyday(frequencyday);
				item.setTotalhours(totalhoursday);

				if(i<10)
				{
					list.add(item);
				}else
				{
					break;
				}
			}

			int sum = 0;
			for (StatisticalAnalysisWarningTopVo item : list) {
				sum += item.getFrequency();
				if (total != 0) {
					BigDecimal b1 = new BigDecimal(item.getFrequency());
					BigDecimal b2 = new BigDecimal(total);

					item.setTotalhours(b1.divide(b2, 2, RoundingMode.HALF_EVEN).doubleValue());
				} else {
					item.setTotalhours(0d);
				}
			}

			if (sum != total) {
				StatisticalAnalysisWarningTopVo item = new StatisticalAnalysisWarningTopVo();
				item.setNum(i++);
				item.setName("其他");
				item.setFrequency(sum);
				if (total != 0) {
					BigDecimal b1 = new BigDecimal(item.getFrequency());
					BigDecimal b2 = new BigDecimal(total);

					item.setTotalhours(b1.divide(b2, 2, RoundingMode.HALF_EVEN).doubleValue());
				}
				list.add(item);
			}

		}
		return list;

	}


	public List<StatisticalAnalysisWarningTopVo> getWarningInfoDayByWarningClassifyPercentage(Date beginDate, Date endDate)  {

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

		WarningInfoDayExample example=new WarningInfoDayExample();
		example.setOrderByClause("frequencyday desc");


		WarningInfoDayExample.Criteria criteria =example.createCriteria();

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

		List<WarningInfoDay> wils=warningInfoDayMapper.selectByExample(example);

		Map<String, List<WarningInfoDay>> lsMap = new LinkedHashMap<String, List<WarningInfoDay>>();
		if(!wils.isEmpty())
		{
			int total=wils.stream().mapToInt(WarningInfoDay::getFrequencyday).sum();//总合计
			for(WarningInfoDay ap:wils)
			{
				Warning2 warning=InitialRunner.warningmap.get(ap.getWarningid());
				WarningClassify warningClassify=InitialRunner.warningClassifymap.get(warning.getWarningclassifyid());
				List<WarningInfoDay> items=null;

				if (lsMap.containsKey(warningClassify.getName())) {
					items = lsMap.get(warningClassify.getName());
					items.add(ap);
					lsMap.put(warningClassify.getName(), items);
				} else {
					items = new ArrayList<>();
					items.add(ap);
					lsMap.put(warningClassify.getName(), items);
				}

			}


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

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

				Warning2 warning= InitialRunner.warningmap.get(entry.getKey());

				StatisticalAnalysisWarningTopVo item = new StatisticalAnalysisWarningTopVo();
				item.setNum(i++);
				item.setName(entry.getKey());
				int frequencyday=ls.stream().mapToInt(WarningInfoDay::getFrequencyday).sum();//合计
				double totalhoursday=ls.stream().mapToDouble(WarningInfoDay::getTotalhoursday).sum();//合计
				item.setFrequency(frequencyday);
				item.setFrequencyday(frequencyday);
				item.setTotalhours(totalhoursday);

				if(i<10)
				{
					list.add(item);
				}else
				{
					break;
				}
			}

			int sum = 0;
			for (StatisticalAnalysisWarningTopVo item : list) {
				sum += item.getFrequency();
				if (total != 0) {
					BigDecimal b1 = new BigDecimal(item.getFrequency());
					BigDecimal b2 = new BigDecimal(total);

					item.setTotalhours(b1.divide(b2, 2, RoundingMode.HALF_EVEN).doubleValue());
				} else {
					item.setTotalhours(0d);
				}
			}

			if (sum != total) {
				StatisticalAnalysisWarningTopVo item = new StatisticalAnalysisWarningTopVo();
				item.setNum(i++);
				item.setName("其他");
				item.setFrequency(sum);
				if (total != 0) {
					BigDecimal b1 = new BigDecimal(item.getFrequency());
					BigDecimal b2 = new BigDecimal(total);

					item.setTotalhours(b1.divide(b2, 2, RoundingMode.HALF_EVEN).doubleValue());
				}
				list.add(item);
			}

		}
		return list;

	}




	public List<StatisticalAnalysisWarningTopVo> getWarningInfoDayByManufacturerPercentage(Date beginDate, Date endDate)  {

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

		WarningInfoDayExample example=new WarningInfoDayExample();
		example.setOrderByClause("frequencyday desc");


		WarningInfoDayExample.Criteria criteria =example.createCriteria();

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

		List<WarningInfoDay> wils=warningInfoDayMapper.selectByExample(example);

		Map<String, List<WarningInfoDay>> lsMap = new LinkedHashMap<String, List<WarningInfoDay>>();
		if(!wils.isEmpty())
		{
			int total=wils.stream().mapToInt(WarningInfoDay::getFrequencyday).sum();//总合计
			for(WarningInfoDay ap:wils)
			{
				Manufacturer manufacturer=InitialRunner.manufacturermap.get(ap.getManufacturerid());

				List<WarningInfoDay> items=null;

				if (lsMap.containsKey(manufacturer.getName())) {
					items = lsMap.get(manufacturer.getName());
					items.add(ap);
					lsMap.put(manufacturer.getName(), items);
				} else {
					items = new ArrayList<>();
					items.add(ap);
					lsMap.put(manufacturer.getName(), items);
				}

			}


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

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

				Warning2 warning= InitialRunner.warningmap.get(entry.getKey());

				StatisticalAnalysisWarningTopVo item = new StatisticalAnalysisWarningTopVo();
				item.setNum(i++);
				item.setName(entry.getKey());
				int frequencyday=ls.stream().mapToInt(WarningInfoDay::getFrequencyday).sum();//合计
				double totalhoursday=ls.stream().mapToDouble(WarningInfoDay::getTotalhoursday).sum();//合计
				item.setFrequency(frequencyday);
				item.setFrequencyday(frequencyday);
				item.setTotalhours(totalhoursday);

				if(i<10)
				{
					list.add(item);
				}else
				{
					break;
				}

			}

			int sum = 0;
			for (StatisticalAnalysisWarningTopVo item : list) {
				sum += item.getFrequency();
				if (total != 0) {
					BigDecimal b1 = new BigDecimal(item.getFrequency());
					BigDecimal b2 = new BigDecimal(total);

					item.setTotalhours(b1.divide(b2, 2, RoundingMode.HALF_EVEN).doubleValue());
				} else {
					item.setTotalhours(0d);
				}
			}

			if (sum != total) {
				StatisticalAnalysisWarningTopVo item = new StatisticalAnalysisWarningTopVo();
				item.setNum(i++);
				item.setName("其他");
				item.setFrequency(sum);
				if (total != 0) {
					BigDecimal b1 = new BigDecimal(item.getFrequency());
					BigDecimal b2 = new BigDecimal(total);

					item.setTotalhours(b1.divide(b2, 2, RoundingMode.HALF_EVEN).doubleValue());
				}
				list.add(item);
			}
		}
		return list;

	}
}
