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

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.widgets.EMenu;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
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.Display;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
import com.gx.obe.business.entity.ScoreItem;
import com.gx.obe.common.file.enumeration.FileConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.dialog.InputDialog;
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.EvaluationFactorEnum;
import com.gx.obe.components.core.enumeration.EvaluationStepEnum;
import com.gx.obe.evaluation.step.dialog.ImportHistoryScoreResultDialog;
import com.gx.obe.evaluation.step.dialog.ImportOtherExpertEvaluationResultDialog;
import com.gx.obe.evaluation.step.dialog.ScoreEvaluationDialog;
import com.gx.obe.evaluation.step.inter.IEvaluationTable;
import com.gx.obe.evaluation.step.listener.EvaluationSupplierFactorListener;
import com.gx.obe.evaluation.step.listener.ImportEvaluationResultListener;
import com.gx.obe.evaluation.step.thread.ImportEvaluationBySupplierScoreThread;
import com.gx.obe.http.json.JsonUtil;
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.Keys;
import com.gx.obe.util.utils.NumberFormatUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.SerializeUtils;
import com.gx.obe.util.utils.SortUtils;
import com.gx.obe.util.utils.StringUtils;
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.auth.AuthUser;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
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.SupplierService;
import com.gx.obe.web.service.evaluation.EvaluationFactorResultService;
import com.gx.obe.web.service.evaluation.EvaluationFactorService;
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.gx.obe.web.vo.FixedFactorItemVo;
import com.gx.obe.web.vo.FixedFactorSupplierVo;
import com.gx.utils.ActionLogUtils;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

