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

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.components.core.util.MoneyUtils;
import com.gx.obe.evaluation.step.calculator.calculate.AbsoluteCalculateImpl;
import com.gx.obe.evaluation.step.calculator.calculate.ICalculate;
import com.gx.obe.evaluation.step.calculator.calculate.LinearStageDeviateCalculateImpl;
import com.gx.obe.evaluation.step.calculator.calculate.PriceCalculateImpl;
import com.gx.obe.evaluation.step.calculator.calculate.PriceDeviateCalculateImpl;
import com.gx.obe.evaluation.step.calculator.calculate.RateStageCalculateImpl;
import com.gx.obe.evaluation.step.calculator.sampling.CountSamplingImpl;
import com.gx.obe.evaluation.step.calculator.sampling.DispersionRangeSamplingImpl;
import com.gx.obe.evaluation.step.calculator.sampling.DispersionSamplingImpl;
import com.gx.obe.evaluation.step.calculator.sampling.ISampling;
import com.gx.obe.evaluation.step.calculator.sampling.PriceCeilingSamplingImpl;
import com.gx.obe.evaluation.step.entity.CalculationResult;
import com.gx.obe.formula.BasePriceRangeParam;
import com.gx.obe.formula.BenchmarkPriceParam;
import com.gx.obe.formula.CalculationParam;
import com.gx.obe.formula.DeviatePriceItem;
import com.gx.obe.formula.DeviateRateItem;
import com.gx.obe.formula.FilterCountConditionItem;
import com.gx.obe.formula.PriceParam;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;

public class PriceScoreCalculator implements Serializable {

	private static final long serialVersionUID = 1L;
	private PriceParam priceParam;
	private TenderProjectRuleEntity tenderProjectRuleEntity;
	private TenderProjectEntity tenderProjectEntity;
	private Logger LOG = Logger.getLogger(PriceScoreCalculator.class);

	/**
	 * 有效报价集合
	 */
//	private List<BigDecimal> evalPriceList;

//	private List<EvaluationPriceCalcuationResult> evaluationPriceCalcuationResultList;

	/**
	 * 价格计算公式的取样参数
	 */
	private BasePriceRangeParam specimenParam;

	/**
	 * 价格计算公式的基准价参数
	 */
	private BenchmarkPriceParam basePriceParam;

	/**
	 * 价格计算公式的得分计算参数
	 */
	private CalculationParam calculationParam;

	/**
	 * 基准价
	 */
	private BigDecimal basePrice;

	private EvaluationFactor evaluationFactor;
//	private EvaluationStep evaluationStep;
	/**
	 * 有效的报价
	 */
	private List<BigDecimal> effectiveEvalPriceList;
	private int effectiveSupplierCount = 0;
	private Map<BigDecimal, BigDecimal> defaultScoreMap;

	/**
	 * @Description: 获得价格公式的说明
	 * @author guoyr
	 * @return
	 */
	public String getCalculateDesc() {
		return "";
	}

