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

import java.io.File;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.eclipse.swt.SWT;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

import 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.button.EvalLeftButton;
import com.gx.obe.common.widget.button.EvaluationButton;
import com.gx.obe.common.widget.composite.TipsComposite;
import com.gx.obe.common.widget.thread.MyRunnable;
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.evaluation.step.listener.ExpertEvaluationListener;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.UuidUtils;
import com.gx.obe.util.utils.XmlUtils;
import com.gx.obe.web.entity.CheckResult;
import com.gx.obe.web.entity.DictionaryValue;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.expert.Expert;
import com.gx.obe.web.entity.expert.ExpertEvaluationResult;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;
import com.gx.obe.web.listener.WebListener;
import com.gx.obe.web.service.BaseDictionaryValueService;
import com.gx.obe.web.service.SupplierService;
import com.gx.obe.web.service.TenderProjectService;
import com.gx.obe.web.service.evaluation.EvaluationStepService;
import com.gx.obe.web.service.expert.ExpertEvaluationStepService;
import com.gx.utils.ActionLogUtils;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

import de.kupzog.ktable.KTable;
import de.kupzog.ktable.SWTX;
import de.kupzog.ktable.editors.KTableCellEditorCheckbox2;
import de.kupzog.ktable.editors.KTableCellEditorMultilineWrapText;
import de.kupzog.ktable.model.CommonKtableCModel;
import de.kupzog.ktable.renderers.CellAlignmentRender;
import de.kupzog.ktable.renderers.CellContentRender;
import de.kupzog.ktable.renderers.CellEditContentRender;
import de.kupzog.ktable.renderers.CellEditorRender;
import de.kupzog.ktable.renderers.CellForegroundRender;
import de.kupzog.ktable.renderers.CellRender;
import de.kupzog.ktable.renderers.CheckableCellRenderer;
import de.kupzog.ktable.renderers.FixedWrapCellRenderer;
import de.kupzog.ktable.renderers.TextWrapCellRenderer;

/** 
 * @Description: 特征码检查
 * @author guoyr 
 */
public class FeatureCodeCheckComposite extends Composite {
	{
		LogUtils.logClass(FeatureCodeCheckComposite.class);
	}
	private SupplierService supplierService = new SupplierService();
	private TenderProjectService tenderProjectService ;
	private EvaluationStepService evaluationStepService = new EvaluationStepService();
	private BaseDictionaryValueService dictionaryValueService;
	private ExpertEvaluationStepService expertEvaluationStepService ;;
	private String EVAL_ACCESSMENT_YES = Messages.Qualified;
	private String EVAL_ACCESSMENT_NO = Messages.Unqualified;
	private List<Supplier> supplierList = new ArrayList<Supplier>();
	private Map<String, Color> contentBackgroundMap = new HashMap<String, Color>();
	private Color lightRedColor = SWTResourceManager.getColor(SWT.COLOR_RED);// 红色
	private StackLayout stackLayout = new StackLayout();
	private ExpertEvaluationListener expertEvaluationListener;
	private Composite contentComposite;
	private Composite left_tool_composite;
	private Composite messageComposite;
	private Composite featureEvaluationResultComposite;
	private Composite loadComposite;
	private String supplierName;
	private String expertUserId;
	private TipsComposite noteMessageLabel;
	private CLabel messageLabel;
	private List<CheckResult> checkResultList;
	private List<DictionaryValue> dictionaryList;
	private EvaluationButton init_default_btn;
	private EvaluationButton save_result_btn;
	private Object[][] contentArray;
	private KTable resultKTable;
	private CommonKtableCModel commKTableModel;
	private TenderProjectEntity tenderProject;
	private TenderProjectRuleEntity tenderProjectRuleEntity;
	private EvaluationStep evaluationStep;
	private Expert CURRENT_EXPERT;
	private EvaluationStep noComplateFilterStep = null;
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public FeatureCodeCheckComposite(Composite parent, int style) {
		super(parent, style);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		setBackgroundMode(SWT.INHERIT_FORCE);
		GridLayout gridLayout = new GridLayout(3, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		tenderProject = (TenderProjectEntity) parent.getData("tenderProjectEntity");
		tenderProjectRuleEntity = (TenderProjectRuleEntity) parent.getData("tenderProjectRuleEntity");
		evaluationStep = (EvaluationStep) parent.getData("evaluationStep");
		CURRENT_EXPERT = (Expert) parent.getData(Constants.CUR_EXPERT);
		
		left_tool_composite = new Composite(this, SWT.NONE);
		left_tool_composite.setBackground(EvalLeftButton.BG);
		left_tool_composite.setBackgroundMode(SWT.INHERIT_FORCE);
		left_tool_composite.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1));
		GridLayout gl_left_tool_composite = new GridLayout(1, false);
		left_tool_composite.setLayout(gl_left_tool_composite);
		
