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

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.listener.RoundBorderLineListener;
import org.eclipse.swt.core.utils.WidgetUtils;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
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 com.gx.obe.ColorConstants;
import com.gx.obe.business.vo.PriceEvaluationFactor;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.components.core.enumeration.BidPriceEnum;
import com.gx.obe.components.core.enumeration.CommonEnum;
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.evaluation.step.inter.IPriceEvaluationTable;
import com.gx.obe.evaluation.step.listener.PriceReviewEvaluationListener;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.util.utils.NumberFormatUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.SortUtils;
import com.gx.obe.util.utils.UuidUtils;
import com.gx.obe.web.entity.EvaluationFactorResult;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.SupplierOpeningResult;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
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.price.BidPrice;
import com.gx.obe.web.entity.price.BidPriceResult;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;
import com.gx.obe.web.service.BidPriceResultService;
import com.gx.obe.web.service.BidPriceService;
import com.gx.obe.web.service.evaluation.EvaluationFactorResultService;
import com.gx.obe.web.service.evaluation.EvaluationFactorService;
import com.gx.obe.web.service.expert.ExpertEvaluationResultService;
import com.gx.obe.web.service.expert.ExpertEvaluationStepService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

import de.kupzog.ktable.KTable;
import de.kupzog.ktable.KTableCellSelectionAdapter;
import de.kupzog.ktable.SWTX;
import de.kupzog.ktable.editors.KTableCellEditorText;
import de.kupzog.ktable.model.CommonKtableCModel;
import de.kupzog.ktable.renderers.CellAlignmentRender;
import de.kupzog.ktable.renderers.CellContentRender;
import de.kupzog.ktable.renderers.CellEditContentRender;
import de.kupzog.ktable.renderers.CellEditorRender;
import de.kupzog.ktable.renderers.CellImageRender;
import de.kupzog.ktable.renderers.CellRender;
import de.kupzog.ktable.renderers.CellTooltipRender;
import de.kupzog.ktable.renderers.FixedWrapCellRenderer;
import de.kupzog.ktable.renderers.TextWrapCellRenderer;

/**
 * @Description: 价格评审面板
 * @author guoyr
 */
public class PriceEvaluationTableComposite extends Composite implements IPriceEvaluationTable{

	private TenderProjectEntity tenderProjectEntity;
	private EvaluationStep evaluationStep;
	private TenderProjectRuleEntity tenderProjectRuleEntity;
	private Expert CURRENT_EXPERT;
	private ImageResolver imageResolver = new ImageResolver();
	private EvaluationFactorService evaluationFactorService = new EvaluationFactorService();
	private ExpertEvaluationStepService expertEvaluationStepService ;
	private EvaluationFactorResultService evaluationFactorResultService = new EvaluationFactorResultService();
	private ExpertEvaluationResultService expertEvaluationResultService;
	private BidPriceService bidPriceService ;
	private BidPriceResultService bidPriceResultService;
	private Composite evaluationComposite;
	private Composite composite_5;
	private Composite evaluationTableComposite;
	private StyledText evaluation_factor_memo;
	private StyledText computerDetailText;
	private List<EvaluationFactor> evaluationFactorList = new ArrayList<EvaluationFactor>();
	private List<PriceEvaluationFactor> priceEvaluationFactorList;
	private int maxLevel = 1;
	private List<Supplier> supplierList;
	private KTable resultKTable;
	private CommonKtableCModel commKTableModel;
	private int curRow = 0;
	private int curCol = 0;
	private boolean resultChanged = true;
	private String supplierName;
//	private Map<String, BigDecimal> defaultEvaluationFactorResultMap = null;
	// 记录供应商每个指标的评审结果
	private Map<String, BidPriceResult> supplierBidPriceResultMap;
	private Map<String, Map<String, EvaluationFactorResult>> supplierEvaluationFactorResultMap;
	// 价格得分计算公式
	private Map<String, PriceScoreCalculator> priceScoreCalculatorMap;
	// 记录专家对供应商的评审汇总结果
	private Map<String, ExpertEvaluationResult> expertEvaluationResultMap;
//	private User curUser;
	private boolean isShowError = false;
	private boolean hasFactorWeight = false;;
	private boolean hasBidPriceTotal = false;
	private int scoreRound = 2;
	private PriceReviewEvaluationListener listener;
	private boolean changeBasePrice = false;
	private boolean changePriceScore = false;
	private ELink goReviewButton;
	private boolean hasCalculator = false;
	private Composite tipsComposite;
	private ELink recalculation;
	private Label priceScoreTips;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public PriceEvaluationTableComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		
		evaluationComposite = new Composite(this, SWT.NONE);
		evaluationComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		evaluationComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_evaluationComposite = new GridLayout(1, false);
		gl_evaluationComposite.verticalSpacing = 0;
		gl_evaluationComposite.horizontalSpacing = 0;
		gl_evaluationComposite.marginHeight = 0;
		gl_evaluationComposite.marginWidth = 0;
		evaluationComposite.setLayout(gl_evaluationComposite);
		
		composite_5 = new Composite(evaluationComposite, SWT.NONE);
		GridData gd_composite_5 = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_composite_5.widthHint = 500;
		composite_5.setLayoutData(gd_composite_5);
		GridLayout gl_composite_5 = new GridLayout(1, false);
		gl_composite_5.marginHeight = 1;
		gl_composite_5.marginWidth = 1;
		gl_composite_5.verticalSpacing = 0;
		gl_composite_5.horizontalSpacing = 0;
		composite_5.setLayout(gl_composite_5);
		composite_5.setBackgroundMode(SWT.INHERIT_FORCE);
		
