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.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.CLabel;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.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.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
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.ComplianceEvaluationDialog;
import com.gx.obe.evaluation.step.dialog.EvaluationOpinionDialog;
import com.gx.obe.evaluation.step.dialog.ImportHistoryScoreResultDialog;
import com.gx.obe.evaluation.step.dialog.ImportOtherExpertEvaluationResultDialog;
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.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.ListSortUtils;
import com.gx.obe.util.utils.ObjectUtils;
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.SupplierOpeningResult;
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.SupplierOpeningResultService;
import com.gx.obe.web.service.evaluation.EvaluationFactorResultService;
import com.gx.obe.web.service.evaluation.EvaluationFactorService;
import com.gx.obe.web.service.expert.ExpertEvaluationResultService;
import com.gx.obe.web.service.expert.ExpertEvaluationStepService;
import com.gx.obe.web.service.expert.ExpertService;
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.KTableCellEditorGRadio;
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 ComplianceEvaluationTableComposite extends Composite implements IEvaluationTable {
	
	private TenderProjectEntity tenderProject;
	private EvaluationStep evaluationStep;
	private TenderProjectRuleEntity projectRule;
	private ImageResolver imageResolver = new ImageResolver();
	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> evalSupplierList = new ArrayList<Supplier>();
    private List<Supplier> notEvalSupplierList = new ArrayList<Supplier>();
	private KTable resultKTable;
	private CommonKtableCModel commKTableModel;
	private int curRow = 0;
	private int curCol = 0;
	private boolean resultChanged = false;
	private Integer autoSaveMinTime = 0;
	private Logger LOG = Logger.getLogger(ComplianceEvaluationTableComposite.class);
	private String supplierName;
	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 Map<String, Color> contentForegroundMap = new HashMap<String, Color>();
	// 记录供应商每个指标的评审结果
	private Map<String, Map<String, EvaluationFactorResult>> supplierEvaluationFactorResultMap;
	// 记录专家对供应商的评审汇总结果
	private Map<String, ExpertEvaluationResult> expertEvaluationResultMap;
	private AuthUser curUser;
	private EvaluationSupplierFactorListener evaluationSupplierFactorListener;
	private String EVAL_ACCESSMENT_YES = Messages.Qualified;
	private String EVAL_ACCESSMENT_NO = Messages.Unqualified;
	private int fontSize = 9;
	private boolean indexIsBidNumber = false;
	private int maxFactorNameLength = -1;
	private boolean allowEditConclusion = false;
	private boolean iKnowTips = false;
	private Composite tipsComposite;
	private int unqualifiedDeviateClauseCount = 1;// 设置偏离性条款否决投标的条数
	
	private ExpertService expertService;
	private ExpertEvaluationStepService expertEvaluationStepService;
	private EvaluationFactorResultService evaluationFactorResultService;
	private EvaluationFactorService evaluationFactorService;
	private ExpertEvaluationResultService expertEvaluationResultService;
	private SupplierOpeningResultService supplierOpeningResultService;
	private Map<String, String> objectiveFactorIdMap = new HashMap<String, String>();
	private final Map<Keys, Color> objectiveBackgroundMap = new HashMap<Keys, Color>();
	private boolean sameScoreCheck = true;
	/**
     * 必须通过供应商数量
     */
    private int mustConformSupplierCount;
	
	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().contains(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 ComplianceEvaluationTableComposite(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);
		GridLayout gl_evaluationTableComposite = new GridLayout(1, false);
		gl_evaluationTableComposite.marginHeight = 0;
		gl_evaluationTableComposite.marginWidth = 0;
		evaluationTableComposite.setLayout(gl_evaluationTableComposite);
		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(3, 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.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);
		new Label(composite_5, SWT.NONE);
		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();
				}
			}
		});
 		evaluationFactorService = new EvaluationFactorService();
		expertEvaluationResultService  = new ExpertEvaluationResultService();
		supplierOpeningResultService = new SupplierOpeningResultService();
	}
	
	/**
	 * @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.evaluationSupplierFactorListener = evaluationSupplierFactorListener;
		supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProject.getTenderType());
		resultChanged = true;
		curRow = 0;
		curCol = 0;
		// 修改评标专家的评审记录
		if(null == expertEvaluationStepService){
			expertEvaluationStepService = new ExpertEvaluationStepService();
		}
		ActionLogUtils.addServerLog(tenderProject.getId(), "进入" + evaluationStep.getStepName(), curUser.getUserName());
		expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.IN_PROGRESS, tenderProject.getId(), evaluationStep, curUser.getId());
		Object indexIsBidNumberObj = evaluationStep.getEvaluationParamByKey(EvaluationStepEnum.PARAM_INDEX_IS_BIDNUMBER);
		indexIsBidNumber = false;
		if (null != indexIsBidNumberObj && indexIsBidNumberObj.equals(CommonEnum.YES)) {
			indexIsBidNumber = true;
		}
		
		allowEditConclusion = evaluationStep.getEvaluationParamByKey(EvaluationStepEnum.PARAM_ALLOW_EDIT_CONCLUSION, Boolean.class, false);
		if (allowEditConclusion) {
			allowEditConclusion = true;
			EVAL_ACCESSMENT_YES = "通过";
			EVAL_ACCESSMENT_NO = "不通过";
		}
		unqualifiedDeviateClauseCount = evaluationStep.getEvaluationParamByKey(EvaluationStepEnum.UNQUALIFIED_DEVIATE_CLAUSE_COUNT, Integer.class, 1);
		
		mustConformSupplierCount = ObjectUtils.getIntValue(evaluationStep.getEvaluationParamByKey(EvaluationStepEnum.PARAM_FILTER_PRICE_SORT), 0);
		sameScoreCheck   = evaluationStep.getEvaluationParamByKey(EvaluationStepEnum.CHECK_SAME_EVALUATION, Boolean.class, true);
		
		initEvaluationResult(supplierList);
		if (Constants.FACTOR_IS_HORIZONTAL) {
			drawLeftFactorEvaluationTable();
		} else {
			drawLeftSupplierEvaluationTable();
		}
		
		
		autoSaveMinTime = projectRule.getAutoSaveTime();
		// 自动保存
		if (null != autoSaveMinTime && autoSaveMinTime > 0) {
			new Thread(autoSave).start();
		}
		refreshObjectiveBackgroundMap();
	}
	
	/**
     * @Description: 初始化供应商列表
     * @author chenxw
     * @param supplierList 
     */
    private void initSupplierList(List<Supplier> supplierList) {
        evalSupplierList.clear();
        notEvalSupplierList.clear();
        
        if (mustConformSupplierCount < 1) {
            evalSupplierList.addAll(supplierList);
            return;
        }
        
        filterUnQualifiedSupplier(supplierList);
        
        Comparator<Supplier> supplierComparator = getSupplierComparator();
        if (null == supplierComparator) {
        	evalSupplierList.addAll(supplierList);
        	return;
		}
        
        List<Supplier> list = new ArrayList<Supplier>();
        ListSortUtils<Supplier> listSortUtils = new ListSortUtils<Supplier>(supplierList, supplierComparator);
		for (int sortNo = 1; sortNo <= listSortUtils.getSortCount(); sortNo++) {
			int evalSupplierCount = list.size();
			if (evalSupplierCount < mustConformSupplierCount) {
				list.addAll(listSortUtils.getList(sortNo));
			} else if (evalSupplierCount == mustConformSupplierCount) {
				addNotEvalSupplier(listSortUtils.getList(sortNo));
			} else if (evalSupplierCount > mustConformSupplierCount) {
				addNotEvalSupplier(listSortUtils.getList(sortNo));
				mustConformSupplierCount++;
			}
		}
        evalSupplierList.addAll(list);
        Collections.sort(evalSupplierList, supplierComparator);
    }
	
	private void addNotEvalSupplier(List<Supplier> list) {
		notEvalSupplierList.addAll(list);
		for (Supplier supplier : list) {
			Map<String, EvaluationFactorResult> supplierFactorResultMap = supplierEvaluationFactorResultMap.get(supplier.getId());
			if (null != supplierFactorResultMap) {
				for (String supplierId : supplierFactorResultMap.keySet()) {
					supplierFactorResultMap.get(supplierId).setEvaluationResult(null);
				}
			}
		}
	}
	
    /**
	 * @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;
						String evaluationResult = null;
						for (EvaluationFactorResult evaluationFactorResult : factorResultList) {
							if (curUser.getId().equals(evaluationFactorResult.getUserId())) {
								continue;
							}
							String result = evaluationFactorResult.getEvaluationResult();
							if (evaluationResult == null) {
								evaluationResult = result;
							} else if (result != null && !evaluationResult.equals(result)) {
								isEqual = false;
							}
						}
						if (isEqual && evaluationResult != null) {
							Map<String, EvaluationFactorResult> evaluationFactorResultMap = supplierEvaluationFactorResultMap.get(key.get(0));
							if (evaluationFactorResultMap != null) {
								EvaluationFactorResult supplierEvaluationFactorResult = evaluationFactorResultMap.get(key.get(1));
								if (supplierEvaluationFactorResult != null) {
									String supplierEvaluationResult = supplierEvaluationFactorResult.getEvaluationResult();
									if (null != supplierEvaluationResult && !evaluationResult.equals(supplierEvaluationResult)) {
										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 (resultKTable != null) {
			resultKTable.redraw();
		}
	}
	
	/**
	 * @Description: 过滤不合格供应商
	 * @author chenxw
	 * @param supplierList 
	 */
	private void filterUnQualifiedSupplier(List<Supplier> supplierList) {
		CollectionUtils.filter(supplierList, t -> {
            String accessment = null;
            ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultMap.get(t.getId());
            if (null != expertEvaluationResult) {
                accessment = expertEvaluationResult.getAccessment();
            }
            if (EvaluationStepEnum.UNQUALIFIED.equals(accessment)) {
                evalSupplierList.add(t);
                return false;
            }
            return true;
        });
	}

	/**
	 * @Description: 获取供应商比较器
	 * @author chenxw
	 * @param openingResultLMap
	 * @return 
	 */
	private Comparator<Supplier> getSupplierComparator() {
		List<SupplierOpeningResult> openingResultList = supplierOpeningResultService.getSupplierOpeningResultList(tenderProject.getId());
		final Map<String, SupplierOpeningResult> openingResultLMap = CollectionUtils.toHashMap(openingResultList, SupplierOpeningResult::getSupplierId);
		if (null == openingResultLMap || openingResultLMap.isEmpty()) {
			return null;
		}
		return new Comparator<Supplier>() {
			public int compare(Supplier supplier0, Supplier supplier1) {
				BigDecimal evaluationPrice0 = null;
				SupplierOpeningResult openingResult0 = openingResultLMap.get(supplier0.getId());
				if (null != openingResult0) {
					evaluationPrice0 = openingResult0.getEvaluationPrice();
				}
				BigDecimal evaluationPrice1 = null;
				SupplierOpeningResult openingResult1 = openingResultLMap.get(supplier1.getId());
				if (null != openingResult1) {
					evaluationPrice1 = openingResult1.getEvaluationPrice();
				}
				return BigDecimalUtils.compareTo(evaluationPrice0, evaluationPrice1);
			}
		};
	}

	/**
	 * @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;
		List<EvaluationFactor> evaluationTopFactorList = evaluationFactorService.getEvaluationStepFactorTreeList(tenderProject.getId(), evaluationStep);
		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();
			StyleRange factorNameTitleStyleRange = getTitleStyleRange(0, Messages.EvalFactor.length() + 1);
			StyleRange styleFactorNameRange = new StyleRange();
			styleFactorNameRange.start = 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");
				int 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(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() {
					ComplianceEvaluationDialog dialog = new ComplianceEvaluationDialog(getShell(), tenderProject, supplier, evaluationFactor, expertEvaluationResult, evalFactorResult, new CallBack() {
						@Override
						public void callBack() {
							showFactorEvaluationOpinion(evalFactorResult);
							resultKTable.redraw(curCol, curRow, 1, 1);
						}
						
					});
					resultKTable.closeCellEditor();
					dialog.open();
					resultChanged = true;
					setConclusion(supplier.getId());
					if (Constants.FACTOR_IS_HORIZONTAL) {//判断当前表格是否为已切换坐标表格
						resultKTable.redraw(curCol, commKTableModel.getRowCount() - 1, 1, 1);
					} else {
						resultKTable.redraw(commKTableModel.getColCount() - 1, curRow, 1, 1);
					}
				}
			});
		}
//		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(List<Supplier> supplierList) {
		if (null == evaluationFactorList) evaluationFactorList = new ArrayList<EvaluationFactor>();
		initEvaluationStepFactor();
		
		// 记录供应商每个指标的评审结果
		supplierEvaluationFactorResultMap = new HashMap<String, Map<String, EvaluationFactorResult>>();
		// 记录专家对供应商的评审汇总结果
		expertEvaluationResultMap = new HashMap<String, ExpertEvaluationResult>();
		
		// 评审当前评标专家所有的评标结果
		if(null == evaluationFactorResultService){
			evaluationFactorResultService = new EvaluationFactorResultService();
		}
 		List<EvaluationFactorResult> memberEvaluateFactorResultList = evaluationFactorResultService.getExpertEvaluationFactorResult(tenderProject.getId(), evaluationStep.getId(), curUser.getId(), null);
 		List<ExpertEvaluationResult> queryExpertEvaluationResultList = expertEvaluationResultService.getExpertStepAllSupplierEvaluationResultList(tenderProject.getId(), evaluationStep.getId(), curUser.getId());
		contentForegroundMap.clear();
		
		Supplier firstSupplier = null;
		EvaluationFactor firstEvaluationFactor = null;
		EvaluationFactorResult firstEvaluationFactorResult = null;
		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>();
			int evalPassNoCount = 0;
			int evalPassYesCount = 0;
			int deviateClauseCount = 0;
			for (EvaluationFactor evaluationFactor : evaluationFactorList) {
				
				// 获得已评审的结果
				EvaluationFactorResult evalFactorResult = null;
				if (null != memberEvaluateFactorResultList && memberEvaluateFactorResultList.size() > 0) {
					for (EvaluationFactorResult factorResult : memberEvaluateFactorResultList) {
						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());
				}
				if (null != evalFactorResult.getEvaluationResult() && evalFactorResult.getEvaluationResult().equals(EvaluationFactorEnum.UNCONFORM)) {
					if(EvaluationFactorEnum.CLAUSE_TYPE_DEVIATE.equals(evaluationFactor.getClauseType())){
						deviateClauseCount ++;
					}else {
						evalPassNoCount++;
					}
				}else if (null != evalFactorResult.getEvaluationResult() && evalFactorResult.getEvaluationResult().equals(EvaluationFactorEnum.CONFORM)) {
					evalPassYesCount++;
				}
				evaluationFactorResultMap.put(evaluationFactor.getId(), evalFactorResult);
				
				if (null == firstEvaluationFactor) {
					firstEvaluationFactor = evaluationFactor;
				}
				if (null == firstEvaluationFactorResult) {
					firstEvaluationFactorResult = evalFactorResult;
				}
			}
			
			if(null == expertEvaluationResult.getAccessment() || expertEvaluationResult.getAccessment().length() == 0){
				resultChanged = true;
				if (evalPassNoCount > 0) {
					expertEvaluationResult.setAccessment(EvaluationStepEnum.UNQUALIFIED);
				}else if (evalPassYesCount > 0) {
					expertEvaluationResult.setAccessment(EvaluationStepEnum.QUALIFIED);
				}
				if(unqualifiedDeviateClauseCount > 0 && deviateClauseCount >= unqualifiedDeviateClauseCount){
					expertEvaluationResult.setAccessment(EvaluationStepEnum.UNQUALIFIED);
				}
			}
			if (null == firstSupplier) {
				firstSupplier = supplier;
			}
			
			expertEvaluationResultMap.put(supplier.getId(), expertEvaluationResult);
			supplierEvaluationFactorResultMap.put(supplier.getId(), evaluationFactorResultMap);
			if (indexIsBidNumber && (null == supplier.getBidNumber() || "".equals(supplier.getBidNumber()))) {
				indexIsBidNumber = false;
			}
		}
		
		initSupplierList(new ArrayList<Supplier>(supplierList));
		
		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();
//		resultClickEventAction(firstSupplier, firstEvaluationFactor, firstEvaluationFactorResult);
	}
	
	/**
	 * @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);
		resultKTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		commKTableModel = new CommonKtableCModel(resultKTable, maxLevel, 1, evalSupplierList.size(), evaluationFactorList.size() + 1);
		commKTableModel.addRowSpan(0, 0, commKTableModel.getFixedRowCount());
		commKTableModel.addRowSpan(0, commKTableModel.getColCount() - 1, commKTableModel.getFixedRowCount());
		
		commKTableModel.setColumnMinWidth(0, 150);
		commKTableModel.setColumnWidth(commKTableModel.getColCount() - 1, allowEditConclusion ? 110 : 50);
		final Map<String, String> tableTitleMap = new HashMap<String, String>();
		for (int row = 0; row < maxLevel; row++) {
			int childCount = 0;
			String parentId = "";
			tableTitleMap.put(row + "_0", supplierName);
			tableTitleMap.put(row + "_" + (commKTableModel.getColCount() - 1), Messages.Conclusion);
			int col = commKTableModel.getFixedColumnCount();
			for (EvaluationFactor evaluationFactor : evaluationFactorList) {
				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, 150);
				commKTableModel.setColumnMinWidth(col, 150);
				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 {
					int supplierIndex = row - commKTableModel.getFixedRowCount();
					Supplier supplier = evalSupplierList.get(supplierIndex);
					if (col < commKTableModel.getFixedColumnCount()) {
						if (col == 0) {
							if (resultKTable.isLockedRow(row)) {
								return (indexIsBidNumber ? supplier.getBidNumber() : (supplierIndex + 1)) + "、" + supplier.getSupplierName() + "[已锁定]";
							} else {
								return (indexIsBidNumber ? supplier.getBidNumber() : (supplierIndex + 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());
						return evalFactorResult.getEvalPassTypeCN(evaluationFactor.getEvalContent());
					} else if (col == commKTableModel.getColCount() - 1) {
						ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultMap.get(supplier.getId());
						if (null != expertEvaluationResult && null != expertEvaluationResult.getAccessment()) {
							if (expertEvaluationResult.getAccessment().equals(EvaluationStepEnum.QUALIFIED)) {
								return EVAL_ACCESSMENT_YES;
							} else if (expertEvaluationResult.getAccessment().equals(EvaluationStepEnum.UNQUALIFIED)) {
								return EVAL_ACCESSMENT_NO;
							}
						}
					}
				}
				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, SWT.NORMAL));
					return textWrapCellRenderer;
				}
			}
		};
		commKTableModel.setCellRender(cellRender);
		
		// 设置单元格编辑器
		final KTableCellEditorGRadio radio = new KTableCellEditorGRadio();
		radio.setSingleClickEdit(true);
		radio.addItem(EvaluationFactorEnum.getEvalPassTypeCN(EvaluationFactorEnum.CONFORM), EvaluationFactorEnum.CONFORM);
		radio.addItem(EvaluationFactorEnum.getEvalPassTypeCN(EvaluationFactorEnum.UNCONFORM), EvaluationFactorEnum.UNCONFORM);
		CellEditorRender cellEditorRender = new CellEditorRender() {
			public Object receiveCellEditor(int row, int col) {
				if (row >= commKTableModel.getFixedRowCount()) {
					if (col >= commKTableModel.getFixedColumnCount() && col < commKTableModel.getFixedColumnCount() + evaluationFactorList.size()) {
						EvaluationFactor evaluationFactor = evaluationFactorList.get(col - commKTableModel.getFixedColumnCount());
						if (null != evaluationFactor && null != evaluationFactor.getEvalContent()) {
							String[] evalContent = evaluationFactor.getEvalContent().split(",");
							if (evalContent.length == 2) {
								radio.setItem(0, evalContent[0], EvaluationFactorEnum.CONFORM);
								radio.setItem(1, evalContent[1], EvaluationFactorEnum.UNCONFORM);
								return radio;
							}
						}

						radio.setItem(0, EvaluationFactorEnum.getEvalPassTypeCN(EvaluationFactorEnum.CONFORM), EvaluationFactorEnum.CONFORM);
						radio.setItem(1, EvaluationFactorEnum.getEvalPassTypeCN(EvaluationFactorEnum.UNCONFORM), EvaluationFactorEnum.UNCONFORM);
					
						return radio;
					} else if (allowEditConclusion && col == commKTableModel.getColCount() - 1) {
						radio.setItem(0, EVAL_ACCESSMENT_YES, EvaluationStepEnum.QUALIFIED);
						radio.setItem(1, EVAL_ACCESSMENT_NO, EvaluationStepEnum.UNQUALIFIED);
						return radio;
					}
				}
				return null;
			}
		};
		commKTableModel.setCellEditorRender(cellEditorRender);
		
		// 编辑单元格内容
		CellEditContentRender editContentRender = new CellEditContentRender() {
			public void receiveContent(int row, int col, Object content) {
				if (row >= commKTableModel.getFixedRowCount()) {
					if (null == content) return;
					resultChanged = true;
					if (col >= commKTableModel.getFixedColumnCount() && col < commKTableModel.getFixedColumnCount() + evaluationFactorList.size()) {
						Supplier supplier = evalSupplierList.get(row - commKTableModel.getFixedRowCount());
						EvaluationFactor evaluationFactor = evaluationFactorList.get(col - commKTableModel.getFixedColumnCount());
						boolean isDeviateClause = false;
						// 如果是偏性条款
						if(EvaluationFactorEnum.CLAUSE_TYPE_DEVIATE.equals(evaluationFactor.getClauseType())){
							isDeviateClause = true;
						}
						ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultMap.get(supplier.getId());
						Map<String, EvaluationFactorResult> supplierFactorResultMap = supplierEvaluationFactorResultMap.get(supplier.getId());
						EvaluationFactorResult evalFactorResult = supplierFactorResultMap.get(evaluationFactor.getId());
						evalFactorResult.setEvaluationResult(content.toString());
//						if(evalFactorResult.getEvaluationResult().equals(EvaluationFactorEnum.UNCONFORM)){
//							EvaluationOpinionInputDialog evaluationOpinionDialog = new EvaluationOpinionInputDialog(getShell(), evaluationFactor, evalFactorResult);
//							evaluationOpinionDialog.setTitle("请输入评审意见");
//							evaluationOpinionDialog.open();
//							evalFactorResult.setEvaluationOpinion(evaluationOpinionDialog.getEvaluationOpinion());
//						}
//						showFactorEvaluationOpinion(evalFactorResult);
						contentForegroundMap.put(evalFactorResult.getId(), null);
						
						boolean isUnqualified = false;
						int deviateClauseCount = 0;
						for (EvaluationFactor factor : evaluationFactorList) {
							EvaluationFactorResult evaluationFactorResult = supplierFactorResultMap.get(factor.getId());
							if (null != evaluationFactorResult) {
								if (EvaluationFactorEnum.UNCONFORM.equals(evaluationFactorResult.getEvaluationResult())) {
									if (EvaluationFactorEnum.CLAUSE_TYPE_DEVIATE.equals(factor.getClauseType())) {
										deviateClauseCount++;
									} else {
										isUnqualified = true;
										break;
									}
								}
							}
						}
						if(!isUnqualified  && unqualifiedDeviateClauseCount > 0 && deviateClauseCount >= unqualifiedDeviateClauseCount){
							isUnqualified = true;
						}
						expertEvaluationResult.setAccessment(isUnqualified ? EvaluationStepEnum.UNQUALIFIED : EvaluationStepEnum.QUALIFIED);
						
 						if (EvaluationFactorEnum.UNCONFORM.equals(evalFactorResult.getEvaluationResult()) && isUnqualified) {
							EvaluationOpinionDialog dialog = new EvaluationOpinionDialog(getShell(), tenderProject, supplier, Constants.USER, expertEvaluationResult);
							dialog.setTitle(supplier.getSupplierName() +" - "+(deviateClauseCount > unqualifiedDeviateClauseCount && unqualifiedDeviateClauseCount > 0? ("偏离条款超过"+unqualifiedDeviateClauseCount+Messages.Number_2):"" )+"否决投标");
							dialog.open();
//							evalFactorResult.setEvaluationOpinion(expertEvaluationResult.getSupplierOpinion());
//							showFactorEvaluationOpinion(evalFactorResult);
						}
 					// 如果是客观项，检查所有评标专家的评标结果是否相同
						if (isDeviateClause || EvaluationFactorEnum.OBJECTIVE_YES.equals(evaluationFactor.getObjective())) {
							refreshObjectiveBackgroundMap();
						}else {
							resultKTable.redraw();
						}
					} else if (allowEditConclusion && col == commKTableModel.getColCount() - 1) {
						Supplier supplier = evalSupplierList.get(row - commKTableModel.getFixedRowCount());
						ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultMap.get(supplier.getId());
						if (null != expertEvaluationResult) {
							if (content.toString().equals(EvaluationStepEnum.UNQUALIFIED)) {
								expertEvaluationResult.setAccessment(EvaluationStepEnum.UNQUALIFIED);
								EvaluationOpinionDialog dialog = new EvaluationOpinionDialog(getShell(), tenderProject, supplier, Constants.USER, expertEvaluationResult);
								dialog.open();
							} else if (content.toString().equals(EvaluationStepEnum.QUALIFIED)) {
								expertEvaluationResult.setAccessment(EvaluationStepEnum.QUALIFIED);
							}
						}
					}
				}
			}
		};
		commKTableModel.setCellEditContentRender(editContentRender);
		
		// 编辑单元格对齐方式
		CellAlignmentRender cellAlignmentRender = new CellAlignmentRender() {
			public int receiveCellAlignment(int row, int col) {
				if (col < commKTableModel.getFixedColumnCount() && row >= commKTableModel.getFixedRowCount()) {
					if (col == 0) {
						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 (row >= commKTableModel.getFixedRowCount()) {
					Supplier supplier = evalSupplierList.get(row - commKTableModel.getFixedRowCount());
					ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultMap.get(supplier.getId());
					if (col >= commKTableModel.getFixedColumnCount() && col < commKTableModel.getFixedColumnCount() + evaluationFactorList.size()) {
						EvaluationFactor evaluationFactor = evaluationFactorList.get(col - commKTableModel.getFixedColumnCount());
						EvaluationFactorResult evalFactorResult = supplierEvaluationFactorResultMap.get(supplier.getId()).get(evaluationFactor.getId());
						Color foreColor = contentForegroundMap.get(evalFactorResult.getId());
						if (null != foreColor) {
							return foreColor;
						} else {
							if (ObjectUtils.equals(evalFactorResult.getEvaluationResult(), EvaluationFactorEnum.CONFORM)) {
								return SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN);
							} else if (ObjectUtils.equals(evalFactorResult.getEvaluationResult(), EvaluationFactorEnum.UNCONFORM)) {
								return SWTResourceManager.getColor(SWT.COLOR_RED);
							}
						}
					} else if (col == commKTableModel.getColCount() - 1) {
						if (null != expertEvaluationResult && null != expertEvaluationResult.getAccessment()) {
							if (expertEvaluationResult.getAccessment().equals(EvaluationStepEnum.QUALIFIED)) {
								return SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN);
							} else if (expertEvaluationResult.getAccessment().equals(EvaluationStepEnum.UNQUALIFIED)) {
								return SWTResourceManager.getColor(SWT.COLOR_RED);
							}
						}
					}
				}
				return null;
			}
		};
		commKTableModel.setCellForegroundRender(cellForegroundRender);
		
		// 设置单元格背景颜色
		CellBackgroundRender cellBackgroundRender = new CellBackgroundRender() {
			public Color receiveCellBackground(int row, int col) {
				if (row >= commKTableModel.getFixedRowCount() && col >= commKTableModel.getFixedColumnCount() && col < commKTableModel.getFixedColumnCount() + evaluationFactorList.size()) {
					Supplier supplier = evalSupplierList.get(row - commKTableModel.getFixedRowCount());
					EvaluationFactor evaluationFactor = evaluationFactorList.get(col - commKTableModel.getFixedColumnCount());
//					ExpertEvaluationResult expertEvaluationResult  = expertEvaluationResultMap.get(supplier.getId());
					if (EvaluationFactorEnum.OBJECTIVE_YES.equals(evaluationFactor.getObjective())) {
						Color bgColor = objectiveBackgroundMap.get(new Keys(supplier.getId(), evaluationFactor.getId()));
						if (null != bgColor) {
							return bgColor;
						}
					}
				}
				return null;
			}
		};
		commKTableModel.setCellBackgroundRender(cellBackgroundRender);
		// 设置单元格提示内容
		CellTooltipRender cellTooltipRender = new CellTooltipRender() {
			public String receiveCellTooltip(int row, int col) {
				Supplier supplier = null;
				if (row >= commKTableModel.getFixedRowCount()) {
					supplier = evalSupplierList.get(row - commKTableModel.getFixedRowCount());
				}
				if (col >= commKTableModel.getFixedColumnCount() && col < commKTableModel.getFixedColumnCount() + evaluationFactorList.size()) {
					EvaluationFactor evaluationFactor = evaluationFactorList.get(col - commKTableModel.getFixedColumnCount());
					if (null != supplier) {
						EvaluationFactorResult evalFactorResult = supplierEvaluationFactorResultMap.get(supplier.getId()).get(evaluationFactor.getId());
						if (null != evalFactorResult && null != evalFactorResult.getEvaluationOpinion()) {
							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 = evalSupplierList.get(row - commKTableModel.getFixedRowCount());
//					expertEvaluationResult  = expertEvaluationResultMap.get(supplier.getId());
				}
				if (col >= commKTableModel.getFixedColumnCount() && col < commKTableModel.getFixedColumnCount() + evaluationFactorList.size()) {
					evaluationFactor = evaluationFactorList.get(col - commKTableModel.getFixedColumnCount());
				}
				
				openOrCloseEvaluationTips(evaluationFactor, "实质性要求", " 提示：技术规范书中标记‘*’的项目为重要参数，不允许有偏差，否则否决其投标，请专家仔细核对。！");
				
				evaluationTableComposite.layout();
				
				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 = evalSupplierList.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 chenxw
	 * @param evaluationFactor
	 * @param factorName
	 * @param content 
	 */
	private void openOrCloseEvaluationTips(EvaluationFactor evaluationFactor, String factorName, String content) {
		if (null != evaluationFactor && evaluationFactor.getFactorName().equals(factorName)) {
			if (!iKnowTips && (null == tipsComposite || tipsComposite.isDisposed())) {
				tipsComposite = new Composite(evaluationTableComposite, SWT.NONE);
				tipsComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_RED));
				GridLayout gl_tipsComposite = new GridLayout(2, false);
				gl_tipsComposite.marginHeight = 0;
				gl_tipsComposite.marginWidth = 0;
				tipsComposite.setLayout(gl_tipsComposite);
				tipsComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
				
				Label evaluationTipsLabel = new Label(tipsComposite, SWT.NONE);
				evaluationTipsLabel.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.BOLD));
				evaluationTipsLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
				evaluationTipsLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1));
				evaluationTipsLabel.setText(content);
				
				CLabel label_1 = new CLabel(tipsComposite, SWT.NONE);
				label_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
				label_1.setText(Messages.Button_iKnown);
				label_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
				label_1.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
				label_1.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/white/close.png"));
				label_1.addMouseListener(new MouseAdapter() {
					public void mouseUp(MouseEvent e) {
						tipsComposite.dispose();
						evaluationTableComposite.layout();
						iKnowTips = true;
					}
				});
			}
		} else {
			if (null != tipsComposite) tipsComposite.dispose();
		}
	}

	/**
	 * @Description: 左边是评审指标表格
	 * @author guoyr
	 */
	private void drawLeftFactorEvaluationTable() {
		
		if (null != resultKTable && !resultKTable.isDisposed()) {
			resultKTable.dispose();
		}
		final Map<String, String> tableTitleMap = new HashMap<String, String>();
		resultKTable = new KTable(evaluationTableComposite, SWTX.AUTO_SCROLL | SWT.FULL_SELECTION | SWTX.EDIT_ON_KEY);
		resultKTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		commKTableModel = new CommonKtableCModel(resultKTable, 1, maxLevel, evaluationFactorList.size() + 1, evalSupplierList.size());
		tableTitleMap.put("0_0", Messages.EvalFactor);
		commKTableModel.addColSpan(0, 0, commKTableModel.getFixedColumnCount());
		commKTableModel.addColSpan(commKTableModel.getRowCount() - 1, 0, commKTableModel.getFixedColumnCount());
		
		int col = commKTableModel.getFixedColumnCount();
		for (Supplier supplier : evalSupplierList) {
			tableTitleMap.put("0_" + col, supplier.getSupplierName());
			commKTableModel.setColumnWidth(col, 110);
			col++;
		}
		
		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() && row < commKTableModel.getFixedRowCount() + evaluationFactorList.size()) {
						if (col == commKTableModel.getFixedColumnCount() - 1) {
							EvaluationFactor evaluationFactor = evaluationFactorList.get(row - commKTableModel.getFixedRowCount());
							if (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);
						}
					} else if (row == commKTableModel.getRowCount() - 1) {
						return Messages.Conclusion;
					}
					return "";
				} else if (col < commKTableModel.getFixedColumnCount() + evalSupplierList.size()) {
					Supplier supplier = evalSupplierList.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() && row < commKTableModel.getFixedRowCount() + evaluationFactorList.size()) {
						EvaluationFactor evaluationFactor = evaluationFactorList.get(row - commKTableModel.getFixedRowCount());
						EvaluationFactorResult evalFactorResult = supplierEvaluationFactorResultMap.get(supplier.getId()).get(evaluationFactor.getId());
						if (null != evalFactorResult) {
							return evalFactorResult.getEvalPassTypeCN(null != evaluationFactor ? evaluationFactor.getEvalContent() : null);
						}
					} else if (row == commKTableModel.getRowCount() - 1) {
						ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultMap.get(supplier.getId());
						if (null != expertEvaluationResult && null != expertEvaluationResult.getAccessment()) {
							if (expertEvaluationResult.getAccessment().equals(EvaluationStepEnum.QUALIFIED)) {
								return EVAL_ACCESSMENT_YES;
							} else if (expertEvaluationResult.getAccessment().equals(EvaluationStepEnum.UNQUALIFIED)) {
								return EVAL_ACCESSMENT_NO;
							}
						}
					}
				}
				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, SWT.NORMAL));
					return textWrapCellRenderer;
				}
			}
		};
		commKTableModel.setCellRender(cellRender);
		
		// 设置单元格编辑器
		final KTableCellEditorGRadio radio = new KTableCellEditorGRadio();
		radio.addItem(EvaluationFactorEnum.getEvalPassTypeCN(EvaluationFactorEnum.CONFORM), EvaluationFactorEnum.CONFORM);
		radio.addItem(EvaluationFactorEnum.getEvalPassTypeCN(EvaluationFactorEnum.UNCONFORM), EvaluationFactorEnum.UNCONFORM);
		radio.setSingleClickEdit(true);
		CellEditorRender cellEditorRender = new CellEditorRender() {
			public Object receiveCellEditor(int row, int col) {
				if (col >= commKTableModel.getFixedColumnCount()) {
					if (row >= commKTableModel.getFixedRowCount() && row < commKTableModel.getFixedRowCount() + evaluationFactorList.size()) {
						EvaluationFactor evaluationFactor = evaluationFactorList.get(row - commKTableModel.getFixedRowCount());
						if (null != evaluationFactor && null != evaluationFactor.getEvalContent()) {
							String[] evalContent = evaluationFactor.getEvalContent().split(",");
							if (evalContent.length == 2) {
								radio.setItem(0, evalContent[0], EvaluationFactorEnum.CONFORM);
								radio.setItem(1, evalContent[1], EvaluationFactorEnum.UNCONFORM);
								return radio;
							}
						} 
						radio.setItem(0, EvaluationFactorEnum.getEvalPassTypeCN(EvaluationFactorEnum.CONFORM), EvaluationFactorEnum.CONFORM);
						radio.setItem(1, EvaluationFactorEnum.getEvalPassTypeCN(EvaluationFactorEnum.UNCONFORM), EvaluationFactorEnum.UNCONFORM);
						return radio;
					} else if (allowEditConclusion && row == commKTableModel.getRowCount() - 1) {
						radio.setItem(0, EVAL_ACCESSMENT_YES, EvaluationStepEnum.QUALIFIED);
						radio.setItem(1, EVAL_ACCESSMENT_NO, EvaluationStepEnum.UNQUALIFIED);
						return radio;
					}
				}
				return null;
			}
		};
		commKTableModel.setCellEditorRender(cellEditorRender);
		
		// 编辑单元格内容
		CellEditContentRender editContentRender = new CellEditContentRender() {
			public void receiveContent(int row, int col, Object content) {
					if (col >= commKTableModel.getFixedColumnCount()) {
					if (null == content) return;
					resultChanged = true;
					Supplier supplier = evalSupplierList.get(col - commKTableModel.getFixedColumnCount());
					if (row >= commKTableModel.getFixedRowCount() && row < commKTableModel.getFixedRowCount() + evaluationFactorList.size()) {
						EvaluationFactor evaluationFactor = evaluationFactorList.get(row - commKTableModel.getFixedRowCount());
						boolean isDeviateClause = false;
						// 如果是偏性条款
						if(EvaluationFactorEnum.CLAUSE_TYPE_DEVIATE.equals(evaluationFactor.getClauseType())){
							isDeviateClause = true;
						}
						ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultMap.get(supplier.getId());
						Map<String, EvaluationFactorResult> supplierFactorResultMap = supplierEvaluationFactorResultMap.get(supplier.getId());
						EvaluationFactorResult evalFactorResult = supplierFactorResultMap.get(evaluationFactor.getId());
						if(EvaluationFactorEnum.CONFORM.equals(content.toString())) {
							evalFactorResult.setEvaluationResult(content.toString());
						}						
//						if(evalFactorResult.getEvaluationResult().equals(EvaluationFactorEnum.UNCONFORM)){
//							EvaluationOpinionInputDialog evaluationOpinionDialog = new EvaluationOpinionInputDialog(getShell(), evaluationFactor, evalFactorResult);
//							evaluationOpinionDialog.setTitle("请输入评审意见");
//							evaluationOpinionDialog.open();
//							evalFactorResult.setEvaluationOpinion(evaluationOpinionDialog.getEvaluationOpinion());
//						}
						
//						showFactorEvaluationOpinion(evalFactorResult);
						contentForegroundMap.put(evalFactorResult.getId(), null);
						
						boolean isUnqualified = false;
						int deviateClauseCount = 0;
						for (EvaluationFactor factor : evaluationFactorList) {
							EvaluationFactorResult evaluationFactorResult = supplierFactorResultMap.get(factor.getId());
							if (null != evaluationFactorResult) {
								if (EvaluationFactorEnum.UNCONFORM.equals(evaluationFactorResult.getEvaluationResult())) {
									if (EvaluationFactorEnum.CLAUSE_TYPE_DEVIATE.equals(factor.getClauseType())) {
										deviateClauseCount++;
									} else {
										isUnqualified = true;
										break;
									}
								}
							}
						}
						if(!isUnqualified && unqualifiedDeviateClauseCount > 0 && deviateClauseCount >= unqualifiedDeviateClauseCount){
							isUnqualified = true;
						}
						expertEvaluationResult.setAccessment(isUnqualified ? EvaluationStepEnum.UNQUALIFIED : EvaluationStepEnum.QUALIFIED);
						
						//如果是合格的，意见不是空
//						boolean changeOpinion = false;
//						changeOpinion = !StringUtils.isNotEmpty(evalFactorResult.getEvaluationOpinion()) && EvaluationFactorEnum.CONFORM.equals(evalFactorResult.getEvaluationResult());
						
//						if ((EvaluationFactorEnum.UNCONFORM.equals(evalFactorResult.getEvaluationResult()) && isUnqualified) || changeOpinion) {
//						if ((EvaluationFactorEnum.UNCONFORM.equals(evalFactorResult.getEvaluationResult()) && isUnqualified)) {
						if (EvaluationFactorEnum.UNCONFORM.equals(content.toString())) {
							
							EvaluationOpinionDialog dialog = new EvaluationOpinionDialog(getShell(), tenderProject, supplier, Constants.USER, expertEvaluationResult,new CallBack() {
								public void callBackBoolean(final boolean isEmpyt) {
									if(isEmpyt) {
										evalFactorResult.setEvaluationResult(content.toString());
										expertEvaluationResult.setAccessment(EvaluationStepEnum.UNQUALIFIED);
										
									}
								}
							});
//							EvaluationOpinionDialog dialog = new EvaluationOpinionDialog(getShell(), tenderProject, supplier, Constants.USER, expertEvaluationResult);
							dialog.setTitle(supplier.getSupplierName() +" - "+(deviateClauseCount > unqualifiedDeviateClauseCount && unqualifiedDeviateClauseCount > 0? ("偏离条款超过"+unqualifiedDeviateClauseCount+Messages.Number_2):"" )+"否决投标");
							dialog.open();
							
							
							//evalFactorResult.setEvaluationOpinion(expertEvaluationResult.getSupplierOpinion());
							showFactorEvaluationOpinion(evalFactorResult);
						}
						// 如果是客观项，检查所有评标专家的评标结果是否相同
						if (isDeviateClause || EvaluationFactorEnum.OBJECTIVE_YES.equals(evaluationFactor.getObjective())) {
							refreshObjectiveBackgroundMap();
						}else {
							resultKTable.redraw();
						}
					} else if (allowEditConclusion && row == commKTableModel.getRowCount() - 1) {
						ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultMap.get(supplier.getId());
						if (null != expertEvaluationResult) {
							if (content.toString().equals(EvaluationStepEnum.UNQUALIFIED)) {
								expertEvaluationResult.setAccessment(EvaluationStepEnum.UNQUALIFIED);
								EvaluationOpinionDialog dialog = new EvaluationOpinionDialog(getShell(), tenderProject, supplier, Constants.USER, expertEvaluationResult);
								dialog.open();
							} else if (content.toString().equals(EvaluationStepEnum.QUALIFIED)) {
								expertEvaluationResult.setAccessment(EvaluationStepEnum.QUALIFIED);
							}
						}
					}
				}
			}
		};
		commKTableModel.setCellEditContentRender(editContentRender);
		
		// 编辑单元格对齐方式
		CellAlignmentRender cellAlignmentRender = new CellAlignmentRender() {
			public int receiveCellAlignment(int row, int col) {
				if (col < commKTableModel.getFixedColumnCount() && row >= commKTableModel.getFixedRowCount()) {
					if (row == commKTableModel.getRowCount() - 1) {
						return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
					} else {
						return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_LEFT;
					}
				}
				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() + evalSupplierList.size() && row >= commKTableModel.getFixedRowCount()) {
					Supplier supplier = evalSupplierList.get(col - commKTableModel.getFixedColumnCount());
					if (row < commKTableModel.getFixedRowCount() + evaluationFactorList.size()) {
						EvaluationFactor evaluationFactor = evaluationFactorList.get(row - commKTableModel.getFixedRowCount());
						EvaluationFactorResult evalFactorResult = supplierEvaluationFactorResultMap.get(supplier.getId()).get(evaluationFactor.getId());
						Color foreColor = contentForegroundMap.get(evalFactorResult.getId());
						if (null != foreColor) {
							return foreColor;
						} else {
							if (ObjectUtils.equals(evalFactorResult.getEvaluationResult(), EvaluationFactorEnum.CONFORM)) {
								return SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN);
							} else if (ObjectUtils.equals(evalFactorResult.getEvaluationResult(), EvaluationFactorEnum.UNCONFORM)) {
								return SWTResourceManager.getColor(SWT.COLOR_RED);
							}
						}
					} else if (row == commKTableModel.getRowCount() - 1) {
						ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultMap.get(supplier.getId());
						if (null != expertEvaluationResult && null != expertEvaluationResult.getAccessment()) {
							if (expertEvaluationResult.getAccessment().equals(EvaluationStepEnum.QUALIFIED)) {
								return SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN);
							} else if (expertEvaluationResult.getAccessment().equals(EvaluationStepEnum.UNQUALIFIED)) {
								return SWTResourceManager.getColor(SWT.COLOR_RED);
							}
						}
					}
				}
				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() + evaluationFactorList.size()) {
					Supplier supplier = evalSupplierList.get(col - commKTableModel.getFixedColumnCount());
					EvaluationFactor evaluationFactor = evaluationFactorList.get(row - commKTableModel.getFixedRowCount());
//					ExpertEvaluationResult expertEvaluationResult  = expertEvaluationResultMap.get(supplier.getId());
					if (EvaluationFactorEnum.OBJECTIVE_YES.equals(evaluationFactor.getObjective())) {
						Color bgColor = objectiveBackgroundMap.get(new Keys(supplier.getId(), evaluationFactor.getId()));
						if (null != bgColor) {
							return bgColor;
						}
					}
				}
				return null;
			}
		};
		commKTableModel.setCellBackgroundRender(cellBackgroundRender);
		
		// 设置单元格内容
		CellTooltipRender cellTooltipRender = new CellTooltipRender() {
			public String receiveCellTooltip(int row, int col) {
				Supplier supplier = null;
				if (col >= commKTableModel.getFixedColumnCount()) {
					supplier = evalSupplierList.get(col - commKTableModel.getFixedColumnCount());
				}
				if (row >= commKTableModel.getFixedRowCount() && row < commKTableModel.getFixedRowCount() + evaluationFactorList.size()) {
					EvaluationFactor evaluationFactor = evaluationFactorList.get(row - commKTableModel.getFixedRowCount());
					if (null != supplier) {
						EvaluationFactorResult evalFactorResult = supplierEvaluationFactorResultMap.get(supplier.getId()).get(evaluationFactor.getId());
						if (null != evalFactorResult && null != evalFactorResult.getEvaluationOpinion()) {
							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 = evalSupplierList.get(col - commKTableModel.getFixedColumnCount());
				}
				if (row >= commKTableModel.getFixedRowCount() && row < commKTableModel.getFixedRowCount() + evaluationFactorList.size()) {
					evaluationFactor = evaluationFactorList.get(row - commKTableModel.getFixedRowCount());
				}
				
				openOrCloseEvaluationTips(evaluationFactor, "实质性要求", " 提示：技术规范书中标记‘*’的项目为重要参数，不允许有偏差，否则否决其投标，请专家仔细核对。！");
				
				evaluationTableComposite.layout();
				
				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 = evalSupplierList.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 = evalSupplierList.get(col - commKTableModel.getFixedColumnCount());
					expertEvaluationResult = expertEvaluationResultMap.get(supplier.getId());
				}
				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());
				}
				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) {
			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) {
			evalFactorResult.setEvaluationOpinion(factor_evaluation_opinion.getText().trim());
		}
	}
	
	/**
	 * @Description: 全部符合
	 * @author guoyr
	 */
	public void initDefaultFactorResult() {
		if (!MessageDialog.openConfirm(getShell(), MessageFormat.format(Messages.Tips_setQuickEvaluation, EvaluationFactorEnum.getEvalPassTypeCN(EvaluationFactorEnum.CONFORM)))) {
			return;
		}
		resultKTable.closeCellEditor();
		
		for (Supplier supplier : evalSupplierList) {
			ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultMap.get(supplier.getId());
			// 当前供应商的所有评审结果
			Map<String, EvaluationFactorResult> supplierFactorResultMap = supplierEvaluationFactorResultMap.get(supplier.getId());
			int evalPassNoCount = 0;
			int deviateClauseCount = 0;
			for (EvaluationFactor evaluationFactor : evaluationFactorList) {
				EvaluationFactorResult evaluationFactorResult = supplierFactorResultMap.get(evaluationFactor.getId());
				if (null == evaluationFactorResult.getEvaluationResult()) {
					evaluationFactorResult.setEvaluationResult(EvaluationFactorEnum.CONFORM);
					contentForegroundMap.put(evaluationFactorResult.getId(), importColor);// 设置为非自己录入的颜色
					resultChanged = true;
				}
				if (null != evaluationFactorResult.getEvaluationResult() && evaluationFactorResult.getEvaluationResult().equals(EvaluationFactorEnum.UNCONFORM)) {
					if(EvaluationFactorEnum.CLAUSE_TYPE_DEVIATE.equals(evaluationFactor.getClauseType())){
						deviateClauseCount ++;
					}else {
						evalPassNoCount++;
					}
				}
			}
			if (evalPassNoCount > 0) {
				expertEvaluationResult.setAccessment(EvaluationStepEnum.UNQUALIFIED);
			} else {
				expertEvaluationResult.setAccessment(EvaluationStepEnum.QUALIFIED);
				if(unqualifiedDeviateClauseCount > 0 && deviateClauseCount >= unqualifiedDeviateClauseCount){
					expertEvaluationResult.setAccessment(EvaluationStepEnum.UNQUALIFIED);
				}
			}
		}
		resultKTable.redraw();
		refreshObjectiveBackgroundMap();
	}
	
	private ImportEvaluationResultListener createImportEvaluationResultListener() {
		return new ImportEvaluationResultListener() {
			public void importEvalResult(Map<String, Map<String, EvaluationFactorResult>> importSupplierResult) {
				for (Supplier supplier : evalSupplierList) {
					ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultMap.get(supplier.getId());
					// 当前供应商的所有评审结果
					Map<String, EvaluationFactorResult> supplierFactorResultMap = supplierEvaluationFactorResultMap.get(supplier.getId());
					Map<String, EvaluationFactorResult> supplierSupplierResultMap = importSupplierResult.get(supplier.getId());
					
					int importCount = 0;
					int evalPassNoCount = 0;
					int deviateClauseCount = 0;
					for (EvaluationFactor evaluationFactor : evaluationFactorList) {
						EvaluationFactorResult evaluationFactorResult = supplierFactorResultMap.get(evaluationFactor.getId());
						if (null != evaluationFactorResult) {
							if (null != supplierSupplierResultMap) {
								EvaluationFactorResult importEvaluationResult = supplierSupplierResultMap.get(evaluationFactor.getId());
								if (null != importEvaluationResult && null != importEvaluationResult.getEvaluationResult()) {
									evaluationFactorResult.setEvaluationResult(importEvaluationResult.getEvaluationResult());
									evaluationFactorResult.setEvaluationOpinion(importEvaluationResult.getEvaluationOpinion());
									// 设置为非自己录入的颜色
									contentForegroundMap.put(evaluationFactorResult.getId(), importColor);
									resultChanged = true;
								}
							}
							if (null != evaluationFactorResult.getEvaluationResult() && evaluationFactorResult.getEvaluationResult().equals(EvaluationFactorEnum.UNCONFORM)) {
								if(EvaluationFactorEnum.CLAUSE_TYPE_DEVIATE.equals(evaluationFactor.getClauseType())){
									deviateClauseCount ++;
								}else {
									evalPassNoCount++;
								}
							}
							importCount++;
						}
					}
					if (evalPassNoCount > 0) {
						expertEvaluationResult.setAccessment(EvaluationStepEnum.UNQUALIFIED);
					} else if (importCount > 0) {
						expertEvaluationResult.setAccessment(EvaluationStepEnum.QUALIFIED);
						if(unqualifiedDeviateClauseCount > 0 && deviateClauseCount >= unqualifiedDeviateClauseCount){
							expertEvaluationResult.setAccessment(EvaluationStepEnum.UNQUALIFIED);
						}
					}
				}
				resultChanged = true;
				refreshObjectiveBackgroundMap();
			}
		};
	}
	
	/**
	 * @Description: 导入其他专家的评审结果
	 * @author guoyr
	 */
	public void importOtherExpertResult() {
		resultKTable.closeCellEditor();
		ImportOtherExpertEvaluationResultDialog dialog = new ImportOtherExpertEvaluationResultDialog(getShell(), tenderProject, evaluationStep, evalSupplierList, evaluationFactorList, createImportEvaluationResultListener());
		dialog.setTitle("导入其他专家的评审结果");
		dialog.open();
	}
	
	/**
	 * @Description: 导入历史评审结果
	 * @author guoyr
	 */
	public void importHistoryResult() {
		resultKTable.closeCellEditor();
		ImportHistoryScoreResultDialog dialog = new ImportHistoryScoreResultDialog(getShell(), tenderProject, evaluationStep, evalSupplierList, evaluationFactorList, createImportEvaluationResultListener());
		dialog.setTitle("导入历史评审结果");
		dialog.open();
	}
	
	/**
	 * @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() {
		boolean saveSuccess = true;
		// 如果修改了结果，才保存结果
		if (resultChanged) {
			if(null == expertEvaluationStepService){
				expertEvaluationStepService = new ExpertEvaluationStepService();
			}
			saveSuccess = expertEvaluationStepService.saveEvaluationResult(tenderProject.getId(), curUser.getId(), evaluationStep, expertEvaluationResultMap, supplierEvaluationFactorResultMap);
			if (saveSuccess) {
				resultChanged = false;
			} else {
				resultChanged = true;
			}
		}
		refreshObjectiveBackgroundMap();
		return saveSuccess;
	}
	
	/**
	 * @Description: 检查是否可以提交
	 * @author guoyr
	 * @return
	 */
	public String checkSubmit() {
		for (Supplier supplier : evalSupplierList) {
			Map<String, EvaluationFactorResult> supplierFactorResultMap = supplierEvaluationFactorResultMap.get(supplier.getId());
			for (EvaluationFactor evaluationFactor : evaluationFactorList) {
				EvaluationFactorResult evaluationFactorResult = supplierFactorResultMap.get(evaluationFactor.getId());
				if (null == evaluationFactorResult.getEvaluationResult()) {
					return MessageFormat.format(Messages.Tips_existNoEvaluationFactor, 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);
				String evaluationResult = null;
				for (EvaluationFactorResult evaluationFactorResult : factorResultList) {
					if(objectiveFactorIdMap.containsKey(evaluationFactorResult.getFactorId())){
						String result = evaluationFactorResult.getEvaluationResult();
						if (result == null) {
							return "还有专家未对一致性评审项进行评审，请等待其他专家保存评审结果，并验证一致性评审项的评审结果一致时才能进行此操作！";
						}
						if (evaluationResult == null) {
							evaluationResult = result;
						} else if (!evaluationResult.equals(result)) {
							return "您与其他专家存在一致性评审项的评审结果不一致，待所有专家对一致性评审项的评审结果一致时才能进行此操作！";
						}
					}
				}
			}
		}
		return null;
	}
	
	/**
     * @Description: 检查是否刷新
     * @author chenxw
     * @return
     */
    public boolean checkRefresh() {
        //没有替补的供应商时不刷新
        if (notEvalSupplierList.isEmpty()) {
            return false;
        }
        //通过供应商的数量
        int conformSupplierCount = 0;
        for (Supplier supplier : evalSupplierList) {
            ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultMap.get(supplier.getId());
            if (EvaluationStepEnum.QUALIFIED.equals(expertEvaluationResult.getAccessment())) {
                conformSupplierCount++;
            }
        }
        //通过的数量等于要求通过的数量时不刷新
        if (conformSupplierCount == mustConformSupplierCount) {
            return false;
        }
        if (conformSupplierCount < mustConformSupplierCount) {
            int addSupplierCount = mustConformSupplierCount - conformSupplierCount;
            MessageDialog.openWarning(getShell(), "您当前评审合格的供应商数不足" + mustConformSupplierCount + "家，" +
                    "将从评标价排名前" + mustConformSupplierCount + "家中自动替补" + addSupplierCount + "家进行评审！");
        }
        if (conformSupplierCount > mustConformSupplierCount) {
            MessageDialog.openWarning(getShell(), "您仅能对评标价排名前" + mustConformSupplierCount + "家的供应商进行评审！");
        }
        return true;
    }
	
	/**
	 * @Description: 提交评审结果
	 * @author guoyr
	 * @return
	 */
	public boolean submitEvaluationFactorResult() {
	    if (!notEvalSupplierList.isEmpty()) {
            for (Supplier supplier : notEvalSupplierList) {
                ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultMap.get(supplier.getId());
                if (null != expertEvaluationResult) {
                    expertEvaluationResult.setAccessment(EvaluationStepEnum.UNQUALIFIED);
                    resultChanged = true;
                }
            }
            saveEvaluationFactorResult();
            evalSupplierList.addAll(notEvalSupplierList);
        }
		return expertEvaluationStepService.submitEvaluationResult(tenderProject.getId(), projectRule, curUser.getId(), evaluationStep, evalSupplierList);
	}
	
	/**
	 * @Description: 释放资源
	 * @author guoyr
	 */
	public void dispose() {
		imageResolver.disposite();
		evaluationFactorList.clear();
		evaluationFactorList = null;
		super.dispose();
	}
	
	@Override
	protected void checkSubclass() {}
	
	/**
	 * @Description: 设置评审结论
	 * @author guoyr
	 */
	private void setConclusion(String supplierId) {
		ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultMap.get(supplierId);
		Map<String, EvaluationFactorResult> supplierFactorResultMap = supplierEvaluationFactorResultMap.get(supplierId);
		int evalPassNoCount = 0;
		int deviateClauseCount = 0;
		for (EvaluationFactor evaluationFactor : evaluationFactorList) {
			EvaluationFactorResult evaluationFactorResult = supplierFactorResultMap.get(evaluationFactor.getId());
			
			if (null != evaluationFactorResult.getEvaluationResult() && evaluationFactorResult.getEvaluationResult().equals(EvaluationFactorEnum.UNCONFORM)) {
				if(EvaluationFactorEnum.CLAUSE_TYPE_DEVIATE.equals(evaluationFactor.getClauseType())){
					deviateClauseCount ++;
				}else {
					evalPassNoCount++;
				}
			}
		}
		if (evalPassNoCount > 0) {
			expertEvaluationResult.setAccessment(EvaluationStepEnum.UNQUALIFIED);
		} else {
			expertEvaluationResult.setAccessment(EvaluationStepEnum.QUALIFIED);
			if(unqualifiedDeviateClauseCount > 0 && deviateClauseCount >= unqualifiedDeviateClauseCount){
				expertEvaluationResult.setAccessment(EvaluationStepEnum.UNQUALIFIED);
			}
		}
	}
}
