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.WarningInfoDayMapper;
import com.gyee.frame.model.auto.*;
import com.gyee.frame.model.custom.SawVo;
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.*;


@Service
public class WarningInfoDayService implements BaseService<WarningInfoDay, WarningInfoDayExample> {
	@Resource
	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<SawVo> getWarningInfoDayByWarningid(Tablepar tablepar, Date beginDate, Date endDate)  {

		List<SawVo> list =new ArrayList<>();;
		String order =null;
		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 ");
			}
			order=String.valueOf(sb);
		}else {
			order=" frequencyday desc";
		}
		PageHelper.startPage(0, 10);
		list =warningInfoDayMapper.getWarningInfoDayByWarningid(DateUtils.formatDateTime(beginDate),DateUtils.formatDateTime(endDate) ,order);
		PageInfo<SawVo> pageInfo = new PageInfo<SawVo>(list);
		return list;

	}


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

		List<SawVo> list =new ArrayList<>();;
		String order =null;
		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 ");
			}
			order=String.valueOf(sb);
		}else {
			order=" frequencyday desc";
		}
		PageHelper.startPage(0, 10);
		list =warningInfoDayMapper.getWarningInfoDayByWarningClassify(DateUtils.formatDateTime(beginDate),DateUtils.formatDateTime(endDate),order);
		PageInfo<SawVo> pageInfo = new PageInfo<SawVo>(list);
		return list;

	}


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

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

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

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

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

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

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

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

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

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

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

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

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

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

						});
					}
					break;
				default:

					break;
			}

		}
		return ls;
	}


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


		List<SawVo> list =new ArrayList<>();;
		String order =null;
		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 ");
			}
			order=String.valueOf(sb);
		}else {
			order=" frequencyday desc";
		}
		PageHelper.startPage(0, 10);
		list =warningInfoDayMapper.getWarningInfoDayByManufacturer(DateUtils.formatDateTime(beginDate),DateUtils.formatDateTime(endDate),order);
		PageInfo<SawVo> pageInfo = new PageInfo<SawVo>(list);
		return list;

	}

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



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

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

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


		WarningInfoDayExample.Criteria criteria =example.createCriteria();

		criteria.andRecorddateGreaterThanOrEqualTo(beginDate).andRecorddateLessThan(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();

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

				SawVo item = new SawVo();
				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 (SawVo 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) {
				SawVo item = new SawVo();
				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<SawVo> getWarningInfoDayByWarningClassifyPercentage(Date beginDate, Date endDate)  {

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

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


		WarningInfoDayExample.Criteria criteria =example.createCriteria();

		criteria.andRecorddateGreaterThanOrEqualTo(beginDate).andRecorddateLessThan(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)
			{
				Warning warning=null;
				if(InitialRunner.warningmap.containsKey(ap.getWarningid()))
				{
					warning=InitialRunner.warningmap.get(ap.getWarningid());
				}

				List<WarningInfoDay> items=null;
				if(StringUtils.notEmp(warning) && StringUtils.notEmp(warning.getWarningclassifyid()) && !warning.getWarningclassifyid().equals(""))
				{
					WarningClassify warningClassify=null;
					if(InitialRunner.warningClassifymap.containsKey(warning.getWarningclassifyid()))
					{
						warningClassify=InitialRunner.warningClassifymap.get(warning.getWarningclassifyid());
					}
					if(StringUtils.notEmp(warningClassify))
					{
						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();

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

				SawVo item = new SawVo();
				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 (SawVo 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) {
				SawVo item = new SawVo();
				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<SawVo> getWarningInfoDayByManufacturerPercentage(Date beginDate, Date endDate)  {

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

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


		WarningInfoDayExample.Criteria criteria =example.createCriteria();

		criteria.andRecorddateGreaterThanOrEqualTo(beginDate).andRecorddateLessThan(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=null;
				if(InitialRunner.manufacturermap.containsKey(ap.getManufacturerid()))
				{
					manufacturer=InitialRunner.manufacturermap.get(ap.getManufacturerid());
				}

				if(null!=manufacturer)
				{
					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();

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

				SawVo item = new SawVo();
				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 (SawVo 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) {
				SawVo item = new SawVo();
				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;

	}


}
