package com.huatek.busi.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class AlgorithmSingleton {

	private static AlgorithmSingleton signleton = new AlgorithmSingleton();

	private AlgorithmSingleton() {}

	public static AlgorithmSingleton getInstance() {
		return signleton;
	}
	
	
	public static double basicsvalue= 273d;
	
	public static double basicsTemperatur =20d;
	
	/**************************************SF6 压力折算/灵敏度计算 Begin***************************************************/
	/*
	 *  X --> 实测温度℃  
	* 			 P1 --> X ℃温度下实测压力   
	* 			 Pa -->计算后折算到 20℃ 的压力
	* 			 Pb --> 基础值
	*            value --> 计算后的灵敏度值
	* @formula  
	*        	Pa =  P1*((273+X)/(273+20))
	* 		  	value = ((Pa-Pb)/pb)*100%
	 * 
	 * */
	
	/** 
	* @Title: conversionPressure 
	* @Description: 折算到20℃ 的压力
	* @createDate: Jul 23, 2019 10:05:13 AM
	* @param    actualTemperature  实测温度
	* @param    actualPressure     实测压力值
	* @param    basicsPressure     基础压力值
	* @return   String  
	* @mark  
	* 			 X --> 实测温度℃  
	* 			 P1 --> X ℃温度下实测压力   
	* 			 Pa -->计算后折算到 20℃ 的压力
	* @formula  
	*        	 Pa =  P1*((273+X)/(273+20))
	* 		  	
	* 
	*/ 
	public double conversionPressure(double actualTemperature , double  actualPressure){
		double  d1 = basicsvalue+actualTemperature;
		double  d2 = basicsvalue+basicsTemperatur;
		double  d3 = d1/d2;
		double  d4 = actualPressure*d3;
		return d4 ;
	}
	
	
	/** 
	* @Title: calcSensitivity 
	* @Description: 灵敏度值(压力) 
	* @createDate: Jul 23, 2019 10:05:13 AM
	* @param    conversionPressure    折算到20℃ 的压力
	* @param    basicsPressure        基础压力值
	* @return   double  
	* @mark  
	* 			 Pa -->计算后折算到 20℃ 的压力
	* 			 Pb --> 基础值
	*            value --> 计算后的灵敏度值
	* @formula  
	* 		  	value = ((Pa-Pb)/pb)*100%
	* 
	*/ 
	public String calcSensitivity( double  conversionPressure,  double basicsPressure){
		double  d1 = conversionPressure-basicsPressure;
		double  d2 = d1/basicsPressure;
		DecimalFormat df = new DecimalFormat("0.00%"); 
		return df.format(d2) ;
	}
	
	
	public String calcSensitivity(double actualTemperature , double  actualPressure, double basicsPressure){
		double  d1 = basicsvalue+actualTemperature;
		double  d2 = basicsvalue+basicsTemperatur;
		double  d3 = d1/d2;
		double  d4 = actualPressure*d3;
		double  d5 = d4-basicsPressure;
		double  d6 = d5/basicsPressure;
		DecimalFormat df = new DecimalFormat("0.00%"); 
		return df.format(d6) ;
	}
	
	
	/**************************************SF6 压力折算/灵敏度计算  End***************************************************/
	
	
	/**************************************计算稳定性指标  Begin***************************************************/
	/** 
	* @Title: calcStability 
	* @Description: 计算稳定性指标 
	* @createDate: Jul 23, 2019 4:19:53 PM
	* @param    list  采集的数据
	* @return   String    
	*/ 
	public String calcStability(List<Double> list){
		int size = list.size();
		if(size < 1){
			return null ;
		}
		double count = Double.valueOf(size -1 );
		double total = 0d;
		for (Double value : list) {
			if(null != value){
				total = total+value;
			}	
		}
		if(total == 0d) {
			return "0.00%" ;
		}
		double avg = total/count;
		if(avg == 0d) {
			return "0.00%" ;
		}
		double sum = 0d;
		for (Double value : list) {
			if(null != value && value != 0d){
				double subtract = value-avg;
				double tmp = Math.pow(subtract, 2.0d);
				sum = sum+tmp;
			}	
		}
		
		double sqrt = Math.sqrt(sum);
		double tmp1 = sqrt/count;
		double tmp2 = 1d/avg;
		double tmp3 = tmp1*tmp2;
		DecimalFormat df = new DecimalFormat("0.00%"); 
		return df.format(tmp3)   ;
	}
	
	/**************************************计算稳定性指标  End***************************************************/	
	
	
	
	/** 
	* @Title: repairValue 
	* @Description: 修匀算法 
	* @createDate: Jul 23, 2019 6:40:03 PM
	* @param    list
	* @return   void    
	*/ 
	public static  double repairValue (List<Double> list){
		int size = list.size();
		if(size < 1){
			return  0;
		}
		
		List<Double> calclist = new ArrayList<>();
		double coefficient = 2d/(size+1);
		double coefficient2 =1d- coefficient;
		
		if(list.size() == 1){
			calclist.add(list.get(0));
		}else if(list.size() > 1){
			double p =0d;
			for(int i= 0; i< list.size(); i++){
				if(i == 0){
					double multiply = coefficient*(list.get(i));
					double multiply2 = coefficient2*(list.get(i+1));
					p = multiply+multiply2;
					calclist.add(p);
				}else{
					double multiply = coefficient*(list.get(i));
					double multiply2 = coefficient2*p;
					p = multiply+multiply2;
					calclist.add(p);
				}
			}
		}
		double sum = 0d;
		for( double value: calclist){
			sum = sum+value;
			log.debug("修正值:"+ value);
		}
		
		double avg =sum/size;
		log.debug("平均值:"+ avg);
		return avg;
	}
	
	public static void main(String [] args){
		DecimalFormat df = new DecimalFormat("0.00%");  
		BigDecimal d=new BigDecimal(0.666);
		String percent=df.format(d);
		log.debug(percent);
		
		List<Integer> list  =new ArrayList<Integer>();
		list.add(1);
		list.add(3);
		list.add(2);
		list.add(4);
		Integer sum = list.stream().reduce(Integer::sum).orElse(0);
		log.debug("{}", sum);

		
		List<Double> list2  =new ArrayList<Double>();
		list2.add(1.19d);
		list2.add(1.22d);
		list2.add(1.05d);
		list2.add(1.35d);
		list2.add(1.47d);
		
		
		
	}
}