	/**
	 * @Description: 初始化公式
	 * @author guoyr
	 * @param tenderProjectEntity
	 * @param tenderProjectRuleEntity
	 * @param evaluationStep
	 * @param priceEvaluationFactor
	 * @param evalPriceList
	 */
	public void init(TenderProjectEntity tenderProjectEntity, TenderProjectRuleEntity tenderProjectRuleEntity,
			EvaluationStep evaluationStep, EvaluationFactor evaluationFactor, List<BigDecimal> evalPriceList) {
		this.tenderProjectEntity = tenderProjectEntity;
		this.tenderProjectRuleEntity = tenderProjectRuleEntity;
//		this.evaluationStep = evaluationStep;
		this.evaluationFactor = evaluationFactor;
		if (null != evalPriceList) {
			this.effectiveEvalPriceList = new ArrayList<BigDecimal>(evalPriceList);
		} else {
			this.effectiveEvalPriceList = new ArrayList<BigDecimal>();
		}

		this.priceParam = PriceParam.deserializePriceParam(evaluationFactor.getComputerParams());
		;
		if (null != priceParam) {
			if (priceParam.getPriceScorePoint() == 0) {
				priceParam.setPriceScorePoint(tenderProjectRuleEntity.getScoreRoundIntVal());
			}
			this.specimenParam = priceParam.getSpecimenParam();
			this.basePriceParam = priceParam.getBasePriceParam();
			this.calculationParam = priceParam.getCalculationParam();
		}
		if (effectiveEvalPriceList.size() > 0) {

			effectiveSupplierCount = effectiveEvalPriceList.size();

			// 将评标价从大到小排序
			sortEffectiveEvalPriceList();
			// 取样
			getSampling();

			if (effectiveEvalPriceList.size() == 0) {
				LOG.error("取样后有效的投标报价家数为0");
				effectiveEvalPriceList.add(BigDecimal.ZERO);
			}
			// 获取基准价
			calcuateBasePriceValue();

		} else {
			// 如果没有有效报价，则默认添加一个0，方便计算
			LOG.error("有效报价的投标人数为0");
			evalPriceList.add(BigDecimal.ZERO);
		}
	}

	public void setDevaultScore(BigDecimal evalPrice, BigDecimal score) {
		if (null != score) {

			if (null == defaultScoreMap) {
				defaultScoreMap = new HashMap<BigDecimal, BigDecimal>();
			}
			defaultScoreMap.put(evalPrice, score);
		}

	}

	/**
	 * @Description: 获得在取样时设置的默认值
	 * @author guoyr
	 * @param evalPrice
	 * @return
	 */
	public BigDecimal getDefaultScore(BigDecimal evalPrice) {
		if (null != defaultScoreMap && null != defaultScoreMap.get(evalPrice)) {
			return defaultScoreMap.get(evalPrice);
		}
		return null;
	}

	/**
	 * @Description: 得到计算结果
	 * @author guoyr
	 * @param evalPrice
	 * @return
	 */
	public CalculationResult calculation(BigDecimal evalPrice) {
		// 公式计算参数
		if (null == calculationParam) {
			calculationParam = new CalculationParam();
			LOG.error("未设置价格公式参数");
		}
		ICalculate calculate = null;
		if (StringUtils.equals(CalculationParam.LINEAR_DEVIATE, calculationParam.getCalculationType())) {
			// 直线插值法
			calculate = new LinearStageDeviateCalculateImpl();
		} else if (StringUtils.equals(CalculationParam.STAGE_DEVIATE, calculationParam.getCalculationType())) {
			// 区间偏离法
			calculate = new RateStageCalculateImpl();
		} else if (StringUtils.equals(CalculationParam.ABSOLUTE, calculationParam.getCalculationType())) {
			// 偏差率法
			calculate = new AbsoluteCalculateImpl();
		} else if (StringUtils.equals(CalculationParam.PRICE_DEVIATEA, calculationParam.getCalculationType())) {
			// 价格偏离法
			calculate = new PriceDeviateCalculateImpl();
		} else {
			// 性价比法
			calculate = new PriceCalculateImpl();
		}
		return calculate.start(this, evalPrice, evaluationFactor, tenderProjectRuleEntity);
	}

	/**
	 * @Description: 将评标价从大到小排序
	 * @author guoyr
	 */
	public void sortEffectiveEvalPriceList() {
		// 将评标价从大到小排序
		Collections.sort(effectiveEvalPriceList, new EvalPriceSort());
	}