import de.kupzog.ktable.KTable;
import de.kupzog.ktable.KTableCellMouseRightAdapter;
import de.kupzog.ktable.KTableCellSelectionAdapter;
import de.kupzog.ktable.SWTX;
import de.kupzog.ktable.editors.KTableCellEditorGCombo;
import de.kupzog.ktable.editors.KTableCellEditorText;
import de.kupzog.ktable.model.CommonKtableCModel;
import de.kupzog.ktable.renderers.CellAlignmentRender;
import de.kupzog.ktable.renderers.CellBackgroundRender;
import de.kupzog.ktable.renderers.CellContentRender;
import de.kupzog.ktable.renderers.CellEditContentRender;
import de.kupzog.ktable.renderers.CellEditorRender;
import de.kupzog.ktable.renderers.CellForegroundRender;
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 ScoreEvaluationTableComposite extends Composite implements IEvaluationTable{

	private TenderProjectEntity tenderProject;
	private EvaluationStep evaluationStep;
	private TenderProjectRuleEntity projectRule;
	private ImageResolver imageResolver = new ImageResolver();
	private EvaluationFactorService evaluationFactorService = new EvaluationFactorService();
	private ExpertEvaluationStepService expertEvaluationStepService = new ExpertEvaluationStepService();
	private EvaluationFactorResultService evaluationFactorResultService;
	private ExpertEvaluationResultService expertEvaluationResultService;
	public EvaluationStepService evaluationStepService = new EvaluationStepService();
	private ExpertService expertService;
	public SupplierService supplierService = new SupplierService();
	private Composite evaluationComposite;
	private Composite composite_5;
	private Composite evaluationTableComposite;
	private Composite composite_9;
	private Text factor_evaluation_opinion;
	private StyledText evaluation_factor_memo;
	private List<EvaluationFactor> evaluationFactorList = new ArrayList<EvaluationFactor>();
	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 int autoSaveMinTime = 0;
	private Logger LOG = Logger.getLogger(ScoreEvaluationTableComposite.class);
	private String supplierName;
	private Map<String, Color> contentBackgroundMap = new HashMap<String, Color>();
	private Map<String, Color> contentForegroundMap = new HashMap<String, Color>();
	private Color importColor = SWTResourceManager.getColor(SWT.COLOR_BLUE);
	private Color rightBgColor = SWTResourceManager.getColor(194, 236, 150);
	private Color errorBgColor = SWTResourceManager.getColor(255, 153, 153);
//	private Color fornRightColor = SWTResourceManager.getColor(SWT.COLOR_BLACK);
//	private Color fornErrorColor = SWTResourceManager.getColor(SWT.COLOR_RED);
	// 记录供应商每个指标的评审结果
	private Map<String, Map<String, EvaluationFactorResult>> supplierEvaluationFactorResultMap;
	// 记录专家对供应商的评审汇总结果
	private Map<String, ExpertEvaluationResult> expertEvaluationResultMap;
	private AuthUser curUser;
	private EvaluationSupplierFactorListener evaluationSupplierFactorListener;
	private boolean isShowError = false;
	private boolean finishEdit = true;
	private BigDecimal evaluationStepWeight;
	private int fontSize = 9;
	private int scoreRound = 2;
	private int expertScoreRound = 2;
	private boolean indexIsBidNumber = false;
	private boolean evaluationBySupplier = false;
	private ImportEvaluationBySupplierScoreThread ImportEvaluationBySupplierScoreThread;
	BigDecimal warningRange = null;
	private int maxFactorNameLength = 10;
	private String emptyScoreContent = null;
	private Map<String, FixedFactorItemVo> supplierFactorScoreRangeMap = new HashMap<String, FixedFactorItemVo>();
	private Map<String, String> fixedFactorMap = new HashMap<String, String>();
	private boolean sameScoreCheck = true;
	private final Map<Keys, Color> objectiveBackgroundMap = new HashMap<Keys, Color>();
	private Map<String, String> objectiveFactorIdMap = new HashMap<String, String>();
	static{
		
		/**
		 * 第三层验证
		 * **/
		try {
			// 这是公钥
			String publicKey = "308201b83082012c06072a8648ce3804013082011f02818100fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b76b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c70215009760508f15230bccb292b982a2eb840bf0581cf502818100f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d0782675159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e13c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243bcca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a0381850002818100def368b8757ce205e46b76640e4886cbd1604753a90cb07527b834a65e83b4f21b97ef443097e882e7c18dea3daea499e31201fa8e4df0f36b0cc00a64a01ccdd831f2856b7549f0eae5d7150fe5800e40bb3b25e3b2a38c51b3f07edd6c90b6f3db70ac12bc11d480cbcaa23a9f30f4db77c0928f3faf477f5e672f43a42a73";
			byte pub[] = com.gx.security.CertUtil.decodeHex(publicKey);
			java.security.spec.X509EncodedKeySpec pubKeySpec = new java.security.spec.X509EncodedKeySpec(pub);
			java.security.KeyFactory keyFactory = java.security.KeyFactory.getInstance(com.gx.security.CertEnum.KEY_INIT_CODE);
			java.security.PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
			java.security.Signature sig = java.security.Signature.getInstance(com.gx.security.CertEnum.SIGN_RNG_NAME);/* 建立一个签名并用公共密钥初始化 */
			sig.initVerify(pubKey);
			// 这里指定license文件
			File licenseFile = new File(FileConstants.PRODUCT_ROOT + com.gx.security.CertValidator.getInstance().getSequence()+".cer");
			BufferedReader in = new BufferedReader(new FileReader(licenseFile));
			String certificateXml = com.gx.security.CertUtil.decodeBase64(in.readLine());// 解码
			in.close();
			com.gx.security.Cert certificate = com.gx.security.CertUtil.fromXML(certificateXml);// 从xml读取license
			sig.update(certificate.getSignnatureData());/* 更新并校验数据 */
			boolean flag = sig.verify(com.gx.security.CertUtil.decodeHex(certificate.getSignature()));
			if(!flag){
				throw new com.gx.security.SecurityRuntimeException("您的授权证书无效 ，请与"+certificate.getIssuer()+"联系。");
			}
			if (certificate.getExpiresDate().before(com.gx.security.NowTimeUtils.getNowDate())) {
				throw new com.gx.security.SecurityRuntimeException("您的授权证书已过期，请与"+certificate.getIssuer()+"联系。");
			}
			boolean valMac = true;
			// 如果是试用版本则不需要验证mac
			if(certificate.getCertType().getType().equals(com.gx.security.Cert.CertType.TRIAL.getType())){
				valMac = false;
			}
			if (valMac && null != certificate.getMotherBoardSN()) {
				if (!certificate.getMotherBoardSN().contains(com.gx.security.CertValidator.getInstance().getMotherBoardSN())) {
					throw new com.gx.security.SecurityRuntimeException("不是当前设备SN的授权文件，请与"+certificate.getIssuer()+"联系。");
				}
			}
			if (valMac && null != certificate.getMac()) {
				if (!certificate.getMac().contains(com.gx.security.CertValidator.getInstance().getMac())) {
					throw new com.gx.security.SecurityRuntimeException("不是当前设备的授权文件，请与"+certificate.getIssuer()+"联系。");
				}
			}
		} catch (Exception e) {
			throw new com.gx.security.SecurityRuntimeException(e);
		}
	}
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public ScoreEvaluationTableComposite(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);
		
		evaluationTableComposite = new Composite(evaluationComposite, SWT.NONE);
		evaluationTableComposite.setLayout(new FillLayout(SWT.HORIZONTAL));
		evaluationTableComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		ESepator factor_sep = new ESepator(evaluationComposite, SWT.NONE);
		factor_sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		factor_sep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		composite_5 = new Composite(evaluationComposite, SWT.NONE);
		composite_5.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		GridLayout gl_composite_5 = new GridLayout(2, 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.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		evaluation_factor_memo.setText("");
		GridData gd_evaluation_factor_memo = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		gd_evaluation_factor_memo.heightHint = 58;
		gd_evaluation_factor_memo.widthHint = 500;
		evaluation_factor_memo.setLayoutData(gd_evaluation_factor_memo);
		
		composite_9 = new Composite(composite_5, SWT.BORDER);
		composite_9.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1));
		GridLayout gl_composite_9 = new GridLayout(1, false);
		gl_composite_9.marginWidth = 0;
		gl_composite_9.verticalSpacing = 0;
		gl_composite_9.horizontalSpacing = 0;
		gl_composite_9.marginHeight = 0;
		composite_9.setLayout(gl_composite_9);
		
		factor_evaluation_opinion = new Text(composite_9, SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		factor_evaluation_opinion.addFocusListener(new FocusListener() {
			@Override
			public void focusLost(FocusEvent e) {
				setFactorEvaluationOpinion();
			}
			@Override
			public void focusGained(FocusEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
//		factor_evaluation_opinion.addMouseTrackListener(new MouseTrackAdapter() {
//			@Override
//			public void mouseExit(MouseEvent e) {
//				setFactorEvaluationOpinion();
//			}
//		});
		factor_evaluation_opinion.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		factor_evaluation_opinion.setTextLimit(500);
		factor_evaluation_opinion.setText("");
		GridData gd_evaluationOpinionText = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_evaluationOpinionText.heightHint = 24;
		gd_evaluationOpinionText.widthHint = 200;
		factor_evaluation_opinion.setLayoutData(gd_evaluationOpinionText);
		factor_evaluation_opinion.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				if(factor_evaluation_opinion.getText().trim().length() == 0){
					Image image = ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/edit_input.png");
					e.gc.setForeground(SWTResourceManager.getColor(80, 80, 80));
					e.gc.drawText(Messages.Tips_enterOpinioForCurFactor, image.getBounds().width + 3, 1);
					e.gc.drawImage(image, 1, 1);
					e.gc.dispose();
				}
			}
		});
		expertEvaluationStepService = new ExpertEvaluationStepService();
		expertEvaluationResultService = new ExpertEvaluationResultService();
	}
	
	/**
	 * @Description: 初始化
	 * @author guoyr
	 */
	public void refresh(TenderProjectEntity tenderProject,TenderProjectRuleEntity projectRule, AuthUser user, EvaluationStep evaluationStep, List<Supplier> supplierList, EvaluationSupplierFactorListener evaluationSupplierFactorListener){
		this.tenderProject = tenderProject;
		this.projectRule = projectRule;
		this.curUser = user;
		this.evaluationStep = evaluationStep;
		this.supplierList = supplierList;
		this.evaluationSupplierFactorListener = evaluationSupplierFactorListener;
		
		evaluationFactorResultService =  new EvaluationFactorResultService();
		
		warningRange = evaluationStep.getEvaluationParamByKey(com.gx.obe.components.core.enumeration.EvaluationStepEnum.PARAM_WARNING_RANGE, BigDecimal.class, BigDecimal.ZERO);
		if(null != warningRange && warningRange.compareTo(BigDecimal.ZERO) > 0){
			// 转换为百分率
			warningRange = BigDecimalUtils.div(warningRange,  new BigDecimal(100));
		}else {
			warningRange = null;
		}
		supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProject.getTenderType());
		resultChanged = true;
		curRow = 0;
		curCol = 0;
		// 修改评标专家的评审记录
		ActionLogUtils.addServerLog(tenderProject.getId(), "进入" + evaluationStep.getStepName(), Constants.USER.getUserName());
		expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.IN_PROGRESS, tenderProject.getId(), evaluationStep, curUser.getId());
		indexIsBidNumber = CommonEnum.YES.equals(evaluationStep.getEvaluationParamByKey(com.gx.obe.components.core.enumeration.EvaluationStepEnum.PARAM_INDEX_IS_BIDNUMBER));
		scoreRound = projectRule.getScoreRoundIntVal();
		expertScoreRound = evaluationStep.getEvaluationParamByKey(com.gx.obe.components.core.enumeration.EvaluationStepEnum.EXPERT_SCORE_ROUND, Integer.class, 2);
		boolean allowEmptyObj = evaluationStep.getEvaluationParamByKey(com.gx.obe.components.core.enumeration.EvaluationStepEnum.PARAM_EVALUATION_EMPTY, Boolean.class, false);
		if(allowEmptyObj){
			emptyScoreContent = "\\";
		}
		
		// 加载定档得分
		initFixedFactorInfo();
		
		initEvaluationResult();
		if(Constants.FACTOR_IS_HORIZONTAL){
			drawLeftFactorEvaluationTable();
		}else {
			drawLeftSupplierEvaluationTable();
		}
		autoSaveMinTime = projectRule.getAutoSaveTimeNotNull();
		// 自动保存
		if(autoSaveMinTime > 0){
			new Thread(autoSave).start();
		}
		sameScoreCheck   = evaluationStep.getEvaluationParamByKey(EvaluationStepEnum.CHECK_SAME_EVALUATION, Boolean.class, true);
		
		// 按供应商打分
		boolean evaluationBySupplierObj = evaluationStep.getEvaluationParamByKey(com.gx.obe.components.core.enumeration.EvaluationStepEnum.PARAM_EVALUATION_BY_SUPPLIER, Boolean.class, false);
		if(null != tenderProject.getParentId() && evaluationBySupplierObj){
			evaluationBySupplier = true;
		}else {
			evaluationBySupplier = false;
		}
		if(evaluationBySupplier){
			ImportEvaluationBySupplierScoreThread = new ImportEvaluationBySupplierScoreThread(tenderProject, supplierList, evaluationFactorList, curUser, createImportEvaluationResultListener(true));
			ImportEvaluationBySupplierScoreThread.start();
		}
		refreshObjectiveBackgroundMap();
	}
	
	/**
	 * @Description: 刷新内容背景颜色map
	 * @author chenxw 
	 */
	private void refreshObjectiveBackgroundMap() {
		if (objectiveFactorIdMap.size() > 0 && sameScoreCheck) {
			new Thread(new Runnable(){
				public void run(){
					List<EvaluationFactorResult> evaluationFactorResultList = evaluationFactorResultService.getEvaluationFactorResultList(tenderProject.getId(), evaluationStep.getId(), null);
					Map<Keys, List<EvaluationFactorResult>> evaluationFactorResultListMap = CollectionUtils.toArrayListHashMap(evaluationFactorResultList, new CollectionUtils.NewMap<Keys, EvaluationFactorResult>() {
						public Keys getKey(EvaluationFactorResult t) {
							return new Keys(t.getSupplierId(), t.getFactorId());
						}
					}, new HashMap<Keys, List<EvaluationFactorResult>>());
					objectiveBackgroundMap.clear();
					for (Keys key : evaluationFactorResultListMap.keySet()) {
						List<EvaluationFactorResult> factorResultList = evaluationFactorResultListMap.get(key);
						boolean isEqual = true;
						BigDecimal evaluationScore = null;
						for (EvaluationFactorResult evaluationFactorResult : factorResultList) {
							if (curUser.getId().equals(evaluationFactorResult.getUserId())) {
								continue;
							}
							BigDecimal score = evaluationFactorResult.getEvaluationScore();
							if (evaluationScore == null) {
								evaluationScore = score;
							} else if (score != null && evaluationScore.compareTo(score) != 0) {
								isEqual = false;
							}
						}
						if (isEqual && evaluationScore != null) {
							Map<String, EvaluationFactorResult> evaluationFactorResultMap = supplierEvaluationFactorResultMap.get(key.get(0));
							if (evaluationFactorResultMap != null) {
								EvaluationFactorResult supplierEvaluationFactorResult = evaluationFactorResultMap.get(key.get(1));
								if (supplierEvaluationFactorResult != null) {
									BigDecimal supplierEvaluationScore = supplierEvaluationFactorResult.getEvaluationScore();
									if (supplierEvaluationScore != null && evaluationScore.compareTo(supplierEvaluationScore) != 0) {
										isEqual = false;
									}
								}
							}
						}
						objectiveBackgroundMap.put(key, isEqual ? rightBgColor : errorBgColor);
					}
					if(!Display.getDefault().isDisposed()){
						Display.getDefault().syncExec(new Runnable(){
							public void run(){
								if (null != resultKTable && !resultKTable.isDisposed()) {
									resultKTable.redraw();
								}
							}
						});
					}
				}
			}).start();
		}
		if (null != resultKTable && !resultKTable.isDisposed()) {
			resultKTable.redraw();
		}
	}
	
	
	/**
	 * @Description: 设置表格中的字体大小
	 * @author guoyr
	 * @param fontSize 
	 */
	public void setTableFontSize(int fontSize){
		// 如果是字体大小不断在缩小，则需要先将行高设小
		if(this.fontSize > fontSize){
			for(int row = 0; row < commKTableModel.getRowCount(); row ++){
				commKTableModel.setRowHeight(row, 25);
			}
		}
		this.fontSize = fontSize;
		resultKTable.redraw();
	}
	
	/**
	 * @Description: 初始化当前评标步骤的评审指标
	 * @author guoyr
	 */
	private void initEvaluationStepFactor(){
		objectiveFactorIdMap.clear();
		evaluationFactorList.clear();
		maxLevel = 1;
		evaluationStepWeight = evaluationStep.getWeight();
		List<EvaluationFactor> evaluationTopFactorList = evaluationFactorService.getExpertEvaluationStepFactorTreeList(tenderProject.getId(), evaluationStep, curUser.getId());
		if(null != evaluationTopFactorList && evaluationTopFactorList.size() > 0){
			for(EvaluationFactor topFactor : evaluationTopFactorList){
				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);
			if(EvaluationFactorEnum.OBJECTIVE_YES.equals(evalFactor.getObjective())){
				objectiveFactorIdMap.put(evalFactor.getId(), "01");
			}
		}
	}
	
	/** 
	 * @Description: 关闭编辑器
	 * @author guoyr 
	 */
	public void closeCellEditor(){
		if(null != resultKTable){
			resultKTable.closeCellEditor();
		}
	}
	
	/**
	 * @Description: 切换评分面板
	 * @author guoyr
	 */
	public void switchCoordinateAction(){
		if(null != resultKTable && !resultKTable.isDisposed()){
			resultKTable.dispose();
		}
		Constants.FACTOR_IS_HORIZONTAL = !Constants.FACTOR_IS_HORIZONTAL;
		if(Constants.FACTOR_IS_HORIZONTAL){
			drawLeftFactorEvaluationTable();
		}else {
			drawLeftSupplierEvaluationTable();
		}
		evaluationTableComposite.layout();
	}
	
	/**
	 * @Description: 评审表格点击事件
	 * @author guoyr
	 * @param row
	 * @param col
	 */
	private void resultClickEventAction(Supplier supplier, EvaluationFactor evaluationFactor, EvaluationFactorResult  evalFactorResult){
		
		if(null != evaluationFactor){
			StringBuilder evaluationFactorMemo = new StringBuilder();
			// 如果当前指标是定档指标
			if(null != fixedFactorMap.get(evaluationFactor.getId())) {
				FixedFactorItemVo fixedFactorItemVo = supplierFactorScoreRangeMap.get(supplier.getId()+evaluationFactor.getId());
				// 如果当前指标是定档指标，但没有定档，则说明进入当前步骤时还没有完成定档，需要重新获取定档信息
				if(null == fixedFactorItemVo) {
					initFixedFactorInfo();
					fixedFactorItemVo = supplierFactorScoreRangeMap.get(supplier.getId()+evaluationFactor.getId());
				}
				if(null != fixedFactorItemVo && null != fixedFactorItemVo.getFactorMinScore() && null != fixedFactorItemVo.getFactorMaxScore()) {
					evaluationFactorMemo.append(Messages.ScoreRange + "：" +fixedFactorItemVo.getName()).append(fixedFactorItemVo.isIncludeFactorMinScore() ?  "[":"(").append(NumberFormatUtils.format(fixedFactorItemVo.getFactorMinScore())).append("-").append(NumberFormatUtils.format(fixedFactorItemVo.getFactorMaxScore())).append(fixedFactorItemVo.isIncludeFactorMaxScore() ?  "]":")").append(Messages.Points);
				}else {
					evaluationFactorMemo.append(Messages.ScoreRange + "：[未定档]");
				}
			}else {
				evaluationFactorMemo.append(Messages.ScoreRange + "：[").append(NumberFormatUtils.format(evaluationFactor.getFactorMinScore())).append("-").append(NumberFormatUtils.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();
			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(Messages.EvalFactor + "：").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);
			if(null != factorMemoTitleStyleRange){
				evaluation_factor_memo.setStyleRange(factorMemoTitleStyleRange);
				evaluation_factor_memo.setStyleRange(styleFactorMemoRange);
			}
		}
		if(null != evalFactorResult){
			showFactorEvaluationOpinion(evalFactorResult);
//			composite_9.setVisible(true);
			factor_evaluation_opinion.setEnabled(true);
		}else {
//			composite_9.setVisible(false);
			factor_evaluation_opinion.setEnabled(false);
		}
//		evaluationComposite.layout();
		if(null != evaluationSupplierFactorListener && null != supplier){
			evaluationSupplierFactorListener.clickSupplierFactor(supplier, evaluationFactor, expertEvaluationResultMap.get(supplier.getId()));
		}
	}
	
	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
	 * @param row
	 * @param col
	 */
	private void resultMouseRightClickEventAction(final Supplier supplier, final EvaluationFactor evaluationFactor, final ExpertEvaluationResult expertEvaluationResult, final EvaluationFactorResult  evalFactorResult){
		final EMenu gmenu = new EMenu(getShell());
		if(curRow >= commKTableModel.getFixedRowCount()){
			
			gmenu.addMenuItem((resultKTable.isLockedRow(curRow)?Messages.Button_unlockRow:Messages.Button_lockRow), imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/my-account.png"), null != supplier, new EMenu.ClickListener() {
				public void click() {
					if(resultKTable.isLockedRow(curRow)){
						resultKTable.releaseLockRow(curRow);
					}else {
						resultKTable.addLockedRow(curRow);
					}
					resultKTable.refreshRowResize(curRow);
				}
			});
		}
		

//		
//		if(null != evaluationFactor){
//			gmenu.addMenuItem("查看指标详情", imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/my-account.png"), null != evaluationFactor, new EMenu.ClickListener() {
//				public void click() {
//					ComplianceFactorInfoDialog dialog = new ComplianceFactorInfoDialog(getShell(), evaluationFactor);
//					dialog.open();
//				}
//			});
//		}
		if(null != supplier){
			
//			gmenu.addMenuItem("查看价格", imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/view.png"), null != supplier, new EMenu.ClickListener() {
//				public void click() {
//					EAction action = (EAction)ActionUtil.getEAction(ViewBidPriceAction.class.getName());
//					action.setTenderProject(tenderProjectEntity);
//					action.setData("tenderProjectRuleEntity", tenderProjectRuleEntity);
//					action.setData("supplier", supplier);
//					action.setName("查看价格");
//					action.setParent(getShell());
//					action.run();
//				}
//			});
			
			gmenu.addMenuItem(Messages.Button_viewLocalFiles, imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/btn/blue/location.png"), null != supplier, new EMenu.ClickListener() {
				public void click() {
					evaluationSupplierFactorListener.openLocalFile(tenderProject, supplier);
				}
			});
			gmenu.addMenuItem(Messages.Button_releaseBidFileCache, imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/btn/blue/clear.png"), true, new EMenu.ClickListener() {
				public void click() {
					evaluationSupplierFactorListener.releaseResource(supplier);
				}
			});
		}
		gmenu.addMenuItem(Messages.Button_setBidFileLoadCache, imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/btn/blue/config.png"), true, new EMenu.ClickListener() {
			public void click() {
				evaluationSupplierFactorListener.configLoadFileCacheCount();
			}
		});
		if(null != evalFactorResult){
			gmenu.addMenuItem(Messages.Button_enterDetailedEvaluationPanel, imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/edit_input.png"), null != evaluationFactor, new EMenu.ClickListener() {
				public void click() {
					ScoreEvaluationDialog dialog = new ScoreEvaluationDialog(getShell(), tenderProject, supplier, evaluationFactor, expertEvaluationResult, evalFactorResult, new CallBack(){
						@Override
						public void callBack() {
							showFactorEvaluationOpinion(evalFactorResult);
							resultKTable.redraw(curCol, curRow, 1, 1);
						}
						
					}); 
					resultKTable.closeCellEditor();
					dialog.open();
					reSetTotalScoreAndSore(evaluationFactor, evalFactorResult, expertEvaluationResult, supplier);
				}
			});
		}
