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

import java.math.BigDecimal;

import com.gx.obe.evaluation.step.calculator.PriceScoreCalculator;
import com.gx.obe.util.utils.BigDecimalUtils;

/** 
 * @Description: 按离散度
 * @author guoyr
 */
public class DispersionSamplingImpl implements ISampling{

	/** 
	 * @Description: 取样
	 * @author guoyr 
	 * @param tool  
	 */
	public void start(PriceScoreCalculator tool) {
		
		// 如果投标人大于5家，最高价高于次高价%5,最低价低于次低价5%，最高最低价无效，最高最低投标人得分为0分，否则取全部有效报价
		if(tool.getEffectiveSupplierCount() > tool.getSpecimenParam().getSupplierNum()){
			
			if(tool.getSpecimenParam().getHeigerSecondPercent() > 0){
				BigDecimal maxEvalPrice = tool.getMaxEvalPrice();// 最高价
				BigDecimal maxNextEvalPrice = tool.getNextMaxEvalPrice();// 次高价
				
				// (最高价-次高价)/次高价>5% -->(最高价-次高价)>次高价*5%
				BigDecimal maxDifference = BigDecimalUtils.sub(maxEvalPrice, maxNextEvalPrice);
				BigDecimal maxDifferenceRate = BigDecimalUtils.div(BigDecimalUtils.mul(maxNextEvalPrice, BigDecimal.valueOf(tool.getSpecimenParam().getHeigerSecondPercent())), new BigDecimal(100));
				
				// 如果最高价高于次高价%5，最高价无效
				if(maxDifference.compareTo(maxDifferenceRate) > 0){
					if(tool.getEffectiveEvalPriceList().size() > 0){
						// 且最高价投标人得分为0
						if(null != tool.getSpecimenParam().getPriceScore()){
							tool.setDevaultScore(tool.getEffectiveEvalPriceList().get(0), tool.getSpecimenParam().getPriceScore());
						}
						tool.getEffectiveEvalPriceList().remove(0);
					}
				}
			}
			
			if(tool.getSpecimenParam().getLessThanSecondPercent() > 0){
				BigDecimal minEvalPrice = tool.getMinEvalPrice();// 最低价
				BigDecimal minNextEvalPrice = tool.getNextMinEvalPrice();// 次低价
				
				// (次低价-最低价)/次低价<*5% -->(次低价-最低价)<次低价*5%
				BigDecimal minDifference = BigDecimalUtils.sub(minNextEvalPrice, minEvalPrice);
				BigDecimal minDifferenceRate = BigDecimalUtils.div(BigDecimalUtils.mul(minNextEvalPrice, BigDecimal.valueOf(tool.getSpecimenParam().getLessThanSecondPercent())), new BigDecimal(100));
				
				// 如果最低价低于次低价5%，最低价无效，且最低价投标人得分为0
				if(minDifference.compareTo(minDifferenceRate) > 0){
					if(tool.getEffectiveEvalPriceList().size() > 0){
						if(null != tool.getSpecimenParam().getPriceScore()){
							tool.setDevaultScore(tool.getEffectiveEvalPriceList().get(tool.getEffectiveEvalPriceList().size() -1), tool.getSpecimenParam().getPriceScore());
						}
						tool.getEffectiveEvalPriceList().remove(tool.getEffectiveEvalPriceList().get(tool.getEffectiveEvalPriceList().size() -1));
					}
				}
			}
		}
	}
	
}
