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.WarningRecordsMapper;
import com.gyee.frame.model.auto.*;
import com.gyee.frame.model.custom.SawVo;
import com.gyee.frame.model.custom.SimpleVo;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


@Service
public class WarningRecordsService implements BaseService<WarningRecords, WarningRecordsExample> {
	@Resource
	private WarningRecordsMapper warningRecordsMapper;
	

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

	
	@Override
	public int updateByPrimaryKeySelective(WarningRecords record) {
		return warningRecordsMapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(WarningRecords record) {
				
			
				
		return warningRecordsMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(WarningRecords record, WarningRecordsExample example) {
		
		return warningRecordsMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(WarningRecords record, WarningRecordsExample example) {
		
		return warningRecordsMapper.updateByExample(record, example);
	}

	@Override
	public List<WarningRecords> selectByExample(WarningRecordsExample example) {
		
		return warningRecordsMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(WarningRecordsExample example) {
		
		return warningRecordsMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(WarningRecordsExample example) {
		
		return warningRecordsMapper.deleteByExample(example);
	}

	public PageInfo<WarningRecords> getWarningRecordsList(Tablepar tablepar, String wtId, Date beginDate,Date endDate) {

		WarningRecordsExample example = new WarningRecordsExample();

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


		PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());

		WarningRecordsExample.Criteria criteria = example.createCriteria();
		if (StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate)) {

			criteria.andWarningtimeGreaterThanOrEqualTo(beginDate).andWarningtimeLessThan(endDate);
		}

		if (StringUtils.notEmp(wtId)) {
			criteria.andWindturbineidEqualTo(wtId);
		}

		List<WarningRecords> list = warningRecordsMapper.selectByExample(example);

		if(!list.isEmpty())
		{
			for(WarningRecords wr:list)
			{
				if(StringUtils.notEmp(wr.getWarningid()) && InitialRunner.warningmap.containsKey(wr.getWarningid()))
				{
					Warning warning2=InitialRunner.warningmap.get(wr.getWarningid());
					wr.setWarnDesc(warning2.getChinesetext());
				}
			}
		}

		PageInfo<WarningRecords> pageInfo = new PageInfo<WarningRecords>(list);

		return pageInfo;
	}



	public List<SawVo> getWarningRecordsTop5(Tablepar tablepar, String wtId, Date beginDate, Date endDate) {

		List<SawVo> sawlist=new ArrayList<>();
		List<SawVo> templist=new ArrayList<>();
		WarningRecordsExample example = new WarningRecordsExample();

		WarningRecordsExample.Criteria criteria = example.createCriteria();
		if (StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate)) {

			criteria.andWarningtimeGreaterThanOrEqualTo(beginDate).andWarningtimeLessThan(endDate);
		}

		if (StringUtils.notEmp(wtId)) {
			criteria.andWindturbineidEqualTo(wtId);
		}

		List<WarningRecords> list = warningRecordsMapper.selectByExample(example);
		Map<String, List<WarningRecords>> lsMap = new LinkedHashMap<>();
		if(!list.isEmpty())
		{
			for(WarningRecords wr:list)
			{

				List<WarningRecords> items=null;
				if (lsMap.containsKey(wr.getWarningid())) {
					items = lsMap.get(wr.getWarningid());
					items.add(wr);
					lsMap.put(wr.getWarningid(), items);
				} else {
					items = new ArrayList<>();
					items.add(wr);
					lsMap.put(wr.getWarningid(), items);
				}

			}

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

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

				Warning warning= InitialRunner.warningmap.get(entry.getKey());
				if(null == warning){
					continue;
				}
				SawVo item = new SawVo();
				item.setNum(i++);
				item.setName(warning.getChinesetext());
				item.setFrequency(ls.size());

				templist.add(item);
			}


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

			});

			i=0;
			for(SawVo vo:templist)
			{
				if(i<5)
				{
					sawlist.add(vo);
				}
				i++;
			}

			sawlist=sortStatisticalAnalysisWarningTopVoList(tablepar,sawlist);
		}

		return sawlist;
	}
	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 WarningRecords getWarningRecords(String wtId) {

		WarningRecords vo=new WarningRecords();
		WarningRecordsExample example = new WarningRecordsExample();

		example.setOrderByClause("warningtime desc");


		PageHelper.startPage(0, 1);

		WarningRecordsExample.Criteria criteria = example.createCriteria();


		if (StringUtils.notEmp(wtId)) {
			criteria.andWindturbineidEqualTo(wtId);
		}

		List<WarningRecords> list = warningRecordsMapper.selectByExample(example);


		PageInfo<WarningRecords> pageInfo = new PageInfo<WarningRecords>(list);

		if(null!=pageInfo && !pageInfo.getList().isEmpty())
		{
			vo=pageInfo.getList().get(0);
		}

		return vo;
	}
	/**
	 * 获取七天内各个风场报警统计
	 * @return
	 */
	public List<SimpleVo> countWpwarnByWp() {

		List<SimpleVo> vos=new ArrayList<>();
		List<SimpleVo> ls= warningRecordsMapper.countWpwarnByWp();
		if(!ls.isEmpty())
		{
			Map<String,SimpleVo> map=new HashMap<>();
			for(SimpleVo vo:ls)
			{
				map.put(vo.getId(),vo);
			}

			for(Windpowerstation wp:InitialRunner.wpls)
			{
				SimpleVo vo=new SimpleVo();
				vo.setId(wp.getId());
				vo.setName(wp.getName());
				vo.setValue(0);
				if(map.containsKey(wp.getId()))
				{
					SimpleVo votemp=map.get(wp.getId());
					vo.setValue(votemp.getValue());
				}
				vos.add(vo);
			}
		}
		return vos;

	}

	/**
	 * 获取七天内各个项目报警统计
	 * @return
	 */
	public List<SimpleVo> countWpwarnByPj() {

		List<SimpleVo> vos=new ArrayList<>();
		List<SimpleVo> ls= warningRecordsMapper.countWpwarnByPj();
		if(!ls.isEmpty())
		{
			Map<String,SimpleVo> map=new HashMap<>();
			for(SimpleVo vo:ls)
			{
				map.put(vo.getId(),vo);
			}

			for(Project pj:InitialRunner.pjls)
			{
				SimpleVo vo=new SimpleVo();
				vo.setId(pj.getId());
				vo.setName(pj.getName());
				vo.setValue(0);
				if(map.containsKey(pj.getId()))
				{
					SimpleVo votemp=map.get(pj.getId());
					vo.setValue(votemp.getValue());
				}
				vos.add(vo);
			}
		}
		return vos;

	}
}