	/**
	 * @Description: 取样
	 * @author guoyr
	 */
	private void getSampling() {
		// 取样参数
		if (null == specimenParam) {
			specimenParam = new BasePriceRangeParam();
			LOG.error("未设置取样参数");
		}
		ISampling sampling = null;
		// 按离散度取样
		if (StringUtils.equals(BasePriceRangeParam.SPECIMEN_TYPE_DISPERSION, specimenParam.getSpecimenType())) {
			sampling = new DispersionSamplingImpl();
		} // 其他取样
		else if (StringUtils.equals(BasePriceRangeParam.SPECIMEN_TYPE_PRICE_CEILING, specimenParam.getSpecimenType())) {
			// 取一次平均价
			if (BasePriceRangeParam.PRICECEILING_AVG.equals(specimenParam.getPriceCeilingType())) {
				specimenParam.setPriceCeiling(getAvgEvalPrice());
			}
			// 如果未设定拦标价，则取预算
			if (null == specimenParam.getPriceCeiling()) {
				LOG.error("未设置拦标价");
//				specimenParam.setPriceCeiling(tenderProjectEntity.getBudgetMoney());
				specimenParam.setPriceCeiling(tenderProjectEntity.getBidPriceLimit());
			}
			sampling = new PriceCeilingSamplingImpl();
		} else if (StringUtils.equals(BasePriceRangeParam.SPECIMEN_TYPE_DISPERSION_RANGE,
				specimenParam.getSpecimenType())) {

			sampling = new DispersionRangeSamplingImpl();

		} else {
			// 通过数量取样
			sampling = new CountSamplingImpl();
		}
		sampling.start(this);
	}

	/**
	 * @Description: 获得基准价
	 * @author guoyr
	 */
	private void calcuateBasePriceValue() {
		// 基准价参数
		if (null == basePriceParam) {
			basePriceParam = new BenchmarkPriceParam();
			LOG.error("未设置基准价参数");
		}

		// 调用基准价计算工具
		BenchmarkPriceCalculator tool = new BenchmarkPriceCalculator();
		basePrice = tool.start(this, tenderProjectRuleEntity);
		if (null != basePrice) {
			if (priceParam.getBasePricePoint() > 0) {
				basePrice = BigDecimalUtils.round(basePrice, priceParam.getBasePricePoint());
			} else {
				basePrice = BigDecimalUtils.round(basePrice, 2);
			}
		}
	}

	/**
	 * @Description: 获得最高价
	 * @author guoyr
	 * @return
	 */
	public BigDecimal getMaxEvalPrice() {
		return effectiveEvalPriceList.get(0);
	}

	/**
	 * @Description: 获得最低价
	 * @author guoyr
	 * @return
	 */
	public BigDecimal getMinEvalPrice() {
		if (basePriceParam.isBasePriceUnderMinEvalPrice()) {
			return getLowestMinEvalPrice();
		}
		return effectiveEvalPriceList.get(effectiveEvalPriceList.size() - 1);
	}

	private BigDecimal getLowestMinEvalPrice() {
		BigDecimal belowFloat = BigDecimalUtils.div(basePriceParam.getBelowFloat(), new BigDecimal(100));
		belowFloat = BigDecimalUtils.sub(new BigDecimal(1), belowFloat);
		BigDecimal minPrice = effectiveEvalPriceList.get(effectiveEvalPriceList.size() - 1);
		for (int i = effectiveEvalPriceList.size() - 2; i > 0; i--) {
			BigDecimal nextPrice = effectiveEvalPriceList.get(i);
			if (BigDecimalUtils.compareTo(minPrice, BigDecimalUtils.mul(nextPrice, belowFloat)) < 0) {
				minPrice = nextPrice;
				effectiveEvalPriceList.remove(i+1);
			} else {
				break;
			}
		}
		return minPrice;
	}

	/**
	 * @Description: 获得有效报价的平均值
	 * @author guoyr
	 * @return
	 */
	public BigDecimal getAvgEvalPrice() {
		BigDecimal total = BigDecimal.ZERO;
		for (BigDecimal evaluationPrice : effectiveEvalPriceList) {
			total = BigDecimalUtils.add(total, evaluationPrice);
		}
		return BigDecimalUtils.div(total, BigDecimal.valueOf(effectiveEvalPriceList.size()));
	}

