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

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

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

/**
 * @Description: 其他取样
 * @author guoyr
 */
public class PriceCeilingSamplingImpl implements ISampling{

	/** 
	 * @Description: 其他取样
	 * @author guoyr 
	 * @param tool  
	 */
	public void start(PriceScoreCalculator tool) {
		
		// X值
		BigDecimal priceCeiling = tool.getSpecimenParam().getPriceCeiling();
		if(null != priceCeiling && priceCeiling.compareTo(BigDecimal.ZERO) > 0){
			
			// 为了避免超过X值的投标人未被废标，再进行检查是否有超过栏标价的报价
			List<BigDecimal> tempEffectiveEvalPriceList = new ArrayList<BigDecimal>();
			tempEffectiveEvalPriceList.addAll(tool.getEffectiveEvalPriceList());
			List<BigDecimal> removeScore = new ArrayList<BigDecimal>();
			if(tool.getEffectiveEvalPriceList().size() > tool.getSpecimenParam().getSupplierNum()){
				
				// 设定的X值的浮动率%
				BigDecimal minPriceCeiling = priceCeiling;
				if(null != tool.getSpecimenParam().getPriceCeilingPercent() && tool.getSpecimenParam().getPriceCeilingPercent().compareTo(BigDecimal.ZERO) > 0){
					minPriceCeiling = BigDecimalUtils.mul(priceCeiling, BigDecimalUtils.div(tool.getSpecimenParam().getPriceCeilingPercent(), new BigDecimal(100)));
				}
				for(BigDecimal evaluationPrice : tempEffectiveEvalPriceList){
					boolean remove = false;
					if(BasePriceRangeParam.CEILING_TYPE_L.equals(tool.getSpecimenParam().getCeilingType())){
						// 取所有<X值的报价
						remove = evaluationPrice.compareTo(priceCeiling) >= 0;
					}else if(BasePriceRangeParam.CEILING_TYPE_LEQ.equals(tool.getSpecimenParam().getCeilingType())){
						// 取所有<=X值的报价
						remove = evaluationPrice.compareTo(priceCeiling) > 0;
					}else if(BasePriceRangeParam.CEILING_TYPE_H.equals(tool.getSpecimenParam().getCeilingType())){
						// 取>X值的所有报价
						remove = evaluationPrice.compareTo(minPriceCeiling) <= 0;
					}else if(BasePriceRangeParam.CEILING_TYPE_HEQ.equals(tool.getSpecimenParam().getCeilingType())){
						// 取>=X值的所有报价
						remove = evaluationPrice.compareTo(minPriceCeiling) < 0;
					}
					if(remove){
						tool.getEffectiveEvalPriceList().remove(evaluationPrice);
						removeScore.add(evaluationPrice);
					}
				}
				// 如果最极端情况下所有报价都被排除的情况下,则取全部
				if(tool.getEffectiveEvalPriceList().size() == 0){
					tool.getEffectiveEvalPriceList().addAll(tempEffectiveEvalPriceList);
					removeScore.clear();
				}
				// 重新将剩余的有效报价按从大到小排序
				tool.sortEffectiveEvalPriceList();
				
				// 取最低或最高的5家有效报价
				if(tool.getSpecimenParam().getEffectiveSupplierNum() > 0 && tool.getEffectiveEvalPriceList().size() > tool.getSpecimenParam().getEffectiveSupplierNum()){
					// 从有效的评标价中取指定数量最小的，还是指定数量最大的
					boolean getMax = BasePriceRangeParam.CEILING_TYPE_L.equals(tool.getSpecimenParam().getCeilingType()) || BasePriceRangeParam.CEILING_TYPE_LEQ.equals(tool.getSpecimenParam().getCeilingType());
					if(null != tool.getSpecimenParam().getPriceScore()){
						int removeCount = tool.getEffectiveEvalPriceList().size() - tool.getSpecimenParam().getEffectiveSupplierNum();
						for(int i = 0; i < tool.getEffectiveEvalPriceList().size(); i++){
							BigDecimal evaluationPrice = tempEffectiveEvalPriceList.get(i);
							if(getMax){
								if(i > tool.getSpecimenParam().getEffectiveSupplierNum() -1){
									removeScore.add(evaluationPrice);
								}
							}else {
								if(i < removeCount){
									removeScore.add(evaluationPrice);
								}
							}
						}
					}
					if(getMax){
						tool.removeInvalidHighOrLowEvalPrice(0, tool.getEffectiveEvalPriceList().size() - tool.getSpecimenParam().getEffectiveSupplierNum());
					}else {
						tool.removeInvalidHighOrLowEvalPrice(tool.getEffectiveEvalPriceList().size() - tool.getSpecimenParam().getEffectiveSupplierNum(), 0);
					}
				}
			}
			
			if(null != tool.getSpecimenParam().getPriceScore() && removeScore.size() > 0){
				// 如果设置了默认值 ，则将不参与基准价计算的金额设置默认得分
				for(int i = 0; i < removeScore.size(); i++){
					BigDecimal evaluationPrice = removeScore.get(i);
					tool.setDevaultScore(evaluationPrice, tool.getSpecimenParam().getPriceScore());
				}
			}
		}
	}
}
