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.ParttemperaturesubMapper;
import com.gyee.frame.model.auto.Parttemperaturesub;
import com.gyee.frame.model.auto.ParttemperaturesubExample;
import com.gyee.frame.model.custom.YxfxVo;
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 ParttemperaturesubService implements BaseService<Parttemperaturesub, ParttemperaturesubExample> {
	@Resource
	private ParttemperaturesubMapper parttemperaturesubMapper;
	

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

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

	
	@Override
	public int updateByExample(Parttemperaturesub record, ParttemperaturesubExample example) {
		
		return parttemperaturesubMapper.updateByExample(record, example);
	}

	@Override
	public List<Parttemperaturesub> selectByExample(ParttemperaturesubExample example) {
		
		return parttemperaturesubMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(ParttemperaturesubExample example) {
		
		return parttemperaturesubMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(ParttemperaturesubExample example) {
		
		return parttemperaturesubMapper.deleteByExample(example);
	}


	public List<Parttemperaturesub> getParttemperaturesubListBywtId(String wtId, Date recorddate) {

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

		if (StringUtils.notEmp(wtId) && StringUtils.notEmp(recorddate) ) {

			ParttemperaturesubExample example=new ParttemperaturesubExample();

			example.setOrderByClause("recodedate DESC");

			ParttemperaturesubExample.Criteria criteria =example.createCriteria();

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

			criteria.andRecodedateGreaterThanOrEqualTo(recorddate).andRecodedateLessThan(endDate);

			criteria.andWtidEqualTo(wtId);

			list= parttemperaturesubMapper.selectByExample(example);

		}
		return list;

	}


	public List<YxfxVo> getListValue(List<Parttemperaturesub> ls) {

		Map<String, Map<String, List<Parttemperaturesub>>> ptmap = new HashMap<>();

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

		if (!ls.isEmpty()) {
			for (Parttemperaturesub sub : ls) {
				if (ptmap.containsKey(sub.getTrange())) {
					Map<String, List<Parttemperaturesub>> map = ptmap.get(sub.getTrange());
					if (map.containsKey(sub.getPartname())) {
						List<Parttemperaturesub> subls = map.get(sub.getPartname());
						subls.add(sub);
					} else {
						List<Parttemperaturesub> subls = new ArrayList<>();
						subls.add(sub);
						map.put(sub.getPartname(), subls);
					}
					ptmap.put(sub.getTrange(), map);
				} else {
					Map<String, List<Parttemperaturesub>> map = new HashMap<>();
					List<Parttemperaturesub> subls = new ArrayList<>();
					subls.add(sub);
					map.put(sub.getPartname(), subls);
					ptmap.put(sub.getTrange(), map);
				}
			}
		}

		Set<Map.Entry<String, Map<String, List<Parttemperaturesub>>>> ptset = ptmap.entrySet();
		//按照温度区间》测点》统计数量
		for (Iterator<Map.Entry<String, Map<String, List<Parttemperaturesub>>>> ptit = ptset.iterator(); ptit.hasNext();) {

			Map.Entry<String, Map<String, List<Parttemperaturesub>>> ptentry = (Map.Entry<String, Map<String, List<Parttemperaturesub>>>) ptit.next();

			Map<String, List<Parttemperaturesub>> map=ptentry.getValue();
			YxfxVo sub=new YxfxVo();
			sub.setName(ptentry.getKey());
			setVoValaue(map, sub,"clx");
			setVoValaue(map, sub,"fdj");
			setVoValaue(map, sub,"zk");
			setVoValaue(map, sub,"bj");
			sub.setNumzz(0);
			sub.setNumph(0);
			sub.setNumbpq(0);
			sub.setNumjc(0);
			list.add(sub);
		}
		Collections.sort(list, new Comparator<YxfxVo>() {
			public int compare(YxfxVo arg0, YxfxVo arg1) {
				return arg0.getName().compareTo(arg1.getName());
			}
		});
		return list;
	}


	private void setVoValaue(Map<String, List<Parttemperaturesub>> map, YxfxVo sub,String key) {
		if(map.containsKey(key))
		{
			int num=0;
			List<Parttemperaturesub> subls=map.get(key);
			for(Parttemperaturesub s:subls)
			{
				num=num+s.getTsnumber();
			}
			switch (key) {
				case "clx":
					sub.setNumclx(num);
					break;
				case "fdj":
					sub.setNumfdj(num);
					break;
				case "zk":
					sub.setNumzk(num);
					break;
				case "bj":
					sub.setNumbj(num);
					break;
				default:
					break;
			}

		}else
		{
			switch (key) {
				case "clx":
					sub.setNumclx(0);
					break;
				case "fdj":
					sub.setNumfdj(0);
					break;
				case "zk":
					sub.setNumzk(0);
					break;
				case "bj":
					sub.setNumbj(0);
					break;
				default:
					break;
			}
		}
	}

}