	/**
	 * @Description: 获得得次高价
	 * @author guoyr
	 * @return
	 */
	public BigDecimal getNextMaxEvalPrice() {
		BigDecimal maxPrice = effectiveEvalPriceList.get(0);
		BigDecimal nextMaxPrice = maxPrice;
		for (int i = 1; i < effectiveEvalPriceList.size(); i++) {
			BigDecimal price = effectiveEvalPriceList.get(i);
			// 从第二个最高价开始对比，如果最高价比当前的价格高，则认为当前价格为次高价
			if (nextMaxPrice.compareTo(price) > 0) {
				nextMaxPrice = price;
				break;
			}
		}
		return nextMaxPrice;
	}

	/**
	 * @Description: 获得次次高价
	 * @author wangx
	 * @return
	 */
	public BigDecimal getThirdMaxEvalPrice() {
		BigDecimal nextMaxPrice = getNextMaxEvalPrice();
		BigDecimal thirdMaxMaxPrice = nextMaxPrice;
		for (int i = 1; i < effectiveEvalPriceList.size(); i++) {
			BigDecimal price = effectiveEvalPriceList.get(i);
			// 从第二个最高价开始对比，如果最高价比当前的价格高，则认为当前价格为次高价
			if (thirdMaxMaxPrice.compareTo(price) > 0) {
				thirdMaxMaxPrice = price;
				break;
			}
		}
		return thirdMaxMaxPrice;
	}

	/**
	 * @Description: 获得得次低价
	 * @author guoyr
	 * @return
	 */
	public BigDecimal getNextMinEvalPrice() {
		BigDecimal minPrice = effectiveEvalPriceList.get(effectiveEvalPriceList.size() - 1);
		BigDecimal nextMinPrice = minPrice;
		for (int i = effectiveEvalPriceList.size() - 2; i >= 0; i--) {
			BigDecimal price = effectiveEvalPriceList.get(i);
			// 从第二个最低价开始对比，如果最低价比当前的价格低，则认为当前价格为次低价
			if (nextMinPrice.compareTo(price) < 0) {
				nextMinPrice = price;
				break;
			}
		}
		return nextMinPrice;
	}

	/**
	 * @Description: 获得次次低价
	 * @author wangx
	 * @return
	 */
	public BigDecimal getThirdMinEvalPrice() {
		BigDecimal nextMinPrice = getNextMinEvalPrice();
		BigDecimal thirdMinPrice = nextMinPrice;
		for (int i = effectiveEvalPriceList.size() - 3; i >= 0; i--) {
			BigDecimal price = effectiveEvalPriceList.get(i);
			// 从第三个最低价开始对比，如果次低价比当前的价格低，则认为当前价格为次次低价
			if (thirdMinPrice.compareTo(price) < 0) {
				thirdMinPrice = price;
				break;
			}
		}
		return thirdMinPrice;
	}

	/**
	 * @Description: 去除指定数量的最高评标价和最低评标价
	 * @author guoyr
	 * @param highDeductNum
	 * @param lowDeductNum
	 */
	public void removeInvalidHighOrLowEvalPrice(int highDeductNum, int lowDeductNum) {
		for (int i = 0; i < highDeductNum; i++) {
			if (effectiveEvalPriceList.size() > 0) {
				effectiveEvalPriceList.remove(0);
			}
		}
		for (int i = 0; i < lowDeductNum; i++) {
			if (effectiveEvalPriceList.size() > 0) {
				effectiveEvalPriceList.remove(effectiveEvalPriceList.size() - 1);
			}
		}

	}

	/**
	 * @Description: 评标价的排序从大到小排序
	 * @author guoyr
	 */
	private class EvalPriceSort implements Comparator<BigDecimal> {
		public int compare(BigDecimal a, BigDecimal b) {
			try {
				return b.compareTo(a);
			} catch (NumberFormatException e) {
				return 0;
			}
		}

	}

