package com.gx.obe.evaluation.step.resutl.composite;

import java.math.BigDecimal;
import java.text.MessageFormat;
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 java.util.Map.Entry;
import java.util.TreeMap;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.utils.WidgetUtils;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
import com.gx.obe.business.entity.ExpertEvaluationStep;
import com.gx.obe.business.entity.SupplierEvaluationStep;
import com.gx.obe.business.enumeration.SupplierEnum;
import com.gx.obe.business.vo.RecommonedFactor;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.components.core.enumeration.EvaluationStepEnum;
import com.gx.obe.components.core.enumeration.TenderProjectEnum;
import com.gx.obe.components.core.util.MoneyUtils;
import com.gx.obe.config.utils.PropertiesUtils;
import com.gx.obe.evaluation.step.dialog.ApplyReEvaluationDialog;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.util.utils.CollectionUtils;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.NumberFormatUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.SupplierEvaluationResult;
import com.gx.obe.web.entity.SupplierOpeningResult;
import com.gx.obe.web.entity.auth.AuthUser;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.expert.Expert;
import com.gx.obe.web.entity.expert.ExpertEvaluationResult;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;
import com.gx.obe.web.service.ProjectRuleService;
import com.gx.obe.web.service.SupplierEvaluationResultService;
import com.gx.obe.web.service.SupplierEvaluationStepService;
import com.gx.obe.web.service.SupplierOpeningResultService;
import com.gx.obe.web.service.SupplierService;
import com.gx.obe.web.service.evaluation.EvaluationStepService;
import com.gx.obe.web.service.expert.ExpertEvaluationResultService;
import com.gx.obe.web.service.expert.ExpertEvaluationStepService;
import com.gx.obe.web.service.expert.ExpertService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

import de.kupzog.ktable.KTable;
import de.kupzog.ktable.SWTX;
import de.kupzog.ktable.model.CommonKtableCModel;
import de.kupzog.ktable.renderers.CellAlignmentRender;
import de.kupzog.ktable.renderers.CellContentRender;

/**
 * @Description: 推荐中标候选人结果
 * @author guoyr
 */
public class RecommonedResultTableComposite extends Composite{
	
	private TenderProjectEntity tenderProjectEntity;
	private TenderProjectRuleEntity tenderProjectRuleEntity;
	private EvaluationStep evaluationStep;
	private Expert CURRENT_EXPERT;
	private Text evaluationOpinion_text;
	private ExpertService expertService ;
	private EvaluationStepService evaluationStepService = new EvaluationStepService();
	private ProjectRuleService projectRuleService;
	private ExpertEvaluationResultService expertEvaluationResultService;
	private SupplierEvaluationStepService supplierEvaluationStepService ;
//	private ExpertEvaluationStepService expertEvaluationStepService ;
	private SupplierService supplierService = new SupplierService();
	private SupplierEvaluationResultService supplierEvaluationResultService ;
	private SupplierOpeningResultService supplierOpeningResultService ;
	private Composite table_composite;
	private KTable resultKTable;
	private CommonKtableCModel commKTableModel;
	private List<Supplier> supplierList;
	private List<RecommonedFactor> recommonedFactorTitleList;
	private Map<String, List<RecommonedFactor>> supplierRecommonedFactorListMap;
	private String supplierName;
	private Composite composite_3;
	private Composite composite_4;
	private Label notLabel;
	private int fixRowCount = 1;
	private boolean hasPriceEvalNode;
	private boolean hasScoreEvalNode;
	private Composite opinionComposite;
	private int scoreRound = 2;
	private ELink applyReEvaluation_link;
	protected boolean isLimitPriceRecommend = false;
	private boolean isCostPerformance = false;
	private boolean isQualifiedMethod = false;
	private boolean isPreEvaluation = false;
	private boolean hasBillEvalNode = false;
	private String billStepName;
	private boolean hasCreditEvalNode = false;
	private String creditStepName;
	private int entryType = 0;// 入围方式
	private Map<String, SupplierEvaluationStep> supplierEvaluationStepBillResultMap;
	private Map<String, SupplierEvaluationStep> supplierEvaluationStepCreditResultMap;
	private boolean hasRankExpertCountCal = false;
	
	private ExpertEvaluationStepService expertEvaluationStepService = new ExpertEvaluationStepService();
	
	{
		LogUtils.logClass(RecommonedResultTableComposite.class);
	}
	
