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

import java.math.BigDecimal;
import java.text.NumberFormat;

import com.gx.obe.components.core.util.MoneyUtils;
import com.gx.obe.evaluation.step.calculator.PriceScoreCalculator;
import com.gx.obe.evaluation.step.entity.CalculationResult;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;

/** 
 * @Description: 绝对值法
 * @author guoyr
 */
public class AbsoluteCalculateImpl implements ICalculate {

	public CalculationResult start(PriceScoreCalculator tool, BigDecimal evaluationPrice, EvaluationFactor evaluationFactor, TenderProjectRuleEntity tenderProjectRuleEntity){

		BigDecimal priceScore = null;
		BigDecimal basePrice = tool.getBasePrice();
		CalculationResult calculationResult = new CalculationResult();
		// 计算偏差率
		BigDecimal deviationRate = tool.getDeviationRate(evaluationPrice);
		calculationResult.setDeviationRate(deviationRate);
		StringBuilder formual = new StringBuilder();
		if(null != basePrice){
			formual.append("基准价为");
			formual.append(MoneyUtils.getMoneyWidthUnit(basePrice, tenderProjectRuleEntity.getEvalPriceUnit()));
		}
		formual.append("，偏差率为 ").append(deviationRate);
		formual.append("%，");
		
		// 如果在取样时设定了价格得分，则不再计算
		if(null != tool.getDefaultScore(evaluationPrice)){
			priceScore = tool.getDefaultScore(evaluationPrice);
			priceScore = tool.getPriceScoreRound(priceScore);
			formual.append("得默值").append(priceScore).append("分。");
		}else {

//			BigDecimal result;
			// （1-绝对值（投标报价-基准价）/基准价）×分值 
//			formual.append("(1 - |");
//			formual.append(MoneyUtils.getDecimalFormat(evaluationPrice.toString(), tenderProjectRuleEntity.getPriceUint()));
//			formual.append(" - ");
//			formual.append(MoneyUtils.getDecimalFormat(basePrice.toString(), tenderProjectRuleEntity.getPriceUint()));
//			
//			formual.append("| ÷ ");//÷
//			formual.append(MoneyUtils.getDecimalFormat(basePrice.toString(), tenderProjectRuleEntity.getPriceUint()));
//			result = BigDecimalUtils.difference(evaluationPrice, basePrice);
//			result = BigDecimalUtils.div(result, basePrice);
//			
//			result = BigDecimalUtils.sub(new BigDecimal(1), result);
//			
//			// 价格分再乘以权重
//			formual.append(") × ");
//			formual.append(evaluationFactor.getFactorScore());
//			formual.append("% ");
			// 价格分按满分计算
			
			BigDecimal absRate = BigDecimalUtils.div(deviationRate, new BigDecimal(100));
			boolean isAbs = null != tool.getCalculationParam().getAbsoluteDeviate() && tool.getCalculationParam().getAbsoluteDeviate().equals("01");
			
			// 绝对值
			if(absRate.compareTo(BigDecimal.ZERO) < 0){
				absRate = BigDecimalUtils.sub(BigDecimal.ZERO, absRate);
			}
			
			BigDecimal result = BigDecimalUtils.sub(new BigDecimal(1), absRate);
			priceScore = BigDecimalUtils.mul(result, evaluationFactor.getFactorScore());
			priceScore = tool.getPriceScoreRound(priceScore);
			if(isAbs){
				formual.append(" ( 1 - | ").append(MoneyUtils.getMoneyFormatByUnit(evaluationPrice, tenderProjectRuleEntity.getEvalPriceUnit()));
				formual.append("-");
				formual.append(MoneyUtils.getMoneyFormatByUnit(basePrice, tenderProjectRuleEntity.getEvalPriceUnit())).append(" |");
				formual.append(" ÷ ");
				formual.append(MoneyUtils.getMoneyFormatByUnit(basePrice, tenderProjectRuleEntity.getEvalPriceUnit()));
				formual.append(" ) * ");
				formual.append(evaluationFactor.getFactorScore());
				formual.append(" = ").append(priceScore).append("分。");
			}else {
				if(deviationRate.compareTo(BigDecimal.ZERO) <= 0){
					priceScore = evaluationFactor.getFactorScore();
					priceScore = tool.getPriceScoreRound(priceScore);
					formual.append("偏差率 <=0 得满分").append(evaluationFactor.getFactorScore()).append("分。");
				}else {
					
					formual.append(" ( 1 - ( ").append(MoneyUtils.getMoneyFormatByUnit(evaluationPrice, tenderProjectRuleEntity.getEvalPriceUnit()));
					formual.append("-");
					formual.append(MoneyUtils.getMoneyFormatByUnit(basePrice, tenderProjectRuleEntity.getEvalPriceUnit())).append(" )");
					formual.append(" ÷ ");
					formual.append(MoneyUtils.getMoneyFormatByUnit(basePrice, tenderProjectRuleEntity.getEvalPriceUnit()));
					formual.append(" ) * ");
					formual.append(evaluationFactor.getFactorScore());
					formual.append(" = ").append(priceScore).append("分。 ");
				}
			}
			// 如果得分超过满分
			if(priceScore.compareTo(evaluationFactor.getFactorScore()) > 0){
				priceScore = evaluationFactor.getFactorScore();
				priceScore = tool.getPriceScoreRound(priceScore);
				formual.append("（超过满分）得满分").append(evaluationFactor.getFactorScore()).append("分。");
			}else {
				
				// 高于偏差率投标人最低得分
				BigDecimal defaultMinScore = (null != tool.getCalculationParam().getDefaultMinScore() && tool.getCalculationParam().getDefaultMinScore().compareTo(BigDecimal.ZERO) > 0) ? tool.getCalculationParam().getDefaultMinScore() : BigDecimal.ZERO;
				// 设置最低分
				if(priceScore.compareTo(defaultMinScore) < 0){
					priceScore = defaultMinScore;
					formual.append("（低于最低分）得最低分").append(priceScore).append("分。");
				}
			}
		}
		
		calculationResult.setPriceScore(priceScore);
		calculationResult.setPriceFinalScore(tool.getScoreByWeight(priceScore));
		if(priceScore.compareTo(BigDecimal.ZERO) != 0 && null != evaluationFactor.getFactorWeight()  && evaluationFactor.getFactorFinalWeight().compareTo(BigDecimal.ZERO) != 0){
			formual.append("权重为");
			formual.append(NumberFormat.getInstance().format(evaluationFactor.getFactorFinalWeight()));
			formual.append("%，权重后得");
			formual.append(calculationResult.getPriceFinalScore());
			formual.append("分。");
		}
		calculationResult.setFormual(formual.toString());
		return calculationResult;
	}
}