	/**
	 * @Description: 计算偏差率
	 * @author guoyr
	 * @param evaluationPrice
	 * @param basePrice
	 * @return
	 */
	public BigDecimal getDeviationRate(BigDecimal evaluationPrice) {

		if (null == evaluationPrice || null == basePrice || basePrice.compareTo(BigDecimal.ZERO) == 0) {
			return BigDecimal.ZERO;
		} else {

			// minuend: 被减数 ;subtrahend: 减数; divisor : 除数
			BigDecimal minuend = getDeviationRateParam(calculationParam.getMinuend(), evaluationPrice, basePrice,
					evaluationPrice);
			BigDecimal subtrahend = getDeviationRateParam(calculationParam.getSubtrahend(), evaluationPrice, basePrice,
					basePrice);
			BigDecimal divisor = getDeviationRateParam(calculationParam.getDivisor(), evaluationPrice, basePrice,
					basePrice);

			BigDecimal deviationRate = BigDecimal.ZERO;
			if (null != divisor && divisor.compareTo(BigDecimal.ZERO) != 0) {
				deviationRate = BigDecimalUtils.mul(
						BigDecimalUtils.div(BigDecimalUtils.sub(minuend, subtrahend), divisor), new BigDecimal(100));
			} else {
				// 报价为本身为偏差率时，只做减法即可
				deviationRate = BigDecimalUtils.sub(minuend, subtrahend);
			}

			// 为了使报表上显示的偏差率和扣除后的值一致，先进行四舍五入
			int deviationRatePoint = (null != calculationParam.getDeviateRatePoint()
					&& calculationParam.getDeviateRatePoint().compareTo(BigDecimal.ZERO) >= 0)
							? (int) calculationParam.getDeviateRatePoint().doubleValue()
							: priceParam.getPriceScorePoint();
			deviationRate = BigDecimalUtils.round(deviationRate, deviationRatePoint);
			return deviationRate;
		}
	}

	/**
	 * @Description: 获得与基准价的价格偏差
	 * @author guoyr
	 * @param evaluationPrice
	 * @param basePrice
	 * @return
	 */
	public BigDecimal getEvalPriceDeviation(BigDecimal evaluationPrice, BigDecimal basePrice) {
		if (null == evaluationPrice || null == basePrice || basePrice.compareTo(BigDecimal.ZERO) == 0) {
			return BigDecimal.ZERO;
		} else {
			return BigDecimalUtils.sub(evaluationPrice, basePrice);
		}
	}

	private BigDecimal getDeviationRateParam(String deviationRateParam, BigDecimal evaluationPrice,
			BigDecimal basePrice, BigDecimal defaultValue) {
		if (deviationRateParam.equals(CalculationParam.EVAL_PRICE)) {
			return evaluationPrice;
		} else if (deviationRateParam.equals(CalculationParam.BASE_PRICE)) {
			return basePrice;
		} else if (deviationRateParam.equals(CalculationParam.MIN_PRICE)) {
			return getMinEvalPrice();
		} else if (deviationRateParam.equals(CalculationParam.AVG_PRICE)) {
			return getAvgEvalPrice();
		} else if (deviationRateParam.equals(CalculationParam.MAX_PRICE)) {
			return getMaxEvalPrice();
		} else if (deviationRateParam.equals(CalculationParam.EMPTY)) {
			return null;
		}
		return defaultValue;
	}

	/**
	 * @Description: 获得权重后后价格得分
	 * @author guoyr
	 * @param priceScore
	 * @param priceWeight
	 * @return
	 */
	public BigDecimal getScoreByWeight(BigDecimal priceScore) {
		BigDecimal priceFinalScore = priceScore;
		if (null != priceScore) {
			if (null != priceScore && priceScore.compareTo(BigDecimal.ZERO) != 0
					&& null != evaluationFactor.getFactorFinalWeight()
					&& evaluationFactor.getFactorFinalWeight().compareTo(BigDecimal.ZERO) != 0) {
				priceFinalScore = BigDecimalUtils.mul(priceScore,
						BigDecimalUtils.div(evaluationFactor.getFactorFinalWeight(), new BigDecimal(100)));
				priceFinalScore = BigDecimalUtils.round(priceFinalScore, priceParam.getPriceScorePoint());
			}
		}
		return priceFinalScore;
	}