	/**
	 * @param parentShell
	 * @param tenderProjectEntity
	 * @param tenderProjectRuleEntity
	 * @param evaluationStep
	 * @param listener
	 */
	public RecommonedResultTableComposite(Composite parentShell, int style) {
		super(parentShell,style);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		
		
		composite_3 = new Composite(this, SWT.NONE);
		composite_3.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_composite_3 = new GridLayout(3, false);
		composite_3.setLayout(gl_composite_3);
		
		notLabel = new Label(composite_3, SWT.WRAP);
		notLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		notLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		notLabel.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		
		applyReEvaluation_link = new ELink(composite_3, SWT.NONE);
		applyReEvaluation_link.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
		applyReEvaluation_link.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/hand.png"));
		applyReEvaluation_link.setText(Messages.Button_appllyForBack);
		new Label(composite_3, SWT.NONE);
		applyReEvaluation_link.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				applyReEvaluationAction();
			}
		});
		
		Composite container = new Composite(this, SWT.NONE);
		GridData gd_container = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_container.heightHint = 300;
		container.setLayoutData(gd_container);
		GridLayout gl_container = new GridLayout(1, false);
		gl_container.marginHeight = 1;
		gl_container.marginWidth = 1;
		gl_container.verticalSpacing = 0;
		gl_container.horizontalSpacing = 0;
		container.setLayout(gl_container);
		container.setBackground(ColorConstants.BORDER_LINE_COLOR);
		
		table_composite = new Composite(container, SWT.NONE);
		table_composite.setLayout(new FillLayout(SWT.HORIZONTAL));
		GridData gd_table_composite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		table_composite.setLayoutData(gd_table_composite);
		table_composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		
		opinionComposite = new Composite(this, SWT.NONE);
		GridData gd_opinionComposite = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
		gd_opinionComposite.heightHint = 100;
		opinionComposite.setLayoutData(gd_opinionComposite);
		opinionComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_opinionComposite = new GridLayout(1, false);
		gl_opinionComposite.verticalSpacing = 1;
		gl_opinionComposite.horizontalSpacing = 0;
		gl_opinionComposite.marginHeight = 1;
		gl_opinionComposite.marginWidth = 1;
		opinionComposite.setLayout(gl_opinionComposite);
		opinionComposite.setBackground(ColorConstants.BORDER_LINE_COLOR);
		
		Composite titleComposite = new Composite(opinionComposite, SWT.NONE);
		titleComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		titleComposite.setLayout(new GridLayout(1, false));
		titleComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		titleComposite.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/title_bg.png"));
		
		Label evaluationOpinion_label = new Label(titleComposite, SWT.NONE);
		evaluationOpinion_label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		evaluationOpinion_label.setText(Messages.Title_evaluationOpinion);
		evaluationOpinion_label.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		evaluationOpinion_label.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		
		composite_4 = new Composite(opinionComposite, SWT.NONE);
		GridData gd_composite_4 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_composite_4.heightHint = 20;
		gd_composite_4.minimumHeight = 80;
		composite_4.setLayoutData(gd_composite_4);
		composite_4.setLayout(new GridLayout(1, false));
		composite_4.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		
		evaluationOpinion_text = new Text(composite_4, SWT.READ_ONLY | SWT.WRAP | SWT.V_SCROLL);
		evaluationOpinion_text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		evaluationOpinion_text.setFont(SWTResourceManager.getFont("宋体", 10, SWT.NORMAL));
		evaluationOpinion_text.setText("");
		
		init();
	}
	
	private void init() {
		supplierEvaluationStepService = new SupplierEvaluationStepService();
		expertService = new ExpertService();
		supplierOpeningResultService = new SupplierOpeningResultService();
		expertEvaluationResultService = new ExpertEvaluationResultService();
		supplierEvaluationResultService = new SupplierEvaluationResultService();
	}


	/**
	 * @Description: 切换推荐类型
	 * @author chenxw 
	 */
	protected void switchRecommendTypeAction() {
		resultKTable.redraw();
	}

	/**
	 * @Description: 刷新评标界面
	 * @author guoyr
	 * @param tenderProjectEntity
	 * @param tenderProjectRuleEntity
	 * @param user
	 * @param evaluationStep
	 * @param supplierList
	 * @param evaluationSupplierFactorListener 
	 */
	public void refresh(TenderProjectEntity tenderProjectEntity, TenderProjectRuleEntity _projectRule, EvaluationStep evaluationStep, String OPERATOR_USER_ID){
		this.tenderProjectEntity = tenderProjectEntity;
		this.tenderProjectRuleEntity = _projectRule;
		this.evaluationStep = evaluationStep;
		this.supplierList = getSupplierList();
		if(null == projectRuleService){
			projectRuleService = new ProjectRuleService();
		}
//		final BigDecimal evalMinScore = evaluationStep.getEvaluationParamByKey(com.gx.obe.components.core.enumeration.EvaluationStepEnum.EVALUATION_MIN_SCORE, BigDecimal.class);
//		if (null != evalMinScore && evalMinScore.compareTo(BigDecimal.ZERO) > 0) {
//			CollectionUtils.filter(supplierList, new CollectionUtils.Filter<Supplier>() {
//				public boolean select(Supplier t) {
//					BigDecimal evaluationFinalScore = null != t.getSupplierEvaluationResult() ? t.getSupplierEvaluationResult().getEvaluationFinalScore() : null;
//					return null != evaluationFinalScore && evaluationFinalScore.compareTo(evalMinScore) >= 0;
//				}
//			});
//		}
		supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProjectEntity.getTenderType());
		CURRENT_EXPERT = expertService.getEvaluationStepExpert(tenderProjectEntity.getId(), evaluationStep);
		applyReEvaluation_link.setVisible(false);
		applyReEvaluation_link.setText(MessageFormat.format(Messages.Button_appllyForBackFormat, evaluationStep.getStepName()));
		tenderProjectRuleEntity = projectRuleService.getProjectRule(tenderProjectEntity.getId());
		if(null == tenderProjectRuleEntity)tenderProjectRuleEntity = new TenderProjectRuleEntity();
		scoreRound = tenderProjectRuleEntity.getScoreRoundIntVal();
		isCostPerformance = false;
		boolean isMinMethod = false;
		if(null != tenderProjectRuleEntity.getEvalMethodType() && tenderProjectRuleEntity.getEvalMethodType().equals(TenderProjectEnum.MIN_METHOD)){
			isMinMethod = true;
			if(null == tenderProjectRuleEntity.getOrderFirst() || tenderProjectRuleEntity.getOrderFirst().length() == 0){
				tenderProjectRuleEntity.setOrderFirst(TenderProjectEnum.EVALUATION_PRICE);
	    	}
		}else if(null != tenderProjectRuleEntity.getEvalMethodType() && tenderProjectRuleEntity.getEvalMethodType().equals(TenderProjectEnum.METHOD_COST_PERFORMANCE)){
			if(null == tenderProjectRuleEntity.getOrderFirst() || tenderProjectRuleEntity.getOrderFirst().length() == 0){
				tenderProjectRuleEntity.setOrderFirst(TenderProjectEnum.COST_PERFORMANCE);
			}
		}
		
		//京能屏蔽性价比法，改为合格制
		if (TenderProjectEnum.COST_PERFORMANCE.equals(tenderProjectRuleEntity.getOrderFirst())) {
//			isCostPerformance  = true;
			isQualifiedMethod = true;
		}
		isPreEvaluation = false;
		if(null != tenderProjectEntity.getPhaseType() && tenderProjectEntity.getPhaseType().equals(TenderProjectEnum.PHASE_TYPE_PRE)){
			isPreEvaluation  = true;
		}
		if(null != tenderProjectEntity.getEnterType() && tenderProjectEntity.getEnterType().equals(TenderProjectEnum.ENTRY_TYPE_FIRST_CHOICE)){
			entryType = 1;
		}else if(null != tenderProjectEntity.getEnterType() && tenderProjectEntity.getEnterType().equals(TenderProjectEnum.ENTRY_TYPE_ENTRY)){
			entryType = 2;
		}
		fixRowCount = 1;
		hasScoreEvalNode = false;
		hasPriceEvalNode = false;
		hasBillEvalNode  = false;
		hasCreditEvalNode  = false;
		// 计算供应商最终得分汇总时,是否去除最高分和最低分
		boolean isRemoveTotalMaxAndMinScore = false;
		// 记录评标步骤下是否存在多个专家
		boolean stepHasMoreExpert = false;
		List<EvaluationStep> scoreEvaluationStepList = null;
		List<EvaluationStep> billEvaluationStepList = null;
		List<EvaluationStep> creditEvaluationStepList = null;
		List<EvaluationStep> priceEvaluationStepList = null;
		supplierRecommonedFactorListMap = new HashMap<String, List<RecommonedFactor>>();
		if(!isMinMethod){
			scoreEvaluationStepList = evaluationStepService.getEvaluationStepListByType(tenderProjectEntity.getId(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.DETAIL);
			if(null != scoreEvaluationStepList && scoreEvaluationStepList.size() > 0){
				hasScoreEvalNode = true;
			}
			// 计算供应商最终得分汇总时,是否去除最高分和最低分
			if(hasScoreEvalNode && null != evaluationStep.getSummaryType() && evaluationStep.getSummaryType().equals(com.gx.obe.components.core.enumeration.EvaluationStepEnum.SUMMARY_REMOVE_MAX_MIN)){
				isRemoveTotalMaxAndMinScore = true;
			}
			priceEvaluationStepList = evaluationStepService.getEvaluationStepListByType(tenderProjectEntity.getId(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.PRICE);
			if(null != priceEvaluationStepList && priceEvaluationStepList.size() > 0){
				hasPriceEvalNode = true;
			}
			
			billEvaluationStepList = evaluationStepService.getEvaluationStepListByType(tenderProjectEntity.getId(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.BILL);
			if(null != billEvaluationStepList && billEvaluationStepList.size() > 0){
				supplierEvaluationStepBillResultMap = new HashMap<String, SupplierEvaluationStep>();
				hasBillEvalNode = true;
				if(billEvaluationStepList.size() == 1){
					billStepName = billEvaluationStepList.get(0).getStepName();
				}else {
					billStepName = PropertiesUtils.getEnum(I18NEnum.BILL_FACTOR, "清单")+"得分";
				}
			}
		}
		creditEvaluationStepList = evaluationStepService.getEvaluationStepListByType(tenderProjectEntity.getId(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.CREDIT);
		if(null != creditEvaluationStepList && creditEvaluationStepList.size() > 0){
			supplierEvaluationStepCreditResultMap = new HashMap<String, SupplierEvaluationStep>();
			hasCreditEvalNode = true;
			if(creditEvaluationStepList.size() == 1){
				creditStepName = creditEvaluationStepList.get(0).getStepName();
			}else {
				creditStepName = PropertiesUtils.getEnum(I18NEnum.CREDIT_FACTOR, "综合信用")+"得分";
			}
		}
		
		
		recommonedFactorTitleList = new ArrayList<RecommonedFactor>();
		
		for(int supplierIndex = 0; supplierIndex < supplierList.size(); supplierIndex++){
			Supplier supplier = supplierList.get(supplierIndex);
			List<RecommonedFactor> recommonedFactorList = new ArrayList<RecommonedFactor>();
			// 查询投标人所有评标步骤的评标结果
			List<SupplierEvaluationStep> supplierEvaluationStepList = supplierEvaluationStepService.getSupplierEvaluationStepByTenderIdAndSupplierId(tenderProjectEntity.getId(),supplier.getId());
		
			// 供应商最终得分汇总
			BigDecimal supplierEvaluationFinalScoreSum = null;
			// 供应商得分汇总
			BigDecimal supplierEvaluationScoreSum = null;
			
			// 价格评标步骤
			BigDecimal priceEvaluationScore = null;
			BigDecimal priceEvaluationFinalScore = null;
			if(null != priceEvaluationStepList && priceEvaluationStepList.size() > 0){
				for(EvaluationStep priceEvaluationStep : priceEvaluationStepList){
					
					// 供应商当前评标节点终得分平均分
					BigDecimal stepAvgFinalScore = null;
					// 供应商当前评标节点平均分
					BigDecimal stepAvgScore = null;

					SupplierEvaluationStep supplierEvaluationStep = null;
					// 如果当前评标步骤评审完成，则以供应商当前评标步骤的结果为主（有可能去除了最高分或最低分）
					if(null != supplierEvaluationStepList && null != priceEvaluationStep.getStepStatus() && priceEvaluationStep.getStepStatus().equals(CommonEnum.COMPLETE)){
						for(SupplierEvaluationStep stepResult : supplierEvaluationStepList){
							if(stepResult.getStepId().equals(priceEvaluationStep.getId())){
								supplierEvaluationStep = stepResult;
								break;
							}
						}
					}
					if(null != supplierEvaluationStep){
						stepAvgScore = supplierEvaluationStep.getEvaluationScore();
						stepAvgFinalScore = supplierEvaluationStep.getEvaluationFinalScore();
					}else {
						BigDecimal[] stepAvgResult = expertEvaluationResultService.getSupplierStepAvgResult(tenderProjectEntity.getId(), priceEvaluationStep.getId(), supplier.getId(), false);
						if(null != stepAvgResult){
							stepAvgScore = stepAvgResult[0];
							stepAvgFinalScore = stepAvgResult[1];
						}
					}
					
					priceEvaluationScore = BigDecimalUtils.add(priceEvaluationScore, stepAvgScore);
					priceEvaluationFinalScore = BigDecimalUtils.add(priceEvaluationFinalScore, stepAvgFinalScore);
				}
			}
			SupplierEvaluationResult supplierEvaluationResult = supplier.getSupplierEvaluationResult();
			if(null == supplierEvaluationResult){
				supplierEvaluationResult = new SupplierEvaluationResult();
				supplierEvaluationResult.setTenderId(tenderProjectEntity.getId());
				supplierEvaluationResult.setSupplierId(supplier.getId());
				supplier.setSupplierEvaluationResult(supplierEvaluationResult);
			}
			supplierEvaluationResult.setPriceScore(priceEvaluationScore);
			supplierEvaluationResult.setPriceFinalScore(priceEvaluationFinalScore);
			
			
			// 记每位专家的打分汇总(只用于存在按总分去除最高分和最低分时)
			Map<String, BigDecimal> expertEvaluationFinalScoreMap = null;
			List<String> expertIdList = null;
			if(isRemoveTotalMaxAndMinScore){
				expertIdList = new ArrayList<String>();
				expertEvaluationFinalScoreMap = new HashMap<String, BigDecimal>();
			}
			// 所有打分评标步骤
			if(null != scoreEvaluationStepList && scoreEvaluationStepList.size() > 0){
				for(EvaluationStep scoreEvaluationStep : scoreEvaluationStepList){
					SupplierEvaluationStep supplierEvaluationStep = null;
					// 如果当前评标步骤评审完成，则以供应商当前评标步骤的结果为主（有可能去除了最高分或最低分）
					if(null != supplierEvaluationStepList && null != scoreEvaluationStep.getStepStatus() && scoreEvaluationStep.getStepStatus().equals(CommonEnum.COMPLETE)){
						for(SupplierEvaluationStep stepResult : supplierEvaluationStepList){
							if(stepResult.getStepId().equals(scoreEvaluationStep.getId())){
								supplierEvaluationStep = stepResult;
								break;
							}
						}
					}
					// 供应商当前评标节点终得分平均分
					BigDecimal stepAvgEvaluationFinalScoreSum = null;
					// 供应商当前评标节点平均分
					BigDecimal stepAvgEvaluationScoreSum = null;
					
					if(null != scoreEvaluationStep.getSummaryType() && scoreEvaluationStep.getSummaryType().equals(com.gx.obe.components.core.enumeration.EvaluationStepEnum.SUMMARY_SCORE_BY_QUALIFIED)){
						if(null != supplierEvaluationStep){
							stepAvgEvaluationScoreSum = supplierEvaluationStep.getEvaluationScore();
							stepAvgEvaluationFinalScoreSum = supplierEvaluationStep.getEvaluationFinalScore();
						}
						// 当前打分步骤专家的得分
						RecommonedFactor recommonedFactor = new RecommonedFactor(scoreEvaluationStep, null, scoreEvaluationStep.getStepName());
						recommonedFactor.setSupplierEvaluationStep(supplierEvaluationStep);
						recommonedFactorList.add(recommonedFactor);
					}else {
						boolean evalByGroup = scoreEvaluationStep.getEvaluationParamByKey(com.gx.obe.components.core.enumeration.EvaluationStepEnum.PARAM_EVAL_BY_GROUP, Boolean.class);
						if(evalByGroup){
							scoreEvaluationStep.setSummaryType(com.gx.obe.components.core.enumeration.EvaluationStepEnum.SUMMARY_BY_FACTOR_SUM);
						}
						List<AuthUser> userList = expertService.getEvaluationStepUserList(tenderProjectEntity.getId(), scoreEvaluationStep);
						int expertCount = 0;
						if(null != userList && userList.size() > 0){
							// 供应商当前评标节点所有专家的最终得分汇总
							BigDecimal expertEvaluationFinalScoreSum = null;
							// 供应商当前评标节点所有专家的得分汇总
							BigDecimal expertEvaluationScoreSum = null;
							List<ExpertEvaluationResult> expertEvaluationResultList = expertEvaluationResultService.getSupplierStepAllExpertEvaluationResultList(tenderProjectEntity.getId(), scoreEvaluationStep.getId(), supplier.getId());
							for(int i = 0; i < userList.size(); i++){
								AuthUser user = userList.get(i);
								if(isRemoveTotalMaxAndMinScore && !expertIdList.contains(user.getId())){
									expertIdList.add(user.getId());
								}
								// 当前打分步骤专家的得分
								RecommonedFactor recommonedFactor = new RecommonedFactor(scoreEvaluationStep, user, userList.size() == 1 ? scoreEvaluationStep.getStepName() : user.getUserName());
								if(i == 0 && userList.size() > 1){
									stepHasMoreExpert = true;
									int mergerCount = userList.size();
									if (scoreEvaluationStepList.size() > 1 || hasPriceEvalNode) {
										mergerCount++;
									}
									if (hasRankExpertCountCal) {
										mergerCount++;
									}
									recommonedFactor.setMergerCount(mergerCount);
								}
								ExpertEvaluationResult expertEvaluationResult = null;
								for(ExpertEvaluationResult result : expertEvaluationResultList){
									if(result.getUserId().equals(user.getId())){
										expertEvaluationResult = result;
										break;
									}
								}
								
								if(null != expertEvaluationResult && null != expertEvaluationResult.getEvaluationScore()){
									expertEvaluationFinalScoreSum = BigDecimalUtils.add(expertEvaluationFinalScoreSum, expertEvaluationResult.getEvaluationFinalScore());
									expertEvaluationScoreSum = BigDecimalUtils.add(expertEvaluationScoreSum, expertEvaluationResult.getEvaluationScore());
									
									if(isRemoveTotalMaxAndMinScore){
										// 记录每们专家的 评分汇总(只用于存在按总分去除最高分和最低分时)
										BigDecimal expertFinalScoreSum = expertEvaluationFinalScoreMap.get(user.getId());
//										expertFinalScoreSum = BigDecimalUtils.add(expertFinalScoreSum, expertEvaluationFinalScoreSum);
										expertFinalScoreSum = BigDecimalUtils.add(expertFinalScoreSum, expertEvaluationResult.getEvaluationFinalScore());
										expertEvaluationFinalScoreMap.put(user.getId(), expertFinalScoreSum);
									}
									expertCount ++;
								}
								recommonedFactor.setExpertEvaluationResult(expertEvaluationResult);
								recommonedFactorList.add(recommonedFactor);
							}
							
							// 默认以所有专家的平均分作为当前评标步骤的得分
							if(null != expertEvaluationScoreSum && expertCount > 0){
								stepAvgEvaluationScoreSum = BigDecimalUtils.div(expertEvaluationScoreSum, new BigDecimal(expertCount), scoreRound);
								stepAvgEvaluationFinalScoreSum = BigDecimalUtils.div(expertEvaluationFinalScoreSum, new BigDecimal(expertCount), scoreRound);
							}
							String scoreSumName = "平均分";
							if(null != scoreEvaluationStep.getSummaryType() && scoreEvaluationStep.getSummaryType().equals(com.gx.obe.components.core.enumeration.EvaluationStepEnum.SUMMARY_BY_FACTOR_SUM)){
								scoreSumName = Messages.TotalScore;
							}
							
							// 如果当前评标步骤评审完成，则以供应商当前评标步骤的结果为主（有可能去除了最高分或最低分）
							if(null != supplierEvaluationStep && CommonEnum.COMPLETE.equals(scoreEvaluationStep.getStepStatus())){
								stepAvgEvaluationScoreSum = supplierEvaluationStep.getEvaluationScore();
								stepAvgEvaluationFinalScoreSum = supplierEvaluationStep.getEvaluationFinalScore();
							}else {
								supplierEvaluationStep = new SupplierEvaluationStep(); 
								BigDecimal[] stepAvgResult = null;
								if(expertCount > 1 && null != scoreEvaluationStep.getSummaryType() && scoreEvaluationStep.getSummaryType().equals(com.gx.obe.components.core.enumeration.EvaluationStepEnum.SUMMARY_IGNORE_OVER_WARNING_CORE)){
									// 如果取掉超出最小偏差率
									stepAvgResult = expertEvaluationResultService.getSupplierStepAvgResult(tenderProjectEntity.getId(), scoreEvaluationStep.getId(), supplier.getId(), false);
									if(null != stepAvgResult){
										stepAvgEvaluationScoreSum = stepAvgResult[0];
										stepAvgEvaluationFinalScoreSum = stepAvgResult[1];
									}else {
										stepAvgResult = new BigDecimal[]{stepAvgEvaluationScoreSum, stepAvgEvaluationFinalScoreSum};
									}
									BigDecimal warningRange = scoreEvaluationStep.getEvaluationParamByKey(com.gx.obe.components.core.enumeration.EvaluationStepEnum.PARAM_WARNING_RANGE, BigDecimal.class);
									if(null != warningRange && warningRange.compareTo(BigDecimal.ZERO) != 0){
										// 转换为百分率
										warningRange = BigDecimalUtils.div(warningRange,  new BigDecimal(100));
										
										if(null != warningRange){
											int noIgnoreCount = 0;
											BigDecimal supplierNoignoreEvaluationScoreSum = BigDecimal.ZERO;
											BigDecimal supplierNoignoreEvaluationFinalScoreSum = BigDecimal.ZERO;
											BigDecimal expertMinScore = null;
											BigDecimal expertMinFinalScore = null;
											for(ExpertEvaluationResult expertEvaluationResult : expertEvaluationResultList){
												if(null != expertEvaluationResult.getEvaluationScore()){
													BigDecimal percentValue = BigDecimal.ZERO;
													if(null != stepAvgEvaluationScoreSum && stepAvgEvaluationScoreSum.compareTo(BigDecimal.ZERO) != 0){
														percentValue = BigDecimalUtils.div(BigDecimalUtils.sub(expertEvaluationResult.getEvaluationScore(), stepAvgEvaluationScoreSum), stepAvgEvaluationScoreSum).abs();//偏差率绝对值
													}
													if(percentValue.compareTo(warningRange) < 0){
														supplierNoignoreEvaluationScoreSum = BigDecimalUtils.add(supplierNoignoreEvaluationScoreSum, expertEvaluationResult.getEvaluationScore());
														supplierNoignoreEvaluationFinalScoreSum = BigDecimalUtils.add(supplierNoignoreEvaluationFinalScoreSum, expertEvaluationResult.getEvaluationFinalScore());
														noIgnoreCount ++;
														
													}
													// 记录分项的最小值 
													if(null == expertMinScore || expertEvaluationResult.getEvaluationScore().compareTo(expertMinScore) < 0){
														expertMinScore = expertEvaluationResult.getEvaluationScore();
														expertMinFinalScore = expertEvaluationResult.getEvaluationFinalScore();
													}
												}
											}
											if(noIgnoreCount > 0){
												stepAvgResult[0] = BigDecimalUtils.div(supplierNoignoreEvaluationScoreSum, new BigDecimal(noIgnoreCount), tenderProjectRuleEntity.getScoreRoundIntVal());
												stepAvgResult[1] = BigDecimalUtils.div(supplierNoignoreEvaluationFinalScoreSum, new BigDecimal(noIgnoreCount), tenderProjectRuleEntity.getScoreRoundIntVal());
											}else {
												boolean useMinScore = evaluationStep.getEvaluationParamByKey(EvaluationStepEnum.PARAM_IGNORE_OVER_WARNING_USE_MIN, Boolean.class, false);
												if(useMinScore) {
													stepAvgResult[0] = null != expertMinScore ? expertMinScore : BigDecimal.ZERO;
													stepAvgResult[1] = null != expertMinFinalScore ? expertMinFinalScore : BigDecimal.ZERO;
												}
											}
										}
									}
									
								}else if(null != scoreEvaluationStep.getSummaryType() && scoreEvaluationStep.getSummaryType().equals(com.gx.obe.components.core.enumeration.EvaluationStepEnum.SUMMARY_REMOVE_FACTOR_MAX_MIN)){
									stepAvgResult = expertEvaluationResultService.getSupplierStepAvgResultByFactor(tenderProjectEntity.getId(), scoreEvaluationStep.getId(), supplier.getId(), expertCount > 2 ? true : false);
								}else if(null != scoreEvaluationStep.getSummaryType() && scoreEvaluationStep.getSummaryType().equals(com.gx.obe.components.core.enumeration.EvaluationStepEnum.SUMMARY_BY_FACTOR_SUM)){
									scoreSumName = Messages.TotalScore;
									stepAvgResult = expertEvaluationResultService.getSupplierStepAvgResultByFactor(tenderProjectEntity.getId(), scoreEvaluationStep.getId(), supplier.getId(), false);
								}else {
									boolean removeMaxAndMin = null != scoreEvaluationStep.getSummaryType() && scoreEvaluationStep.getSummaryType().equals(com.gx.obe.components.core.enumeration.EvaluationStepEnum.SUMMARY_REMOVE_MAX_MIN);
									stepAvgResult = expertEvaluationResultService.getSupplierStepAvgResult(tenderProjectEntity.getId(), scoreEvaluationStep.getId(), supplier.getId(), expertCount > 2 ? removeMaxAndMin : false);
								}
								
								if(null != stepAvgResult){
									stepAvgEvaluationScoreSum = BigDecimalUtils.round(stepAvgResult[0], scoreRound);
									stepAvgEvaluationFinalScoreSum = BigDecimalUtils.round(stepAvgResult[1], scoreRound);
								}
								supplierEvaluationStep.setEvaluationScore(stepAvgEvaluationScoreSum);
								supplierEvaluationStep.setEvaluationFinalScore(stepAvgEvaluationFinalScoreSum);
							}
							if(userList.size() > 1 && (scoreEvaluationStepList.size() > 1 || hasPriceEvalNode)){
								
								// 评标步骤汇总分
								RecommonedFactor recommonedFactor = new RecommonedFactor(scoreEvaluationStep, null, scoreSumName);
								recommonedFactor.setSupplierEvaluationStep(supplierEvaluationStep);
								recommonedFactorList.add(recommonedFactor);
							}
							if(hasRankExpertCountCal){
								RecommonedFactor recommonedFactor = new RecommonedFactor(scoreEvaluationStep, null, "排名情况");
								recommonedFactor.setRankExpertCount(getRankExpertCount(expertEvaluationResultList));
								recommonedFactorList.add(recommonedFactor);
							}
						}else {
							// 当前打分步骤专家的得分
							RecommonedFactor recommonedFactor = new RecommonedFactor(scoreEvaluationStep, null, scoreEvaluationStep.getStepName());
							recommonedFactor.setSupplierEvaluationStep(supplierEvaluationStep);
							recommonedFactorList.add(recommonedFactor);
						}
					}
					supplierEvaluationScoreSum = BigDecimalUtils.add(supplierEvaluationScoreSum, stepAvgEvaluationScoreSum);
					supplierEvaluationFinalScoreSum = BigDecimalUtils.add(supplierEvaluationFinalScoreSum, stepAvgEvaluationFinalScoreSum);
				}
			}
			
			// 清单评审
			if (null != billEvaluationStepList && billEvaluationStepList.size() > 0) {
				SupplierEvaluationStep supplierEvaluationStep = null;
				if(billEvaluationStepList.size() > 1){
					supplierEvaluationStep = new SupplierEvaluationStep();
					supplierEvaluationStep.setSupplierId(supplier.getId());
				}
				for(EvaluationStep billEvaluationStep : billEvaluationStepList){
					if(null != billEvaluationStep.getStepStatus() && billEvaluationStep.getStepStatus().equals(CommonEnum.COMPLETE)){
						for(SupplierEvaluationStep stepResult : supplierEvaluationStepList){
							if(stepResult.getStepId().equals(billEvaluationStep.getId())){
								if(null != supplierEvaluationStep){
									supplierEvaluationStep.setEvaluationScore(BigDecimalUtils.add(supplierEvaluationStep.getEvaluationScore(), stepResult.getEvaluationScore()));
									supplierEvaluationStep.setEvaluationFinalScore(BigDecimalUtils.add(supplierEvaluationStep.getEvaluationFinalScore(), stepResult.getEvaluationFinalScore()));
								}else {
									supplierEvaluationStep = stepResult;
								}
								break;
							}
						}
					}
				}
				if (null != supplierEvaluationStep) {
					supplierEvaluationStepBillResultMap.put(supplier.getId(), supplierEvaluationStep);
					
					// 供应商最终得分是评分的汇总+价格得分+清单得分
					supplierEvaluationScoreSum = BigDecimalUtils.add(supplierEvaluationScoreSum, supplierEvaluationStep.getEvaluationScore());
					supplierEvaluationFinalScoreSum = BigDecimalUtils.add(supplierEvaluationFinalScoreSum, supplierEvaluationStep.getEvaluationFinalScore());
				}
			}
			
			// 信用评审
			if (null != creditEvaluationStepList && creditEvaluationStepList.size() > 0) {
				SupplierEvaluationStep supplierEvaluationStep = null;
				if(creditEvaluationStepList.size() > 1){
					supplierEvaluationStep = new SupplierEvaluationStep();
					supplierEvaluationStep.setSupplierId(supplier.getId());
				}
				for(EvaluationStep creditEvaluationStep : creditEvaluationStepList){
					if(null != creditEvaluationStep.getStepStatus() && creditEvaluationStep.getStepStatus().equals(CommonEnum.COMPLETE)){
						for(SupplierEvaluationStep stepResult : supplierEvaluationStepList){
							if(stepResult.getStepId().equals(creditEvaluationStep.getId())){
								if(null != supplierEvaluationStep){
									supplierEvaluationStep.setEvaluationScore(BigDecimalUtils.add(supplierEvaluationStep.getEvaluationScore(), stepResult.getEvaluationScore()));
									supplierEvaluationStep.setEvaluationFinalScore(BigDecimalUtils.add(supplierEvaluationStep.getEvaluationFinalScore(), stepResult.getEvaluationFinalScore()));
								}else {
									supplierEvaluationStep = stepResult;
								}
								break;
							}
						}
					}
				}
				if (null != supplierEvaluationStep) {
					supplierEvaluationStepCreditResultMap.put(supplier.getId(), supplierEvaluationStep);
					
					// 供应商最终得分是评分的汇总+价格得分+清单得分 + 信用得分
					supplierEvaluationScoreSum = BigDecimalUtils.add(supplierEvaluationScoreSum, supplierEvaluationStep.getEvaluationScore());
					supplierEvaluationFinalScoreSum = BigDecimalUtils.add(supplierEvaluationFinalScoreSum, supplierEvaluationStep.getEvaluationFinalScore());
				}
			}
			
			
			// 如果未完成推荐，则根据计算的分显示，已完成，则直接显示最终结果
			if(!CommonEnum.COMPLETE.equals(evaluationStep.getStepStatus())){
				// 供应商最终得分是评分的汇总+价格得分
				supplierEvaluationScoreSum = BigDecimalUtils.add(supplierEvaluationScoreSum, supplierEvaluationResult.getPriceScore());
				supplierEvaluationFinalScoreSum = BigDecimalUtils.add(supplierEvaluationFinalScoreSum, supplierEvaluationResult.getPriceFinalScore());
				supplierEvaluationFinalScoreSum = BigDecimalUtils.round(supplierEvaluationFinalScoreSum, scoreRound);
				supplierEvaluationResult.setEvaluationFinalScore(supplierEvaluationFinalScoreSum);
				supplierEvaluationScoreSum = BigDecimalUtils.round(supplierEvaluationScoreSum, scoreRound);
				supplierEvaluationResult.setEvaluationScore(supplierEvaluationScoreSum);
				
				// 如果打分专家超过2人,且去除一个汇总的最高分和最低分
				if(isRemoveTotalMaxAndMinScore && expertIdList.size() > 2){
					BigDecimal minScore = BigDecimal.ZERO;
					BigDecimal maxScore = BigDecimal.ZERO;
					BigDecimal finalScoreSum = null;
					boolean hasMin = false;
					for(Entry<String, BigDecimal> expertFinalScoreEntry : expertEvaluationFinalScoreMap.entrySet()){
						BigDecimal expertFinalScore = expertFinalScoreEntry.getValue();
						expertFinalScore = BigDecimalUtils.add(expertFinalScore, supplierEvaluationResult.getPriceFinalScore());
						if(null != expertFinalScore){
							if(!hasMin){
								minScore = expertFinalScore;
								hasMin = true;
							}
							if(expertFinalScore.compareTo(minScore) < 0){
								minScore = expertFinalScore;
							}
							if(expertFinalScore.compareTo(maxScore) > 0){
								maxScore = expertFinalScore;
							}
							finalScoreSum = BigDecimalUtils.add(finalScoreSum, expertFinalScore);
						}
						
					}
					if(null != finalScoreSum){
						supplierEvaluationResult.setEvaluationFinalScore(BigDecimalUtils.div(finalScoreSum, new BigDecimal(expertIdList.size() -2), scoreRound));
					}
				}
			}
			supplierRecommonedFactorListMap.put(supplier.getId(), recommonedFactorList);
			if(supplierIndex == 0){
				recommonedFactorTitleList = recommonedFactorList;
			}
		}

		if(stepHasMoreExpert){
			fixRowCount = 2;
			if(scoreEvaluationStepList.size() ==  1 && !hasPriceEvalNode){
				fixRowCount = 1;
			}
			
		}
		// 排序后设置默认推荐
		Integer recommendCount = tenderProjectRuleEntity.getRecommendCountNotNull();
		if(null == recommendCount || recommendCount < 1){
			tenderProjectRuleEntity.setRecommendCount(1);
		}
		boolean complate = null != evaluationStep.getStepStatus() && evaluationStep.getStepStatus().equals(CommonEnum.COMPLETE);
		if(complate){
			if(null != OPERATOR_USER_ID && OPERATOR_USER_ID.equals(Constants.USER.getId())){
				applyReEvaluation_link.setVisible(true);
			}else{
				((GridData)applyReEvaluation_link.getLayoutData()).exclude = true;
				applyReEvaluation_link.setVisible(false);
			}
		}
		MySort sortComparator = new MySort();
		Collections.sort(supplierList, sortComparator);
		int recommonedCount = 0;
		int sortNo = 1;
		for(Supplier supplier : supplierList){
			SupplierEvaluationResult supplierEvaluationResult = supplier.getSupplierEvaluationResult();
			if(null != supplierEvaluationResult){
				if(StringUtils.isEmpty(supplierEvaluationResult.getWinBidRecommend())){
//					hasWinBidRecommend = false;
				}
				// 如果未推荐,则不显示推荐结果
				if(complate){
					if(null != supplierEvaluationResult.getRecommend() && supplierEvaluationResult.getRecommend().equals(SupplierEnum.RECOMMONED_YES)){
						recommonedCount ++;
					}
				}else {
					supplierEvaluationResult.setSortNo(sortNo);
					supplierEvaluationResult.setRecommend(null);
				}
			}
			
//			if(tenderProjectRuleEntity.getRecommendCount() > 0){
//				if(sortNo <= tenderProjectRuleEntity.getRecommendCount()){
//					supplier.getSupplierEvaluationResult().setRecommend(SupplierEnum.RECOMMONED_YES);
//				}
//			}
			sortNo ++;
		}
		drawEvaluationTable();
		if(StringUtils.hasLength(tenderProjectEntity.getEvaluationOpinion())){
			evaluationOpinion_text.setText(tenderProjectEntity.getEvaluationOpinion());
		}else {
			WidgetUtils.setControlVisible(opinionComposite, false);
		}
		StringBuilder msg = new StringBuilder();
		if(null != evaluationStep.getStepStatus() &&evaluationStep.getStepStatus().equals(CommonEnum.COMPLETE)){
			msg.append(MessageFormat.format(Messages.Tips_progressComplate, evaluationStep.getStepName()));
		}else if(null != evaluationStep.getStepStatus() &&evaluationStep.getStepStatus().equals(CommonEnum.IN_PROGRESS)){
			msg.append(MessageFormat.format(Messages.Tips_progressIn, evaluationStep.getStepName()));
		}else {
			msg.append(MessageFormat.format(Messages.Tips_progressNotStart, evaluationStep.getStepName()));
		}
		notLabel.setText(msg.toString());
	}
	
	/**
	 * @Description: 获取排名专家数量
	 * @author chenxw
	 * @param expertEvaluationResultList
	 * @return 
	 */
	private String getRankExpertCount(List<ExpertEvaluationResult> expertEvaluationResultList) {
		Map<Integer, Integer> map = new TreeMap<Integer, Integer>();
		for (ExpertEvaluationResult expertEvaluationResult : expertEvaluationResultList) {
			int sortNo = expertEvaluationResult.getSortNoIntValue();
			if (sortNo != 0) {
				if (map.containsKey(sortNo)) {
					map.put(sortNo, map.get(sortNo) + 1);
				} else {
					map.put(sortNo, 1);
				}
			}
		}
		StringBuilder rankExpertCount = new StringBuilder();
		for (int sortNo : map.keySet()) {
			if (rankExpertCount.length() != 0) {
				rankExpertCount.append("、");
			}
			rankExpertCount.append(String.valueOf(sortNo));
			rankExpertCount.append("×");
			rankExpertCount.append(String.valueOf(map.get(sortNo)));
		}
		return rankExpertCount.toString();
	}
	
	/**
	 * @Description: 实现评审表格
	 * @author guoyr
	 */
	private void drawEvaluationTable(){
		
		resultKTable = new KTable(table_composite, SWTX.AUTO_SCROLL | SWT.FULL_SELECTION | SWTX.EDIT_ON_KEY);
		resultKTable.setColorLeftBorder(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		resultKTable.setColorTopBorder(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		resultKTable.setColorRightBorder(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		
		int contentCol = isQualifiedMethod ? 1 : 2;//排序&推荐
		if (hasScoreEvalNode) {
			contentCol += recommonedFactorTitleList.size();//得分汇总
		}
		
		if (hasBillEvalNode) {
			contentCol += 1;//清单得分
		}
		if (hasCreditEvalNode) {
			contentCol += 1;//信用得分
		}
		
		if (hasPriceEvalNode) {
			contentCol += 1;//价格得分
		}
		boolean showFinalScore = false;
		if (hasScoreEvalNode || hasPriceEvalNode || hasBillEvalNode) {
			contentCol += 1;//最终得分
			showFinalScore = true;
		}
		if(isCostPerformance){
			contentCol += 1;//性价比
		}
		boolean showEvaluationPrice = false;
		if (!isPreEvaluation) {
			contentCol += 1;//评标价
			showEvaluationPrice = true;
			if(hasBillEvalNode && !hasPriceEvalNode){
				showEvaluationPrice = false;
				contentCol -= 1;//评标价
			}
		}
		commKTableModel = new CommonKtableCModel(resultKTable, fixRowCount, 1, supplierList.size() , contentCol);
		commKTableModel.setColumnMaxWidth(0, 250);

		int scoreStartCol = hasScoreEvalNode ? recommonedFactorTitleList.size() : 0;
		// 评标价|价格得分|最终得分|性价比|排序|推荐
		final int evalBillFinalScoreCol = hasBillEvalNode ? (++scoreStartCol) : 0;
		final int creditScoreCol = hasCreditEvalNode ? (++scoreStartCol) : 0;
		final int evalPriceCol = showEvaluationPrice ? (++scoreStartCol) : 0;
		final int evalPriceFinalScoreCol = hasPriceEvalNode ? (++scoreStartCol) : 0;
		final int evaluationFinalScore = showFinalScore ? (++scoreStartCol) : 0;
		final int costPerformance = isCostPerformance ? (++scoreStartCol) : 0;
		final int sortNoCol = isQualifiedMethod ? 0 : commKTableModel.getColCount() -2;
		final int recommonedCol = commKTableModel.getColCount() -1;
		if(fixRowCount > 1){
			commKTableModel.addRowSpan(0, 0, fixRowCount);
			if(evalBillFinalScoreCol > 0){
				commKTableModel.addRowSpan(0, evalBillFinalScoreCol, fixRowCount);
			}
			if(creditScoreCol > 0){
				commKTableModel.addRowSpan(0, creditScoreCol, fixRowCount);
			}

			if(evalPriceCol > 0){
				commKTableModel.addRowSpan(0, evalPriceCol, fixRowCount);
			}
			if(evalPriceFinalScoreCol > 0){
				commKTableModel.addRowSpan(0, evalPriceFinalScoreCol, fixRowCount);
			}
			if(evaluationFinalScore > 0){
				commKTableModel.addRowSpan(0, evaluationFinalScore, fixRowCount);
			}
			if(costPerformance > 0){
				commKTableModel.addRowSpan(0, costPerformance, fixRowCount);
			}
			if(sortNoCol > 0){
				commKTableModel.addRowSpan(0, sortNoCol, fixRowCount);
			}
			if(recommonedCol > 0){
				commKTableModel.addRowSpan(0, recommonedCol, fixRowCount);
			}
			int col = commKTableModel.getFixedColumnCount();
			String curStepId = null;
			for(RecommonedFactor recommonedFactor : recommonedFactorTitleList){
				if(recommonedFactor.getMergerCount() > 1){
					curStepId = recommonedFactor.getEvaluationStep().getId();
					commKTableModel.addColSpan(0, col, recommonedFactor.getMergerCount());
				}else if(null != recommonedFactor.getEvaluationStep() && (null == curStepId || (null != curStepId && !curStepId.equals(recommonedFactor.getEvaluationStep().getId())) && recommonedFactor.getMergerCount() == 0)){
					commKTableModel.addRowSpan(0, col, 2);
				}
				col++;
			}
		}
		
		// 设置单元格内容
		CellContentRender contentRender = new CellContentRender(){
			public Object receiveContent(int row, int col) {
				if(row < commKTableModel.getFixedHeaderRowCount()){
					if(col < commKTableModel.getFixedColumnCount()){
						return supplierName;
					}else if(col >= commKTableModel.getFixedColumnCount() && col < commKTableModel.getFixedColumnCount() + recommonedFactorTitleList.size()){
						RecommonedFactor recommonedFactor = recommonedFactorTitleList.get(col - commKTableModel.getFixedColumnCount());
						if(null != recommonedFactor){
							if(fixRowCount > 1 && row == 0){
								return recommonedFactor.getEvaluationStep().getStepName();
							}else {
								return recommonedFactor.getFactorName();
							}
						}
					}else if(col == evalPriceCol){
						return Messages.EvaluationPrice+"（"+tenderProjectRuleEntity.getPriceUnitUseDefault()+"）";
					}else if(col == evalBillFinalScoreCol) {
						return billStepName;
					}else if(col == creditScoreCol) {
						return creditStepName;
					}else if(col == evalPriceFinalScoreCol){
						return Messages.PriceScore;
					}else if(col == evaluationFinalScore){
						return Messages.FinalScore;
					}else if(col == costPerformance) {
						return "性价比";
					}else if(col == sortNoCol && sortNoCol > 0){
						return isLimitPriceRecommend ? "限中标排名" : Messages.SortNo;
					}else if(col == recommonedCol){
						if(entryType == 1){
							return Messages.Recommoned_chose;
						}else if(entryType == 2){
							return Messages.Recommoned_shortlisted;
						}else {
							return isLimitPriceRecommend ? "限中标推荐" : Messages.Recommoned_recommoned;
						}
					}
				}else {
					Supplier supplier = supplierList.get(row - commKTableModel.getFixedHeaderRowCount());
					SupplierOpeningResult supplierOpeningResult = supplier.getSupplierOpeningResult();
					SupplierEvaluationResult supplierEvaluationResult = supplier.getSupplierEvaluationResult();
					if(col < commKTableModel.getFixedColumnCount()){
						return supplier.getSupplierName();
					}else if(col >= commKTableModel.getFixedColumnCount() && col < commKTableModel.getFixedColumnCount() + recommonedFactorTitleList.size()){
						List<RecommonedFactor> recommonedFactorList = supplierRecommonedFactorListMap.get(supplier.getId());
						RecommonedFactor recommonedFactor = recommonedFactorList.get(col - commKTableModel.getFixedColumnCount());
						if(null != recommonedFactor){
							if(null != recommonedFactor.getExpertEvaluationResult() && null != recommonedFactor.getExpertEvaluationResult().getEvaluationFinalScore()){
								// 每个专家的打分结果
								return NumberFormatUtils.format(recommonedFactor.getExpertEvaluationResult().getEvaluationFinalScore(), scoreRound);
							}else if(null != recommonedFactor.getSupplierEvaluationStep() && null != recommonedFactor.getSupplierEvaluationStep().getEvaluationFinalScore()){
								// 供应商当前步骤的汇总
								return NumberFormatUtils.format(recommonedFactor.getSupplierEvaluationStep().getEvaluationFinalScore(), scoreRound);
							} else if (null != recommonedFactor.getRankExpertCount()) {
								return recommonedFactor.getRankExpertCount();
							}
						}
					}else if(col == evalBillFinalScoreCol){
						SupplierEvaluationStep supplierEvaluationStep = supplierEvaluationStepBillResultMap.get(supplier.getId());
						if (null != supplierEvaluationStep) {
							return NumberFormatUtils.format(supplierEvaluationStep.getEvaluationFinalScore(), scoreRound);
						}
					}else if(col == creditScoreCol){
						SupplierEvaluationStep supplierEvaluationStep = supplierEvaluationStepCreditResultMap.get(supplier.getId());
						if (null != supplierEvaluationStep) {
							return NumberFormatUtils.format(supplierEvaluationStep.getEvaluationFinalScore(), scoreRound);
						}
					}else if(col == evalPriceCol){
						if(null != supplierOpeningResult && null != supplierOpeningResult.getEvaluationPrice()){
							return supplierOpeningResult.getFormatEvaluatoinPriceByUnit(tenderProjectRuleEntity.getEvalPriceUnit());
						}
					}else if(col == evalPriceFinalScoreCol){
						if(null != supplierEvaluationResult.getPriceFinalScore()){
							return NumberFormatUtils.format(supplierEvaluationResult.getPriceFinalScore(), scoreRound);
						}
					}else if(col == evaluationFinalScore){
						if(null != supplierEvaluationResult.getEvaluationFinalScore()){
							return NumberFormatUtils.format(supplierEvaluationResult.getEvaluationFinalScore(), scoreRound);
						}
					}else if(col == costPerformance){
						BigDecimal score = supplierEvaluationResult.getEvaluationFinalScore();
						BigDecimal price = supplierOpeningResult.getEvaluationPrice();
						price = MoneyUtils.getMoneyByUnit(price, tenderProjectRuleEntity.getEvalPriceUnit());
						if(null != score && null != price && price.compareTo(BigDecimal.ZERO) != 0) {
							return NumberFormatUtils.format(BigDecimalUtils.div(score, price), 6);
						}
					}else if(col == sortNoCol && sortNoCol > 0){
						if (isLimitPriceRecommend) {
							if(supplierEvaluationResult.getWinBidSortNo() > 0){
								return supplierEvaluationResult.getWinBidSortNo();
							}
						} else {
							if(supplierEvaluationResult.getSortNo() > 0){
								return supplierEvaluationResult.getSortNo();
							}
						}
					} else if (col == recommonedCol) {
						if (isLimitPriceRecommend) {
							supplierEvaluationResult.getWinBidRecommendCn(tenderProjectEntity.getEnterType());
						} else {
							return supplierEvaluationResult.getRecommendCn(tenderProjectEntity.getEnterType());
						}
					}
				}
				return "";
			}
		};
		commKTableModel.setCellContentRender(contentRender);
		
		// 编辑单元格对齐方式
		CellAlignmentRender cellAlignmentRender = new CellAlignmentRender(){
			public int receiveCellAlignment(int row, int col) {
				if(commKTableModel.isFixedCell(col, row)){
					if(row >= commKTableModel.getFixedRowCount() && col < commKTableModel.getFixedColumnCount()){
						return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_LEFT;
					}
				}else if(col == evalPriceCol){
					return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_RIGHT;
				}
				return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
			}
		};
		commKTableModel.setCellAlignmentRender(cellAlignmentRender);
		resultKTable.setModel(commKTableModel);
	}
	
	/**
	 * @Description: 刷新投标人的价格分
	 * @author guoyr
	 * @return
	 */
	private List<Supplier> getSupplierList(){
		
		List<Supplier> supplierList = supplierService.getEvaluationStepSupplierList(tenderProjectEntity.getId(), evaluationStep);
		if(null != supplierList && supplierList.size() > 0){
			// 则获取开标成功的投标人
			List<SupplierOpeningResult> supplierOpeningResultList = supplierOpeningResultService.getSupplierOpeningResultListByTenderIdAndBidOpeningStatus(tenderProjectEntity.getId(), SupplierEnum.OPENBID_SUCCESS);
			
			// 获取投标人评标结果
			List<SupplierEvaluationResult> supplierEvaluationResultList = supplierEvaluationResultService.getSupplierEvaluationResultListByTenderId( tenderProjectEntity.getId());
		
			for(Supplier supplier : supplierList){
				SupplierOpeningResult supplierOpeningResult = null;
				if(null != supplierOpeningResultList && supplierOpeningResultList.size() > 0){
					for(SupplierOpeningResult result : supplierOpeningResultList){
						if(StringUtils.equals(result.getSupplierId(), supplier.getId())){
							if(null == result.getEvaluationPrice()){
								result.setEvaluationPrice(result.getBidPrice());
							}
							supplierOpeningResult = result;
							break;
						}
					}
				}
				if(null == supplierOpeningResult){
					supplierOpeningResult = new SupplierOpeningResult();
					supplierOpeningResult.setTenderId(tenderProjectEntity.getId());
					supplierOpeningResult.setSupplierId(supplier.getId());
				}
				supplier.setSupplierOpeningResult(supplierOpeningResult);
				
				SupplierEvaluationResult supplierEvaluationResult = null;
				if(null != supplierEvaluationResultList && supplierEvaluationResultList.size() > 0){
					for(SupplierEvaluationResult evalResult : supplierEvaluationResultList){
						if(StringUtils.equals(evalResult.getSupplierId(), supplier.getId())){
							supplierEvaluationResult = evalResult;
							break;
						}
					}
				}
				
				if(null == supplierEvaluationResult){
					supplierEvaluationResult = new SupplierEvaluationResult();
					supplierEvaluationResult.setTenderId(tenderProjectEntity.getId());
					supplierEvaluationResult.setSupplierId(supplier.getId());
				}
				supplier.setSupplierEvaluationResult(supplierEvaluationResult);
			}
		}
		CollectionUtils.filter(supplierList, t -> null != t.getSupplierEvaluationResult() && !com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED.equals(t.getSupplierEvaluationResult().getAccessment()));
		if(null == supplierList)supplierList = new ArrayList<Supplier>();
		return supplierList;
	}
	
	/** 
	 * @Description: 排序
	 * @author guoyr 
	 */
	class MySort implements Comparator<Supplier> {
	    public int compare(Supplier a, Supplier b) {
	    	String firstOrder = tenderProjectRuleEntity.getOrderFirst();
	    	String secondOrder = tenderProjectRuleEntity.getOrderSecond();
	    	String thirdOrder = tenderProjectRuleEntity.getOrderThird();
	    	// 如果存在
	    	if(hasScoreEvalNode || hasBillEvalNode || hasCreditEvalNode){
	    		if(null == firstOrder || "".equals(firstOrder)){
	    			firstOrder = TenderProjectEnum.EVAL_TOTAL_SCORE;
	    		}
	    		if(null == secondOrder ||"".equals(secondOrder)){
	    			secondOrder = TenderProjectEnum.EVALUATION_PRICE;
	    		}
	    		if(null == thirdOrder || "".equals(thirdOrder)){
	    			thirdOrder = TenderProjectEnum.DETAIL_SCORE;
	    		}
	    	}else if(hasPriceEvalNode){
	    		if(null == firstOrder || "".equals(firstOrder)){
	    			firstOrder = TenderProjectEnum.PRICE_SCORE;
	    		}
	    		if(null == secondOrder || "".equals(secondOrder)){
	    			secondOrder = TenderProjectEnum.EVALUATION_PRICE;
	    		}
	    	}else {
	    		if(null == firstOrder || "".equals(firstOrder)){
	    			firstOrder = TenderProjectEnum.EVALUATION_PRICE;
	    		}
	    	}
	    	int compType = comparScore(a, b, firstOrder);
			if(compType == 0){
				compType = comparScore(a, b, secondOrder);
				if(compType == 0){
					return comparScore(a, b, thirdOrder);
				}
			}
			return compType;
	    }
	    
	    private int comparScore(Supplier a, Supplier b, String order){
			
			if (null == order) {
				return 0;
			}
			if (order.equals(TenderProjectEnum.EVAL_TOTAL_SCORE)) {
				BigDecimal aScore = null;
				BigDecimal bScore = null;
				if (null != a.getSupplierEvaluationResult() && null != a.getSupplierEvaluationResult().getEvaluationFinalScore()) {
					aScore = a.getSupplierEvaluationResult().getEvaluationFinalScore();
				}
				if (null != b.getSupplierEvaluationResult() && null != b.getSupplierEvaluationResult().getEvaluationFinalScore()) {
					bScore = b.getSupplierEvaluationResult().getEvaluationFinalScore();
				}
				if (null != aScore && null != bScore) {
					// 按得分从高到低的顺序
					return bScore.compareTo(aScore);
				}
			} else if(order.equals(TenderProjectEnum.DETAIL_SCORE)){
				BigDecimal aScore = null;
				BigDecimal bScore = null;
				// 总分减去价格分就是商务技术得分
				if(null != a.getSupplierEvaluationResult() && null != a.getSupplierEvaluationResult().getPriceFinalScore()){
					aScore = BigDecimalUtils.sub(a.getSupplierEvaluationResult().getEvaluationFinalScore(), a.getSupplierEvaluationResult().getPriceFinalScore());
				}
				if(null != b.getSupplierEvaluationResult() && null != b.getSupplierEvaluationResult().getPriceFinalScore()){
					bScore = BigDecimalUtils.sub(b.getSupplierEvaluationResult().getEvaluationFinalScore(), b.getSupplierEvaluationResult().getPriceFinalScore());
				}
				if(null != aScore && null != bScore){
					
					// 按得商务技术分从高到低的顺序
					return bScore.compareTo(aScore);
				}
			} else if (order.equals(TenderProjectEnum.PRICE_SCORE)) {
				BigDecimal aScore = null;
				BigDecimal bScore = null;
				if (null != a.getSupplierEvaluationResult() && null != a.getSupplierEvaluationResult().getPriceFinalScore()) {
					aScore = a.getSupplierEvaluationResult().getPriceFinalScore();
				}
				if (null != b.getSupplierEvaluationResult() && null != b.getSupplierEvaluationResult().getPriceFinalScore()) {
					bScore = b.getSupplierEvaluationResult().getPriceFinalScore();
				}
				if (null != aScore && null != bScore) {
					// 按得分从高到低的顺序
					return bScore.compareTo(aScore);
				}else if(null != aScore && null == bScore){
					return -1;
				}
			} else if (order.equals(TenderProjectEnum.EVALUATION_PRICE_DESC)) {
				
				BigDecimal aPrice = null;
				BigDecimal bPrice = null;
				if (null != a.getSupplierOpeningResult() && null != a.getSupplierOpeningResult().getEvaluationPrice()) {
					aPrice = a.getSupplierOpeningResult().getEvaluationPrice();
				}
				if (null != b.getSupplierOpeningResult() && null != b.getSupplierOpeningResult().getEvaluationPrice()) {
					bPrice = b.getSupplierOpeningResult().getEvaluationPrice();
				}
				if (null != aPrice && null != bPrice) {
					// 报价从高到低
					return bPrice.compareTo(aPrice);
				}
			}else if (order.equals(TenderProjectEnum.EVALUATION_PRICE)) {
				
				BigDecimal aPrice = null;
				BigDecimal bPrice = null;
				if (null != a.getSupplierOpeningResult() && null != a.getSupplierOpeningResult().getEvaluationPrice()) {
					aPrice = a.getSupplierOpeningResult().getEvaluationPrice();
				}
				if (null != b.getSupplierOpeningResult() && null != b.getSupplierOpeningResult().getEvaluationPrice()) {
					bPrice = b.getSupplierOpeningResult().getEvaluationPrice();
				}
				if (null != aPrice && null != bPrice) {
					// 报价从低到高排序
					return aPrice.compareTo(bPrice);
				}
			} else if (order.equals(TenderProjectEnum.COST_PERFORMANCE)) {
				BigDecimal aScore = null;
				BigDecimal bScore = null;
				if (null != a.getSupplierEvaluationResult() && null != a.getSupplierEvaluationResult().getEvaluationFinalScore()) {
					aScore = a.getSupplierEvaluationResult().getEvaluationFinalScore();
				}
				if (null != b.getSupplierEvaluationResult() && null != b.getSupplierEvaluationResult().getEvaluationFinalScore()) {
					bScore = b.getSupplierEvaluationResult().getEvaluationFinalScore();
				}
				BigDecimal aPrice = null;
				BigDecimal bPrice = null;
				if (null != a.getSupplierOpeningResult() && null != a.getSupplierOpeningResult().getEvaluationPrice()) {
					aPrice = a.getSupplierOpeningResult().getEvaluationPrice();
				}
				if (null != b.getSupplierOpeningResult() && null != b.getSupplierOpeningResult().getEvaluationPrice()) {
					bPrice = b.getSupplierOpeningResult().getEvaluationPrice();
				}
				if (null != aScore && null != bScore && null != aPrice && null != bPrice && aPrice.doubleValue() != 0 && bPrice.doubleValue() != 0) {
					return BigDecimalUtils.div(bScore, bPrice).compareTo(BigDecimalUtils.div(aScore, aPrice));
				}
			}
			return 0;
		}
	}
	
	/**
	 * @Description: 申请回退
	 * @author guoyr 
	 */
	private void applyReEvaluationAction(){
		if(applyReEvaluation_link.getEnabled()){
			ExpertEvaluationStep expertEvaluationStep = expertEvaluationStepService.getExpertEvaluationStep(tenderProjectEntity.getId(), evaluationStep.getId(), CURRENT_EXPERT.getUserId());
			if(null != expertEvaluationStep){
				ApplyReEvaluationDialog applyReEvaluationDialog = new ApplyReEvaluationDialog(getShell(), evaluationStep, expertEvaluationStep);
				applyReEvaluationDialog.open();
			}
		}
	}
}