		evaluation_factor_memo = new StyledText(composite_5, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		evaluation_factor_memo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		evaluation_factor_memo.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		evaluation_factor_memo.setText("");
		
		computerDetailText = new StyledText(composite_5, SWT.MULTI | SWT.WRAP);
		computerDetailText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		computerDetailText.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		computerDetailText.setText("");
		
		tipsComposite = new Composite(evaluationComposite, SWT.NONE);
		GridLayout gl_tipsComposite = new GridLayout(4, false);
		gl_tipsComposite.marginWidth = 0;
		gl_tipsComposite.marginHeight = 0;
		gl_tipsComposite.horizontalSpacing = 0;
		tipsComposite.setLayout(gl_tipsComposite);
		tipsComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
//		CLabel tips = new CLabel(tipsComposite, SWT.NONE);
//		tips.setForeground(ColorConstants.FONT_BLACK_COLOR);
//		tips.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
//		tips.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/tips_blue.png"));
//		tips.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, true, 1, 1));
//		tips.setText("");
		
		priceScoreTips = new Label(tipsComposite, SWT.WRAP);
		priceScoreTips.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		priceScoreTips.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		GridData gd_tips = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_tips.widthHint = 500;
		priceScoreTips.setLayoutData(gd_tips);
		priceScoreTips.setText(Messages.Tips_checkScoreBeforeSavePriceEvaluation);
	
		goReviewButton = new ELink(tipsComposite, SWT.NONE);
		goReviewButton.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/edit.png"));
		goReviewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				listener.priceReviewEvaluation();
			}
		});
		goReviewButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, true, 1, 1));
		goReviewButton.setText(Messages.Button_correctionBidPrice);
		
		recalculation = new ELink(tipsComposite, SWT.NONE);
		recalculation.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/refresh.png"));
		recalculation.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		recalculation.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		recalculation.setText(Messages.Button_recalculatePriceScore);
		recalculation.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				recalculationAction();
			}
		});
		
		evaluationTableComposite = new Composite(evaluationComposite, SWT.NONE);
		evaluationTableComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		evaluationTableComposite.addPaintListener(new RoundBorderLineListener(ColorConstants.BORDER_LINE_COLOR));
		GridLayout gl_evaluationTableComposite = new GridLayout(1, false);
		gl_evaluationTableComposite.marginHeight = 1;
		gl_evaluationTableComposite.marginWidth = 1;
		evaluationTableComposite.setLayout(gl_evaluationTableComposite);
		GridData gd_evaluationTableComposite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_evaluationTableComposite.minimumHeight = 200;
		evaluationTableComposite.setLayoutData(gd_evaluationTableComposite);
		expertEvaluationStepService = new ExpertEvaluationStepService();
		bidPriceService = new BidPriceService();
		expertEvaluationResultService = new ExpertEvaluationResultService();
	}
	
	/**
	 * @Description: 重新计算报价得分按钮点击事件
	 * @author chenxw 
	 */
	private void recalculationAction() {
		if (!MessageDialog.openConfirm(getShell(), Messages.Button_recalculatePriceScore, MessageFormat.format(Messages.Tips_confirmOperate, Messages.Button_recalculatePriceScore), new String[]{Messages.Button_confirm, Messages.Button_cancel})) {
			return;
		}
		if (expertEvaluationResultService.deleteExpertEvaluationAndFactorResult(tenderProjectEntity.getId(), evaluationStep.getId(), CURRENT_EXPERT.getUserId())) {
			drawLeftSupplierEvaluationTable();
		}
	}

	/**
	 * @Description: 初始化
	 * @author guoyr
	 */
	public void refresh(TenderProjectEntity tenderProjectEntity, TenderProjectRuleEntity tenderProjectRuleEntity, Expert curExpert, EvaluationStep evaluationStep, List<Supplier> supplierList){
		this.tenderProjectEntity = tenderProjectEntity;
		this.tenderProjectRuleEntity = tenderProjectRuleEntity;
		this.evaluationStep = evaluationStep;
		this.CURRENT_EXPERT = curExpert;
		this.supplierList = supplierList;
		
		if(!ObjectUtils.equals(tenderProjectRuleEntity.getCorrectPrice(), CommonEnum.YES)){
			WidgetUtils.setControlVisible(goReviewButton, false);
		}
		
		supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProjectEntity.getTenderType());
		curRow = 0;
		curCol = 0;
		// 修改评标专家的评审记录