	/**
	 * @Description: 根据权重分值获得百分制的分值
	 * @author guoyr
	 * @param priceWeightScore
	 * @param priceWeight
	 * @return
	 */
	public BigDecimal getScoreFromWeightScore(BigDecimal priceFinalScore) {
		BigDecimal priceScore = priceFinalScore;
		if (null != priceFinalScore && priceScore.compareTo(BigDecimal.ZERO) != 0
				&& null != evaluationFactor.getFactorWeight()) {
			priceScore = BigDecimalUtils.div(priceFinalScore,
					BigDecimalUtils.div(evaluationFactor.getFactorWeight(), new BigDecimal(100)));
			priceScore = BigDecimalUtils.round(priceScore, priceParam.getPriceScorePoint());
		}
		return priceScore;
	}

	/**
	 * @Description: 根据权重分值获得百分制的分值
	 * @author guoyr
	 * @param priceWeightScore
	 * @param priceWeight
	 * @return
	 */
	public BigDecimal getScoreFromFinalWeightScore(BigDecimal priceFinalScore) {
		BigDecimal priceScore = priceFinalScore;
		if (null != priceFinalScore && priceScore.compareTo(BigDecimal.ZERO) != 0
				&& null != evaluationFactor.getFactorFinalWeight()
				&& evaluationFactor.getFactorFinalWeight().compareTo(BigDecimal.ZERO) != 0) {
			priceScore = BigDecimalUtils.div(priceFinalScore,
					BigDecimalUtils.div(evaluationFactor.getFactorFinalWeight(), new BigDecimal(100)));
			priceScore = BigDecimalUtils.round(priceScore, priceParam.getPriceScorePoint());
		}
		return priceScore;
	}

	/**
	 * @Description: 获得价格分的四舍五入
	 * @author guoyr
	 * @param priceScore
	 * @return
	 */
	public BigDecimal getPriceScoreRound(BigDecimal priceScore) {
		return BigDecimalUtils.round(priceScore, priceParam.getPriceScorePoint());
	}

	public BasePriceRangeParam getSpecimenParam() {
		return specimenParam;
	}

	public void setSpecimenParam(BasePriceRangeParam specimenParam) {
		this.specimenParam = specimenParam;
	}

	public BenchmarkPriceParam getBasePriceParam() {
		return basePriceParam;
	}

	public void setBasePriceParam(BenchmarkPriceParam basePriceParam) {
		this.basePriceParam = basePriceParam;
	}

	public CalculationParam getCalculationParam() {
		return calculationParam;
	}

	public void setCalculationParam(CalculationParam calculationParam) {
		this.calculationParam = calculationParam;
	}

	public BigDecimal getBasePrice() {
		return basePrice;
	}

	public void setBasePrice(BigDecimal basePrice) {
		if (null != basePrice) {
			if (priceParam.getBasePricePoint() > 0) {
				basePrice = BigDecimalUtils.round(basePrice, priceParam.getBasePricePoint());
			} else {
				basePrice = BigDecimalUtils.round(basePrice, 2);
			}
			this.basePrice = basePrice;
		}
	}

	public List<BigDecimal> getEffectiveEvalPriceList() {
		return effectiveEvalPriceList;
	}

	public void setEffectiveEvalPriceList(List<BigDecimal> effectiveEvalPriceList) {
		this.effectiveEvalPriceList = effectiveEvalPriceList;
	}

//	public List<EvaluationPriceCalcuationResult> getEvalPriceCalcuationResultList() {
//		return evaluationPriceCalcuationResultList;
//	}
//
//	public void setEvalPriceCalcuationResultList(
//			List<EvaluationPriceCalcuationResult> evaluationPriceCalcuationResultList) {
//		this.evaluationPriceCalcuationResultList = evaluationPriceCalcuationResultList;
//	}