//		gmenu.addMenuItem("发起询标", imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/edit_input.png"), null != supplier, new EMenu.ClickListener() {
//			public void click() {
//				InquireEnvelopDialog dialog = new InquireEnvelopDialog(getShell(), null, tenderProjectEntity, supplier, curUser, evaluationStep); 
//				if(null != evaluationFactor){
//					dialog.setContent(evaluationFactor.getFactorName()+ "\r\n"+ObjectUtils.getObjString(evaluationFactor.getMemo()));
//				}
//				dialog.open();
//			}
//		});
//	

		gmenu.addSeparator();
		gmenu.addMenuItem(Messages.Button_setMaxFactorNameLength, null, true, new EMenu.ClickListener() {
			public void click() {
				InputDialog dialog = new InputDialog(getShell(), Messages.Button_setMaxFactorNameLength, maxFactorNameLength > 0 ? maxFactorNameLength + "" : "");
				dialog.open();
				String pageSizeStr = dialog.getInputValue();
				try {
					int length = Integer.parseInt(pageSizeStr);
					maxFactorNameLength = Math.max(5, length);
				} catch (NumberFormatException e) {
					maxFactorNameLength = -1;
				}finally{
					if(Constants.FACTOR_IS_HORIZONTAL){
						drawLeftFactorEvaluationTable();
					}else {
						drawLeftSupplierEvaluationTable();
					}
				}
			}
		});
		gmenu.addMenuItem(Messages.Button_enlargeColumnWidth, ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/enlarge_width.png"), true, new EMenu.ClickListener() {
			public void click() {
				for(int i = 0; i < commKTableModel.getFixedRowCount(); i++){
					commKTableModel.setRowHeight(i, 25);
				}
				resultKTable.fillColumnWidth();
//				for (int i = 0; i < commKTableModel.getFixedRowCount(); i++) {
//					resultKTable.refreshRowResize(i);
//				}
			}
		});
		gmenu.addMenuItem(Messages.Button_reducedColumnWidth, ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/reduce_width.png"), true, new EMenu.ClickListener() {
			public void click() {
				resizeColumn(110);
			}
		});
		gmenu.open();
	}
	
	/**
	 * @Description: 递归获取当前指标指定上级如果上级为空则为本身
	 * @author guoyr
	 * @param factor
	 * @param row
	 * @return
	 */
	private EvaluationFactor getParentFactor(EvaluationFactor evaluationFactor, int level){
		EvaluationFactor parent = evaluationFactor.getParentFactor();
		if(null != parent){
			if(parent.getLevel() == level){
				return parent;
			}else if(parent.getLevel() > level) {
				return getParentFactor(parent, level);
			}else {
				return evaluationFactor;
			}
		}
		return parent;
	}
	
	/**
	 * @Description: 递归获取当前指标的所有下级指标的个数
	 * @author guoyr
	 * @param factor
	 * @return
	 */
	private int getChildCount(EvaluationFactor evaluationFactor){
		List<EvaluationFactor> childrenList = evaluationFactor.getChildFactorList();
		int childCount = null != childrenList?childrenList.size():0;
		if(childCount > 0){
			for(EvaluationFactor childrenFactor : childrenList){
				List<EvaluationFactor> subChildrenList = childrenFactor.getChildFactorList();
				if(null != subChildrenList && subChildrenList.size() > 0){
					childCount--;
					childCount += getChildCount(childrenFactor);
				}
			}
		}
		return childCount;
	}
	
	
	/**
	 * @Description: 初始化评标结果
	 * @author guoyr 
	 */
	private void initEvaluationResult(){
		
		if(null == evaluationFactorList)evaluationFactorList = new ArrayList<EvaluationFactor>();
		initEvaluationStepFactor();
		
		// 记录供应商每个指标的评审结果
		supplierEvaluationFactorResultMap = new HashMap<String, Map<String, EvaluationFactorResult>>();
		// 记录专家对供应商的评审汇总结果
		expertEvaluationResultMap = new HashMap<String, ExpertEvaluationResult>();
		
		// 评审当前评标专家所有的评标结果
		List<EvaluationFactorResult> expertEvaluateFactorResultList = evaluationFactorResultService.getExpertEvaluationFactorResult(tenderProject.getId(), evaluationStep.getId(), curUser.getId(), null);
		List<ExpertEvaluationResult> queryExpertEvaluationResultList = expertEvaluationResultService.getExpertStepAllSupplierEvaluationResultList(tenderProject.getId(), evaluationStep.getId(), curUser.getId());
		contentBackgroundMap.clear();
		contentForegroundMap.clear();

		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;
						break;
					}
				}
			}
			if(null == expertEvaluationResult){
				expertEvaluationResult = new ExpertEvaluationResult();
				expertEvaluationResult.setTenderId(tenderProject.getId());
				expertEvaluationResult.setStepId(evaluationStep.getId());
				expertEvaluationResult.setUserId(curUser.getId());
				expertEvaluationResult.setSupplierId(supplier.getId());
			}
			
			Map<String, EvaluationFactorResult> evaluationFactorResultMap = new HashMap<String, EvaluationFactorResult>();
			BigDecimal totalEvaluationScore = null;
			BigDecimal totalEvaluationFinalScore = null;
			
			for(EvaluationFactor evaluationFactor : evaluationFactorList){
				
				// 获得已评审的结果
				EvaluationFactorResult  evalFactorResult = null;
				if(null != expertEvaluateFactorResultList && expertEvaluateFactorResultList.size() > 0){
					for(EvaluationFactorResult factorResult : expertEvaluateFactorResultList){
						if(ObjectUtils.equals(supplier.getId(), factorResult.getSupplierId()) && ObjectUtils.equals(evaluationFactor.getId(), factorResult.getFactorId())){
							evalFactorResult = factorResult;
							break;
						}
					}
				}
				if(null == evalFactorResult){
					evalFactorResult = new EvaluationFactorResult();
					evalFactorResult.setTenderId(tenderProject.getId());
					evalFactorResult.setStepId(evaluationStep.getId());
					evalFactorResult.setUserId(curUser.getId());
					evalFactorResult.setSupplierId(supplier.getId());
					evalFactorResult.setFactorId(evaluationFactor.getId());
					evalFactorResult.setFactorName(evaluationFactor.getFactorName());
					evalFactorResult.setId(UuidUtils.getUUID());
				}else {
					if(null != evalFactorResult.getEvaluationScore()) {
						boolean errorScore = false;
						
						if(null != fixedFactorMap.get(evaluationFactor.getId())) {
							FixedFactorItemVo fixedFactorItemVo = supplierFactorScoreRangeMap.get(supplier.getId()+evaluationFactor.getId());
							if(null != fixedFactorItemVo && null != fixedFactorItemVo.getFactorMinScore() && null != fixedFactorItemVo.getFactorMaxScore()) {
								BigDecimal minScore = fixedFactorItemVo.getFactorMinScore();
								BigDecimal maxScore = fixedFactorItemVo.getFactorMaxScore();
								if(fixedFactorItemVo.isIncludeFactorMinScore()) {
									if(evalFactorResult.getEvaluationScore().compareTo(minScore) < 0) {
										errorScore = true;
									}
								}else {
									if(evalFactorResult.getEvaluationScore().compareTo(minScore) <= 0) {
										errorScore = true;
									}
								}
								if(fixedFactorItemVo.isIncludeFactorMaxScore()) {
									if(evalFactorResult.getEvaluationScore().compareTo(maxScore) > 0) {
										errorScore = true;
									}
								}else {
									if(evalFactorResult.getEvaluationScore().compareTo(maxScore) >= 0) {
										errorScore = true;
									}
								}
							}else {
								// 这里不能标识为错误，当退回定档步骤时，当前分值会丢失
//								errorScore = true;
							}
						}else {
							if(evalFactorResult.getEvaluationScore().compareTo(evaluationFactor.getFactorMaxScore()) > 0 || evalFactorResult.getEvaluationScore().compareTo(evaluationFactor.getFactorMinScore()) < 0) {
								errorScore = true;
							}
						}
						if(errorScore) {
							evalFactorResult.setEvaluationScore(null);
							evalFactorResult.setEvaluationFinalScore(null);
							resultChanged = true;
						}
					}
				}
				if(null != evalFactorResult.getEvaluationScore()){
					totalEvaluationScore = BigDecimalUtils.add(totalEvaluationScore, evalFactorResult.getEvaluationScore());
				}
				evalFactorResult.setEvaluationFinalScore(getEvaluationFinalScore(evaluationFactor, evalFactorResult.getEvaluationScore()));
				if(null != evalFactorResult.getEvaluationFinalScore()){
					totalEvaluationFinalScore = BigDecimalUtils.add(totalEvaluationFinalScore, evalFactorResult.getEvaluationFinalScore());
				}
				evaluationFactorResultMap.put(evaluationFactor.getId(), evalFactorResult);
				
				if(null == firstEvaluationFactor){
					firstEvaluationFactor = evaluationFactor;
				}
				if(null == firstEvaluationFactorResult){
					firstEvaluationFactorResult = evalFactorResult;
				}
				
			}
			expertEvaluationResult.setAccessment(EvaluationStepEnum.QUALIFIED);
			expertEvaluationResult.setEvaluationScore(BigDecimalUtils.round(totalEvaluationScore, scoreRound));
			expertEvaluationResult.setEvaluationFinalScore(BigDecimalUtils.round(totalEvaluationFinalScore, scoreRound));
			expertEvaluationResultMap.put(supplier.getId(), expertEvaluationResult);
			if(null != totalEvaluationFinalScore){
				sortList.add(totalEvaluationFinalScore.doubleValue());
			}
			if(null == firstSupplier){
				firstSupplier = supplier;
			}
			
			supplierEvaluationFactorResultMap.put(supplier.getId(), evaluationFactorResultMap);
			if(indexIsBidNumber && (null == supplier.getBidNumber() || "".equals(supplier.getBidNumber()))){
				indexIsBidNumber = false;
			}
		}
		reSetSortNo(sortList);
		final Supplier _firstSupplier = firstSupplier;
		final EvaluationFactor _firstEvaluationFactor = firstEvaluationFactor;
		final EvaluationFactorResult _firstEvaluationFactorResult = firstEvaluationFactorResult;
		new Thread(new Runnable(){
			public void run(){
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
				}
				Display.getDefault().syncExec(new Runnable(){
					public void run(){
						if(!evaluation_factor_memo.isDisposed()){
							resultClickEventAction(_firstSupplier, _firstEvaluationFactor, _firstEvaluationFactorResult);
						}
					}
				});
			}
		}).start();
	}
	
	/**
	 * @Description: 设置排名
	 * @author guoyr 
	 */
	private void reSetSortNo(List<Double> sortList){
		if(null == sortList){
			sortList = new ArrayList<Double>();
			for(Entry<String, ExpertEvaluationResult> expertEvaluationResultEntry : expertEvaluationResultMap.entrySet()){
				ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultEntry.getValue();
				if(null != expertEvaluationResult.getEvaluationFinalScore()){
					sortList.add(expertEvaluationResult.getEvaluationFinalScore().doubleValue());
				}
				
			}
		}
		SortUtils sortUtil = new SortUtils(sortList);
		List<Map<String, Object>> supplierSumEvaluationScoreList = null;
		if(null != warningRange){
			supplierSumEvaluationScoreList = expertEvaluationResultService.getSupplierSumEvaluationResultList(tenderProject.getId(), evaluationStep.getId(), curUser.getId());
		}
		for(Entry<String, ExpertEvaluationResult> expertEvaluationResultEntry : expertEvaluationResultMap.entrySet()){
			String supplierId = expertEvaluationResultEntry.getKey();
			ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultEntry.getValue();
			boolean overRange = false;
			
			if(null != supplierSumEvaluationScoreList && null != expertEvaluationResult.getEvaluationScore()){
				int expertCount = 0;
				BigDecimal sumResult = expertEvaluationResult.getEvaluationScore();
				for(Map<String, Object> supplierSumEvaluationScore : supplierSumEvaluationScoreList){
					
					String _supId = (String)supplierSumEvaluationScore.get("SUPPLIER_ID");
					if(null != _supId && _supId.equals(supplierId)){
						try {
							expertCount = Integer.parseInt(supplierSumEvaluationScore.get("COUNT").toString());
						} catch (NumberFormatException e) {
						}finally {
							expertCount += 1;
						}
						sumResult = BigDecimalUtils.add(sumResult, ObjectUtils.getObjBigDecimal(supplierSumEvaluationScore.get("SUM_EVALUATION_SCORE")));
						if(expertCount > 1){
							BigDecimal avgValue = BigDecimalUtils.div(sumResult, new BigDecimal(expertCount));
							BigDecimal percentValue = BigDecimalUtils.sub(expertEvaluationResult.getEvaluationScore(), avgValue) ;//获取差值
							if(avgValue.compareTo(BigDecimal.ZERO) != 0){
								percentValue = BigDecimalUtils.div(percentValue, avgValue).abs();//偏差率绝对值
							}
							if(percentValue.compareTo(warningRange) >= 0){
								overRange = true;
							}
						}
						break;
					}
				}
			}
			contentBackgroundMap.put(supplierId, overRange ? errorBgColor : null);
			
			if(null != expertEvaluationResult.getEvaluationFinalScore()){
				expertEvaluationResultEntry.getValue().setSortNo(sortUtil.getSortNo(expertEvaluationResult.getEvaluationFinalScore().doubleValue()));
			}else {
				expertEvaluationResultEntry.getValue().setSortNo(0);
			}
		}
	}
	/**
	 * @Description: 左边是供应商表格
	 * @author guoyr 
	 */
	private void drawLeftSupplierEvaluationTable(){
		if(null == evaluationFactorList)evaluationFactorList = new ArrayList<EvaluationFactor>();
		if(null != resultKTable && !resultKTable.isDisposed()){
			resultKTable.dispose();
		}
		
		resultKTable = new KTable(evaluationTableComposite, SWTX.AUTO_SCROLL | SWT.FULL_SELECTION | SWTX.EDIT_ON_KEY);
		commKTableModel = new CommonKtableCModel(resultKTable, maxLevel, 1, supplierList.size() , evaluationFactorList.size() + 2 + (null != evaluationStepWeight ? 1 : 0));
		commKTableModel.addRowSpan(0, 0, commKTableModel.getFixedRowCount());
		
		commKTableModel.setColumnMinWidth(0, 100);
		
		// 排名列
		final int sortCol = commKTableModel.getColCount() -1;
		commKTableModel.addRowSpan(0, sortCol, commKTableModel.getFixedRowCount());
		commKTableModel.setColumnWidth(sortCol, 40);
		commKTableModel.setColumnMaxWidth(sortCol, 80);
		// 最终得分列
		final int finalScoreSumCol= commKTableModel.getColCount() -2;
		commKTableModel.setColumnWidth(finalScoreSumCol, 40);
		commKTableModel.setColumnMaxWidth(finalScoreSumCol, 80);
		commKTableModel.addRowSpan(0, finalScoreSumCol, commKTableModel.getFixedRowCount());
		// 得分汇总列
		final int scoreSumCol = null != evaluationStepWeight?commKTableModel.getColCount() -3 : -1;
		if(scoreSumCol > 0){
			commKTableModel.addRowSpan(0, scoreSumCol, commKTableModel.getFixedRowCount());
			commKTableModel.setColumnWidth(scoreSumCol, 40);
			commKTableModel.setColumnMaxWidth(scoreSumCol, 80);
		}
		final Map<String, String> tableTitleMap = new HashMap<String, String>();
		
		for(int row = 0; row < maxLevel; row++){
			int childCount = 0;
			String parentId = "";
			int col = commKTableModel.getFixedColumnCount();
			tableTitleMap.put(row+"_0", MessageFormat.format(Messages.Supplier, Messages.Name));
			tableTitleMap.put(row+"_"+sortCol, Messages.SortNo);
			if(scoreSumCol > 0){
				tableTitleMap.put(row+"_"+scoreSumCol, Messages.TotalScore);
				tableTitleMap.put(row+"_"+finalScoreSumCol, Messages.FinalScore);
			}else {
				tableTitleMap.put(row+"_"+finalScoreSumCol, Messages.FinalScore);
			}
			for(EvaluationFactor evaluationFactor : evaluationFactorList){
				if(col < commKTableModel.getFixedColumnCount()){
				}else {
					EvaluationFactor parentFactor = getParentFactor(evaluationFactor, row + 1 );
					if(null != parentFactor && StringUtils.isNotEmpty(parentFactor.getId())){
						if(!parentId.equals(parentFactor.getId())){
							childCount = 0;
						}
						if(childCount++ == 0){
							commKTableModel.addColSpan(row, col, getChildCount(parentFactor));
						}
						tableTitleMap.put(row+"_"+col, maxFactorNameLength > 0 ? parentFactor.getFactorName().substring(0, Math.min(maxFactorNameLength, parentFactor.getFactorName().length())) : parentFactor.getFactorName());
						parentId = parentFactor.getId();
					}else {
						childCount = 0;
						tableTitleMap.put(row+"_"+col, maxFactorNameLength > 0 ? evaluationFactor.getFactorName().substring(0, Math.min(maxFactorNameLength, evaluationFactor.getFactorName().length())) : evaluationFactor.getFactorName());
					}
					if(row + 1 == evaluationFactor.getLevel()){
						commKTableModel.addRowSpan(row, col, commKTableModel.getFixedRowCount() - row);
					}
					commKTableModel.setColumnWidth(col, 60);
					commKTableModel.setColumnMinWidth(col, 80);
				}
				col ++;
			}
		}
	
		// 设置单元格内容
		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()){
						if(resultKTable.isLockedRow(row)){
							return (indexIsBidNumber ? supplier.getBidNumber() : (row - commKTableModel.getFixedRowCount() +1))+"、"+supplier.getSupplierName() + "[已锁定]";
						}else {
							return (indexIsBidNumber ? supplier.getBidNumber() : (row - commKTableModel.getFixedRowCount() +1))+"、"+supplier.getSupplierName();
						}
					}else if(col < commKTableModel.getFixedColumnCount() + evaluationFactorList.size()){
						EvaluationFactor evaluationFactor = evaluationFactorList.get(col - commKTableModel.getFixedColumnCount());
						EvaluationFactorResult evalFactorResult = supplierEvaluationFactorResultMap.get(supplier.getId()).get(evaluationFactor.getId());
						if(null != evalFactorResult && null != evalFactorResult.getEvaluationScore()){
							if(null != emptyScoreContent && evalFactorResult.getEvaluationScore().compareTo(BigDecimal.ZERO) == 0){
								return emptyScoreContent;
							}
							return NumberFormatUtils.format(evalFactorResult.getEvaluationScore(), expertScoreRound);
						}
					}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 FixedWrapCellRenderer fixedWrapCellRenderer = new FixedWrapCellRenderer();
		final TextWrapCellRenderer textWrapCellRenderer = new TextWrapCellRenderer();
		CellRender cellRender = new CellRender(){
			public Object receiveCellRender(int row, int col) {
				if(commKTableModel.isFixedCell(col, row)){
					fixedWrapCellRenderer.setFont(SWTResourceManager.getFont("微软雅黑", fontSize, SWT.NORMAL));
					return fixedWrapCellRenderer;
				}else{
					textWrapCellRenderer.setFont(SWTResourceManager.getFont("微软雅黑", fontSize + 1, SWT.NORMAL));
					return textWrapCellRenderer;
				}
		}};
		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() + evaluationFactorList.size()){
					EvaluationFactor evaluationFactor = evaluationFactorList.get(col - commKTableModel.getFixedColumnCount());
					if(null != evaluationFactor && StringUtils.equals(EvaluationFactorEnum.SCORE_TYPE_OBJECTIVE, evaluationFactor.getScoreType())){
						List<ScoreItem> itemList = SerializeUtils.deserializeObject(evaluationFactor.getObjectiveItem());
						if(null != itemList && itemList.size() > 0){
							KTableCellEditorGCombo combo = new KTableCellEditorGCombo();
							combo.setSingleClickEdit(true);
							for(ScoreItem item : itemList){
								BigDecimal score = item.getScore();
								String name = item.getName();
								combo.addItem(score+"分"+(StringUtils.isNotEmpty(name) ? " "+name : ""), score);
							}
							return combo;
						}
					}
					return text;
				}
				return null;
			}};
		commKTableModel.setCellEditorRender(cellEditorRender);
		
		// 编辑单元格内容
		CellEditContentRender editContentRender = new CellEditContentRender(){
			public void receiveContent(int row, int col, Object content) {
				if(col >= commKTableModel.getFixedColumnCount() && col < commKTableModel.getFixedColumnCount() + evaluationFactorList.size() && row >= commKTableModel.getFixedRowCount() && row < commKTableModel.getFixedRowCount() + supplierList.size()){
					BigDecimal evaluationScore = null;
					if(null != content && !"".equals(content.toString())){
						if(null != emptyScoreContent && content.toString().equals(emptyScoreContent)){
							content = "0";
						}
						evaluationScore = ObjectUtils.getObjBigDecimal(content);
						if(null != evaluationScore){
							evaluationScore = BigDecimalUtils.round(evaluationScore, expertScoreRound);
						}
					}
					Supplier supplier = supplierList.get(row - commKTableModel.getFixedRowCount());
					EvaluationFactor evaluationFactor = evaluationFactorList.get(col - commKTableModel.getFixedColumnCount());
					ExpertEvaluationResult expertEvaluationResult  = expertEvaluationResultMap.get(supplier.getId());
					Map<String, EvaluationFactorResult> supplierFactorResultMap = supplierEvaluationFactorResultMap.get(supplier.getId());
					EvaluationFactorResult evalFactorResult = supplierFactorResultMap.get(evaluationFactor.getId());
					if(null != evaluationScore){
						String errorMsg = null;
						
						// 如果当前指标是定档指标
						if(null != fixedFactorMap.get(evaluationFactor.getId())) {
							FixedFactorItemVo fixedFactorItemVo = supplierFactorScoreRangeMap.get(supplier.getId()+evaluationFactor.getId());
							if(null != fixedFactorItemVo && null != fixedFactorItemVo.getFactorMinScore() && null != fixedFactorItemVo.getFactorMaxScore()) {
								boolean errorScore = false;
								BigDecimal minScore = fixedFactorItemVo.getFactorMinScore();
								BigDecimal maxScore = fixedFactorItemVo.getFactorMaxScore();
								if(fixedFactorItemVo.isIncludeFactorMinScore()) {
									if(evaluationScore.compareTo(minScore) < 0) {
										errorScore = true;
									}
								}else {
									if(evaluationScore.compareTo(minScore) <= 0) {
										errorScore = true;
									}
								}
								if(fixedFactorItemVo.isIncludeFactorMaxScore()) {
									if(evaluationScore.compareTo(maxScore) > 0) {
										errorScore = true;
									}
								}else {
									if(evaluationScore.compareTo(maxScore) >= 0) {
										errorScore = true;
									}
								}
								if(errorScore){
									errorMsg = MessageFormat.format(Messages.Tips_entryScroeRange, (fixedFactorItemVo.isIncludeFactorMinScore() ? "≥" : ">") +NumberFormatUtils.format(minScore), (fixedFactorItemVo.isIncludeFactorMaxScore() ? "≤" : "<") + NumberFormatUtils.format(maxScore));
								}
							}else {
								errorMsg = "当前评审项需要定档后才能评审，请确认完成定档后刷新重试！";
							}
						}else {
							if(evaluationScore.compareTo(evaluationFactor.getFactorMinScore()) < 0 || evaluationScore.compareTo(evaluationFactor.getFactorMaxScore()) > 0){
								errorMsg = MessageFormat.format(Messages.Tips_entryScroeRange, evaluationFactor.getFactorMinScore(), evaluationFactor.getFactorMaxScore());
							}
						}
						if(null != errorMsg) {
							if(!isShowError){
								isShowError = true;
								MessageDialog.openError(getShell(), errorMsg);
								isShowError = false;
							}
							return;
						}
					}					
					evalFactorResult.setEvaluationScore(evaluationScore);
					evalFactorResult.setEvaluationFinalScore(getEvaluationFinalScore(evaluationFactor, evalFactorResult.getEvaluationScore()));
					resultChanged = true;
					contentForegroundMap.put(evalFactorResult.getId(), null);
					BigDecimal totalEvaluationScore = null;
					BigDecimal totalEvaluationFinalScore = null;
					for(Entry<String, EvaluationFactorResult> factorResultEntry : supplierFactorResultMap.entrySet()){
						EvaluationFactorResult evaluationFactorResult = factorResultEntry.getValue();
						if(null != evaluationFactorResult.getEvaluationScore()){
							totalEvaluationScore = BigDecimalUtils.add(totalEvaluationScore, evaluationFactorResult.getEvaluationScore());
						}
						if(null != evaluationFactorResult.getEvaluationFinalScore()){
							totalEvaluationFinalScore = BigDecimalUtils.add(totalEvaluationFinalScore, evaluationFactorResult.getEvaluationFinalScore());
						}
					}
					expertEvaluationResult.setEvaluationScore(BigDecimalUtils.round(totalEvaluationScore, scoreRound));
					expertEvaluationResult.setEvaluationFinalScore(BigDecimalUtils.round(totalEvaluationFinalScore, scoreRound));
					if(EvaluationFactorEnum.OBJECTIVE_YES.equals(evaluationFactor.getObjective())){
						refreshObjectiveBackgroundMap();
					}
					reSetSortNo(null);
					resultKTable.redraw();
				}
			}
		};
		commKTableModel.setCellEditContentRender(editContentRender);
		
		// 编辑单元格对齐方式
		CellAlignmentRender cellAlignmentRender = new CellAlignmentRender(){
			public int receiveCellAlignment(int row, int col) {
				if(col < commKTableModel.getFixedColumnCount() && row >= commKTableModel.getFixedRowCount()){
					if(row < commKTableModel.getFixedRowCount() + supplierList.size()){
						return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_LEFT;
					}else {
						return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
					}
				}
				return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
			}
		};
		commKTableModel.setCellAlignmentRender(cellAlignmentRender);
		
		// 设置单元格前景颜色
		CellForegroundRender cellForegroundRender = new CellForegroundRender(){
			public Color receiveCellForeground(int row, int col) {
				if(col >= commKTableModel.getFixedColumnCount() && col < commKTableModel.getFixedColumnCount() + evaluationFactorList.size() && row >= commKTableModel.getFixedRowCount() && row < commKTableModel.getFixedRowCount() + supplierList.size()){
					Supplier supplier = supplierList.get(row - commKTableModel.getFixedRowCount());
//					ExpertEvaluationResult expertEvaluationResult  = expertEvaluationResultMap.get(supplier.getId());
					EvaluationFactor evaluationFactor = evaluationFactorList.get(col - commKTableModel.getFixedColumnCount());
					EvaluationFactorResult  evalFactorResult = supplierEvaluationFactorResultMap.get(supplier.getId()).get(evaluationFactor.getId());
					Color foreColor = contentForegroundMap.get(evalFactorResult.getId());
					return foreColor;
				
				}
				return null;
		}};
		commKTableModel.setCellForegroundRender(cellForegroundRender);
		
		// 设置单元格背景颜色
		CellBackgroundRender cellBackgroundRender = new CellBackgroundRender(){
			public Color receiveCellBackground(int row, int col) {
				if(col >= commKTableModel.getFixedColumnCount() && row >= commKTableModel.getFixedRowCount() && row < commKTableModel.getFixedRowCount() + supplierList.size()){
					Supplier supplier = supplierList.get(row - commKTableModel.getFixedRowCount());
					if(col < commKTableModel.getFixedColumnCount() + evaluationFactorList.size()){
						EvaluationFactor evaluationFactor = evaluationFactorList.get(col - commKTableModel.getFixedColumnCount());
						if(EvaluationFactorEnum.OBJECTIVE_YES.equals(evaluationFactor.getObjective())){
							Color bgColor = objectiveBackgroundMap.get(new Keys(supplier.getId(), evaluationFactor.getId()));
							if (null != bgColor) {
								return bgColor;
							}
						}
					}else if(col == scoreSumCol || col == finalScoreSumCol){
						Color bgColor = contentBackgroundMap.get(supplier.getId());
						return bgColor;
					}
					
				}
				return null;
			}
		};
		commKTableModel.setCellBackgroundRender(cellBackgroundRender);
		// 设置单元格内容
		CellTooltipRender cellTooltipRender = new CellTooltipRender(){
			public String receiveCellTooltip(int row, int col) {
				Supplier supplier = null;
				EvaluationFactor evaluationFactor = null;
				if(row >= commKTableModel.getFixedRowCount()){
					supplier = supplierList.get(row - commKTableModel.getFixedRowCount());
				}
				if(col >= commKTableModel.getFixedColumnCount() && col < commKTableModel.getFixedColumnCount() + evaluationFactorList.size()){
					evaluationFactor = evaluationFactorList.get(col - commKTableModel.getFixedColumnCount());
					if(null != supplier){
						EvaluationFactorResult  evalFactorResult = supplierEvaluationFactorResultMap.get(supplier.getId()).get(evaluationFactor.getId());
						if(null != evalFactorResult && null != evalFactorResult.getEvaluationOpinion() && evalFactorResult.getEvaluationOpinion().length() > 0){
							return evalFactorResult.getEvaluationOpinion();
						}
					}
					if(null != evaluationFactor && null != evaluationFactor.getMemo() && evaluationFactor.getMemo().length() > 0){
						return Messages.EvalExplain + "：\r\n"+evaluationFactor.getMemo();
					}
				}
				
				return Messages.Tips_selectMoreActions;
			}
		};	
		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;
				Supplier supplier = null;
				EvaluationFactor evaluationFactor = null;
				EvaluationFactorResult  evalFactorResult = null;
