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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.xwpf.converter.core.utils.StringUtils;
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.SashForm;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;

import com.gx.obe.ColorConstants;
import com.gx.obe.business.entity.ExpertEvaluationStep;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.components.core.enumeration.CommonEnum;
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.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.ExpertEvaluationResult;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.BaseDictionaryValueService;
import com.gx.obe.web.service.CheckResultService;
import com.gx.obe.web.service.SupplierService;
import com.gx.obe.web.service.expert.ExpertEvaluationResultService;
import com.gx.obe.web.service.expert.ExpertEvaluationStepService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

import de.kupzog.ktable.KTable;
import de.kupzog.ktable.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.CellImageRender;
import de.kupzog.ktable.renderers.CellRender;
import de.kupzog.ktable.renderers.CheckableCellRenderer;
import de.kupzog.ktable.renderers.FixedWrapCellRenderer;
import de.kupzog.ktable.renderers.TextCellRenderer;
import de.kupzog.ktable.renderers.TextWrapCellRenderer;

/**
 * @Description:
 * @author guoyr
 */
public class CleanTenderResultKtableComposite extends Composite {
	
	private SupplierService supplierService = new SupplierService();
	private BaseDictionaryValueService dictionaryValueService = new BaseDictionaryValueService();
	private ExpertEvaluationStepService expertEvaluationStepService = new ExpertEvaluationStepService();
	private CheckResultService checkResultService = new CheckResultService();
	private ExpertEvaluationResultService expertEvaluationResultService = new ExpertEvaluationResultService();
	private TenderProjectEntity tenderProject;
	private EvaluationStep evaluationStep;
	private List<Supplier> supplierList;
	private List<DictionaryValue> dictionaryList;
	private ExpertEvaluationStep expertEvaluationStep;
	private KTable resultKTable;
	private CommonKtableCModel commKTableModel;
	private Object[][] contentArray;
	// 淡红色色
	private Color lightRedColor = SWTResourceManager.getColor(SWT.COLOR_RED);
	private Color lightGreenColor = SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN);
	private SashForm sashForm;
	private Composite contentComposite;
	private Composite messageComposite;
	private CLabel messageLabel;
	private Composite cleanTenderComposite;
	private Label noteMsgLabel;
	private Composite composite_1;
	private EButton init_default_btn;
	private Map<String, Color> contentBackgroundMap = new HashMap<String, Color>();
	private CleanTenderListener listener;
	private String expertUserId;
	private String supplierName;
	private String EVAL_ACCESSMENT_YES = "合格";
	private String EVAL_ACCESSMENT_NO = "不合格";
	private String billCheckPass = "清单检查合理";
	private String billCheckFailed = "清单检查不合理";
	private Composite composite;
	private EButton save_result_btn;
	private boolean hasCheckResult = false;
	
	{
		LogUtils.logClass(CleanTenderResultKtableComposite.class);
	}
	
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public CleanTenderResultKtableComposite(Composite parent, int style) {
		super(parent, style);
		setLayout(new FillLayout(SWT.HORIZONTAL));
		tenderProject = (TenderProjectEntity) parent.getData("tenderProject");
		evaluationStep = (EvaluationStep) parent.getData("evaluationStep");
		expertUserId = (String) parent.getData("expertUserId");
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				contentBackgroundMap.clear();
			}
		});
		sashForm = new SashForm(this, SWT.VERTICAL);
		
		cleanTenderComposite = new Composite(sashForm, SWT.NONE);
		GridLayout gl_cleanTenderComposite = new GridLayout(1, false);
		gl_cleanTenderComposite.verticalSpacing = 0;
		gl_cleanTenderComposite.horizontalSpacing = 0;
		gl_cleanTenderComposite.marginHeight = 0;
		gl_cleanTenderComposite.marginWidth = 0;
		cleanTenderComposite.setLayout(gl_cleanTenderComposite);
		
		composite = new Composite(cleanTenderComposite, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		CLabel cleanTenderStepName = new CLabel(composite, SWT.NONE);
		GridData gd_cleanTenderStepName = new GridData(SWT.FILL, SWT.CENTER, false, true, 1, 1);
		cleanTenderStepName.setLayoutData(gd_cleanTenderStepName);
		cleanTenderStepName.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/tips_blue.png"));
		cleanTenderStepName.setText("");
		
		noteMsgLabel = new Label(composite, SWT.NONE);
		noteMsgLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		noteMsgLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1));
		noteMsgLabel.setText("红色数字表示" + evaluationStep.getStepName() + "过程中出现的错误数，您可录入或修改评审意见，在最后一列选择”对勾“表示" + evaluationStep.getStepName() + "结果" + EVAL_ACCESSMENT_YES + "。");
		
		ESepator topsep = new ESepator(cleanTenderComposite, SWT.SEPARATOR | SWT.HORIZONTAL);
		topsep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		topsep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		contentComposite = new Composite(cleanTenderComposite, SWT.NONE);
		contentComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		contentComposite.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		ESepator sep = new ESepator(cleanTenderComposite, SWT.SEPARATOR | SWT.HORIZONTAL);
		sep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		composite_1 = new Composite(cleanTenderComposite, SWT.NONE);
		composite_1.setBackgroundMode(SWT.INHERIT_FORCE);
		GridLayout gl_composite_1 = new GridLayout(3, true);
		composite_1.setLayout(gl_composite_1);
		composite_1.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
		
		init_default_btn = new EButton(composite_1, SWT.NONE | EButton.CSS_BLUE);
		init_default_btn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		GridData gd_init_default_btn = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		init_default_btn.setText("一键" + EVAL_ACCESSMENT_YES);
		init_default_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/white/select_all.png"));
		gd_init_default_btn.heightHint = 30;
		init_default_btn.setLayoutData(gd_init_default_btn);
		init_default_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				passAllAction();
			}
		});
		
		save_result_btn = new EButton(composite_1, SWT.NONE | EButton.CSS_GREEN);
		save_result_btn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		save_result_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/white/save.png"));
		save_result_btn.setText("暂存结果");
		GridData gd_save_result_btn = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_save_result_btn.heightHint = 30;
		save_result_btn.setLayoutData(gd_save_result_btn);
		save_result_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				saveEvaluationResultAction();
			}
		});
		
		EButton submit_result_btn = new EButton(composite_1, SWT.NONE | EButton.CSS_GREEN);
		submit_result_btn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		GridData gd_submit_result_btn = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_submit_result_btn.heightHint = 30;
		submit_result_btn.setLayoutData(gd_submit_result_btn);
		submit_result_btn.setText("完成评审");
		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(sashForm, 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("正在加载检查结果，请稍等...");
		
		sashForm.setWeights(new int[] { 1, 1 });
		init();
	}
	
	/**
	 * @Description: 初始化
	 * @author guoyr
	 */
	private void init() {
		// 修改评标专家的评审记录
		expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.IN_PROGRESS, tenderProject.getId(), evaluationStep, expertUserId);
		
		supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProject.getTenderType());
		supplierList = supplierService.getEvaluationStepSupplierList(tenderProject.getId(), evaluationStep);
		
		// 获得清标检查项
		dictionaryList = dictionaryValueService.getCleanTenderItemList(tenderProject.getId());
		
		// 如果不筛选，则表示全部通过
		if (ObjectUtils.equals(com.gx.obe.components.core.enumeration.EvaluationStepEnum.FILTER_NO, evaluationStep.getFilterType())) {
			init_default_btn.dispose();
		}
		
		showTips();
		// 开始清标
		startCleanTender();
	}
	
	/**
	 * @Description: 显示提示信息
	 * @author guoyr
	 */
	private void showTips() {
		StringBuilder noteMsg = new StringBuilder();
		noteMsg.append("红色数字表示");
		noteMsg.append(evaluationStep.getStepName());
		noteMsg.append("的错误数，您可录入或修改评审见，");
		noteMsg.append("并在最后一列选择是否");
		noteMsg.append(EVAL_ACCESSMENT_YES);
		noteMsg.append("。");
		noteMsgLabel.setText(noteMsg.toString());
		messageLabel.setText("正在加载" + evaluationStep.getStepName() + "结果，请稍等...");
	}
	
	/**
	 * @Description: 开始清标
	 * @author guoyr
	 */
	private void startCleanTender() {
		sashForm.setMaximizedControl(messageComposite);
		
		Thread drawKTableThread = new Thread(new Runnable() {
			public void run() {
				initKtableContent();
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {}
				Display.getDefault().syncExec(new Runnable() {
					public void run() {
						if (null != contentComposite && !contentComposite.isDisposed()) {
							drawKtable();
							sashForm.setMaximizedControl(cleanTenderComposite);
						}
					}
				});
			}
		});
		drawKTableThread.start();
	}
	
	/**
	 * @Description: 初始化清标结果的内容
	 * @author guoyr
	 */
	private void initKtableContent() {
		int rowCount = supplierList.size() + 2;
		int colcount = dictionaryList.size() * (hasCheckResult ? 2 : 1) + 4;
		contentArray = new Object[rowCount][colcount];
		// 设置标题
		contentArray[0][0] = "序号";
		contentArray[0][1] = supplierName + "名称";
		contentArray[0][contentArray[0].length - 2] = "评审意见";
		contentArray[0][contentArray[0].length - 1] = "结论";
//		contentArray[0][contentArray[0].length - 1] = "是否"+EVAL_ACCESSMENT_YES;
		
		List<ExpertEvaluationResult> expertEvaluationResultList = expertEvaluationResultService.getExpertStepAllSupplierEvaluationResultList(tenderProject.getId(), evaluationStep.getId(), expertUserId);
		Map<String, String> expertEvaluationResultMap = new HashMap<String, String>();
		if (null != expertEvaluationResultList) {
			for (ExpertEvaluationResult expertEvaluationResult : expertEvaluationResultList) {
				expertEvaluationResultMap.put(expertEvaluationResult.getSupplierId(), expertEvaluationResult.getSupplierOpinion());
			}
		}
		// 设置内容
		int row = 2;
		int supplierIndex = 1;
		for (Supplier supplier : supplierList) {
			
			StringBuilder opinion = new StringBuilder();
			// 供应商名称
			contentArray[row][0] = (supplierIndex) + "";
			contentArray[row][1] = supplier.getSupplierName();
			
			boolean supplierAccessment = true;// 标记供应商清标是否通过
			int col = 2;
			for (DictionaryValue dictionaryValue : dictionaryList) {
				if (row == 2) {
					contentArray[0][col] = dictionaryValue.getDictValueName();
					contentArray[1][col] = "错误数";
					if (hasCheckResult) {
						contentArray[1][col + 1] = "提示信息";
					}
				}
				
				int errorNum = 0;
				boolean isAccessment = true;// 标记每一项检查是否通过
				CheckResult checkResult = checkResultService.getCheckResultByCondition(tenderProject.getId(), expertUserId, supplier.getId(), dictionaryValue.getId(), evaluationStep.getId());
				if (null != checkResult) {
					errorNum = checkResult.getErrorNum();
					isAccessment = checkResult.getAccessment().equals(com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED) ? false : true;
				}
				contentArray[row][col] = errorNum;
				if (hasCheckResult) {
					contentArray[row][col + 1] = isAccessment;
				}
				
				if (isAccessment) {
					contentBackgroundMap.put(row + "_" + col, null);
					if (hasCheckResult) {
						contentBackgroundMap.put(row + "_" + (col + 1), lightGreenColor);
					}
				} else {
					contentBackgroundMap.put(row + "_" + col, lightRedColor);
					if (hasCheckResult) {
						contentBackgroundMap.put(row + "_" + (col + 1), lightRedColor);
					}
				}
				
				// 如果有一项清标不通过，则认为供应商不通过
				if (!isAccessment) {
					supplierAccessment = false;
				}
				
				if (errorNum > 0) {
					opinion.append(errorNum);
					opinion.append("项");
					opinion.append(dictionaryValue.getDictValueName());
					opinion.append("错误；");
				}
				col += (hasCheckResult ? 2 : 1);
			}
			String supplierOpinion = expertEvaluationResultMap.get(supplier.getId());
			if (StringUtils.isNotEmpty(supplierOpinion)) {
				contentArray[row][contentArray[0].length - 2] = supplierOpinion;
			} else {
				String useSystemClearOpinion = evaluationStep.getEvaluationParamByKey(com.gx.obe.components.core.enumeration.EvaluationStepEnum.USE_SYSTEM_CLEAR_OPINION);
				if (null != useSystemClearOpinion && useSystemClearOpinion.equals(CommonEnum.YES)) {
					if (opinion.length() > 0) {
						contentArray[row][contentArray[0].length - 2] = opinion.toString();
					} else {
						contentArray[row][contentArray[0].length - 2] = "无错误。";
					}
				}
			}
			// 如果不筛选，则表示全部通过
			if (ObjectUtils.equals(com.gx.obe.components.core.enumeration.EvaluationStepEnum.FILTER_NO, evaluationStep.getFilterType())) {
				supplierAccessment = true;
			}
			contentArray[row][contentArray[0].length - 1] = supplierAccessment;
			row++;
			supplierIndex++;
		}
	}
	
	/**
	 * @Description: 渲染Ktable
	 * @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, 2, 2, supplierList.size(), dictionaryList.size() * (hasCheckResult ? 2 : 1) + 2);
		
		commKTableModel.setContentRowHeight(30);// 设置行高
		commKTableModel.setColumnWidth(0, 60);
		commKTableModel.setColumnMaxWidth(0, 60);
		commKTableModel.setColumnMinWidth(1, 200);
		commKTableModel.setColumnMaxWidth(commKTableModel.getColCount() - 1, 50);// 设置最后一列宽为70
		commKTableModel.setColumnMinWidth(commKTableModel.getColCount() - 2, 150);// 设置评审意见列宽为200
		commKTableModel.setColumnMaxWidth(commKTableModel.getColCount() - 2, 200);// 设置评审意见列宽为200
		commKTableModel.setColumnMinWidth(0, 150);
		
		// 合并行
		commKTableModel.addRowSpan(0, 0, 2);// 序号
		commKTableModel.addRowSpan(0, 1, 2);// 投标单位
		commKTableModel.addRowSpan(0, commKTableModel.getColumnCount() - 2, 2);// 评审意见
		commKTableModel.addRowSpan(0, commKTableModel.getColumnCount() - 1, 2);// 是否通过
		
		// 合并列
		for (int col = commKTableModel.getFixedColumnCount(); col < commKTableModel.getFixedColumnCount() + dictionaryList.size() * (hasCheckResult ? 2 : 1); col = col + (hasCheckResult ? 2 : 1)) {
			if (hasCheckResult) {
				commKTableModel.setColumnWidth(col, 45);
				commKTableModel.addColSpan(0, col, 2);
				commKTableModel.setColumnWidth(col + 1, 60);
			} else {
				commKTableModel.setColumnWidth(col, 100);
			}
		}
		
		// 设置单元格内容
		CellContentRender contentRender = new CellContentRender() {
			public Object receiveContent(int row, int col) {
				Object content = contentArray[row][col];
				if (commKTableModel.isFixedCell(col, row)) {
					return null != content ? contentArray[row][col] : "";
				} else {
					if (col == commKTableModel.getColCount() - 1) {
						return null != content ? (Boolean) content : true;
					} else if (col == commKTableModel.getColCount() - 2) {
						return null != content ? content : "";
					} else {
						if (hasCheckResult) {
							if ((col - commKTableModel.getFixedColumnCount()) % 2 == 0) {// 显示错误数的列
								return null != content ? Integer.parseInt(content.toString()) : 0;
							} else {
								boolean result = null != content ? (Boolean) content : true;// 显示是否通过
								return result ? billCheckPass : billCheckFailed;
							}
						} else {
							return null != content ? Integer.parseInt(content.toString()) : 0;
						}
					}
				}
			}
		};
		commKTableModel.setCellContentRender(contentRender);
		
		final FixedWrapCellRenderer fixedWrapCellRenderer = new FixedWrapCellRenderer();
		final CheckableCellRenderer checkableCellRenderer = new CheckableCellRenderer();
		final TextWrapCellRenderer textWrapCellRenderer = new TextWrapCellRenderer();
		final TextCellRenderer textCellRenderer = new TextCellRenderer();
		// 设置单元格渲染器
		CellRender cellRender = new CellRender() {
			public Object receiveCellRender(int row, int col) {
				if (commKTableModel.isFixedCell(col, row)) {
					return fixedWrapCellRenderer;
				} else {
					if (col == commKTableModel.getColCount() - 1) {
						return checkableCellRenderer;
					} else if (col == commKTableModel.getColCount() - 2) {
						return textWrapCellRenderer;
					} else {
						if (hasCheckResult) {
							if (col - commKTableModel.getFixedColumnCount() % 2 == 0) {// 显示错误数的列
								return textWrapCellRenderer;
							} else {
								return textCellRenderer;
							}
						} else {
							return textCellRenderer;
						}
					}
				}
			}
		};
		commKTableModel.setCellRender(cellRender);
		
		// 设置单元格编辑器
		final KTableCellEditorMultilineWrapText text = new KTableCellEditorMultilineWrapText();
		final KTableCellEditorCheckbox2 checkBox = new KTableCellEditorCheckbox2();
		text.setSingleClickEdit(true);
		text.setTextLimit(200);
		CellEditorRender cellEditorRender = new CellEditorRender() {
			public Object receiveCellEditor(int row, int col) {
				// 是否通过(多选框)
				if (col == commKTableModel.getColumnCount() - 1) {
					return checkBox;
				} else if (col == commKTableModel.getColumnCount() - 2) {// 评审意见
					return text;
				}
				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 == 1 || 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);
		final Image editImage = ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/edit.png");
		// 设置单元格图片
		CellImageRender cellImageRender = new CellImageRender() {
			public Image receiveCellImage(int row, int col) {
				if (row >= commKTableModel.getFixedRowCount() && col == commKTableModel.getColCount() - 2) {
					return editImage;
				}
				return null;
			}
		};
		commKTableModel.setCellImageRender(cellImageRender);
		// 设置单元格前景色
		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);
	}
	
	/**
	 * @Description: 获得评标专家对供应商的评标结果
	 * @author guoyr
	 * @return
	 */
	private ExpertEvaluationResult initExpertEvaluationResult() {
		ExpertEvaluationResult expertEvaluationResult = new ExpertEvaluationResult();
		expertEvaluationResult.setId(UuidUtils.getUUID());
		expertEvaluationResult.setTenderId(tenderProject.getId());
		expertEvaluationResult.setStepId(evaluationStep.getId());
		expertEvaluationResult.setUserId(expertUserId);
		
		return expertEvaluationResult;
	}
	
	/**
	 * @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();
		// 如果保存后，再没有修改过结果
//		if(!resultChanged){
//			return true;
//		}
		int row = commKTableModel.getFixedRowCount();
		List<ExpertEvaluationResult> expertEvaluationResultList = new ArrayList<ExpertEvaluationResult>();
		
		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.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++;
		}
		
		boolean saveSuccess = expertEvaluationStepService.saveCleanTenderEvaluationResult(tenderProject.getId(), expertUserId, evaluationStep, expertEvaluationResultList);;
		if (saveSuccess) {} else {}
		return saveSuccess;
		
	}
	
	/**
	 * @Description: 保存评标结果
	 * @author guoyr
	 * @param isSubmit
	 * @return
	 */
	private boolean saveEvaluationResultAction() {
		boolean saveSuccess = saveResult();
		if (saveSuccess) {
			MessageDialog.openShortMessage(evaluationStep.getStepName() + "结果暂存成功！");
		} else {
			MessageDialog.openError(getShell(), evaluationStep.getStepName() + "结果暂存失败！");
		}
		return saveSuccess;
	}
	
	/**
	 * @Description: 提效清标结果
	 * @author guoyr
	 */
	private void submitEvaluationResultAction() {
		
		if (!saveResult()) {
			MessageDialog.openError(getShell(), evaluationStep.getStepName() + "结果保存失败！");
			return;
		}
		
//		// 如果当前评审节点不是退回节点，则在提交时验证一下，现在评审节点是否被改为退回了
//		if(evaluationStepService.isEvaluationStepRebackedInProgress(evaluationStep)){
//			MessageDialog.openInformation(getShell(), "["+evaluationStep.getStepName()+"]已被授权重新操作！");
//			if(null != listener){
//				listener.complate();
//			}
//			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(rowArray[1]);
				noAccessment.append("\r\n");
				noAccessmentCount++;
				String opinion = (String) contentArray[row][rowArray.length - 2];
				if (null == opinion || opinion.length() < 1) {
					MessageDialog.openWarning(getShell(), "请为[" + rowArray[1] + "]录入评审意见！");
					return;
				}
			}
		}
		StringBuilder msg = new StringBuilder();
		msg.append("完成评审后将不能再对当前评标结果进行修改，");
		if (noAccessmentCount > 0) {
			msg.append("其中以下");
			msg.append(noAccessmentCount);
			msg.append("家");
			msg.append(supplierName);
			msg.append(evaluationStep.getStepName());
			msg.append(EVAL_ACCESSMENT_NO);
			msg.append("：\r\n");
			msg.append(noAccessment.toString());
		}
		msg.append("确认完成[").append(evaluationStep.getStepName()).append("]的评审操作吗？");
		
		if (MessageDialog.openConfirm(getShell(), "确认是否完成" + evaluationStep.getStepName(), msg.toString(), new String[] { "确认完成", "继续评审" })) {
			if (expertEvaluationStepService.submitCleanTenderEvaluationResult(tenderProject.getId(), expertUserId, evaluationStep, expertEvaluationStep, supplierList)) {
				if (null != listener) {
					listener.complate();
				}
			} else {
				MessageDialog.openError(getShell(), Messages.Tips_saveFail);
			}
		}
	}
	
	public void setCleanTenderListener(CleanTenderListener listener) {
		this.listener = listener;
	}
	
	/**
	 * @Description: 刷新表格的大小
	 * @author guoyr
	 */
	public void refreshTableSize() {
		if (null != resultKTable && !resultKTable.isDisposed()) {
			resultKTable.resizeColumnOptimal();
		}
	}
	
	public interface CleanTenderListener {
		public void complate();
	}
	
	protected void checkSubclass() {}
}
