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.formula.CalculationParam;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;

/** 
 * @Description: 性价比法
 * @author guoyr
 */
public class PriceCalculateImpl implements ICalculate {

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

		BigDecimal basePrice = tool.getBasePrice();
		BigDecimal priceScore = null;
		
		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("；");
		}
		// 如果在取样时设定了价格得分，则不再计算
		if(null != tool.getDefaultScore(evaluationPrice)){
			priceScore = tool.getDefaultScore(evaluationPrice);
			priceScore = tool.getPriceScoreRound(priceScore);
			formual.append("得默值").append(priceScore).append("分。");
		}else {
			
			// 性价比法的得分类型【01：得分=(评标基准价/投标报价)*总分；02：得分=(投标报价/评标基准价)*总分】
			formual.append(" ( ");
			if(ObjectUtils.equals(tool.getCalculationParam().getXjbScoreType(), CalculationParam.XJBF_SCORE_PRICE)){
				
				priceScore = BigDecimalUtils.div(evaluationPrice, basePrice);
				formual.append(MoneyUtils.getMoneyFormatByUnit(evaluationPrice, tenderProjectRuleEntity.getEvalPriceUnit()));
				formual.append(" ÷ ");
				formual.append(MoneyUtils.getMoneyFormatByUnit(basePrice, tenderProjectRuleEntity.getEvalPriceUnit()));
			}else {
				priceScore = BigDecimalUtils.div(basePrice, evaluationPrice);
				formual.append(MoneyUtils.getMoneyFormatByUnit(basePrice, tenderProjectRuleEntity.getEvalPriceUnit()));
				formual.append(" ÷ ");
				formual.append(MoneyUtils.getMoneyFormatByUnit(evaluationPrice, tenderProjectRuleEntity.getEvalPriceUnit()));
			}
			formual.append(" ) ");
			priceScore = BigDecimalUtils.mul(priceScore, evaluationFactor.getFactorScore());
			priceScore = tool.getPriceScoreRound(priceScore);
			formual.append(" × ");
			formual.append(BigDecimalUtils.round(evaluationFactor.getFactorScore(), 2));
			
			priceScore = tool.getPriceScoreRound(priceScore);
			formual.append(" = ").append(priceScore).append("分。");
			
			// 如果得分超过满分
			if(priceScore.compareTo(evaluationFactor.getFactorScore()) > 0){
				priceScore = evaluationFactor.getFactorScore();
				priceScore = tool.getPriceScoreRound(priceScore);
				formual.append("（超过满分）得满分").append(evaluationFactor.getFactorScore()).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;
	}
}
