package com.ekexiu.portal.util;

import java.math.BigDecimal;

import org.jfw.apt.annotation.Bean;

@Bean
public class Calculate {
	private double starWeight = 0.5;
	private double consultWeight = 0.5;
	
	public double getStarWeight() {
		return starWeight;
	}

	public void setStarWeight(double starWeight) {
		this.starWeight = starWeight;
	}

	public double getConsultWeight() {
		return consultWeight;
	}

	public void setConsultWeight(double consultWeight) {
		this.consultWeight = consultWeight;
	}

	public BigDecimal calcAvgStar(BigDecimal bigStar, BigDecimal smallStar, 
			BigDecimal bigConsult, BigDecimal smallConsult, BigDecimal star, BigDecimal consult){
		BigDecimal sWeight = new BigDecimal(this.starWeight);
		BigDecimal cWeight = new BigDecimal(this.consultWeight);
		BigDecimal normalStar = new BigDecimal(0);
		BigDecimal normalConsult = new BigDecimal(0);
		//计算归一化后的星级
		if(sub(bigStar, smallStar).doubleValue() != new BigDecimal(0).doubleValue()){
			normalStar = div(sub(star, smallStar), sub(bigStar, smallStar));
		}
		//计算归一化后的咨询次数
		if(sub(bigConsult, smallConsult).doubleValue() != new BigDecimal(0).doubleValue()){
			normalConsult = div(sub(consult, smallConsult), sub(bigConsult, smallConsult));
		}
		//按权重计算平均星级
		BigDecimal avgStar = add(mul(sWeight, normalStar), mul(cWeight, normalConsult));
		return avgStar;
	}
	
	public static BigDecimal add(BigDecimal d1,BigDecimal d2) {
		return d1.add(d2);
	}
	
	public static BigDecimal sub(BigDecimal d1,BigDecimal d2) {
		return d1.subtract(d2);
	}
	
	public static BigDecimal mul(BigDecimal d1,BigDecimal d2) {
		return d1.multiply(d2);
	}
	
	public static BigDecimal div(BigDecimal d1,BigDecimal d2) {
		return d1.divide(d2);
	}
	
}