		EvalLeftButton goto_back = new EvalLeftButton(left_tool_composite, SWT.NONE);
		goto_back.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		goto_back.setImage(ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/back.png"));
		goto_back.setToolTipText(Messages.Button_return);
		goto_back.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				goBackAction();
			}
		});
		
		EvalLeftButton refresh_evaluation = new EvalLeftButton(left_tool_composite, SWT.NONE);
		refresh_evaluation.setImage(ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/refresh.png"));
		refresh_evaluation.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		refresh_evaluation.setToolTipText(Messages.Button_refresh);
		refresh_evaluation.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				refreshAction();
			}
		});
		
		ESepator menu_sep1 = new ESepator(left_tool_composite, SWT.NONE);
		menu_sep1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		ESepator toolv_sepator = new ESepator(this, SWT.NONE);
		toolv_sepator.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1));
		toolv_sepator.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		loadComposite = new Composite(this, SWT.NONE);
		loadComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		loadComposite.setLayout(stackLayout);
		
		featureEvaluationResultComposite = new Composite(loadComposite, SWT.NONE);
		GridLayout gl_featureEvaluationResultComposite = new GridLayout(1, false);
		gl_featureEvaluationResultComposite.marginHeight = 0;
		gl_featureEvaluationResultComposite.marginWidth = 0;
		gl_featureEvaluationResultComposite.verticalSpacing = 0;
		featureEvaluationResultComposite.setLayout(gl_featureEvaluationResultComposite);
		
		Composite composite_6 = new Composite(featureEvaluationResultComposite, SWT.NONE);
		composite_6.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_composite_6 = new GridLayout(2, false);
		gl_composite_6.horizontalSpacing = 0;
		composite_6.setLayout(gl_composite_6);
		composite_6.setBackgroundMode(SWT.INHERIT_FORCE);
		
		noteMessageLabel = new TipsComposite(composite_6, SWT.WRAP);
		noteMessageLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		noteMessageLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1));
		
		ESepator topSep = new ESepator(featureEvaluationResultComposite, SWT.NONE);
		topSep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		topSep.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		contentComposite = new Composite(featureEvaluationResultComposite, SWT.NONE);
		contentComposite.setLayout(new FillLayout(SWT.HORIZONTAL));
		contentComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		ESepator sep = new ESepator(featureEvaluationResultComposite, SWT.SEPARATOR | SWT.HORIZONTAL);
		sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		sep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		Composite composite_1 = new Composite(featureEvaluationResultComposite, SWT.NONE);
		composite_1.setBackgroundMode(SWT.INHERIT_FORCE);
		composite_1.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
		composite_1.setLayout(new GridLayout(3, false));
		
		init_default_btn = new EvaluationButton(composite_1, SWT.NONE);
		init_default_btn.setText(MessageFormat.format(Messages.Button_setAllFormat, EVAL_ACCESSMENT_YES));
		init_default_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/white/select_all.png"));
		init_default_btn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		init_default_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				passAllAction();
			}
		});
		
		save_result_btn = new EvaluationButton(composite_1, SWT.NONE);
		save_result_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/white/save.png"));
		save_result_btn.setText(Messages.Button_saveEvaluationResult);
		save_result_btn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		save_result_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				saveEvaluationResultAction();
			}
		});
		
		EvaluationButton submit_result_btn = new EvaluationButton(composite_1, SWT.NONE | EButton.CSS_GREEN);
		submit_result_btn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		submit_result_btn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		submit_result_btn.setText(Messages.Button_submitEvaluationResult);
		submit_result_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/white/submit.png"));
		submit_result_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				submitEvaluationResultAction();
			}
		});
		
		messageComposite = new Composite(loadComposite, SWT.NONE);
		messageComposite.setLayout(new GridLayout(1, false));
		
		messageLabel = new CLabel(messageComposite, SWT.NONE);
		messageLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/hourglass.png"));
		messageLabel.setFont(SWTResourceManager.getFont("微软雅黑", 20, SWT.BOLD));
		messageLabel.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		messageLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
		messageLabel.setText("正在进行特征码检查，请稍等...");
		tenderProjectService = new TenderProjectService();
		init();
	}
	
	/**
	 * @Description:  初始化界面
	 * @author guoyr
	 */
	private void init(){
		expertEvaluationStepService = new ExpertEvaluationStepService();
		expertUserId = CURRENT_EXPERT.getUserId();
		// 修改评标专家的评审记录
		ActionLogUtils.addServerLog(tenderProject.getId(), "进入" + evaluationStep.getStepName(), Constants.USER.getUserName());
		expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.IN_PROGRESS, tenderProject.getId(), evaluationStep, expertUserId);
		supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProject.getTenderType());
		refreshAction();
	}
	
	/**
	 * @Description: 显示提示信息
	 * @author guoyr 
	 */
	private void showTips(){
		StringBuilder noteMsg = new StringBuilder();
		noteMessageLabel.addTips(MessageFormat.format(Messages.Tips_featureCheckResult, Messages.Conclusion, EVAL_ACCESSMENT_YES, EVAL_ACCESSMENT_NO));
		if(ObjectUtils.equals(evaluationStep.getFilterType(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.FILTER_YES)){
			if(ObjectUtils.equals(evaluationStep.getPassType(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.PASS_MORE)){
				noteMsg.append(MessageFormat.format(Messages.Tips_evaluationRule_filter, Messages.MinorityObeyMajority, EVAL_ACCESSMENT_NO, supplierName));
			}else {
				noteMsg.append(MessageFormat.format(Messages.Tips_evaluationRule_filter, Messages.OneTicketVeto, EVAL_ACCESSMENT_NO, supplierName));
			}
		}else {
			noteMsg.append(MessageFormat.format(Messages.Tips_evaluationRule_noFilter, supplierName));
		}
		noteMessageLabel.addTips(noteMsg.toString());
		messageLabel.setText(MessageFormat.format(Messages.Tips_progressInAndWait, evaluationStep.getStepName()));
		stackLayout.topControl = messageComposite;
		loadComposite.layout();
	}
	
	/**
	 * @Description: 初始化特征码检查的内容
	 * @author guoyr 
	 */
	private void initKtableContent(){
		
		int rowCount = supplierList.size() + 1;
		int colcount = dictionaryList.size() + 3;
		contentArray = new Object[rowCount][colcount];
		// 设置标题
		contentArray[0][0] = supplierName;
		
		contentArray[0][contentArray[0].length - 2] = Messages.CheckResult;
		contentArray[0][contentArray[0].length - 1] = Messages.Conclusion;
		
		// 记录检查结果
		Map<String, List<String>> supplierCheckResultMap = new HashMap<String, List<String>>();
		
		// 获得项目路径
		String tenderPath = FileConstants.getBidFilePath(tenderProject.getId());
		
		// 设置内容
		int row = 1;
		for(Supplier supplier : supplierList){
			
			// 供应商名称
			contentArray[row][0] = row+"、"+supplier.getSupplierName();
			Map<String, String> supplierFeatureCode = getSupplierFeatureCodeValue(supplier,tenderPath + supplier.getId() + File.separator + FileConstants.FEATURE_IDENTIFY);
			int col = 1;
			for(DictionaryValue dictionaryValue : dictionaryList){
				
				if(row == 1){
					contentArray[0][col] = dictionaryValue.getDictValueName();
				}
				// 获得当前特征码的内容
				String featureCode = (String) supplierFeatureCode.get(dictionaryValue.getDictValueCode());
				if(null != featureCode){
					// 如果已经存在该 特征码
					if(supplierCheckResultMap.containsKey(dictionaryValue.getDictValueCode() + "_"+featureCode)){
						// 获得存在相同特征码的供应商名称集合，并将当前供应商也加入，然后更新检查结果
						List<String> eqSupplier = supplierCheckResultMap.get(dictionaryValue.getDictValueCode() + "_"+featureCode);
						eqSupplier.add(supplier.getSupplierName());
						supplierCheckResultMap.put(dictionaryValue.getDictValueCode() + "_"+featureCode, eqSupplier);
					}else {
						List<String> eqSupplier = new ArrayList<String>();
						eqSupplier.add(supplier.getSupplierName());
						supplierCheckResultMap.put(dictionaryValue.getDictValueCode() + "_"+featureCode, eqSupplier);
					}
					contentArray[row][col] = featureCode;
				}
				col ++;
			}
			row++;
		}
		
		if(null != checkResultList){
			checkResultList.clear();
		}
		checkResultList = new ArrayList<CheckResult>();
		// 遍历特征码检查结果
		for(row = 1; row < contentArray.length; row ++){
			Supplier supplier = supplierList.get(row - 1);
			
			int col = 1;
			StringBuilder checkOption = new StringBuilder();
			boolean checkPass = true;
			for(DictionaryValue dictionaryValue : dictionaryList){
				String featureCode = (String)contentArray[row][col];
				List<String> eqSupplier = supplierCheckResultMap.get(dictionaryValue.getDictValueCode() + "_"+featureCode);
				
				CheckResult checkResult = new CheckResult();
				checkResult.setId(UuidUtils.getUUID());
				checkResult.setTenderId(tenderProject.getId());
				checkResult.setCheckItemId(dictionaryValue.getId());
				checkResult.setStepId(evaluationStep.getId());
				checkResult.setSupplierId(supplier.getId());
				checkResult.setUserId(expertUserId);
				checkResult.setCheckContent(featureCode);
				
				if(null != eqSupplier && eqSupplier.size() > 1){
					
					checkOption.append(MessageFormat.format(Messages.Tips_sameFeatureCode, eqSupplier.size(), supplierName, dictionaryValue.getDictValueName()));
					contentArray[row][contentArray[0].length - 2] = checkOption.toString();
					checkPass = false;
					contentBackgroundMap.put(row +"_"+ col, lightRedColor);
					checkResult.setErrorNum(eqSupplier.size());
					checkResult.setAccessment(com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED);
				}else {
					contentBackgroundMap.put(row +"_"+ col, null);
					checkResult.setErrorNum(0);
					checkResult.setAccessment(com.gx.obe.components.core.enumeration.EvaluationStepEnum.QUALIFIED);
				}
				checkResultList.add(checkResult);
				col ++;
			}
			if(checkOption.length() == 0){
				contentArray[row][contentArray[0].length - 2] = Messages.Tips_notDetectSameFeatureCode;
			}
			// 如果不筛选，则表示全部通过
			if(ObjectUtils.equals(com.gx.obe.components.core.enumeration.EvaluationStepEnum.FILTER_NO, evaluationStep.getFilterType())){
				checkPass = true;
			}
			contentArray[row][contentArray[0].length - 1] = checkPass;
		}
	}
	
	/**
	 * @Description: 画表格
	 * @author guoyr 
	 */
	private void drawKtable(){
		
		if(null != resultKTable && !resultKTable.isDisposed()){
			resultKTable.dispose();
		}
		resultKTable = new KTable(contentComposite, SWTX.AUTO_SCROLL | SWT.FULL_SELECTION | SWTX.EDIT_ON_KEY);
		commKTableModel = new CommonKtableCModel(resultKTable, 1, 1, supplierList.size(), dictionaryList.size()+2);
		commKTableModel.setContentRowHeight(30);// 设置行高
		commKTableModel.setColumnWidth(commKTableModel.getColCount() - 1, 70);// 设置最后一列宽为70
		commKTableModel.setColumnWidth(commKTableModel.getColCount() - 2, 200);// 设置评审意见列宽为200
		commKTableModel.setColumnMinWidth(commKTableModel.getColCount() - 2, 250);// 设置评审意见列宽为200
		commKTableModel.setColumnMinWidth(0, 150);
		
		// 设置单元格内容
		CellContentRender contentRender = new CellContentRender(){
			public Object receiveContent(int row, int col) {
				if(row == 0){
					return ObjectUtils.getObjString(contentArray[row][col]);
				}else {
					if(col == commKTableModel.getColCount() -1){
						return (Boolean)contentArray[row][col];
					}else {
						return ObjectUtils.getObjString(contentArray[row][col]);
					}
				}
			}
		};
		commKTableModel.setCellContentRender(contentRender);
		
		// 设置单元格渲染器
		CellRender cellRender = new CellRender(){
			public Object receiveCellRender(int row, int col) {
				if(commKTableModel.isFixedCell(col, row)){
					return new FixedWrapCellRenderer();
				}else{
					if(col == commKTableModel.getColumnCount() - 1){
						return new CheckableCellRenderer();
					}else {
						return new TextWrapCellRenderer();
					}
				}
			}
		};
		commKTableModel.setCellRender(cellRender);
		
		// 设置单元格编辑器
		final KTableCellEditorMultilineWrapText text = new KTableCellEditorMultilineWrapText();
		text.setSingleClickEdit(true);
		CellEditorRender cellEditorRender = new CellEditorRender(){
			public Object receiveCellEditor(int row, int col) {
				if(col == commKTableModel.getColumnCount() - 1){// 是否通过(多选框)
					return new KTableCellEditorCheckbox2();
				}else if(col == commKTableModel.getColumnCount() - 2){// 评审意见
					text.setTextLimit(200);
					return text;
				}else {
					return null;
				}
			}
		};
		commKTableModel.setCellEditorRender(cellEditorRender);
		
		if(ObjectUtils.equals(com.gx.obe.components.core.enumeration.EvaluationStepEnum.FILTER_YES, evaluationStep.getFilterType())){
			// 编辑单元格内容
			CellEditContentRender editContentRender = new CellEditContentRender(){
				public void receiveContent(int row, int col, Object content) {
					// 设置"是否通过"和"评审意见"的值
					contentArray[row][col] = content;
				}
			};
			commKTableModel.setCellEditContentRender(editContentRender);
		}
		
		// 编辑单元格对齐方式
		CellAlignmentRender cellAlignmentRender = new CellAlignmentRender(){
			public int receiveCellAlignment(int row, int col) {
				if(row < commKTableModel.getFixedRowCount()){
					return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
				}else {
					if(col == 0 || col == commKTableModel.getColCount() - 2){
						
						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 (!commKTableModel.isFixedCell(col, row)) {
					Color bgColor = contentBackgroundMap.get(row + "_" + col);
					return bgColor;
				}
				return null;
			}
		};
		commKTableModel.setCellForegroundRender(cellForegroundRender);
		
		resultKTable.setModel(commKTableModel);
		contentComposite.layout();
	}
	
	/**
	 * @Description: 读取特征码
	 * @author guoyr
	 * @param supplierFeaturePath
	 * @return 
	 */
	private Map<String, String> getSupplierFeatureCodeValue(Supplier supplier, String supplierFeaturePath){
		Map<String, String> valueList = new HashMap<String, String>();
		if(null != supplier){
			if(null != supplier.getTenderIP()){
				valueList.put("tenderIP", supplier.getTenderIP());
			}
		}
		// 获得供应商的特征码Document
		Document supplierDoc = XmlUtils.getXmlDocument(supplierFeaturePath);
		if(null != supplierDoc){
			// 获得根元素
			Element rootElement = supplierDoc.getRootElement();
			// 遍历字典值(使用字典值编码，获得特征码指标项值。)
			for(DictionaryValue dictionaryValue : dictionaryList){
				String dictValueCode = dictionaryValue.getDictValueCode();
				String value = rootElement.elementText(dictValueCode);
				if(null != value){
					valueList.put(dictValueCode, value.trim());
				}
			}
		}
		return valueList;
	}
	
	/**
	 * @Description: 一键全部通过
	 * @author guoyr 
	 */
	private void passAllAction(){
		for(int row = 1; row < contentArray.length; row ++){
			Object[] contentRowArray = contentArray[row];
			contentRowArray[contentRowArray.length - 1] = true;
		}
		resultKTable.redraw();
	}
	
	private boolean saveResult(){
		resultKTable.closeCellEditor();
		List<ExpertEvaluationResult> expertEvaluationResultList = new ArrayList<ExpertEvaluationResult>();
		int row = commKTableModel.getFixedRowCount();
		for(Supplier supplier : supplierList){
			// 供应商名称
			String supplierOpinion = (String)contentArray[row][contentArray[0].length -2];
			boolean accessment = (Boolean)contentArray[row][contentArray[0].length -1];
			// 获得当前供应商的评标结果
			ExpertEvaluationResult expertEvaluationResult = null;
			expertEvaluationResult = initExpertEvaluationResult();
			expertEvaluationResult.setId(UuidUtils.getUUID());
			expertEvaluationResult.setSupplierId(supplier.getId());
			
			expertEvaluationResult.setSupplierId(supplier.getId());
			expertEvaluationResult.setSupplierOpinion(supplierOpinion);
			expertEvaluationResult.setAccessment(accessment?com.gx.obe.components.core.enumeration.EvaluationStepEnum.QUALIFIED:com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED);
			expertEvaluationResultList.add(expertEvaluationResult);
			row ++;
		}
		
		return expertEvaluationStepService.saveFeatureEvaluationResult(tenderProject.getId(), expertUserId, evaluationStep, checkResultList, expertEvaluationResultList);
	}
	/**
	 * @Description: 暂存结果
	 * @author guoyr
	 * @return 
	 */
	private boolean saveEvaluationResultAction(){
		boolean saveSuccess = saveResult();
		if(saveSuccess){
			MessageDialog.openShortMessage(Messages.Tips_saveEvalResultSuccess);
		}else {
			MessageDialog.openInformation(getShell(), Messages.Dialog_warmTips, Messages.Tips_saveEvalResultFailed);
		}
		return saveSuccess;
	}
	
	/**
	 * @Description: 提交结果
	 * @author guoyr 
	 */
	private void submitEvaluationResultAction(){
		if(!saveResult()){
			MessageDialog.openInformation(getShell(), Messages.Dialog_warmTips, Messages.Tips_saveEvalResultFailed);
			return;
		}
		
		// 检查是否存在当前评标步骤之前未完成的步骤
		boolean isFreeProgress = ObjectUtils.equals(tenderProjectRuleEntity.getOrderReview(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.ORDER_SELF_FREE);
		if (isFreeProgress) {
			List<EvaluationStep> evaluationStepList = evaluationStepService.getNotComplateStepList(tenderProject.getId(), evaluationStep.getSortNo());
			if (null != evaluationStepList && evaluationStepList.size() > 0) {
				MessageDialog.openWarning(getShell(), Messages.Dialog_warmTips, MessageFormat.format(Messages.Tips_saveAllComplate, evaluationStepList.get(0).getStepName()));
				return ;
			}
		}
		
		if(null != noComplateFilterStep){
			if (MessageDialog.openConfirm(getShell(), Messages.Dialog_warmTips, MessageFormat.format(Messages.Tips_preStepNotCompletedBeforeEntry, noComplateFilterStep.getStepName()), new String[]{Messages.Button_refresh, Messages.Button_cancel})) {
				refreshAction();
			}
			return;
		}
		
		StringBuilder noAccessment = new StringBuilder();
		int noAccessmentCount = 0;
		
		for(int row = commKTableModel.getFixedRowCount(); row < contentArray.length; row++){
			Object[] rowArray = contentArray[row];
			boolean accessment = (Boolean)rowArray[rowArray.length -1];
			if(!accessment){
				if(noAccessmentCount > 0){
				}
				noAccessment.append("    ");
				noAccessment.append(rowArray[0]);
				noAccessment.append("\r\n");
				noAccessmentCount++;
			}
		}
		String message = MessageFormat.format(Messages.Tips_submitEvaluationResult, evaluationStep.getStepName());
		if (!MessageDialog.openConfirm(getShell(), Messages.Button_submitEvaluationResult, message, new String[] { Messages.Button_confirmComplate, Messages.Button_continueEvaluation})) {
			return;
		}
		
		if(expertEvaluationStepService.submitFeatureEvaluationResult(tenderProject.getId(), expertUserId, evaluationStep, supplierList)){
			goBackAction();
		}else {
			MessageDialog.openError(getShell(), Messages.Tips_operateFail);
		}
	}
	
	/**
	 * @Description: 获得未完成的可能会存在废标的评审节点
	 * @author guoyr 
	 */
	private void getNoComplateFilterStep(){
		List<EvaluationStep> evaluationStepList = evaluationStepService.getNotComplateFilterStepList(tenderProject.getId(), evaluationStep.getSortNo());
		if (null != evaluationStepList && evaluationStepList.size() > 0) {
			noComplateFilterStep =  evaluationStepList.get(0);
		}else {
			noComplateFilterStep = null;
		}
	}
	
	/**
	 * @Description: 返回
	 * @author guoyr
	 */
	/**
	 * @Description:  获得评标专家对供应商的评标结果
	 * @author guoyr
	 * @return
	 */
	private ExpertEvaluationResult initExpertEvaluationResult(){
		ExpertEvaluationResult expertEvaluationResult = new ExpertEvaluationResult();
		expertEvaluationResult.setTenderId(tenderProject.getId());
		expertEvaluationResult.setStepId(evaluationStep.getId());
		expertEvaluationResult.setUserId(expertUserId);
		return expertEvaluationResult;
	}
	
	/**
	 * @Description: 返回
	 * @author guoyr
	 */
	private void goBackAction(){
		if(null != expertEvaluationListener){
			expertEvaluationListener.complete();
		}
	}
	
	class RefreshActionThread extends MyRunnable{
		private WebListener listener;
		public RefreshActionThread(WebListener listener){
			this.listener = listener;	
		}
		@Override
		public void run() {
			tenderProject = tenderProjectService.getById(tenderProject.getId());
			
			supplierList = supplierService.getEvaluationStepSupplierList(tenderProject.getId(), evaluationStep);
			if(null == dictionaryValueService){
				dictionaryValueService = new BaseDictionaryValueService();
			}
			// 获得进行特征码检查的项目
			dictionaryList = dictionaryValueService.getFeatureCheckItemList(tenderProject.getId());
 			listener.run();
		}
	}
	
	/**
	 * @Description: 刷新项目信息
	 * @author guoyr
	 */
	private void refreshAction(){
		getNoComplateFilterStep();
		showTips();
		RefreshActionThread RefreshActionThread = new RefreshActionThread(new WebListener() {
			
			@Override
			public void run() {
				final Thread initKtableContentThread = new Thread(new Runnable(){
					public void run(){
						initKtableContent();
					}
				});
				initKtableContentThread.start();
				Thread drawKTableThread = new Thread(new Runnable(){
					public void run(){

						try {
							initKtableContentThread.join();
							try {
								Thread.sleep(500);
							} catch (InterruptedException e) {
							}
						} catch (InterruptedException e) {
						}
						Display.getDefault().syncExec(new Runnable(){
							public void run(){
								if(null != contentComposite && !contentComposite.isDisposed()){
									drawKtable();
									stackLayout.topControl = featureEvaluationResultComposite;
									loadComposite.layout();
								}
							}
						});
					}
				});
				drawKTableThread.start();
			}
		});
		RefreshActionThread.start();
	}
	
	public void addExpertEvaluationListener(ExpertEvaluationListener expertEvaluationListener){
		this.expertEvaluationListener = expertEvaluationListener;
	}
	
	protected void checkSubclass() {}
}