	public PriceParam getPriceParam() {
		return priceParam;
	}

	public void setPriceParam(PriceParam priceParam) {
		this.priceParam = priceParam;
	}

	public int getEffectiveSupplierCount() {
		return effectiveSupplierCount;
	}

	public void setEffectiveSupplierCount(int effectiveSupplierCount) {
		this.effectiveSupplierCount = effectiveSupplierCount;
	}

	public static String getDeviateRateDesc(DeviateRateItem deviateRateItem) {

		StringBuilder desc = new StringBuilder();
		if (deviateRateItem.isHighDeviate()) {
			desc.append("偏差率大于");
		} else {
			desc.append("偏差率小于");
		}
		if (null != deviateRateItem.getDeviateRateStart()) {
			desc.append(NumberFormat.getInstance().format(deviateRateItem.getDeviateRateStart()));
			desc.append("%（不含");
			desc.append(NumberFormat.getInstance().format(deviateRateItem.getDeviateRateStart()));
			desc.append("%）时，");
		}
		if (deviateRateItem.isHighDeviate()) {
			desc.append("每高");
		} else {
			desc.append("每低");
		}
		desc.append(null != deviateRateItem.getDeviatePercentUnit()
				? NumberFormat.getInstance().format(deviateRateItem.getDeviatePercentUnit())
				: " ");
		desc.append("%");
		if (deviateRateItem.isReduceScore()) {
			desc.append("扣");
		} else {
			desc.append("加");
		}
		desc.append(null != deviateRateItem.getDeviatePercentValue()
				? NumberFormat.getInstance().format(deviateRateItem.getDeviatePercentValue())
				: " ");
		desc.append("分，");
		if (null != deviateRateItem.getDefaultScore()) {
			if (deviateRateItem.isReduceScore()) {
				desc.append("扣至");
			} else {
				desc.append("加至");
			}
			desc.append(NumberFormat.getInstance().format(deviateRateItem.getDefaultScore()));
			desc.append("分");
		}
		return desc.toString();

	}

	public static String getStageDeviateRateDesc(DeviateRateItem deviateRateItem) {
		StringBuilder desc = new StringBuilder();
		if (deviateRateItem.isHighDeviate()) {
			desc.append("偏差率大于");
		} else {
			desc.append("偏差率小于");
		}
		if (null != deviateRateItem.getDeviateRateStart()) {
			desc.append(NumberFormat.getInstance().format(deviateRateItem.getDeviateRateStart()));
			desc.append("%（不含");
			desc.append(NumberFormat.getInstance().format(deviateRateItem.getDeviateRateStart()));
			desc.append("%）时，");
		}
		if (deviateRateItem.isHighDeviate()) {
			desc.append("每增加");
		} else {
			desc.append("每降低");
		}
		desc.append(null != deviateRateItem.getDeviatePercentUnit()
				? NumberFormat.getInstance().format(deviateRateItem.getDeviatePercentUnit())
				: " ");

		desc.append("%");

		if (null != deviateRateItem.getDeviateRateStart() && null != deviateRateItem.getDeviatePercentUnit()) {
			desc.append("（含");
			if (deviateRateItem.isHighDeviate()) {
				desc.append(NumberFormat.getInstance().format(BigDecimalUtils.add(deviateRateItem.getDeviateRateStart(),
						deviateRateItem.getDeviatePercentUnit())));
			} else {
				desc.append(NumberFormat.getInstance().format(BigDecimalUtils.sub(deviateRateItem.getDeviateRateStart(),
						deviateRateItem.getDeviatePercentUnit())));
			}
			desc.append("%）");
		}
		if (deviateRateItem.isReduceScore()) {
			desc.append("扣");
		} else {
			desc.append("加");
		}
		desc.append(null != deviateRateItem.getDeviatePercentValue()
				? NumberFormat.getInstance().format(deviateRateItem.getDeviatePercentValue())
				: " ");
		desc.append("分，");
		if (null != deviateRateItem.getDefaultScore()) {
			if (deviateRateItem.isReduceScore()) {
				desc.append("依次类推扣至");
			} else {
				desc.append("依次类推加至");
			}
			desc.append(NumberFormat.getInstance().format(deviateRateItem.getDefaultScore()));
			desc.append("分");
		}
		return desc.toString();
	}