//				ExpertEvaluationResult expertEvaluationResult = null;
				
				if(row >= commKTableModel.getFixedRowCount()){
					supplier = supplierList.get(row - commKTableModel.getFixedRowCount());
//					expertEvaluationResult  = expertEvaluationResultMap.get(supplier.getId());
				}
				if(col >= commKTableModel.getFixedColumnCount() && col < commKTableModel.getFixedColumnCount() + evaluationFactorList.size()){
					evaluationFactor = evaluationFactorList.get(col - commKTableModel.getFixedColumnCount());
				}
				if(null != supplier && null != evaluationFactor){
					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);
				if(col >= commKTableModel.getFixedColumnCount() && col < commKTableModel.getFixedColumnCount() + evaluationFactorList.size()){
					EvaluationFactor evaluationFactor = evaluationFactorList.get(col - commKTableModel.getFixedColumnCount());
					resultClickEventAction(null, evaluationFactor, null);
				}
			}
		});
		
		resultKTable.addCellMouseRightListener(new KTableCellMouseRightAdapter(){
		
			public void cellMouseRightClick(int col, int row, Rectangle rectangle) {
				curCol = col; 
				curRow = row;
				Supplier supplier = null;
				EvaluationFactor evaluationFactor = null;
				EvaluationFactorResult  evalFactorResult = null;
				ExpertEvaluationResult expertEvaluationResult = null;
				if(row >= commKTableModel.getFixedRowCount()){
					supplier = supplierList.get(row - commKTableModel.getFixedRowCount());
					expertEvaluationResult  = expertEvaluationResultMap.get(supplier.getId());
				}
				if(col >= commKTableModel.getFixedColumnCount() && col < commKTableModel.getFixedColumnCount() + evaluationFactorList.size()){
					evaluationFactor = evaluationFactorList.get(col - commKTableModel.getFixedColumnCount());
				}
				if(null != supplier && null != evaluationFactor){
					evalFactorResult = supplierEvaluationFactorResultMap.get(supplier.getId()).get(evaluationFactor.getId());
				}
				resultMouseRightClickEventAction(supplier, evaluationFactor, expertEvaluationResult, evalFactorResult);
			}
		});
		resultKTable.setModel(commKTableModel);
		evaluationTableComposite.layout();
	}
	
	/**
	 * @Description: 左边是评审指标表格
	 * @author guoyr 
	 */
	private void drawLeftFactorEvaluationTable(){
		
		if(null != resultKTable && !resultKTable.isDisposed()){
			resultKTable.dispose();
		}
		resultKTable = new KTable(evaluationTableComposite, SWTX.AUTO_SCROLL | SWT.FULL_SELECTION | SWTX.EDIT_ON_KEY);
		commKTableModel = new CommonKtableCModel(resultKTable, 1, maxLevel, evaluationFactorList.size() + 2 + (null != evaluationStepWeight? 1 : 0), supplierList.size());
		final Map<String, String> tableTitleMap = new HashMap<String, String>();
		tableTitleMap.put("0_0", Messages.EvalFactor);
		commKTableModel.addColSpan(0, 0, commKTableModel.getFixedColumnCount());
		
		int col = commKTableModel.getFixedColumnCount();
		for(Supplier supplier : supplierList){
			tableTitleMap.put("0_"+col, supplier.getSupplierName());
			commKTableModel.setColumnWidth(col, 110);
			col ++;
		}
		// 排名列
		final int sortRow = commKTableModel.getRowCount() -1;
		commKTableModel.addColSpan(sortRow, 0, commKTableModel.getFixedColumnCount());
		tableTitleMap.put(sortRow +"_0", Messages.SortNo);
		// 最终得分列
		final int finalScoreSumRow= commKTableModel.getRowCount() -2;
		tableTitleMap.put(finalScoreSumRow +"_0", Messages.EvalFactor);
		commKTableModel.addColSpan(finalScoreSumRow, 0, commKTableModel.getFixedColumnCount());
		// 得分汇总列
		final int scoreSumRow = null != evaluationStepWeight?commKTableModel.getRowCount() -3 : -1;
		if(scoreSumRow > 0){
			commKTableModel.addColSpan(scoreSumRow, 0, commKTableModel.getFixedColumnCount());
			tableTitleMap.put(scoreSumRow+"_0", Messages.TotalScore);
			tableTitleMap.put(finalScoreSumRow+"_0", Messages.FinalScore);
		}else {
			tableTitleMap.put(finalScoreSumRow+"_0", Messages.FinalScore);
		}
		String parentId = "";
		int childCount = 0;
		for(col = 0; col < maxLevel; col++){
			commKTableModel.addRowSpan(0, col, commKTableModel.getFixedRowCount());
			if(col == maxLevel -1){
				// 将指标的最后一列设置宽一点
				commKTableModel.setColumnMaxWidth(col, 250);
			}else {
				commKTableModel.setColumnMaxWidth(col, 200);
			}
			int row = commKTableModel.getFixedRowCount();
			for(EvaluationFactor evaluationFactor : evaluationFactorList){
				EvaluationFactor parentFactor = getParentFactor(evaluationFactor, col + 1 );
				if(null != parentFactor && StringUtils.isNotEmpty(parentFactor.getId())){
					if(!parentId.equals(parentFactor.getId())){
						childCount = 0;
					}
					if(childCount++ == 0 ){
						commKTableModel.addRowSpan(row, col, getChildCount(parentFactor));
					}
					parentId = parentFactor.getId();
					tableTitleMap.put(row+"_"+col, maxFactorNameLength > 0 ? parentFactor.getFactorName().substring(0, Math.min(maxFactorNameLength, parentFactor.getFactorName().length())) : parentFactor.getFactorName());
				}else {
					childCount = 0;
					tableTitleMap.put(row+"_"+col, maxFactorNameLength > 0 ? evaluationFactor.getFactorName().substring(0, Math.min(maxFactorNameLength, evaluationFactor.getFactorName().length())) : evaluationFactor.getFactorName());
				}
				if(col + 1 == evaluationFactor.getLevel()){
					commKTableModel.addColSpan(row, col, commKTableModel.getFixedColumnCount() - col);
				}
//				if(null != parentFactor){
//					tableTitleMap.put(row+"_"+col, maxFactorNameLength > 0 ? parentFactor.getFactorName().substring(0, Math.min(maxFactorNameLength, parentFactor.getFactorName().length())) : parentFactor.getFactorName());
//				}
				if(col < commKTableModel.getFixedColumnCount()){
					if(null != evaluationFactor){
						tableTitleMap.put(row+"_"+(maxLevel -1), maxFactorNameLength > 0 ? evaluationFactor.getFactorName().substring(0, Math.min(maxFactorNameLength, evaluationFactor.getFactorName().length())) : evaluationFactor.getFactorName());
					}
				}
				row ++;
			}
		}

		// 设置单元格内容
		CellContentRender contentRender = new CellContentRender(){
			public Object receiveContent(int row, int col) {
				// 表头
				if(col < commKTableModel.getFixedColumnCount()){
					if(row < commKTableModel.getFixedRowCount()){
						return Messages.EvalFactor;
					}else if(row < commKTableModel.getFixedRowCount() + evaluationFactorList.size() && col == commKTableModel.getFixedColumnCount() -1){
						EvaluationFactor evaluationFactor = evaluationFactorList.get(row - commKTableModel.getFixedRowCount());
						if(col == commKTableModel.getFixedColumnCount() -1 && resultKTable.isLockedRow(row)){
							return (maxFactorNameLength > 0 ? evaluationFactor.getFactorName().substring(0, Math.min(maxFactorNameLength, evaluationFactor.getFactorName().length())) : evaluationFactor.getFactorName())+"\r\n[已锁定]";
						}else {
							return maxFactorNameLength > 0 ? evaluationFactor.getFactorName().substring(0, Math.min(maxFactorNameLength, evaluationFactor.getFactorName().length())) : evaluationFactor.getFactorName();
						}
					}else if(null != tableTitleMap.get(row+"_"+col)){
						return tableTitleMap.get(row+"_"+col);
					}
					return "";
				}else if(col < commKTableModel.getFixedColumnCount() + supplierList.size()){
					Supplier supplier = supplierList.get(col - commKTableModel.getFixedColumnCount());
					if(row < commKTableModel.getFixedRowCount()){
						if(indexIsBidNumber){
							return supplier.getBidNumber() + "、"+supplier.getSupplierName();
						}else {
							return (col - commKTableModel.getFixedColumnCount() +1)+"、"+supplier.getSupplierName();
						}
					}else if(row < commKTableModel.getFixedRowCount() + evaluationFactorList.size()){
						EvaluationFactor evaluationFactor = evaluationFactorList.get(row - commKTableModel.getFixedRowCount());
						EvaluationFactorResult evalFactorResult = supplierEvaluationFactorResultMap.get(supplier.getId()).get(evaluationFactor.getId());
						if(null != evalFactorResult && null != evalFactorResult.getEvaluationScore()){
							if(null != emptyScoreContent && evalFactorResult.getEvaluationScore().compareTo(BigDecimal.ZERO) == 0){
								return emptyScoreContent;
							}
							return NumberFormatUtils.format(evalFactorResult.getEvaluationScore(), expertScoreRound);
						}
					}else {
						ExpertEvaluationResult expertEvaluationResult  = expertEvaluationResultMap.get(supplier.getId());
						if(null != expertEvaluationResult){
							if(row == scoreSumRow){
								if(null != expertEvaluationResult.getEvaluationScore()){
									return NumberFormatUtils.format(expertEvaluationResult.getEvaluationScore(), scoreRound);
								}
							}else if(row == finalScoreSumRow){
								if(null != expertEvaluationResult.getEvaluationFinalScore()){
									return NumberFormatUtils.format(expertEvaluationResult.getEvaluationFinalScore(), scoreRound);
								}
							}else if(row == sortRow){
								if(expertEvaluationResult.getSortNoIntValue() > 0){
									return expertEvaluationResult.getSortNoIntValue();
								}
							}
						}
					}
				}
				return "";
			}
		};
		commKTableModel.setCellContentRender(contentRender);
		final FixedWrapCellRenderer fixedWrapCellRenderer = new FixedWrapCellRenderer();
		final TextWrapCellRenderer textWrapCellRenderer = new TextWrapCellRenderer();
		// 设置单元格渲染器
		CellRender cellRender = new CellRender(){
			public Object receiveCellRender(int row, int col) {
				if(commKTableModel.isFixedCell(col, row)){
					fixedWrapCellRenderer.setFont(SWTResourceManager.getFont("微软雅黑", fontSize, SWT.NORMAL));
					return fixedWrapCellRenderer;
				}else{
					textWrapCellRenderer.setFont(SWTResourceManager.getFont("微软雅黑", fontSize + 1, SWT.NORMAL));
					return textWrapCellRenderer;
				}
			}
		};
		commKTableModel.setCellRender(cellRender);
		
		// 设置单元格编辑器
        final KTableCellEditorText text = new KTableCellEditorText();
		text.setSingleClickEdit(true);
		CellEditorRender cellEditorRender = new CellEditorRender(){
			public Object receiveCellEditor(int row, int col) {
				if(col >= commKTableModel.getFixedColumnCount() && col < commKTableModel.getFixedColumnCount() + supplierList.size() && row >= commKTableModel.getFixedRowCount() && row < commKTableModel.getFixedRowCount() + evaluationFactorList.size()){
					EvaluationFactor evaluationFactor = evaluationFactorList.get(row - commKTableModel.getFixedRowCount());
					if(null != evaluationFactor && StringUtils.equals(EvaluationFactorEnum.SCORE_TYPE_OBJECTIVE, evaluationFactor.getScoreType())){
						List<ScoreItem> itemList = SerializeUtils.deserializeObject(evaluationFactor.getObjectiveItem());
						if(null != itemList && itemList.size() > 0){
							KTableCellEditorGCombo combo = new KTableCellEditorGCombo();
							combo.setSingleClickEdit(true);
							for(ScoreItem item : itemList){
								BigDecimal score = item.getScore();
								String name = item.getName();
								combo.addItem(score+"分"+(StringUtils.isNotEmpty(name) ? " "+name : ""), score);
							}
							return combo;
						}
					}
					return text;
				}
				return null;
			}
		};
		commKTableModel.setCellEditorRender(cellEditorRender);
		
		// 编辑单元格内容
		CellEditContentRender editContentRender = new CellEditContentRender(){
			public void receiveContent(int row, int col, Object content) {
				if(col >= commKTableModel.getFixedColumnCount() && row >= commKTableModel.getFixedRowCount() && row < commKTableModel.getFixedRowCount() + evaluationFactorList.size()){
					if(!finishEdit) {
						return;
					}
					finishEdit = false;
					BigDecimal evaluationScore = null;
					if(null != content && !"".equals(content.toString())){
						if(null != emptyScoreContent && content.toString().equals(emptyScoreContent)){
							content = "0";
						}
						evaluationScore = ObjectUtils.getObjBigDecimal(content);
						if(null != evaluationScore){
							evaluationScore = BigDecimalUtils.round(evaluationScore, expertScoreRound);
						}
					}
					Supplier supplier = supplierList.get(col - commKTableModel.getFixedColumnCount());
					EvaluationFactor evaluationFactor = evaluationFactorList.get(row - commKTableModel.getFixedRowCount());
					ExpertEvaluationResult expertEvaluationResult  = expertEvaluationResultMap.get(supplier.getId());
					Map<String, EvaluationFactorResult> supplierFactorResultMap = supplierEvaluationFactorResultMap.get(supplier.getId());
					EvaluationFactorResult evalFactorResult = supplierFactorResultMap.get(evaluationFactor.getId());
					if(null != evaluationScore){
						String errorMsg = null;
						// 如果当前指标是定档指标
						if(null != fixedFactorMap.get(evaluationFactor.getId())) {
							FixedFactorItemVo fixedFactorItemVo = supplierFactorScoreRangeMap.get(supplier.getId()+evaluationFactor.getId());
							if(null != fixedFactorItemVo && null != fixedFactorItemVo.getFactorMinScore() && null != fixedFactorItemVo.getFactorMaxScore()) {
								boolean errorScore = false;
								BigDecimal minScore = fixedFactorItemVo.getFactorMinScore();
								BigDecimal maxScore = fixedFactorItemVo.getFactorMaxScore();
								if(fixedFactorItemVo.isIncludeFactorMinScore()) {
									if(evaluationScore.compareTo(minScore) < 0) {
										errorScore = true;
									}
								}else {
									if(evaluationScore.compareTo(minScore) <= 0) {
										errorScore = true;
									}
								}
								if(fixedFactorItemVo.isIncludeFactorMaxScore()) {
									if(evaluationScore.compareTo(maxScore) > 0) {
										errorScore = true;
									}
								}else {
									if(evaluationScore.compareTo(maxScore) >= 0) {
										errorScore = true;
									}
								}
								if(errorScore){
									errorMsg = MessageFormat.format(Messages.Tips_entryScroeRange, (fixedFactorItemVo.isIncludeFactorMinScore() ? "≥" : ">") +NumberFormatUtils.format(minScore), (fixedFactorItemVo.isIncludeFactorMaxScore() ? "≤" : "<") + NumberFormatUtils.format(maxScore));
								}
							}else {
								errorMsg = "当前评审项需要定档后才能评审，请确认完成定档后刷新重试！";
							}
						}else {
							if(evaluationScore.compareTo(evaluationFactor.getFactorMinScore()) < 0 || evaluationScore.compareTo(evaluationFactor.getFactorMaxScore()) > 0){
								errorMsg = MessageFormat.format(Messages.Tips_entryScroeRange, evaluationFactor.getFactorMinScore(), evaluationFactor.getFactorMaxScore());
							}
						}
						if(null != errorMsg) {
							if(!isShowError){
								isShowError = true;
								MessageDialog.openError(getShell(), errorMsg);
								isShowError = false;
							}
							finishEdit = true;
							return;
						}
					}					
					evalFactorResult.setEvaluationScore(evaluationScore);
					evalFactorResult.setEvaluationFinalScore(getEvaluationFinalScore(evaluationFactor, evaluationScore));
					// 如果是客观项，检查所有评标专家的评标结果是否相同
					contentForegroundMap.put(evalFactorResult.getId(), null);
					resultChanged = true;
					BigDecimal totalEvaluationScore = null;
					BigDecimal totalEvaluationFinalScore = null;
					for(Entry<String, EvaluationFactorResult> factorResultEntry : supplierFactorResultMap.entrySet()){
						EvaluationFactorResult evaluationFactorResult = factorResultEntry.getValue();
						if(null != evaluationFactorResult.getEvaluationScore()){
							totalEvaluationScore = BigDecimalUtils.add(totalEvaluationScore, evaluationFactorResult.getEvaluationScore());
						}
						if(null != evaluationFactorResult.getEvaluationFinalScore()){
							totalEvaluationFinalScore = BigDecimalUtils.add(totalEvaluationFinalScore, evaluationFactorResult.getEvaluationFinalScore());
						}
					}
					expertEvaluationResult.setEvaluationScore(BigDecimalUtils.round(totalEvaluationScore, scoreRound));
					expertEvaluationResult.setEvaluationFinalScore(BigDecimalUtils.round(totalEvaluationFinalScore, scoreRound));
					if(EvaluationFactorEnum.OBJECTIVE_YES.equals(evaluationFactor.getObjective())){
						refreshObjectiveBackgroundMap();
					}
					reSetSortNo(null);
					resultKTable.redraw();
					finishEdit = true;
				}
			}
		};
		commKTableModel.setCellEditContentRender(editContentRender);
		
		// 编辑单元格对齐方式
		CellAlignmentRender cellAlignmentRender = new CellAlignmentRender(){
			public int receiveCellAlignment(int row, int col) {
				if(col < commKTableModel.getFixedColumnCount() && row >= commKTableModel.getFixedRowCount()){
					if(row < commKTableModel.getFixedRowCount() + evaluationFactorList.size()){
						return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_LEFT;
					}else {
						return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
					}
				}
				return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
			}
		};
		commKTableModel.setCellAlignmentRender(cellAlignmentRender);
		
		// 设置单元格前景颜色
		CellForegroundRender cellForegroundRender = new CellForegroundRender(){
			public Color receiveCellForeground(int row, int col) {
				if(col >= commKTableModel.getFixedColumnCount() && row >= commKTableModel.getFixedRowCount() && row < commKTableModel.getFixedRowCount() + evaluationFactorList.size()){
					Supplier supplier = supplierList.get(col - commKTableModel.getFixedColumnCount());
					EvaluationFactor evaluationFactor = evaluationFactorList.get(row - commKTableModel.getFixedRowCount());
					EvaluationFactorResult evalFactorResult = supplierEvaluationFactorResultMap.get(supplier.getId()).get(evaluationFactor.getId());
					Color foreColor = contentForegroundMap.get(evalFactorResult.getId());
					return foreColor;
				}
				return null;
			}
		};
		commKTableModel.setCellForegroundRender(cellForegroundRender);
		
		// 设置单元格背景颜色
		CellBackgroundRender cellBackgroundRender = new CellBackgroundRender(){
			public Color receiveCellBackground(int row, int col) {
				if(col >= commKTableModel.getFixedColumnCount() && row >= commKTableModel.getFixedRowCount()){
					Supplier supplier = supplierList.get(col - commKTableModel.getFixedColumnCount());
					if(row < commKTableModel.getFixedRowCount() + evaluationFactorList.size()){
						EvaluationFactor evaluationFactor = evaluationFactorList.get(row - commKTableModel.getFixedRowCount());
						if(EvaluationFactorEnum.OBJECTIVE_YES.equals(evaluationFactor.getObjective())){
							Color bgColor = objectiveBackgroundMap.get(new Keys(supplier.getId(), evaluationFactor.getId()));
							if (null != bgColor) {
								return bgColor;
							}
						}
					}else if(row == scoreSumRow || row == finalScoreSumRow){
						return contentBackgroundMap.get(supplier.getId());
					}
				}
				return null;
			}
		};
		commKTableModel.setCellBackgroundRender(cellBackgroundRender);
		
		// 设置单元格内容
		CellTooltipRender cellTooltipRender = new CellTooltipRender(){
			public String receiveCellTooltip(int row, int col) {
				Supplier supplier = null;
				EvaluationFactor evaluationFactor = null;
				if(col >= commKTableModel.getFixedColumnCount()){
					supplier = supplierList.get(col - commKTableModel.getFixedColumnCount());
				} 
				if(row >= commKTableModel.getFixedRowCount() && row < commKTableModel.getFixedRowCount() + evaluationFactorList.size()){
					evaluationFactor = evaluationFactorList.get(row - commKTableModel.getFixedRowCount());
					if(null != supplier){
						EvaluationFactorResult evalFactorResult = supplierEvaluationFactorResultMap.get(supplier.getId()).get(evaluationFactor.getId());
						if(null != evalFactorResult && null != evalFactorResult.getEvaluationOpinion() && evalFactorResult.getEvaluationOpinion().length() > 0){
							return evalFactorResult.getEvaluationOpinion();
						}
					}
					if(null != evaluationFactor && null != evaluationFactor.getMemo() && evaluationFactor.getMemo().length() > 0){
						return Messages.EvalExplain + "：\r\n"+evaluationFactor.getMemo();
					}
				}
				
				return Messages.Tips_selectMoreActions;
			}
		};	
		commKTableModel.setCellTooltipRender(cellTooltipRender);
		
		resultKTable.addCellSelectionListener(new KTableCellSelectionAdapter(){
			@Override
			public void cellSelected(int col, int row, int statemask) {
				Supplier supplier = null;
				EvaluationFactor evaluationFactor = null;	
				EvaluationFactorResult  evalFactorResult = null;
//				ExpertEvaluationResult expertEvaluationResult = null;
				if(col >= commKTableModel.getFixedColumnCount()){
					supplier = supplierList.get(col - commKTableModel.getFixedColumnCount());
				}
				if(row >= commKTableModel.getFixedRowCount() && row < commKTableModel.getFixedRowCount() + evaluationFactorList.size()){
					evaluationFactor = evaluationFactorList.get(row - commKTableModel.getFixedRowCount());
				}
				if(null != supplier && null != evaluationFactor){
					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);
				Supplier supplier = null;
				if(col >= commKTableModel.getFixedColumnCount()){
					supplier = supplierList.get(col - commKTableModel.getFixedColumnCount());
				}
				resultClickEventAction(supplier, null, null);
			}
		});
		
		resultKTable.addCellMouseRightListener(new KTableCellMouseRightAdapter(){
			public void cellMouseRightClick(int col, int row, Rectangle rectangle) {
				Supplier supplier = null;
				EvaluationFactor evaluationFactor = null;
				EvaluationFactorResult  evalFactorResult = null;
				ExpertEvaluationResult expertEvaluationResult = null;
				if(col >= commKTableModel.getFixedColumnCount()){
					supplier = supplierList.get(col - commKTableModel.getFixedColumnCount());
				}
				if(row >= commKTableModel.getFixedRowCount() && row < commKTableModel.getFixedRowCount() + evaluationFactorList.size()){
					evaluationFactor = evaluationFactorList.get(row - commKTableModel.getFixedRowCount());
				}
				if(null != supplier && null != evaluationFactor){
					evalFactorResult = supplierEvaluationFactorResultMap.get(supplier.getId()).get(evaluationFactor.getId());
					expertEvaluationResult = expertEvaluationResultMap.get(supplier.getId());
				}
				resultMouseRightClickEventAction(supplier, evaluationFactor, expertEvaluationResult, evalFactorResult);
			}
		});
		resultKTable.setModel(commKTableModel);
		evaluationTableComposite.layout();
	}
	
	public void resizeColumn(int width){
		for(int i = commKTableModel.getFixedColumnCount(); i < commKTableModel.getColCount(); i++){
			commKTableModel.setColumnWidth(i, width);
		}
		resultKTable.autoComputerColumnWidth();
	}
	/**
	 * @Description: 显示指标的评审意见
	 * @author guoyr
	 */
	private void showFactorEvaluationOpinion(EvaluationFactorResult evalFactorResult){
		if(null != evalFactorResult){
			System.out.println("第一处"+ObjectUtils.getObjString(evalFactorResult.getEvaluationOpinion()));
			factor_evaluation_opinion.setText(ObjectUtils.getObjString(evalFactorResult.getEvaluationOpinion()));
			factor_evaluation_opinion.setToolTipText(ObjectUtils.getObjString(factor_evaluation_opinion.getText()));
			factor_evaluation_opinion.setData("evalFactorResult", evalFactorResult);
		}
	}
	
	/**
	 * @Description: 输入完评审意见后保存评审意见
	 * @author guoyr
	 */
	private void setFactorEvaluationOpinion(){
		EvaluationFactorResult  evalFactorResult = (EvaluationFactorResult)factor_evaluation_opinion.getData("evalFactorResult");
		if(null != evalFactorResult){
			System.out.println("第二处"+factor_evaluation_opinion.getText().trim());
			evalFactorResult.setEvaluationOpinion(factor_evaluation_opinion.getText().trim());
		}
	}

	/**
	 * @Description: 全部满分
	 * @author guoyr
	 */
	public void initDefaultFactorResult(){
		if(!MessageDialog.openConfirm(getShell(), Messages.Tips_setQuickEvaluationScore)){
			return;
		}
		resultKTable.closeCellEditor();
		List<Double> sortList = new ArrayList<Double>();
		for(Supplier supplier : supplierList){
			ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultMap.get(supplier.getId());
			// 当前供应商的所有评审结果
			Map<String, EvaluationFactorResult> supplierFactorResultMap = supplierEvaluationFactorResultMap.get(supplier.getId());
			BigDecimal totalEvaluationScore = null;
			BigDecimal totalEvaluationFinalScore = null;
			for(EvaluationFactor evaluationFactor : evaluationFactorList){
				EvaluationFactorResult evaluationFactorResult = supplierFactorResultMap.get(evaluationFactor.getId());
				BigDecimal scoreResult =  evaluationFactorResult.getEvaluationScore();
				boolean setFullScore = false;
				if(null == scoreResult) {
					if(null != fixedFactorMap.get(evaluationFactor.getId())) {
						// 如果定档指标，则不自动满分
						setFullScore = false;
					}else {
						
						setFullScore = true;
					}
				}else {
					// 如果有分值 ，则要看目前的分值是不是超过分值范围
					if(null == evaluationFactor.getFactorMaxScore() ) {
						evaluationFactor.setFactorMaxScore(evaluationFactor.getFactorScore());
					}
					if(null != fixedFactorMap.get(evaluationFactor.getId())) {
						FixedFactorItemVo fixedFactorItemVo = supplierFactorScoreRangeMap.get(supplier.getId()+evaluationFactor.getId());
						boolean errorScore = false;
						if(null != fixedFactorItemVo && null != fixedFactorItemVo.getFactorMinScore() && null != fixedFactorItemVo.getFactorMaxScore()) {
							BigDecimal minScore = fixedFactorItemVo.getFactorMinScore();
							BigDecimal maxScore = fixedFactorItemVo.getFactorMaxScore();
							
							if(fixedFactorItemVo.isIncludeFactorMinScore()) {
								if(scoreResult.compareTo(minScore) < 0) {
									errorScore = true;
								}
							}else {
								if(scoreResult.compareTo(minScore) <= 0) {
									errorScore = true;
								}
							}
							if(fixedFactorItemVo.isIncludeFactorMaxScore()) {
								if(scoreResult.compareTo(maxScore) > 0) {
									errorScore = true;
								}
							}else {
								if(scoreResult.compareTo(maxScore) >= 0) {
									errorScore = true;
								}
							}
						}else {
							errorScore = true;
						}
						
						if(errorScore) {
							setFullScore = false;
							resultChanged = true;
							LOG.error("未定档或分值超过定档得分");
							evaluationFactorResult.setEvaluationScore(null);
						}
					}else {
						if(scoreResult.compareTo(evaluationFactor.getFactorMaxScore()) > 0) {
							setFullScore = true;
						}
					}
				}
				
				if(setFullScore){
					if(StringUtils.equals(EvaluationFactorEnum.SCORE_TYPE_OBJECTIVE, evaluationFactor.getScoreType())){
						// 如果是客观评分项，则设置为最大值
						List<ScoreItem> itemList = SerializeUtils.deserializeObject(evaluationFactor.getObjectiveItem());
						if(null != itemList && itemList.size() > 0){
							BigDecimal maxScore = null;
							for(ScoreItem item : itemList){
								BigDecimal score = item.getScore();
								if(null == maxScore || score.compareTo(maxScore) > 0){
									maxScore = score;
								}
							}
							evaluationFactorResult.setEvaluationScore(maxScore);
						}
					}else {
						evaluationFactorResult.setEvaluationScore(evaluationFactor.getFactorMaxScore());
					}
					contentForegroundMap.put(evaluationFactorResult.getId(), importColor);// 设置为非自己录入的颜色
					resultChanged = true;
				}
				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());
				}
			}
			expertEvaluationResult.setEvaluationScore(BigDecimalUtils.round(totalEvaluationScore, scoreRound));
			expertEvaluationResult.setEvaluationFinalScore(BigDecimalUtils.round(totalEvaluationFinalScore, scoreRound));
			if(null != totalEvaluationFinalScore){
				sortList.add(totalEvaluationFinalScore.doubleValue());
			}
		}
		reSetSortNo(null);
		refreshObjectiveBackgroundMap();
	}
	
	private ImportEvaluationResultListener createImportEvaluationResultListener(final boolean ignoreExist){
		return new ImportEvaluationResultListener(){
			public void importEvalResult(Map<String, Map<String, EvaluationFactorResult>> importSupplierResult) {
				if(null == importSupplierResult){
					return;
				}
				List<Double> sortList = new ArrayList<Double>();
				for(Supplier supplier : supplierList){
					ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultMap.get(supplier.getId());
					// 当前供应商的所有评审结果
					Map<String, EvaluationFactorResult> supplierFactorResultMap = supplierEvaluationFactorResultMap.get(supplier.getId());
					
					Map<String, EvaluationFactorResult> importSupplierResultMap = importSupplierResult.get(supplier.getId());
					BigDecimal totalEvaluationScore = null;
					BigDecimal totalEvaluationFinalScore = null;
					for(EvaluationFactor evaluationFactor : evaluationFactorList){
						EvaluationFactorResult evaluationFactorResult = supplierFactorResultMap.get(evaluationFactor.getId());
						if(null != evaluationFactorResult){
							if(ignoreExist && null != evaluationFactorResult.getEvaluationScore()){
								// 如果忽略有值，并且已有打分，则不导入
							}else if(null != importSupplierResultMap){
								EvaluationFactorResult importEvaluationResult = importSupplierResultMap.get(evaluationFactor.getId());
								if(null != importEvaluationResult && null != importEvaluationResult.getEvaluationScore()){
									boolean allowImport = true;
									if(null != fixedFactorMap.get(evaluationFactor.getId())) {
										FixedFactorItemVo fixedFactorItemVo = supplierFactorScoreRangeMap.get(supplier.getId()+evaluationFactor.getId());
										if(null != fixedFactorItemVo && null != fixedFactorItemVo.getFactorMinScore() && null != fixedFactorItemVo.getFactorMaxScore()) {
											BigDecimal minScore = fixedFactorItemVo.getFactorMinScore();
											BigDecimal maxScore = fixedFactorItemVo.getFactorMaxScore();
											if(fixedFactorItemVo.isIncludeFactorMinScore()) {
												if(importEvaluationResult.getEvaluationScore().compareTo(minScore) < 0) {
													allowImport = false;
												}
											}else {
												if(importEvaluationResult.getEvaluationScore().compareTo(minScore) <= 0) {
													allowImport = false;
												}
											}
											if(fixedFactorItemVo.isIncludeFactorMaxScore()) {
												if(importEvaluationResult.getEvaluationScore().compareTo(maxScore) > 0) {
													allowImport = false;
												}
											}else {
												if(importEvaluationResult.getEvaluationScore().compareTo(maxScore) >= 0) {
													allowImport = false;
												}
											}
										}else {
											allowImport = false;
										}
									}else {
										if(importEvaluationResult.getEvaluationScore().compareTo(evaluationFactor.getFactorMinScore()) < 0 || importEvaluationResult.getEvaluationScore().compareTo(evaluationFactor.getFactorMaxScore()) > 0) {
											allowImport = false;
										}
									}
									
									if(allowImport) {
										evaluationFactorResult.setEvaluationScore(importEvaluationResult.getEvaluationScore());
										evaluationFactorResult.setEvaluationOpinion(importEvaluationResult.getEvaluationOpinion());
										// 设置为非自己录入的颜色
										contentForegroundMap.put(evaluationFactorResult.getId(), importColor);
										resultChanged = true;
									}
								}
							}
						
							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());
							}
						}
					}
					if(null != expertEvaluationResult){
						expertEvaluationResult.setEvaluationScore(BigDecimalUtils.round(totalEvaluationScore, scoreRound));
						expertEvaluationResult.setEvaluationFinalScore(BigDecimalUtils.round(totalEvaluationFinalScore, scoreRound));
					}
					if(null != totalEvaluationFinalScore){
						sortList.add(totalEvaluationFinalScore.doubleValue());
					}
				}
				reSetSortNo(sortList);
				resultChanged = true;
				if(!Display.getDefault().isDisposed()){
					Display.getDefault().syncExec(new Runnable(){
						public void run(){
							if (null != resultKTable && !resultKTable.isDisposed()) {
								refreshObjectiveBackgroundMap();
							}
						}
					});
				}
			}
		};
	}
	
	/**
	 * @Description: 导入其他专家的评审结果
	 * @author guoyr
	 */
	public void importOtherExpertResult(){
		resultKTable.closeCellEditor();
		ImportOtherExpertEvaluationResultDialog dialog = new ImportOtherExpertEvaluationResultDialog(getShell(), tenderProject, evaluationStep, supplierList, evaluationFactorList, createImportEvaluationResultListener(false));
		dialog.setTitle("导入其他专家的打分结果");
		dialog.open();
	}
	
	/**
	 * @Description: 导入历史评审结果
	 * @author guoyr
	 */
	public void importHistoryResult(){
		resultKTable.closeCellEditor();
		ImportHistoryScoreResultDialog dialog = new ImportHistoryScoreResultDialog(getShell(), tenderProject, evaluationStep, supplierList, evaluationFactorList, createImportEvaluationResultListener(false));
		dialog.setTitle("导入历史打分结果");
		dialog.open();
	}
	/**
	 * @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);
		return evaluationFinalScore;
	}
	/**
	 * @Description: 自动保存线程
	 * @author guoyr
	 * @param isSubmit
	 */
	Runnable autoSave = new Runnable(){
		public void run() {
			while(autoSaveMinTime > 0){
				try {
					// 每隔指定的autoSaveMinTime分钟自动保存
					Thread.sleep(1000 * 60 * autoSaveMinTime);
				} catch (InterruptedException e) {
				}
				Display.getDefault().syncExec(new Runnable() {
					public void run() {
						if(null != resultKTable && !resultKTable.isDisposed()){
							if(resultChanged){
								LOG.debug((new Date()) + "自动保存");
								saveEvaluationFactorResult();
							}
						}else {
							autoSaveMinTime = -1;
						}
					}
				});
			}
		}
	};
	
	/**
	 * @Description: 保存评审结果
	 * @author guoyr
	 * @return 
	 */
	public boolean saveEvaluationFactorResult(){
		resultKTable.closeCellEditor();
		boolean saveSuccess = true;
		// 如果修改了结果，才保存结果
		if(resultChanged){
			saveSuccess = expertEvaluationStepService.saveEvaluationResult(tenderProject.getId(), curUser.getId(), evaluationStep, expertEvaluationResultMap, supplierEvaluationFactorResultMap);
			if(saveSuccess){
				resultChanged = false;
				if(evaluationBySupplier){
					saveEvaluationFactorResultBySupplier();
				}
			}else {
				resultChanged = true;
			}
		}
		refreshObjectiveBackgroundMap();
		return saveSuccess;
	}
	
	/**
	 * @Description: 保存按供应商打分结果
	 * @author guoyr 
	 */
	private void saveEvaluationFactorResultBySupplier(){
		new Thread(new Runnable(){
			public void run(){
				List<Map<String, Object>> supplierEvaluationFactorResultList = new ArrayList<Map<String, Object>>();
				String groupCode = tenderProject.getPurcategoryNames();
				for(Supplier supplier : supplierList){
					Map<String, EvaluationFactorResult> supplierFactorResultMap = supplierEvaluationFactorResultMap.get(supplier.getId());
					for(Entry<String, EvaluationFactorResult> resultEntry : supplierFactorResultMap.entrySet()){
						EvaluationFactorResult evaluationFactorResult = resultEntry.getValue();
						if(null != evaluationFactorResult.getEvaluationScore()){
							Map<String, Object> resultMap = new HashMap<String, Object>();
							resultMap.put("SUPPLIER_NAME", supplier.getSupplierName());
							resultMap.put("FACTOR_NAME", evaluationFactorResult.getFactorName());
							resultMap.put("EVALUATION_SCORE", evaluationFactorResult.getEvaluationScore());
							supplierEvaluationFactorResultList.add(resultMap);
						}
					}
				}
				evaluationFactorResultService.saveEvaluationResultBySupplier(tenderProject.getParentId(), groupCode, curUser.getId(), supplierEvaluationFactorResultList);
			}
		}).start();
	}
	/**
	 * @Description: 检查是否可以提交
	 * @author guoyr
	 * @return 
	 */
	public String checkSubmit(){
		resultKTable.closeCellEditor();
		for(Supplier supplier : supplierList){
			Map<String, EvaluationFactorResult> supplierFactorResultMap = supplierEvaluationFactorResultMap.get(supplier.getId());
//			ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultMap.get(supplier.getId());
			for(Entry<String, EvaluationFactorResult> factorResultEntry : supplierFactorResultMap.entrySet()){
				EvaluationFactorResult evaluationFactorResult = factorResultEntry.getValue();
				if(null == evaluationFactorResult.getEvaluationFinalScore() || null == evaluationFactorResult.getEvaluationScore()){
					return MessageFormat.format(Messages.Tips_existNoScoreFactor, supplier.getSupplierName());
				}
			}
		}
		
		boolean sameScoreFilter = evaluationStep.getEvaluationParamByKey(EvaluationStepEnum.CHECK_SAME_EVALUATION_FILTER, Boolean.class, true);
		if (objectiveFactorIdMap.size() > 0 && sameScoreFilter) {
			if(null == expertService) {
				expertService = new ExpertService();
			}
			int expertCount = expertService.getEvaluationStepExpertCount(tenderProject.getId(), evaluationStep);
			
			List<EvaluationFactorResult> evaluationFactorResultList = evaluationFactorResultService.getEvaluationFactorResultList(tenderProject.getId(), evaluationStep.getId(), null);
			
			Map<String, List<EvaluationFactorResult>> useridMap = CollectionUtils.toArrayListHashMap(evaluationFactorResultList, new CollectionUtils.NewMap<String, EvaluationFactorResult>() {
				public String getKey(EvaluationFactorResult t) {
					return t.getUserId();
				}
			}, new HashMap<String, List<EvaluationFactorResult>>());
			
			if (expertCount > useridMap.size()) {
				return "还有专家未进行评审，请等待其他专家保存打分结果，并验证一致性评审项的打分结果一致时才能进行此操作！";
			}
			
			Map<Keys, List<EvaluationFactorResult>> evaluationFactorResultListMap = CollectionUtils.toArrayListHashMap(evaluationFactorResultList, new CollectionUtils.NewMap<Keys, EvaluationFactorResult>() {
				public Keys getKey(EvaluationFactorResult t) {
					return new Keys(t.getSupplierId(), t.getFactorId());
				}
			}, new HashMap<Keys, List<EvaluationFactorResult>>());
			
			
			for (Keys key : evaluationFactorResultListMap.keySet()) {
				List<EvaluationFactorResult> factorResultList = evaluationFactorResultListMap.get(key);
				BigDecimal evaluationScore = null;
				for (EvaluationFactorResult evaluationFactorResult : factorResultList) {
					if(objectiveFactorIdMap.containsKey(evaluationFactorResult.getFactorId())){
						BigDecimal score = evaluationFactorResult.getEvaluationScore();
						if (score == null) {
							return "还有专家未对一致性评审项进行打分，请等待其他专家保存打分结果，并验证一致性评审项的打分结果一致时才能进行此操作！";
						}
						if (evaluationScore == null) {
							evaluationScore = score;
						} else if (evaluationScore.compareTo(score) != 0) {
							return "您与其他专家存在一致性评审项的打分结果不一致，待所有专家对一致性评审项的打分结果一致时才能进行此操作！";
						}
					}
				}
			}
		}
		return null;
	}
	
	public boolean checkRefresh() {
        return false;
    }
	
	/** 
	 * @Description: 提交评审结果
	 * @author guoyr
	 * @return 
	 */
	public boolean submitEvaluationFactorResult() {
		return expertEvaluationStepService.submitEvaluationResult(tenderProject.getId(),projectRule, curUser.getId(), evaluationStep, supplierList);
	}

	/** 
	 * @Description: 释放资源
	 * @author guoyr 
	 */
	public void dispose() {
		imageResolver.disposite();
		evaluationFactorList.clear();
		evaluationFactorList = null;
		if(null != ImportEvaluationBySupplierScoreThread){
			ImportEvaluationBySupplierScoreThread.stop();
		}
		super.dispose();
	}
	/**
	 * @Description: 重新计算排名与总得分
	 * @param evaluationFactor
	 * @param evalFactorResult
	 * @param expertEvaluationResult
	 * @param supplier
	 */
	private void reSetTotalScoreAndSore(EvaluationFactor evaluationFactor, EvaluationFactorResult evalFactorResult, ExpertEvaluationResult expertEvaluationResult, Supplier supplier){
		// 如果是客观项，检查所有评标专家的评标结果是否相同
		Map<String, EvaluationFactorResult> supplierFactorResultMap = supplierEvaluationFactorResultMap.get(supplier.getId());
		resultChanged = true;
		contentForegroundMap.put(evalFactorResult.getId(), null);
		BigDecimal totalEvaluationScore = null;
		BigDecimal totalEvaluationFinalScore = null;
		for(Entry<String, EvaluationFactorResult> factorResultEntry : supplierFactorResultMap.entrySet()){
			EvaluationFactorResult evaluationFactorResult = factorResultEntry.getValue();
			if(null != evaluationFactorResult.getEvaluationScore()){
				totalEvaluationScore = BigDecimalUtils.add(totalEvaluationScore, evaluationFactorResult.getEvaluationScore());
			}
			if(null != evaluationFactorResult.getEvaluationFinalScore()){
				totalEvaluationFinalScore = BigDecimalUtils.add(totalEvaluationFinalScore, evaluationFactorResult.getEvaluationFinalScore());
			}
		}
		expertEvaluationResult.setEvaluationScore(BigDecimalUtils.round(totalEvaluationScore, scoreRound));
		expertEvaluationResult.setEvaluationFinalScore(BigDecimalUtils.round(totalEvaluationFinalScore, scoreRound));
	
		reSetSortNo(null);
		refreshObjectiveBackgroundMap();
	}
	/**
	 * @Description: 检查观项指标的结果是否一致
	 * @author guoyr
	 * @param evaluationFactorResult
	 * @param supplierId
	 * @param factorId 
	 */
	private void checkScoreFactorResult(EvaluationFactorResult evaluationFactorResult, String supplierId, String factorId){
		boolean overRange = false;
		BigDecimal curEvaluationScore = evaluationFactorResult.getEvaluationScore();
		if(null != curEvaluationScore && null != warningRange){
//			QueryWrapper queryWrapper = new QueryWrapper(EvaluationFactorResult.class);
//			queryWrapper.setQueryParm("evaluationScore");
//			queryWrapper.setWhereParam("tenderId", tenderProject.getId());
//			queryWrapper.setWhereParam("stepId", evaluationStep.getId());
//			queryWrapper.setWhereParam("supplierId", supplierId);
//			queryWrapper.setWhereParam("factorId", factorId);
//			queryWrapper.setWhereParam("userId", curUser.getId(), WhereParam.OP_NOT_EQ);//不包含自己当前的分值
//			List<Object> evaluationResultList = evaluationFactorResultService.queryColumnList(queryWrapper);
			List<BigDecimal> evaluationResultList = evaluationFactorResultService.getEvaluationScoreListNotUserId(tenderProject.getId(), evaluationStep.getId(), supplierId, factorId, curUser.getId());
			BigDecimal totalScore = curEvaluationScore;//把自身报价添加进列表
			int count = 1;
			if(null != evaluationResultList && evaluationResultList.size() > 0){
				for(BigDecimal evaluationResult : evaluationResultList){
					if(null != evaluationResult){
						count ++;
						totalScore = BigDecimalUtils.add(totalScore, evaluationResult);
					}
				}
			
				BigDecimal avgValue = BigDecimalUtils.div(totalScore, new BigDecimal(count));
				BigDecimal percentValue = BigDecimalUtils.sub(curEvaluationScore, avgValue) ;//获取差值
				if(avgValue.compareTo(BigDecimal.ZERO) != 0){
					percentValue = BigDecimalUtils.div(percentValue, avgValue).abs();//偏差率绝对值
				}
				if(percentValue.compareTo(warningRange) >= 0){
					overRange = true;
				}
			}
		}
		contentBackgroundMap.put(evaluationFactorResult.getId(), overRange ? errorBgColor : null);
	}
	
	private void initFixedFactorInfo() {
		if(null != supplierFactorScoreRangeMap) {
			supplierFactorScoreRangeMap.clear();
		}
		if(null != fixedFactorMap) {
			fixedFactorMap.clear();
		}
		List<EvaluationStep> evaluationStepList = evaluationStepService.getEvaluationStepListByType(tenderProject.getId(), EvaluationStepEnum.FIXED_FACTOR);
		if(null != evaluationStepList && evaluationStepList.size() > 0) {
			
			for(EvaluationStep step : evaluationStepList) {

				String PARAM_FIXED_FACTOR_ID = step.getEvaluationParamByKey(EvaluationStepEnum.PARAM_FIXED_FACTOR_ID);
				String PARAM_FIXED_FACTOR_CODE = step.getEvaluationParamByKey(EvaluationStepEnum.PARAM_FIXED_FACTOR_CODE);
				EvaluationFactor fixFactor = null;
				if(StringUtils.hasLength(PARAM_FIXED_FACTOR_CODE)) {
					fixFactor = evaluationFactorService.getFactorByFactorCode(step.getTenderId(), PARAM_FIXED_FACTOR_CODE);
				}
				if(null == fixFactor && StringUtils.hasLength(PARAM_FIXED_FACTOR_ID)) {
					fixFactor = evaluationFactorService.findById(PARAM_FIXED_FACTOR_ID);
				}
				if(null != fixFactor) {
					fixedFactorMap.put(fixFactor.getId(), "01");
					if(CommonEnum.COMPLETE.equals(step.getStepStatus())) {
						// 只有在完成定档评审时才加载
						String PARAM_FIXED_FACTOR_LIST = step.getEvaluationParamByKey(EvaluationStepEnum.PARAM_FIXED_FACTOR_LIST);
						List<FixedFactorItemVo> fixedFactorItemVoList = null;
						if(StringUtils.hasLength(PARAM_FIXED_FACTOR_LIST)) {
							fixedFactorItemVoList = JsonUtil.strToList(PARAM_FIXED_FACTOR_LIST, FixedFactorItemVo.class);
						}
						
						if(null != fixedFactorItemVoList) {
							String PARAM_FIXED_FACTOR_SUPPLIER_LIST = step.getEvaluationParamByKey(EvaluationStepEnum.PARAM_FIXED_FACTOR_SUPPLIER_LIST);
							List<FixedFactorSupplierVo> fixedFactorSupplierVoList = JsonUtil.strToList(PARAM_FIXED_FACTOR_SUPPLIER_LIST, FixedFactorSupplierVo.class);
							if(null != fixedFactorSupplierVoList && fixedFactorSupplierVoList.size() > 0) {
								for(FixedFactorSupplierVo vo : fixedFactorSupplierVoList) {
									for(FixedFactorItemVo itemVo : fixedFactorItemVoList) {
										if(ObjectUtils.equals(vo.getName(), itemVo.getName())) {
											if(null != itemVo.getFactorMinScore() && null != itemVo.getFactorMaxScore()) {
												supplierFactorScoreRangeMap.put(vo.getSupplierId()+fixFactor.getId(), itemVo);
											}
											break;
										}
									}
								}
								
							}
						}
					}
				}
        	
			}
		}
		
	}
	
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