//		expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.IN_PROGRESS, tenderProjectEntity.getId(), evaluationStep, CURRENT_EXPERT.getUserId());
		scoreRound = tenderProjectRuleEntity.getScoreRoundIntVal();
		hasBidPriceTotal = evaluationFactorService.hasTotalBidPriceFactor(tenderProjectEntity.getId(), null);
		
		changeBasePrice = evaluationStep.getEvaluationParamByKey(com.gx.obe.components.core.enumeration.EvaluationStepEnum.PARAM_CHANGEBASEPRICE, Boolean.class);
		changePriceScore = evaluationStep.getEvaluationParamByKey(com.gx.obe.components.core.enumeration.EvaluationStepEnum.PARAM_CHANGEPRICESCORE, Boolean.class);
		
		
		drawLeftSupplierEvaluationTable();
		resultChanged = true;
	}
	
	/**
	 * @Description: 初始化当前评标步骤的评审指标
	 * @author guoyr
	 */
	private void initEvaluationStepFactor(){
		evaluationFactorList.clear();
		maxLevel = 1;
		List<EvaluationFactor> evaluationTopFactorList = evaluationFactorService.getEvaluationStepFactorTreeList(tenderProjectEntity.getId(), evaluationStep);
		hasFactorWeight = false;
		if(null != evaluationTopFactorList && evaluationTopFactorList.size() > 0){
			for(EvaluationFactor topFactor : evaluationTopFactorList){
				// 当前评标步骤是否存在权重
				if(null != topFactor.getFactorWeight() && (topFactor.getFactorWeight().compareTo(BigDecimal.ZERO) >0 && topFactor.getFactorWeight().compareTo(new BigDecimal(100)) < 0)){
					hasFactorWeight = true;
				}
				initAddChildEvaluationFactor(topFactor);
			}
		}
	}
	
	/**
	 * @Description: 递归获取所有的评审叶子指标
	 * @author guoyr
	 * @param evalFactor
	 */
	private void initAddChildEvaluationFactor(EvaluationFactor evalFactor){
		if(null != evalFactor.getChildFactorList() && evalFactor.getChildFactorList().size() > 0){
			for(EvaluationFactor childFactor : evalFactor.getChildFactorList()){
				childFactor.setLevel(evalFactor.getLevel() + 1);
				initAddChildEvaluationFactor(childFactor);
			}
		}else {
			maxLevel = Math.max(maxLevel, evalFactor.getLevel());
			evaluationFactorList.add(evalFactor);
		}
	}
	
	/**
	 * @Description: 评审表格点击事件
	 * @author guoyr
	 * @param row
	 * @param col
	 */
	private void resultClickEventAction(Supplier supplier, EvaluationFactor evaluationFactor, EvaluationFactorResult  evalFactorResult){
		
		evaluation_factor_memo.setText("");
		if(null != evaluationFactor){
			StringBuilder evaluationFactorMemo = new StringBuilder();
			evaluationFactorMemo.append(Messages.ScoreRange + "：[").append(null != evaluationFactor.getFactorMinScore() ? NumberFormat.getInstance().format(evaluationFactor.getFactorMinScore()) : " ").append("-").append(null != evaluationFactor.getFactorMaxScore() ? NumberFormat.getInstance().format(evaluationFactor.getFactorMaxScore()) : "").append(Messages.Points + "]");
			if(null != evaluationFactor.getFactorWeight()){
				evaluationFactorMemo.append("("+Messages.Weight+"").append(BigDecimalUtils.round(evaluationFactor.getFactorWeight(), 2).toString()).append("%)");
			}
			StyleRange styleScoreRange = new StyleRange(); 
			styleScoreRange.start = Messages.ScoreRange.length() + 1; 
			styleScoreRange.length = evaluationFactorMemo.length() - (Messages.ScoreRange.length() + 1); 
			styleScoreRange.foreground = SWTResourceManager.getColor(77, 163, 19); 
			styleScoreRange.fontStyle = SWT.BOLD; 
			evaluationFactorMemo.append("   ");
			
			int startIndex = evaluationFactorMemo.length();
			evaluationFactorMemo.append(Messages.EvalFactor + "：");
			StyleRange factorNameTitleStyleRange = getTitleStyleRange(startIndex, Messages.EvalFactor.length() + 1);
			
			StyleRange styleFactorNameRange = new StyleRange(); 
			styleFactorNameRange.start = startIndex + Messages.EvalFactor.length() + 1; 
			styleFactorNameRange.length = evaluationFactor.getFactorName().length(); 
			styleFactorNameRange.foreground = SWTResourceManager.getColor(20, 20, 20); 
			styleFactorNameRange.fontStyle = SWT.NORMAL; 
			evaluationFactorMemo.append(evaluationFactor.getFactorName());
			
			StyleRange factorMemoTitleStyleRange = null;
			StyleRange styleFactorMemoRange = null;
			if(null != evaluationFactor.getMemo() && evaluationFactor.getMemo().length() > 0){
				evaluationFactorMemo.append("\r\n");
				startIndex = evaluationFactorMemo.length();
				factorMemoTitleStyleRange = getTitleStyleRange(startIndex, Messages.EvalExplain.length() + 1);
				evaluationFactorMemo.append(Messages.EvalExplain + "：").append(evaluationFactor.getMemo());
				styleFactorMemoRange = new StyleRange(); 
				styleFactorMemoRange.start = startIndex + Messages.EvalExplain.length() + 1; 
				styleFactorMemoRange.length = evaluationFactor.getMemo().length(); 
				styleFactorMemoRange.foreground = SWTResourceManager.getColor(100, 100, 100); 
				styleFactorMemoRange.fontStyle = SWT.NORMAL; 
			}
			evaluation_factor_memo.setText(evaluationFactorMemo.toString());
			evaluation_factor_memo.setStyleRange(getTitleStyleRange(0, Messages.EvalExplain.length() + 1));
			evaluation_factor_memo.setStyleRange(styleScoreRange);
			evaluation_factor_memo.setStyleRange(factorNameTitleStyleRange);
			evaluation_factor_memo.setStyleRange(styleFactorNameRange);
			GridData gridData = (GridData)evaluation_factor_memo.getLayoutData();
			if(evaluation_factor_memo.computeSize(SWT.DEFAULT, SWT.DEFAULT).y > 58){
				gridData.heightHint = 58;
			}

			if(null != factorMemoTitleStyleRange){
				evaluation_factor_memo.setStyleRange(factorMemoTitleStyleRange);
				evaluation_factor_memo.setStyleRange(styleFactorMemoRange);
			}
		}
		
		if(null != evalFactorResult && null != evalFactorResult.getEvaluationOpinion() && evalFactorResult.getEvaluationOpinion().length() > 0){
			computerDetailText.setText(Messages.ComputationalProcess + "："+evalFactorResult.getEvaluationOpinion());
			StyleRange computerDetailStyleRange = getTitleStyleRange(Messages.ComputationalProcess.length() + 1, evalFactorResult.getEvaluationOpinion().length());
			computerDetailStyleRange.foreground = SWTResourceManager.getColor(77, 163, 19); 
//			computerDetailStyleRange.fontStyle = SWT.NORMAL; 
			computerDetailText.setStyleRange(getTitleStyleRange(0, Messages.ComputationalProcess.length() + 1));
			computerDetailText.setStyleRange(computerDetailStyleRange);
			((GridData)computerDetailText.getLayoutData()).exclude = false;
			computerDetailText.setVisible(true);
		}else {
			((GridData)computerDetailText.getLayoutData()).exclude = true;
			computerDetailText.setVisible(false);
		}
		
		computerDetailText.getParent().layout();
		evaluationComposite.layout();
	}
	
	private StyleRange getTitleStyleRange(int start, int length){
		StyleRange styleRange = new StyleRange(); 
		styleRange.start = start; 
		styleRange.length = length; 
//		styleRange.foreground = SWTResourceManager.getColor(SWT.COLOR_WHITE); 
		styleRange.foreground = SWTResourceManager.getColor(SWT.COLOR_BLUE);
		styleRange.fontStyle = SWT.BOLD; 
		return styleRange;
	}
	
	/**
	 * @Description: 初始化评标结果
	 * @author guoyr 
	 */
	private void initEvaluationResult(){
		
		initEvaluationStepFactor();
		if(null == evaluationFactorList)evaluationFactorList = new ArrayList<EvaluationFactor>();
		priceEvaluationFactorList = new ArrayList<PriceEvaluationFactor>();
		
		// 记录供应商每个指标的评审结果
		supplierEvaluationFactorResultMap = new HashMap<String, Map<String, EvaluationFactorResult>>();
		
		// 记录供应商的评审汇总结果
//		supplierEvaluationStepResultMap = new HashMap<String, SupplierEvaluationStep>();
		
		// 价格得分计算公式
		priceScoreCalculatorMap = new HashMap<String, PriceScoreCalculator>();
		hasCalculator = false;
		// 记录专家对供应商的评审汇总结果
		expertEvaluationResultMap = new HashMap<String, ExpertEvaluationResult>();
		
		// 评审当前评标专家所有的评标结果
		List<EvaluationFactorResult> expertEvaluateFactorResultList = evaluationFactorResultService.getExpertEvaluationFactorResult(tenderProjectEntity.getId(), evaluationStep.getId(), CURRENT_EXPERT.getUserId(), null);
		
		List<ExpertEvaluationResult> queryExpertEvaluationResultList = expertEvaluationResultService.getExpertStepAllSupplierEvaluationResultList(tenderProjectEntity.getId(), evaluationStep.getId(), CURRENT_EXPERT.getUserId());
		
		Map<String, EvaluationFactorResult> priceFactorResultMap = null;
		if(null != expertEvaluateFactorResultList && expertEvaluateFactorResultList.size() > 0){
			priceFactorResultMap = new HashMap<String, EvaluationFactorResult>();
			for(EvaluationFactorResult factorResult : expertEvaluateFactorResultList){
				priceFactorResultMap.put(factorResult.getSupplierId()+factorResult.getFactorId(), factorResult);
			}
		}
		
		Supplier firstSupplier = null;
		EvaluationFactor firstEvaluationFactor = null;
		EvaluationFactorResult firstEvaluationFactorResult = null;
		List<Double> sortList = new ArrayList<Double>();
		for(Supplier supplier : supplierList){
			
			// 获取评标专家对投标人的评审记录
			ExpertEvaluationResult expertEvaluationResult = null;
			if(null != queryExpertEvaluationResultList && queryExpertEvaluationResultList.size() > 0){
				for(ExpertEvaluationResult queryExpertEvaluationResult : queryExpertEvaluationResultList){
					if(ObjectUtils.equals(queryExpertEvaluationResult.getSupplierId(), supplier.getId())){
						expertEvaluationResult = queryExpertEvaluationResult;
					}
				}
			}
			if(null == expertEvaluationResult){
				expertEvaluationResult = new ExpertEvaluationResult();
				expertEvaluationResult.setTenderId(tenderProjectEntity.getId());
				expertEvaluationResult.setStepId(evaluationStep.getId());
				expertEvaluationResult.setUserId(CURRENT_EXPERT.getUserId());
				expertEvaluationResult.setSupplierId(supplier.getId());
			}
			Map<String, EvaluationFactorResult> evaluationFactorResultMap = new HashMap<String, EvaluationFactorResult>();
			BigDecimal totalEvaluationScore = null;
			BigDecimal totalEvaluationFinalScore = null;
			
			for(EvaluationFactor evaluationFactor : evaluationFactorList){
				// 获得已评审的结果
				EvaluationFactorResult evaluationFactorResult = null;
				if(null != expertEvaluateFactorResultList && expertEvaluateFactorResultList.size() > 0){
					for(EvaluationFactorResult factorResult : expertEvaluateFactorResultList){
						if(ObjectUtils.equals(supplier.getId(), factorResult.getSupplierId()) && ObjectUtils.equals(evaluationFactor.getId(), factorResult.getFactorId())){
							evaluationFactorResult = factorResult;
							break;
						}
					}
				}
				if(null == evaluationFactorResult){
					evaluationFactorResult = new EvaluationFactorResult();
					evaluationFactorResult.setTenderId(tenderProjectEntity.getId());
					evaluationFactorResult.setStepId(evaluationStep.getId());
					evaluationFactorResult.setUserId(CURRENT_EXPERT.getUserId());
					evaluationFactorResult.setSupplierId(supplier.getId());
					evaluationFactorResult.setFactorId(evaluationFactor.getId());
					evaluationFactorResult.setFactorName(evaluationFactor.getFactorName());
					evaluationFactorResult.setId(UuidUtils.getUUID());
				}
			
				if(null != evaluationFactorResult.getEvaluationScore()){
					totalEvaluationScore = BigDecimalUtils.add(totalEvaluationScore, evaluationFactorResult.getEvaluationScore());
				}
				evaluationFactorResult.setEvaluationFinalScore(getEvaluationFinalScore(evaluationFactor, evaluationFactorResult.getEvaluationScore()));
				if(null != evaluationFactorResult.getEvaluationFinalScore()){
					totalEvaluationFinalScore = BigDecimalUtils.add(totalEvaluationFinalScore, evaluationFactorResult.getEvaluationFinalScore());
				}
				
				evaluationFactorResultMap.put(evaluationFactor.getId(), evaluationFactorResult);
				
				if(null == firstEvaluationFactor){
					firstEvaluationFactor = evaluationFactor;
				}
				if(null == firstEvaluationFactorResult){
					firstEvaluationFactorResult = evaluationFactorResult;
				}
			}
			
			expertEvaluationResult.setAccessment(com.gx.obe.components.core.enumeration.EvaluationStepEnum.QUALIFIED);
			expertEvaluationResult.setEvaluationScore(totalEvaluationScore);
			expertEvaluationResult.setEvaluationFinalScore(totalEvaluationFinalScore);
			expertEvaluationResultMap.put(supplier.getId(), expertEvaluationResult);
			if(null != totalEvaluationFinalScore){
				sortList.add(totalEvaluationFinalScore.doubleValue());
			}
			if(null == firstSupplier){
				firstSupplier = supplier;
			}
		
			supplierEvaluationFactorResultMap.put(supplier.getId(), evaluationFactorResultMap);
		}
		
		List<BidPrice> bidPriceList = null;
		if(!hasBidPriceTotal){
			bidPriceList = bidPriceService.getTenderBidPriceList(tenderProjectEntity.getId());
		}
		
		// 记录供应商分项报价的结果
		supplierBidPriceResultMap = new HashMap<String, BidPriceResult>();
		int index = 0;
		for(EvaluationFactor evaluationFactor : evaluationFactorList){
			index ++;
			BidPrice factorBidPrice = null;
			PriceEvaluationFactor priceEvaluationFactorFinalScore = new PriceEvaluationFactor(evaluationFactor);
			if(null != bidPriceList && bidPriceList.size() > 0){
				for(BidPrice bidPrice : bidPriceList){
					// 如果当前指标是分项报价，且已设置公式，则说明对该报价用公式进行价格得分计算
					if(null != evaluationFactor.getBidPriceCode() && evaluationFactor.getBidPriceCode().equals(bidPrice.getBidPriceCode()) && null != evaluationFactor.getComputerParams()){
						factorBidPrice = bidPrice;
						break;
					}
				}
			}
			
			
			priceEvaluationFactorFinalScore.setBidPrice(factorBidPrice);
			
			if(null != factorBidPrice){
				// 添加报表列
				priceEvaluationFactorList.add(priceEvaluationFactorFinalScore.clone(PriceEvaluationFactor.BID_PRICE));
				// 添加基准价列
				priceEvaluationFactorList.add(priceEvaluationFactorFinalScore.clone(PriceEvaluationFactor.BASE_PRICE));
				priceEvaluationFactorList.add(priceEvaluationFactorFinalScore);
				if(null != evaluationFactor.getFactorWeight()){//null != evaluationFactor.getFactorFinalWeight()){
					// 添加价格得分列
					priceEvaluationFactorList.add(priceEvaluationFactorFinalScore.clone(PriceEvaluationFactor.PRICE_FINAL_SCORE));
				}
			}else if(null != evaluationFactor.getBidPriceCode() && evaluationFactor.getBidPriceCode().equals(BidPrice.BID_PRICE_TOTAL_CODE)){
				// 添加报价列
				priceEvaluationFactorList.add(priceEvaluationFactorFinalScore.clone(PriceEvaluationFactor.BID_EVAL_PRICE_TOTAL));
				// 添加基准价列
				priceEvaluationFactorList.add(priceEvaluationFactorFinalScore.clone(PriceEvaluationFactor.BASE_PRICE));
				priceEvaluationFactorList.add(priceEvaluationFactorFinalScore);
				if(null != evaluationFactor.getFactorWeight()){// null != evaluationFactor.getFactorFinalWeight()){
					// 添加价格得分列
					priceEvaluationFactorList.add(priceEvaluationFactorFinalScore.clone(PriceEvaluationFactor.PRICE_FINAL_SCORE));
				}
			}else {
				priceEvaluationFactorList.add(priceEvaluationFactorFinalScore);
				if(null != evaluationFactor.getFactorWeight()){//null != evaluationFactor.getFactorFinalWeight()){
					// 添加价格得分列
					priceEvaluationFactorList.add(priceEvaluationFactorFinalScore.clone(PriceEvaluationFactor.PRICE_FINAL_SCORE));
				}
			}
			refreshCalculator(priceEvaluationFactorFinalScore, null, null == priceFactorResultMap);
		}
		reSetSortNo();
		resultClickEventAction(firstSupplier, firstEvaluationFactor, firstEvaluationFactorResult);
		
		WidgetUtils.setControlVisible(recalculation, hasCalculator);
		tipsComposite.layout();
	}
	
	/**
	 * @Description: 设置排名
	 * @author guoyr 
	 */
	private void reSetSortNo(){
		List<Double> sortList = new ArrayList<Double>();
		
		// 每次进入评审界面都采用公式重新计算的
		for(Supplier supplier : supplierList){
			BigDecimal evaluationFinalScoreSum = null;
			BigDecimal evaluationScoreSum = null;
			Map<String, EvaluationFactorResult> evaluationFactorResultMap = supplierEvaluationFactorResultMap.get(supplier.getId());
			for(Entry<String,  EvaluationFactorResult> evaluationFactorResultEntry : evaluationFactorResultMap.entrySet()){
				EvaluationFactorResult evaluationFactorResult = evaluationFactorResultEntry.getValue();
				evaluationFinalScoreSum = BigDecimalUtils.add(evaluationFinalScoreSum, evaluationFactorResult.getEvaluationFinalScore());
				evaluationScoreSum = BigDecimalUtils.add(evaluationScoreSum, evaluationFactorResult.getEvaluationScore());
			}
			ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultMap.get(supplier.getId());
			if(null != expertEvaluationResult){
				if(null != evaluationFinalScoreSum){
					expertEvaluationResult.setEvaluationScore(evaluationScoreSum);
					expertEvaluationResult.setEvaluationFinalScore(evaluationFinalScoreSum);
					sortList.add(evaluationFinalScoreSum.doubleValue());
				}else if(null != expertEvaluationResult.getEvaluationFinalScore()){
					sortList.add(expertEvaluationResult.getEvaluationFinalScore().doubleValue());
				}
			}
		}
		SortUtils sortUtil = new SortUtils(sortList);
		for(Entry<String, ExpertEvaluationResult> expertEvaluationResultEntry : expertEvaluationResultMap.entrySet()){
			ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultEntry.getValue();
			if(null != expertEvaluationResult.getEvaluationFinalScore()){
				expertEvaluationResultEntry.getValue().setSortNo(sortUtil.getSortNo(expertEvaluationResult.getEvaluationFinalScore().doubleValue()));
			}else {
				expertEvaluationResultEntry.getValue().setSortNo(0);
			}
		}
	}
	
	/**
	 * @Description: 左边是供应商表格
	 * @author guoyr 
	 */
	private void drawLeftSupplierEvaluationTable(){
		priceScoreTips.setText(Messages.Tips_checkScoreBeforeSavePriceEvaluation);
		
		initEvaluationResult();
		if(null != resultKTable && !resultKTable.isDisposed()){
			resultKTable.dispose();
		}
		resultKTable = new KTable(evaluationTableComposite, SWTX.AUTO_SCROLL /*| SWT.FULL_SELECTION */| SWTX.FILL_WITH_LASTCOL | SWTX.EDIT_ON_KEY);
		resultKTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		resultKTable.setLayout(new GridLayout(1, false));
		boolean hasFinalSumScore = false;// 标记是否存在最终得分
		boolean hasSumScore = false;// 标记是否存在得分汇总
		if(evaluationFactorList.size() > 1){
			hasFinalSumScore = true;
			commKTableModel = new CommonKtableCModel(resultKTable, 2, 1, supplierList.size() , priceEvaluationFactorList.size() + (hasFactorWeight ? 3 : 2));
			if(hasFactorWeight){
				hasSumScore = true;
			}
		}else {
			commKTableModel = new CommonKtableCModel(resultKTable, 1, 1, supplierList.size() , priceEvaluationFactorList.size() + 1);
		}
		commKTableModel.addRowSpan(0, 0, commKTableModel.getFixedRowCount());
		
		commKTableModel.setColumnMinWidth(0, 100);
		
		final Map<String, String> tableTitleMap = new HashMap<String, String>();
		
		// 排名列
		final int sortCol = commKTableModel.getColCount() -1;
		commKTableModel.addRowSpan(0, sortCol, commKTableModel.getFixedRowCount());
		commKTableModel.setColumnWidth(sortCol, 40);
		commKTableModel.setColumnMaxWidth(sortCol, 80);
		tableTitleMap.put("0_"+ sortCol, Messages.SortNo);
		// 最终得分列
		final int finalScoreSumCol= hasFinalSumScore?commKTableModel.getColCount() -2:-1;
		if(finalScoreSumCol > 0){
			commKTableModel.setColumnWidth(finalScoreSumCol, 40);
			commKTableModel.setColumnMaxWidth(finalScoreSumCol, 80);
			commKTableModel.addRowSpan(0, finalScoreSumCol, commKTableModel.getFixedRowCount());
			if(hasFactorWeight){
				tableTitleMap.put("0_"+ finalScoreSumCol, Messages.FinalScore);
			}else {
				tableTitleMap.put("0_"+ finalScoreSumCol, Messages.TotalScore);
			}
		}
		// 得分汇总列
		final int scoreSumCol = hasSumScore?commKTableModel.getColCount() -3 : -1;
		if(scoreSumCol > 0){
			commKTableModel.addRowSpan(0, scoreSumCol, commKTableModel.getFixedRowCount());
			commKTableModel.setColumnWidth(scoreSumCol, 40);
			commKTableModel.setColumnMaxWidth(scoreSumCol, 80);
			tableTitleMap.put("0_"+ scoreSumCol, Messages.TotalScore);
		}
		
		tableTitleMap.put("0_0", MessageFormat.format(Messages.Supplier, Messages.Name));
		commKTableModel.addRowSpan(0, 0, commKTableModel.getFixedRowCount());
		int col = commKTableModel.getFixedColumnCount();
		int margerColCount = 0;
		String preFactorId = null;
		
		for(PriceEvaluationFactor priceEvaluationFactor : priceEvaluationFactorList){
			EvaluationFactor evaluationFactor = priceEvaluationFactor.getEvaluationFactor();
			if(commKTableModel.getFixedRowCount() > 1){
				if(null == preFactorId){
					preFactorId = evaluationFactor.getId();
				}
				tableTitleMap.put("0_"+col, evaluationFactor.getFactorName());
				if(null != preFactorId && !evaluationFactor.getId().equals(preFactorId)){
					commKTableModel.addColSpan(0, col - margerColCount, margerColCount);
					margerColCount = 0;
					preFactorId = evaluationFactor.getId();
				}
				margerColCount ++;
			}
			if(priceEvaluationFactor.getFactorType() == PriceEvaluationFactor.BID_PRICE || priceEvaluationFactor.getFactorType() == PriceEvaluationFactor.BID_EVAL_PRICE_TOTAL){
				BidPrice bidPrice = priceEvaluationFactor.getBidPrice();
				String priceUnit = null != tenderProjectRuleEntity ? ("\r\n("+ tenderProjectRuleEntity.getEvalPriceUnit()+")") : "";
				if(null != bidPrice && BidPriceEnum.IS_RATE.equals(bidPrice.getPriceType())) {
					priceUnit = "("+bidPrice.getPriceUnitCN("%")+")";
				}
				tableTitleMap.put((commKTableModel.getFixedRowCount() -1)+"_"+col, Messages.EvaluationPrice+priceUnit);
			}else if(priceEvaluationFactor.getFactorType() == PriceEvaluationFactor.BASE_PRICE){
				BidPrice bidPrice = priceEvaluationFactor.getBidPrice();
				String priceUnit = null != tenderProjectRuleEntity ? ("\r\n("+ tenderProjectRuleEntity.getEvalPriceUnit()+")") : "";
				if(null != bidPrice && BidPriceEnum.IS_RATE.equals(bidPrice.getPriceType())) {
					priceUnit = "("+bidPrice.getPriceUnitCN("%")+")";
				}
				tableTitleMap.put((commKTableModel.getFixedRowCount() -1)+"_"+col, Messages.BasePrice+ priceUnit);
				commKTableModel.addRowSpan(commKTableModel.getFixedRowCount(), col, supplierList.size());
			}else if(priceEvaluationFactor.getFactorType() == PriceEvaluationFactor.PRICE_SCORE){
				tableTitleMap.put((commKTableModel.getFixedRowCount() -1)+"_"+col, Messages.PriceScore);
			}else if(priceEvaluationFactor.getFactorType() == PriceEvaluationFactor.PRICE_FINAL_SCORE){
				if(null != evaluationFactor.getFactorFinalWeight()){
					tableTitleMap.put((commKTableModel.getFixedRowCount() -1)+"_"+col, Messages.WeightScore +"\r\n(" + NumberFormat.getInstance().format(evaluationFactor.getFactorFinalWeight())+"%)");
				}else {
					tableTitleMap.put((commKTableModel.getFixedRowCount() -1)+"_"+col, Messages.PriceScore);
					// 如果存在多个指标（多表头），且当前指标是分值录入的指标，且没有权重，则合并行
					if(margerColCount == 1 && commKTableModel.getFixedRowCount() > 1){
						commKTableModel.addRowSpan(0, col, commKTableModel.getFixedRowCount());
					}
				}
			}
			commKTableModel.setColumnWidth(col, 80);
			col ++;
		}
		if(margerColCount > 0){
			commKTableModel.addColSpan(0, col - margerColCount, margerColCount);
		}
		
		// 设置单元格内容
		CellContentRender contentRender = new CellContentRender(){
			public Object receiveContent(int row, int col) {
				// 表头
				if(row < commKTableModel.getHeadRow()){
					if(null != tableTitleMap.get(row+"_"+col)){
						return tableTitleMap.get(row+"_"+col);
					}else {
						return "";
					}
				}else {
					Supplier supplier = supplierList.get(row - commKTableModel.getFixedRowCount());
					if(col < commKTableModel.getFixedColumnCount()){
						return supplier.getSupplierName();
					}else if(col < commKTableModel.getFixedColumnCount() + priceEvaluationFactorList.size()){
						
						PriceEvaluationFactor priceEvaluationFactor = priceEvaluationFactorList.get(col - commKTableModel.getFixedColumnCount());
						EvaluationFactor evaluationFactor = priceEvaluationFactor.getEvaluationFactor();
						BidPrice bidPrice = priceEvaluationFactor.getBidPrice();
						if(priceEvaluationFactor.getFactorType() == PriceEvaluationFactor.BID_EVAL_PRICE_TOTAL){
							SupplierOpeningResult supplierOpeningResult = supplier.getSupplierOpeningResult();
							if(null != supplierOpeningResult && null != supplierOpeningResult.getEvaluationPrice()){
								if(null != bidPrice && BidPriceEnum.IS_RATE.equals(bidPrice.getPriceType())) {
									return NumberFormatUtils.format(supplierOpeningResult.getEvaluationPrice());
								}else {
									return MoneyUtils.getMoneyFormatByUnit(supplierOpeningResult.getEvaluationPrice(), tenderProjectRuleEntity.getEvalPriceUnit());
								}
							}
						}else if(priceEvaluationFactor.getFactorType() == PriceEvaluationFactor.BID_PRICE){
							if(null != bidPrice){
								BidPriceResult bidPriceResult = supplierBidPriceResultMap.get(supplier.getId()+"_"+bidPrice.getId());
								if(null != bidPriceResult){
									if(null != bidPrice && BidPriceEnum.IS_RATE.equals(bidPrice.getPriceType())) {
										return NumberFormatUtils.format(bidPriceResult.getEvaluationPrice());
									}else {
										return MoneyUtils.getMoneyFormatByUnit(bidPriceResult.getEvaluationPrice(), tenderProjectRuleEntity.getEvalPriceUnit());
									}
								}
							}
						}else {
							EvaluationFactorResult evalFactorResult = supplierEvaluationFactorResultMap.get(supplier.getId()).get(evaluationFactor.getId());
							if(priceEvaluationFactor.getFactorType() == PriceEvaluationFactor.BASE_PRICE){
								if(null != evalFactorResult.getBasePrice()){
									if(null != bidPrice && BidPriceEnum.IS_RATE.equals(bidPrice.getPriceType())) {
										return NumberFormatUtils.format(evalFactorResult.getBasePrice());
									}else {
										
										return MoneyUtils.getMoneyFormatByUnit(evalFactorResult.getBasePrice(), tenderProjectRuleEntity.getEvalPriceUnit());
									}
								}
							}else  if(priceEvaluationFactor.getFactorType() == PriceEvaluationFactor.PRICE_SCORE){
								if(null != evalFactorResult.getEvaluationScore()){
									return NumberFormatUtils.format(evalFactorResult.getEvaluationScore(), scoreRound);
								}
							}else if(priceEvaluationFactor.getFactorType() == PriceEvaluationFactor.PRICE_FINAL_SCORE){
								if(null != evalFactorResult.getEvaluationFinalScore()){
									return NumberFormatUtils.format(evalFactorResult.getEvaluationFinalScore(), scoreRound);
								}
							}
						}
					}else {
						//供应商的汇总
						ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultMap.get(supplier.getId());
						if(null != expertEvaluationResult){
							if(col == scoreSumCol){
								if(null != expertEvaluationResult.getEvaluationScore()){
									return NumberFormatUtils.format(expertEvaluationResult.getEvaluationScore(), scoreRound);
								}
							}else if(col == finalScoreSumCol){
								if(null != expertEvaluationResult.getEvaluationFinalScore()){
									return NumberFormatUtils.format(expertEvaluationResult.getEvaluationFinalScore(), scoreRound);
								}
							}else if(col == sortCol){
								if(expertEvaluationResult.getSortNoIntValue() > 0){
									return expertEvaluationResult.getSortNoIntValue();
								}
							}
						}
					}
				}
				return "";
		}};
		commKTableModel.setCellContentRender(contentRender);
		
		final TextWrapCellRenderer textCellRenderer = new TextWrapCellRenderer();
		textCellRenderer.setAlignment(SWT.CENTER);
		// 设置单元格渲染器
		CellRender cellRender = new CellRender(){
			public Object receiveCellRender(int row, int col) {
				if(commKTableModel.isFixedCell(col, row)){
					return new FixedWrapCellRenderer();
				}else if(row >= commKTableModel.getFixedRowCount() && col >= commKTableModel.getFixedColumnCount() && col < commKTableModel.getFixedColumnCount() + priceEvaluationFactorList.size()){
//					return new TextWrapCellRenderer();
					return textCellRenderer;
				}
				return textCellRenderer;
		}};
		commKTableModel.setCellRender(cellRender);
		
		// 设置单元格编辑器
        final KTableCellEditorText text = new KTableCellEditorText();
		text.setSingleClickEdit(true);
		CellEditorRender cellEditorRender = new CellEditorRender(){
			public Object receiveCellEditor(int row, int col) {
				if(row >= commKTableModel.getFixedRowCount() && col >= commKTableModel.getFixedColumnCount() && col < commKTableModel.getFixedColumnCount() + priceEvaluationFactorList.size()){
//					Supplier supplier = supplierList.get(row - commKTableModel.getFixedRowCount());
					PriceEvaluationFactor priceEvaluationFactor = priceEvaluationFactorList.get(col - commKTableModel.getFixedColumnCount());
					EvaluationFactor evaluationFactor = priceEvaluationFactor.getEvaluationFactor();
//					EvaluationFactorResult evalFactorResult = supplierEvaluationFactorResultMap.get(supplier.getId()).get(evaluationFactor.getId());
					if(priceEvaluationFactor.getFactorType() == PriceEvaluationFactor.BASE_PRICE){
						if(changeBasePrice){
							return text;
						}
					}else if(priceEvaluationFactor.getFactorType() == PriceEvaluationFactor.PRICE_SCORE){
						if(changePriceScore || null == evaluationFactor.getBidPriceCode()){
							return text;
						}
					}else if(priceEvaluationFactor.getFactorType() == PriceEvaluationFactor.PRICE_FINAL_SCORE){
						// 只有不带权重的指标才允许修改最终得分
						if(null == evaluationFactor.getFactorFinalWeight()){
							return text;
						}
					}
				}
				return null;
			}};
		commKTableModel.setCellEditorRender(cellEditorRender);
		
		final Image editImage = ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/edit.png");
		// 设置单元格图片
		CellImageRender cellImageRender = new CellImageRender() {
			public Image receiveCellImage(int row, int col) {
				if(row >= commKTableModel.getFixedRowCount() && col >= commKTableModel.getFixedColumnCount() && col < commKTableModel.getFixedColumnCount() + priceEvaluationFactorList.size()){
					PriceEvaluationFactor priceEvaluationFactor = priceEvaluationFactorList.get(col - commKTableModel.getFixedColumnCount());
					EvaluationFactor evaluationFactor = priceEvaluationFactor.getEvaluationFactor();
					if(priceEvaluationFactor.getFactorType() == PriceEvaluationFactor.BASE_PRICE && changeBasePrice){
						return editImage;
					}else if(priceEvaluationFactor.getFactorType() == PriceEvaluationFactor.PRICE_SCORE && (changePriceScore || null == evaluationFactor.getBidPriceCode())){
						return editImage;
					}else if(priceEvaluationFactor.getFactorType() == PriceEvaluationFactor.PRICE_FINAL_SCORE){
						// 只有不带权重的指标才允许修改最终得分
						if(null == evaluationFactor.getFactorFinalWeight()){
							return editImage;
						}
					}
				}
				return null;
		}};
		commKTableModel.setCellImageRender(cellImageRender);
		// 编辑单元格内容
		CellEditContentRender editContentRender = new CellEditContentRender(){
			public void receiveContent(int row, int col, Object content) {
				if(null != content && !"".equals(content.toString())){
					if(row >= commKTableModel.getFixedRowCount() && col >= commKTableModel.getFixedColumnCount() && col < commKTableModel.getFixedColumnCount() + priceEvaluationFactorList.size()){
						Supplier supplier = supplierList.get(row - commKTableModel.getFixedRowCount());
						PriceEvaluationFactor priceEvaluationFactor = priceEvaluationFactorList.get(col - commKTableModel.getFixedColumnCount());
						EvaluationFactor evaluationFactor = priceEvaluationFactor.getEvaluationFactor();
						EvaluationFactorResult evalFactorResult = supplierEvaluationFactorResultMap.get(supplier.getId()).get(evaluationFactor.getId());
						if(priceEvaluationFactor.getFactorType() == PriceEvaluationFactor.BASE_PRICE){
							BigDecimal basePrice = MoneyUtils.getMoneyFromText(content.toString(), tenderProjectRuleEntity.getEvalPriceUnit());
							if(null != basePrice){
								if(basePrice.doubleValue() > 0 && (null == evalFactorResult.getBasePrice() || basePrice.compareTo(evalFactorResult.getBasePrice()) != 0)){
									// 基准价前一列就是投标报价
									PriceEvaluationFactor bidPriceEvaluationFactor = priceEvaluationFactorList.get(col - commKTableModel.getFixedColumnCount() -1);
									refreshCalculator(bidPriceEvaluationFactor, basePrice, true);
								}
							}
						}else {
							BigDecimal evaluationScore = ObjectUtils.getObjBigDecimal(content);
							if(null != evaluationScore){
								
								if(evaluationScore.doubleValue() < 0 || evaluationScore.doubleValue() > evaluationFactor.getFactorScore().doubleValue()){
									if(!isShowError){
										isShowError = true;
//										MessageDialog.openShortMessage("请输入0-"+NumberFormatUtils.format(evaluationFactor.getFactorScore())+"范围的分值！");
										isShowError = false;
									}
									return;
								}
								// 对所有供应商的当前指标重新设置基准价，并通过公式计算价格得分
								Map<String, EvaluationFactorResult> evaluationFactorResultMap = supplierEvaluationFactorResultMap.get(supplier.getId());
								EvaluationFactorResult evaluationFactorResult = evaluationFactorResultMap.get(evaluationFactor.getId());

								PriceScoreCalculator priceScoreCalculator = priceScoreCalculatorMap.get(evaluationFactor.getId());
								if(null != priceScoreCalculator){
									evaluationScore = priceScoreCalculator.getPriceScoreRound(evaluationScore);
									evaluationFactorResult.setEvaluationScore(evaluationScore);
									evaluationFactorResult.setEvaluationFinalScore(priceScoreCalculator.getScoreByWeight(evaluationFactorResult.getEvaluationScore()));
								}else {
									
									evaluationScore = BigDecimalUtils.round(evaluationScore, scoreRound);
									evaluationFactorResult.setEvaluationScore(evaluationScore);
									evaluationFactorResult.setEvaluationFinalScore(getEvaluationFinalScore(evaluationFactor, evaluationFactorResult.getEvaluationScore()));
								}
								evaluationFactorResult.setEvaluationOpinion(null);
							
							}
						}
					}
				}
				resultChanged = true;
				reSetSortNo();
				resultKTable.redraw();
			}
		};
		commKTableModel.setCellEditContentRender(editContentRender);
		
		// 编辑单元格对齐方式
		CellAlignmentRender cellAlignmentRender = new CellAlignmentRender(){
			public int receiveCellAlignment(int row, int col) {
				if(row < commKTableModel.getFixedRowCount()){
					return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
				}else {
					if(col < commKTableModel.getFixedColumnCount()){
						return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_LEFT;
					}else if(col < commKTableModel.getFixedColumnCount() + priceEvaluationFactorList.size()){
						PriceEvaluationFactor priceEvaluationFactor = priceEvaluationFactorList.get(col - commKTableModel.getFixedColumnCount());
						if(priceEvaluationFactor.getFactorType() == PriceEvaluationFactor.BID_EVAL_PRICE_TOTAL){
							return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_RIGHT;
						}else if(priceEvaluationFactor.getFactorType() == PriceEvaluationFactor.BID_PRICE){
							return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_RIGHT;
						}else if(priceEvaluationFactor.getFactorType() == PriceEvaluationFactor.BASE_PRICE){
							return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_RIGHT;
						}
					}
				}
				return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
			}
		};
		commKTableModel.setCellAlignmentRender(cellAlignmentRender);
		
		// 设置单元格内容
		CellTooltipRender cellTooltipRender = new CellTooltipRender(){
			public String receiveCellTooltip(int row, int col) {
				if(row >= commKTableModel.getFixedRowCount() && col >= commKTableModel.getFixedColumnCount() && col < commKTableModel.getFixedColumnCount() + priceEvaluationFactorList.size()){
					Supplier supplier = supplierList.get(row - commKTableModel.getFixedRowCount());
					PriceEvaluationFactor priceEvaluationFactor = priceEvaluationFactorList.get(col - commKTableModel.getFixedColumnCount());
					EvaluationFactor evaluationFactor = priceEvaluationFactor.getEvaluationFactor();
					EvaluationFactorResult evalFactorResult = supplierEvaluationFactorResultMap.get(supplier.getId()).get(evaluationFactor.getId());
					if(null != evalFactorResult && null != evalFactorResult.getEvaluationOpinion()){
						return evalFactorResult.getEvaluationOpinion();
					}
				}
				return "";
			}
		};	
		commKTableModel.setCellTooltipRender(cellTooltipRender);
		resultKTable.addCellSelectionListener(new KTableCellSelectionAdapter(){
			@Override
			public void cellSelected(int col, int row, int statemask) {
				if(curCol == col && curRow == row){
					return;
				}
				curCol = col; 
				curRow = row;
				if(row >= commKTableModel.getFixedRowCount() && col >= commKTableModel.getFixedColumnCount() && col < commKTableModel.getFixedColumnCount() + priceEvaluationFactorList.size()){
					Supplier supplier = supplierList.get(row - commKTableModel.getFixedRowCount());
					PriceEvaluationFactor priceEvaluationFactor = priceEvaluationFactorList.get(col - commKTableModel.getFixedColumnCount());
					EvaluationFactor evaluationFactor = priceEvaluationFactor.getEvaluationFactor();
					EvaluationFactorResult evalFactorResult = supplierEvaluationFactorResultMap.get(supplier.getId()).get(evaluationFactor.getId());
					resultClickEventAction(supplier, evaluationFactor, evalFactorResult);
				}
				return;
			}
			@Override
			public void fixedCellSelected(int col, int row, int statemask) {
				resultKTable.setSelection(curCol > commKTableModel.getFixedColumnCount()?curCol:commKTableModel.getFixedColumnCount(), row, true);
			}
		});
		resultKTable.setModel(commKTableModel);
		evaluationTableComposite.layout();
	}
	
	/**
	 * @Description: 刷新公式计算
	 * @author guoyr
	 * @param priceEvaluationFactor
	 * @param basePrice 
	 */
	private void refreshCalculator(PriceEvaluationFactor priceEvaluationFactor, BigDecimal basePrice, boolean useCalculator){
		if(null == bidPriceResultService){
			bidPriceResultService = new BidPriceResultService();
		}
		EvaluationFactor evaluationFactor = priceEvaluationFactor.getEvaluationFactor();
		
		if(null != evaluationFactor && null != evaluationFactor.getComputerParams()){
			BidPrice factorBidPrice = priceEvaluationFactor.getBidPrice();
			
			List<BigDecimal> evalPriceList = new ArrayList<BigDecimal>();
			
			if(null != factorBidPrice){
				List<BidPriceResult> bidPriceResultList = bidPriceResultService.getBidPriceResultList(tenderProjectEntity.getId(), factorBidPrice.getId());
				if(null != bidPriceResultList && bidPriceResultList.size() > 0){
					for(Supplier supplier : supplierList){
						for(BidPriceResult bidPriceResult : bidPriceResultList){
							if(null != bidPriceResult.getSupplierId() && supplier.getId().equals(bidPriceResult.getSupplierId())){
								supplierBidPriceResultMap.put(bidPriceResult.getSupplierId()+"_"+bidPriceResult.getBidPriceId(), bidPriceResult);
								if(null != bidPriceResult.getEvaluationPrice()){
									evalPriceList.add(bidPriceResult.getEvaluationPrice());
								}
								break;
							}
						}
					}
				}
			}else if(null != evaluationFactor.getBidPriceCode() && evaluationFactor.getBidPriceCode().equals(BidPrice.BID_PRICE_TOTAL_CODE)){
				for(Supplier supplier : supplierList){
					SupplierOpeningResult supplierOpeningResult = supplier.getSupplierOpeningResult();
					if(null != supplierOpeningResult && null != supplierOpeningResult.getEvaluationPrice()){
						evalPriceList.add(supplierOpeningResult.getEvaluationPrice());
					}
				}
			}
			PriceScoreCalculator priceScoreCalculator = null; 
			if(evalPriceList.size() > 0){
				priceScoreCalculator = priceScoreCalculatorMap.get(evaluationFactor.getId()); 
				// 为每个分项报价设置一个计算器，并初始化
				if(null == priceScoreCalculator){
					priceScoreCalculator = new PriceScoreCalculator();
					priceScoreCalculatorMap.put(evaluationFactor.getId(), priceScoreCalculator);
					priceScoreCalculator.init(tenderProjectEntity, tenderProjectRuleEntity, null, evaluationFactor, evalPriceList);
				}
				if(null != basePrice){
					priceScoreCalculator.setBasePrice(basePrice);
				}else {
					priceScoreCalculator.init(tenderProjectEntity, tenderProjectRuleEntity, null, evaluationFactor, evalPriceList);
				}
				hasCalculator = true;
				if(null != factorBidPrice){
					for(Supplier supplier : supplierList){
						BidPriceResult bidPriceResult = supplierBidPriceResultMap.get(supplier.getId()+"_"+factorBidPrice.getId());
						EvaluationFactorResult evaluationFactorResult = supplierEvaluationFactorResultMap.get(supplier.getId()).get(evaluationFactor.getId());
						if(null != evaluationFactorResult && null != bidPriceResult && null != bidPriceResult.getEvaluationPrice()){
							CalculationResult calculationResult = priceScoreCalculator.calculation(bidPriceResult.getEvaluationPrice());
							if(useCalculator){
								evaluationFactorResult.setBasePrice(priceScoreCalculator.getBasePrice());
								evaluationFactorResult.setDeviationRate(calculationResult.getDeviationRate());
								evaluationFactorResult.setEvaluationOpinion(calculationResult.getFormual());
								evaluationFactorResult.setEvaluationScore(calculationResult.getPriceScore());
								evaluationFactorResult.setEvaluationFinalScore(calculationResult.getPriceFinalScore());
							}else {
								priceScoreTips.setText(MessageFormat.format(Messages.Tips_notSameWidthFormulaScore, Messages.Button_recalculatePriceScore));
							}
						}
					}
				}else if(null != evaluationFactor.getBidPriceCode() && evaluationFactor.getBidPriceCode().equals(BidPrice.BID_PRICE_TOTAL_CODE)){
					for(Supplier supplier : supplierList){
						SupplierOpeningResult supplierOpeningResult = supplier.getSupplierOpeningResult();
						EvaluationFactorResult evaluationFactorResult = supplierEvaluationFactorResultMap.get(supplier.getId()).get(evaluationFactor.getId());
						if(null != evaluationFactorResult && null != supplierOpeningResult && null != supplierOpeningResult.getEvaluationPrice()){
							CalculationResult calculationResult = priceScoreCalculator.calculation(supplierOpeningResult.getEvaluationPrice());
							if(useCalculator){
								evaluationFactorResult.setBasePrice(priceScoreCalculator.getBasePrice());
								evaluationFactorResult.setDeviationRate(calculationResult.getDeviationRate());
								evaluationFactorResult.setEvaluationOpinion(calculationResult.getFormual());
								evaluationFactorResult.setEvaluationScore(calculationResult.getPriceScore());
								evaluationFactorResult.setEvaluationFinalScore(calculationResult.getPriceFinalScore());
							}else {
								priceScoreTips.setText(MessageFormat.format(Messages.Tips_notSameWidthFormulaScore, Messages.Button_recalculatePriceScore));
							}
						}
					}
				}
			}
			
		}
	}
	
	/**
	 * @Description: 添加监听
	 * @author wangxiang
	 * @param listener
	 */
	public void addPriceReviewEvaluationListener(PriceReviewEvaluationListener listener){
		this.listener= listener;
	}
	
	/**
	 * @Description: 得到最终得分
	 * @author guoyr
	 * @param evaluationFactor
	 * @param evaluationScore
	 * @return 
	 */
	private BigDecimal getEvaluationFinalScore(EvaluationFactor evaluationFactor, BigDecimal evaluationScore){
		BigDecimal evaluationFinalScore = evaluationScore;
		if(null != evaluationScore && null != evaluationFactor.getFactorFinalWeight()){
			evaluationFinalScore = BigDecimalUtils.div(BigDecimalUtils.mul(evaluationFactor.getFactorFinalWeight(), evaluationScore), new BigDecimal(100));
		}
		return BigDecimalUtils.round(evaluationFinalScore, scoreRound);
	}
	
	/**
	 * @Description: 保存评审结果
	 * @author guoyr
	 * @return 
	 */
	public boolean savePriceEvaluationResult(){
		expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.IN_PROGRESS, tenderProjectEntity.getId(), evaluationStep,CURRENT_EXPERT.getUserId());
		boolean saveSuccess = true;
		// 如果修改了结果，才保存结果
		if(resultChanged){
			saveSuccess = expertEvaluationStepService.saveEvaluationResult(tenderProjectEntity.getId(), CURRENT_EXPERT.getUserId(), evaluationStep, expertEvaluationResultMap, supplierEvaluationFactorResultMap);
			if(saveSuccess){
				resultChanged = false;
			}else {
				resultChanged = true;
			}
		}
		return saveSuccess;
	}
	
	/** 
	 * @Description: 提交评审结果
	 * @author guoyr
	 * @return 
	 */
	public boolean submitPriceEvaluationResult() {
		String errorMessage = null;
		for(Supplier supplier : supplierList){
			Map<String, EvaluationFactorResult> supplierFactorResultMap = supplierEvaluationFactorResultMap.get(supplier.getId());
			for(Entry<String, EvaluationFactorResult> factorResultEntry : supplierFactorResultMap.entrySet()){
				EvaluationFactorResult evaluationFactorResult = factorResultEntry.getValue();
				if(null == evaluationFactorResult.getEvaluationFinalScore()){
					errorMessage = MessageFormat.format(Messages.Tips_existNoScoreFactor, supplier.getSupplierName());
					break;
				}
			}
		}
		if(null != errorMessage){
			MessageDialog.openWarning(getShell(), errorMessage);
			return false;
		}
		String message = MessageFormat.format(Messages.Tips_submitEvaluationResult, evaluationStep.getStepName());
		if(!MessageDialog.openConfirm(getShell(), Messages.Button_submitEvaluationResult, message, new String[]{Messages.Button_confirmComplate, Messages.Button_continueEvaluation})){
			return false;
		}
		return expertEvaluationStepService.submitEvaluationResult(tenderProjectEntity.getId(),tenderProjectRuleEntity, CURRENT_EXPERT.getUserId(), evaluationStep, supplierList);
	}

	/** 
	 * @Description: 释放资源
	 * @author guoyr 
	 */
	public void dispose() {
		imageResolver.disposite();
		evaluationFactorList.clear();
		evaluationFactorList = null;
		super.dispose();
	}
	@Override
	
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