	public static String getDeviatePriceDesc(TenderProjectRuleEntity tenderProjectRuleEntity,
			DeviatePriceItem deviateRateItem) {
		StringBuilder desc = new StringBuilder();
		if (deviateRateItem.isHighDeviate()) {
			desc.append("高于基准价");
		} else {
			desc.append("低于基准价");
		}
		if (null != deviateRateItem.getDeviateRateStart()
				&& deviateRateItem.getDeviateRateStart().compareTo(BigDecimal.ZERO) != 0) {
			desc.append(MoneyUtils.getMoneyFormatByUnit(deviateRateItem.getDeviateRateStart(),
					tenderProjectRuleEntity.getEvalPriceUnit()));
			desc.append(tenderProjectRuleEntity.getEvalPriceUnit()).append("（不含）以上");
		}
		if (deviateRateItem.isHighDeviate()) {
			desc.append("时，每高");
		} else {
			desc.append("时，每低");
		}

		if (null != deviateRateItem.getDeviatePercentUnit()) {
			desc.append(MoneyUtils.getMoneyFormatByUnit(deviateRateItem.getDeviatePercentUnit(),
					tenderProjectRuleEntity.getEvalPriceUnit()));
		} else {
			desc.append(" ");
		}
		desc.append(tenderProjectRuleEntity.getPriceUnitUseDefault());
		if (deviateRateItem.isReduceScore()) {
			desc.append("扣");
		} else {
			desc.append("加");
		}
		desc.append(null != deviateRateItem.getDeviatePercentValue()
				? NumberFormat.getInstance().format(deviateRateItem.getDeviatePercentValue())
				: " ");
		desc.append("分，");
		if (null != deviateRateItem.getDefaultScore()) {
			if (deviateRateItem.isReduceScore()) {
				desc.append("扣至");
			} else {
				desc.append("加至");
			}
			desc.append(NumberFormat.getInstance().format(deviateRateItem.getDefaultScore()));
			desc.append("分");
		}
		return desc.toString();

	}

	public static String getFilterCountConditionDesc(FilterCountConditionItem filterCountConditionItem, int index) {
		String supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME);
		String priceName = Messages.EvaluationPrice;
		StringBuilder desc = new StringBuilder();
		desc.append("当有效");
		desc.append(supplierName);
		if (filterCountConditionItem.isInclude()) {
			desc.append("大于等于");
		} else {
			desc.append("大于");
		}
		desc.append(filterCountConditionItem.getMorethanCount());
		desc.append("家时，去掉");
		if (filterCountConditionItem.getFilterHighest() > 0) {
			desc.append(filterCountConditionItem.getFilterHighest());
			desc.append("个最高");
		}
		if (filterCountConditionItem.getFilterLowest() > 0) {
			if (filterCountConditionItem.getFilterHighest() > 0) {
				desc.append("和");
			}
			desc.append(filterCountConditionItem.getFilterLowest());
			desc.append("个最低");
		}
		desc.append("有效");
		desc.append(priceName);
		desc.append("，取剩余所有有效");
		desc.append(priceName);
		if (index == 0) {
			desc.append("；当有效");
			desc.append(supplierName);
			if (filterCountConditionItem.isInclude()) {
				desc.append("小于");
			} else {
				desc.append("小于等于");
			}
			desc.append(filterCountConditionItem.getMorethanCount());
			desc.append("家时，取全部有效");
			desc.append(priceName);
		}
		return desc.toString();

	}

}
