package com.gx.obe.evaluation.step.calculator;

import java.math.BigDecimal;

import com.gx.obe.evaluation.step.entity.BusinessParam;
import com.gx.obe.util.utils.BigDecimalUtils;

/**
 * @Description: 商务得分计算器
 * @author chenxw
 */
public class BusinessScoreCalculator {
	
	private BusinessParam.ScoreCalculation scoreCalculation;
	
	private BigDecimal totalScore;
	
	private BigDecimal basePrice;
	
	public void setScoreCalculation(BusinessParam.ScoreCalculation scoreCalculation) {
		this.scoreCalculation = scoreCalculation;
	}
	
	public void setTotalScore(BigDecimal totalScore) {
		this.totalScore = totalScore;
	}
	
	public void setBasePrice(BigDecimal basePrice) {
		this.basePrice = basePrice;
	}
	
	/**
	 * @Description: 计算偏差百分比
	 * @author chenxw
	 * @param price
	 * @return
	 * @throws Exception
	 */
	public BigDecimal calculateBiasPercentage(BigDecimal price) {
		if (null == basePrice || null == price) {
			return null;
		}
		return BigDecimalUtils.div(price.subtract(basePrice), basePrice).multiply(BigDecimal.valueOf(100));
	}
	
	/**
	 * @Description: 计算得分
	 * @author chenxw
	 * @param price
	 * @return
	 * @throws Exception
	 */
	public BigDecimal calculateScore(BigDecimal biasPercentage) {
		if (null == biasPercentage) {
			return null;
		}
		if (biasPercentage.compareTo(BigDecimal.ZERO) == 0) {
			return totalScore;
		}
		BigDecimal deductPoints = null;
		if (biasPercentage.compareTo(BigDecimal.ZERO) > 0) {
			deductPoints = biasPercentage.multiply(BigDecimalUtils.div(scoreCalculation.getHighBasePriceDeductPoints(), scoreCalculation.getHighBasePricePercentage()));
		} else {
			biasPercentage = biasPercentage.multiply(BigDecimal.valueOf(-1));
			deductPoints = biasPercentage.multiply(BigDecimalUtils.div(scoreCalculation.getLowBasePriceDeductPoints(), scoreCalculation.getLowBasePricePercentage()));
		}
		if (totalScore.compareTo(deductPoints) < 0) {
			return BigDecimal.ZERO;
		}
		return totalScore.subtract(deductPoints);
	}
	
}
