package com.gyee.frame.service;

import com.gyee.frame.common.base.BaseService;
import com.gyee.frame.mapper.auto.PartBjCountSubMapper;
import com.gyee.frame.model.auto.Alertrule2;
import com.gyee.frame.model.auto.Alertrule2Example;
import com.gyee.frame.model.auto.PartBjCountSub;
import com.gyee.frame.model.auto.PartBjCountSubExample;
import com.gyee.frame.model.custom.WindturbinebjCountVo;
import com.gyee.frame.util.DateUtils;
import com.gyee.frame.util.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * PartBjCountSubService
 * @Title: PartBjCountSubService.java 
 * @Package com.gyee.frame.service 
 * @author gyee_自动生成
 * @email 1@qq.com
 * @date 2019-12-31 14:58:09  
 **/
@Service
public class PartBjCountSubService implements BaseService<PartBjCountSub, PartBjCountSubExample> {
	@Resource
	private PartBjCountSubMapper partBjCountSubMapper;
	@Resource
	private Alertrule2ervice alertrule2ervice;



	@Override
	public int deleteByPrimaryKey(String ids) {

		return -1;

	}
	
	
	@Override
	public PartBjCountSub selectByPrimaryKey(String id) {
				
			return new PartBjCountSub();
				
	}

	
	@Override
	public int updateByPrimaryKeySelective(PartBjCountSub record) {
		return -1;
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(PartBjCountSub record) {

				
		return partBjCountSubMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(PartBjCountSub record, PartBjCountSubExample example) {
		
		return partBjCountSubMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(PartBjCountSub record, PartBjCountSubExample example) {
		
		return partBjCountSubMapper.updateByExample(record, example);
	}

	@Override
	public List<PartBjCountSub> selectByExample(PartBjCountSubExample example) {
		
		return partBjCountSubMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(PartBjCountSubExample example) {
		
		return partBjCountSubMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(PartBjCountSubExample example) {
		
		return partBjCountSubMapper.deleteByExample(example);
	}



	public List<WindturbinebjCountVo>  getWindturbinebjCountVoList(String lnid, String part,Date recorddate) throws Exception{

		List<WindturbinebjCountVo> vos=new ArrayList<>();

		if( StringUtils.notEmp(recorddate))
		{
			Map<String,Alertrule2> map=new HashMap<>();
			Alertrule2Example example=new Alertrule2Example();
			List<Alertrule2> alertrule2ls=alertrule2ervice.selectByExample(example);
			if(!alertrule2ls.isEmpty())
			{
				for(Alertrule2 ar:alertrule2ls)
				{
					map.put(ar.getId(),ar);
				}
			}


			Date endDate= DateUtils.addDays(recorddate,1);
			List<WindturbinebjCountVo> ls=partBjCountSubMapper.getWindturbinebjCountVoList(lnid, part,recorddate,endDate);
			if(!ls.isEmpty())
			{
				for(WindturbinebjCountVo vo:ls)
				{
					if(map.containsKey(vo.getAlertruleid()))
					{
						vo.setAlertrulename(map.get(vo.getAlertruleid()).getName());
					}

				}
			}


			for (WindturbinebjCountVo wc : ls) {
				if (isExceedingStandard(wc) == true) {
					vos.add(wc);
				}
			}

		}

		return vos;
	}


	private Boolean isExceedingStandard(WindturbinebjCountVo wb) {
		if (wb.getCount() == 0 || wb.getCount() <= wb.getReference()) {
			return false;
		}
		if (("" + wb.getReference()).length() == 1) {
			if (wb.getReference() == 0 && wb.getCount() >= 8) {
				return true;
			} else if (wb.getReference() == 1 && wb.getCount() >= 20) {
				return true;
			} else if (wb.getReference() == 2 && wb.getCount() >= 30) {
				return true;
			} else if (wb.getReference() == 3 && wb.getCount() >= 30) {
				return true;
			} else if (wb.getReference() == 4 && wb.getCount() >= 30) {
				return true;
			} else if (wb.getReference() == 5 && wb.getCount() >= 30) {
				return true;
			} else if (wb.getReference() == 6 && wb.getCount() >= 30) {
				return true;
			} else if (wb.getReference() == 7 && wb.getCount() >= 30) {
				return true;
			} else if (wb.getReference() == 8 && wb.getCount() >= 30) {
				return true;
			} else if (wb.getReference() == 9 && wb.getCount() >= 30) {
				return true;
			} else {
				return false;
			}
		} else if (("" + wb.getReference()).length() == 2) {
			if (getFirstDigit(wb.getReference().intValue()) == 1
					&& wb.getCount() >= (wb.getReference() * 3)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 2
					&& wb.getCount() >= (wb.getReference() * 2)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 3
					&& wb.getCount() >= (wb.getReference() * 1.5)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 4
					&& wb.getCount() >= (wb.getReference() * 1.4)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 5
					&& wb.getCount() >= (wb.getReference() * 1.3)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 6
					&& wb.getCount() >= (wb.getReference() * 1.3)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 7
					&& wb.getCount() >= (wb.getReference() * 1.3)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 8
					&& wb.getCount() >= (wb.getReference() * 1.3)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 9
					&& wb.getCount() >= (wb.getReference() * 1.3)) {
				return true;
			} else {
				return false;
			}
		} else if (("" + wb.getReference()).length() == 3) {
			if (getFirstDigit(wb.getReference().intValue()) == 1
					&& wb.getCount() >= (wb.getReference() * 2)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 2
					&& wb.getCount() >= (wb.getReference() * 1.5)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 3
					&& wb.getCount() >= (wb.getReference() * 1.4)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 4
					&& wb.getCount() >= (wb.getReference() * 1.3)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 5
					&& wb.getCount() >= (wb.getReference() * 1.3)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 6
					&& wb.getCount() >= (wb.getReference() * 1.3)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 7
					&& wb.getCount() >= (wb.getReference() * 1.3)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 8
					&& wb.getCount() >= (wb.getReference() * 1.3)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 9
					&& wb.getCount() >= (wb.getReference() * 1.3)) {
				return true;
			} else {
				return false;
			}
		} else {
			if (getFirstDigit(wb.getReference().intValue()) == 1
					&& wb.getCount() >= (wb.getReference() * 1.9)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 2
					&& wb.getCount() >= (wb.getReference() * 1.5)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 3
					&& wb.getCount() >= (wb.getReference() * 1.4)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 4
					&& wb.getCount() >= (wb.getReference() * 1.3)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 5
					&& wb.getCount() >= (wb.getReference() * 1.2)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 6
					&& wb.getCount() >= (wb.getReference() * 1.2)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 7
					&& wb.getCount() >= (wb.getReference() * 1.2)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 8
					&& wb.getCount() >= (wb.getReference() * 1.2)) {
				return true;
			} else if (getFirstDigit(wb.getReference().intValue()) == 9
					&& wb.getCount() >= (wb.getReference() * 1.2)) {
				return true;
			} else {
				return false;
			}
		}

	}

	private int getFirstDigit(int number) {

		number = Math.abs(number);

		if (number < 10) {

			return number;
		}
		return getFirstDigit((number - (number % 10)) / 10);

	}
}
