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

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

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.core.widgets.RectangleRadioButton;
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.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;

import com.gx.obe.ColorConstants;
import com.gx.obe.business.entity.ExpertEvaluationStep;
import com.gx.obe.business.entity.SupplierEvaluationStep;
import com.gx.obe.common.icons.IconsActivator;
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.ExpertEnum;
import com.gx.obe.evaluation.step.dialog.ApplyReEvaluationDialog;
import com.gx.obe.evaluation.step.dialog.ViewBoqDialog;
import com.gx.obe.evaluation.step.dialog.ViewCleanTenderDetailDialog;
import com.gx.obe.evaluation.step.listener.ExpertEvaluationListener;
import com.gx.obe.util.utils.DateUtils;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
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.auth.AuthUser;
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.service.BaseDictionaryValueService;
import com.gx.obe.web.service.CheckResultService;
import com.gx.obe.web.service.SupplierEvaluationStepService;
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.gx.obe.web.service.expert.ExpertService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

import de.kupzog.ktable.KTable;
import de.kupzog.ktable.KTableCellDoubleClickListener;
import de.kupzog.ktable.SWTX;
import de.kupzog.ktable.model.CommonKtableCModel;
import de.kupzog.ktable.renderers.CellAlignmentRender;
import de.kupzog.ktable.renderers.CellContentRender;
import de.kupzog.ktable.renderers.CellForegroundRender;
import de.kupzog.ktable.renderers.CellRender;
import de.kupzog.ktable.renderers.FixedWrapCellRenderer;
import de.kupzog.ktable.renderers.TextCellRenderer;
import de.kupzog.ktable.renderers.TextWrapCellRenderer;

/**
 * 
 * @Description:
 * @author guoyr
 */
public class CleanTenderCheckResultComposite extends Composite {
	
	private TenderProjectEntity tenderProject;
	private TenderProjectRuleEntity projectRule;
	private EvaluationStep evaluationStep;
	private Expert CURRENT_EXPERT;
	private ImageResolver imageResolver = new ImageResolver();
	private SupplierService supplierService = new SupplierService();
	private ExpertService expertService = new ExpertService();
	private ExpertEvaluationResultService expertEvaluationResultService = new ExpertEvaluationResultService();
	private ExpertEvaluationStepService expertEvaluationStepService = new ExpertEvaluationStepService();
	private SupplierEvaluationStepService supplierEvaluationStepService = new SupplierEvaluationStepService();
	private BaseDictionaryValueService dictionaryValueService = new BaseDictionaryValueService();
	private CheckResultService checkResultService = new CheckResultService();
	private List<DictionaryValue> dictionaryList;
	private List<Expert> expertList;
	private List<Supplier> supplierList;
	private KTable resultKTable;
	private CommonKtableCModel commKTableModel;
	private Object[][] contentArray;
	private Color lightGreenColor = SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN);// 绿色
	private Color lightRedColor = SWTResourceManager.getColor(SWT.COLOR_RED);// 红色
	private Map<String, Color> contentBackgroundMap = new HashMap<String, Color>();
	private String hiddenResultTable;
	private String supplierName;
	private String EVAL_QUALIFIED = "符合";
	private String EVAL_UNQUALIFIED = "不符合";
	private Composite tabItem_composite;
	private Composite expert_list_composite;
	private Label message_label;
	private RectangleRadioButton curExpert;
	private Composite load_composite;
	private Map<String, RectangleRadioButton> expertEvaluationBtnMap;
	private ExpertEvaluationListener expertEvaluationListener;
	private ELink applyReEvaluation_link;
	
	{
		LogUtils.logClass(CleanTenderCheckResultComposite.class);
	}
	
	/**
	 * @param parent
	 * @param style
	 */
	public CleanTenderCheckResultComposite(Composite parent, int style) {
		super(parent, style);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				imageResolver.disposite();
				if (null != expertList) {
					expertList.clear();
				}
				contentBackgroundMap.clear();
			}
		});
		this.projectRule = (TenderProjectRuleEntity) parent.getData("projectRule");
		this.tenderProject = (TenderProjectEntity) parent.getData("tenderProject");
		this.evaluationStep = (EvaluationStep) parent.getData("evaluationStep");
		this.CURRENT_EXPERT = (Expert) parent.getData(Constants.CUR_EXPERT);
		this.hiddenResultTable = (String) parent.getData("hiddenResultTable");
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 1;
		gridLayout.marginWidth = 1;
		setLayout(gridLayout);
		
		tabItem_composite = new Composite(this, SWT.NONE);
		tabItem_composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_tabItem_composite = new GridLayout(1, false);
		gl_tabItem_composite.marginTop = 5;
		gl_tabItem_composite.verticalSpacing = 0;
		gl_tabItem_composite.marginWidth = 0;
		gl_tabItem_composite.marginHeight = 0;
		tabItem_composite.setLayout(gl_tabItem_composite);
		
		expert_list_composite = new Composite(tabItem_composite, SWT.NONE);
		expert_list_composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		RowLayout rl_expert_list_composite = new RowLayout(SWT.HORIZONTAL);
		rl_expert_list_composite.marginLeft = 0;
		rl_expert_list_composite.marginBottom = 0;
		rl_expert_list_composite.spacing = 5;
		expert_list_composite.setLayout(rl_expert_list_composite);
		
		RectangleRadioButton label = new RectangleRadioButton(expert_list_composite, SWT.NONE);
		label.setText("郭永荣");
		
		ESepator expert_tab = new ESepator(tabItem_composite, SWT.NONE);
		expert_tab.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		expert_tab.setBackground(ColorConstants.SKIN_BG);
		expert_tab.setLineWidth(2);
		
		Composite message_composite = new Composite(this, SWT.NONE);
		GridLayout gl_composite_6 = new GridLayout(3, false);
		message_composite.setLayout(gl_composite_6);
		message_composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		message_label = new Label(message_composite, SWT.NONE);
		message_label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		message_label.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		message_label.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		
		applyReEvaluation_link = new ELink(message_composite, SWT.NONE);
		applyReEvaluation_link.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/hand.png"));
		applyReEvaluation_link.setText("申请退回");
		applyReEvaluation_link.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				applyReEvaluationAction();
			}
		});
		
		ELink changeAxisBtn = new ELink(message_composite, SWT.NONE);
		changeAxisBtn.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		changeAxisBtn.setText("查看工程量清单");
		changeAxisBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/view.png"));
		changeAxisBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				ViewBoqAction();
			}
		});
		
		Composite composite_2 = new Composite(this, SWT.NONE);
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		composite_2.setBackground(ColorConstants.BORDER_LINE_COLOR);
		GridLayout gl_composite_2 = new GridLayout(1, false);
		gl_composite_2.marginHeight = 1;
		gl_composite_2.marginWidth = 1;
		composite_2.setLayout(gl_composite_2);
		
		load_composite = new Composite(composite_2, SWT.NONE);
		load_composite.setBackgroundMode(SWT.INHERIT_FORCE);
		load_composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_load_composite = new GridLayout(1, false);
		gl_load_composite.verticalSpacing = 0;
		gl_load_composite.horizontalSpacing = 0;
		gl_load_composite.marginHeight = 0;
		gl_load_composite.marginWidth = 0;
		load_composite.setLayout(gl_load_composite);
		load_composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		init();
	}
	
	/**
	 * @Description: 界面数据初始化
	 * @author guoyr
	 */
	private void init() {
		supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProject.getTenderType());
		if (null == projectRule) projectRule = new TenderProjectRuleEntity();
		
		// 获得清标检查项
		dictionaryList = dictionaryValueService.getCleanTenderItemList(tenderProject.getId());
		
		expertList = expertService.getEvaluationStepExpertList(tenderProject.getId(), evaluationStep);
		
		supplierList = supplierService.getEvaluationStepSupplierList(tenderProject.getId(), evaluationStep);
		
		if (null != CURRENT_EXPERT && Constants.USER.getId().equals(CURRENT_EXPERT.getUserId())) {
			applyReEvaluation_link.setText("申请退回[" + evaluationStep.getStepName() + "]");
		} else {
			applyReEvaluation_link.dispose();
		}
		
		// 为了显示样式，没有专家时生成一个临时专家
		if (null == expertList || expertList.size() == 0) {
			expertList = new ArrayList<Expert>();
			Expert expert = new Expert();
			expertList.add(expert);
		}
		// 为了显示样式，没有投标人时生成临时投标人
		if (null == supplierList || supplierList.size() == 0) {
			supplierList = new ArrayList<Supplier>();
			Supplier tempSupplier = new Supplier();
			tempSupplier.setSupplierName("                    ");
			supplierList.add(tempSupplier);
		}
		for (Control control : expert_list_composite.getChildren()) {
			control.dispose();
		}
		if (null != expertList && expertList.size() > 0) {
			expertEvaluationBtnMap = new HashMap<String, RectangleRadioButton>();
			int count = 0;
			for (final Expert _expert : expertList) {
				AuthUser user = _expert.getUser();
				if (null != this.CURRENT_EXPERT && !ObjectUtils.equals(this.CURRENT_EXPERT.getUserId(), _expert.getUserId())) {
					continue;
				}
				
				final RectangleRadioButton label = new RectangleRadioButton(expert_list_composite, SWT.NONE);
				label.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseUp(MouseEvent e) {
						slectExpertAction(label, _expert);
					}
				});
				if (null != user && null != user.getUserName()) {
					label.setText(user.getUserName() + (!ExpertEnum.EXPERT.equals(_expert.getExpertType()) ? "（" + _expert.getExpertTypeCN() + "）" : ""));
				} else {
					label.setText("专家" + (count + 1));
				}
				expertEvaluationBtnMap.put(_expert.getUserId(), label);
				if (count++ == 0) {
					curExpert = label;
				}
			}
			
			if (null == hiddenResultTable) {
				final RectangleRadioButton label = new RectangleRadioButton(expert_list_composite, SWT.NONE);
				label.addMouseListener(new MouseAdapter() {
					
					@Override
					public void mouseUp(MouseEvent e) {
						slectExpertAction(label, null);
					}
				});
				label.setText(evaluationStep.getStepName() + "汇总");
				expertEvaluationBtnMap.put("totalExpert", label);
			}
			if (null != curExpert) {
				curExpert.notifyListeners(SWT.MouseUp, null);
			}
		}
	}
	
	/**
	 * @Description: 加载评审表格界面
	 * @author guoyr
	 * @param evaluationTableComposite
	 * @param userId
	 */
	private void drawEvaluationTable(String userId) {
		int rowCount = supplierList.size() + 2;
		int colcount = dictionaryList.size() * 2 + 2;
		contentBackgroundMap.clear();
		contentArray = new Object[rowCount][colcount];
		// 设置标题
		contentArray[0][0] = supplierName + "名称";
		contentArray[0][contentArray[0].length - 1] = "结论";
		
		// 设置内容
		int row = 2;
		for (Supplier supplier : supplierList) {
			
			// 供应商名称
			contentArray[row][0] = supplier.getSupplierName();
			
			int col = 1;
			for (DictionaryValue dictionaryValue : dictionaryList) {
				if (row == 2) {
					contentArray[0][col] = dictionaryValue.getDictValueName();
					contentArray[1][col] = "结果";
					contentArray[1][col + 1] = "是否通过";
				}
				
				String accessMent = "";
				CheckResult checkResult = checkResultService.getCheckResultByCondition(tenderProject.getId(), userId, supplier.getId(), dictionaryValue.getId(), evaluationStep.getId());
				if (null != checkResult) {
					if (ObjectUtils.equals(checkResult.getAccessment(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.QUALIFIED)) {
						accessMent = EVAL_QUALIFIED;
						contentBackgroundMap.put(row + "_" + col, null);
						contentBackgroundMap.put(row + "_" + (col + 1), lightGreenColor);
					}
					if (ObjectUtils.equals(checkResult.getAccessment(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED)) {
						accessMent = EVAL_UNQUALIFIED;
						contentBackgroundMap.put(row + "_" + col, lightRedColor);
						contentBackgroundMap.put(row + "_" + (col + 1), lightRedColor);
					}
					contentArray[row][col] = checkResult.getCheckResult();
				}
				contentArray[row][col + 1] = accessMent;
				col += 2;
			}
			
			ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultService.getExpertEvaluationResult(tenderProject.getId(), evaluationStep.getId(), supplier.getId(), userId);
			if (null != expertEvaluationResult) {
				if (null != expertEvaluationResult.getAccessment()) {
					if (expertEvaluationResult.getAccessment().equals(com.gx.obe.components.core.enumeration.EvaluationStepEnum.QUALIFIED)) {
						contentBackgroundMap.put(row + "_" + (contentArray[0].length - 1), lightGreenColor);
						contentArray[row][contentArray[0].length - 1] = EVAL_QUALIFIED;
					} else if (expertEvaluationResult.getAccessment().equals(com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED)) {
						contentBackgroundMap.put(row + "_" + (contentArray[0].length - 1), lightRedColor);
						contentArray[row][contentArray[0].length - 1] = EVAL_UNQUALIFIED;
					}
				}
			}
			row++;
		}
		
		if (null != resultKTable && !resultKTable.isDisposed()) {
			resultKTable.dispose();
		}
		resultKTable = new KTable(load_composite, 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, 2, 1, supplierList.size(), dictionaryList.size() * 2 + 1);
		
		commKTableModel.setContentRowHeight(30);// 设置行高
		commKTableModel.setColumnMaxWidth(commKTableModel.getColCount() - 1, 50);// 设置最后一列宽为70
		commKTableModel.setColumnMinWidth(0, 200);
		
		// 合并行
		commKTableModel.addRowSpan(0, 0, 2);// 投标单位
		commKTableModel.addRowSpan(0, commKTableModel.getColumnCount() - 1, 2);// 是否通过
		
		// 合并列
		for (int col = 1; col < dictionaryList.size() * 2; col = col + 2) {
			commKTableModel.addColSpan(0, col, 2);
			commKTableModel.setColumnWidth(col, 200);
			commKTableModel.setColumnWidth(col + 1, 60);
		}
		
		// 设置单元格内容
		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.getFixedColumnCount()) % 2 == 0) {
					return null != content ? content.toString() : "";
				} else {
					return null != content ? contentArray[row][col] : "";
				}
			}
		};
		commKTableModel.setCellContentRender(contentRender);
		
		final FixedWrapCellRenderer fixedWrapCellRenderer = new FixedWrapCellRenderer();
		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 textCellRenderer;
					} else {
						if ((col - commKTableModel.getFixedColumnCount()) % 2 == 0) {// 显示错误数的列
							return textWrapCellRenderer;
						} else {
//							return checkableCellRenderer;// 显示是否通过
							return textCellRenderer;// 显示是否通过
						}
					}
				}
			}
		};
		commKTableModel.setCellRender(cellRender);
		
		// 编辑单元格对齐方式
		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 {
					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.addCellDoubleClickListener(new KTableCellDoubleClickListener() {
			public void cellDoubleClicked(int col, int row, int statemask) {
				if (col >= commKTableModel.getFixedColumnCount() && supplierList.size() > 0) {
					Supplier supplier = supplierList.get(row - commKTableModel.getFixedRowCount());
					if (((col - commKTableModel.getFixedColumnCount()) / 2) < dictionaryList.size()) {
						DictionaryValue dictValue = dictionaryList.get((col - commKTableModel.getFixedColumnCount()) / 2);
						ViewCleanTenderDetailDialog dialog = new ViewCleanTenderDetailDialog(getShell(), tenderProject, supplier, evaluationStep, dictValue);
						dialog.open();
					}
				}
			}
			
			public void fixedCellDoubleClicked(int arg0, int arg1, int arg2) {
				
			}
		});
		
		resultKTable.setModel(commKTableModel);
		
	}
	
	/**
	 * @Description: 加载评审汇总表界面
	 * @author guoyr
	 * @param evaluationTableComposite
	 */
	private void drawEvaluationResultTable() {
		
		KTable resultKTable = new KTable(load_composite, SWTX.AUTO_SCROLL | SWT.FULL_SELECTION | SWTX.EDIT_ON_KEY);
		resultKTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		final CommonKtableCModel commKTableModel = new CommonKtableCModel(resultKTable, 1, 1, supplierList.size(), 2);
		final String contentArray[][] = new String[commKTableModel.getRowCount()][commKTableModel.getColCount()];
		final Map<String, Color> contentBackgroundMap = new HashMap<String, Color>();
		
		List<SupplierEvaluationStep> supplierEvaluationStepList = supplierEvaluationStepService.getSupplierEvaluationStepByTenderIdAndStepId(tenderProject.getId(), evaluationStep.getId());
		contentArray[0][0] = supplierName + "名称";
		contentArray[0][1] = "评审统计";
		contentArray[0][2] = "结论";
		for (int row = commKTableModel.getFixedRowCount(); row < commKTableModel.getFixedRowCount() + supplierList.size(); row++) {
			Supplier supplier = supplierList.get(row - commKTableModel.getFixedRowCount());
			contentArray[row][0] = supplier.getSupplierName();
			// 获取评标专家对供应商的评审记录
			SupplierEvaluationStep supplierEvaluationStep = null;
			if (null != supplierEvaluationStepList && supplierEvaluationStepList.size() > 0) {
				for (SupplierEvaluationStep querySupplierEvaluationStep : supplierEvaluationStepList) {
					if (ObjectUtils.equals(querySupplierEvaluationStep.getSupplierId(), supplier.getId())) {
						supplierEvaluationStep = querySupplierEvaluationStep;
					}
				}
			}
			List<String> memberSupplierResutlList = expertEvaluationResultService.getSupplierStepAllExpertEvaluationAccessmentList(tenderProject.getId(), evaluationStep.getId(), supplier.getId());
			if (null != memberSupplierResutlList && memberSupplierResutlList.size() > 0) {
				StringBuilder msg = new StringBuilder();
				int accessYesCount = 0;
				int accessNoCount = 0;
				for (Object accessmentObj : memberSupplierResutlList) {
					if (ObjectUtils.equals(ObjectUtils.getObjString(accessmentObj), com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED)) {
						accessNoCount++;
					} else {
						accessYesCount++;
					}
				}
				if (accessYesCount > 0) {
					msg.append(accessYesCount);
					msg.append("位专家评审");
					msg.append(EVAL_QUALIFIED);
				}
				if (accessNoCount > 0) {
					if (accessYesCount > 0) {
						msg.append("；");
					}
					msg.append(accessNoCount);
					msg.append("位专家评审");
					msg.append(EVAL_UNQUALIFIED);
				}
				msg.append("。");
				contentArray[row][1] = msg.toString();
			}
			
			if (null != supplierEvaluationStep) {
				if (ObjectUtils.equals(supplierEvaluationStep.getAccessment(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.QUALIFIED)) {
					contentArray[row][2] = EVAL_QUALIFIED;
					contentBackgroundMap.put(row + "_" + 2, SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
				} else {
					contentArray[row][2] = EVAL_UNQUALIFIED;
					contentBackgroundMap.put(row + "_" + 2, SWTResourceManager.getColor(SWT.COLOR_RED));
					
				}
			}
		}
		
		// 设置单元格内容
		CellContentRender contentRender = new CellContentRender() {
			public Object receiveContent(int row, int col) {
				if (null != contentArray[row][col]) {
					return contentArray[row][col];
				}
				return "";
			}
		};
		commKTableModel.setCellContentRender(contentRender);
		
		// 设置单元格渲染器
		CellRender cellRender = new CellRender() {
			public Object receiveCellRender(int row, int col) {
				if (commKTableModel.isFixedCell(col, row)) {
					return new FixedWrapCellRenderer();
				} else {
					return new TextWrapCellRenderer();
				}
			}
		};
		commKTableModel.setCellRender(cellRender);
		
		// 编辑单元格对齐方式
		CellAlignmentRender cellAlignmentRender = new CellAlignmentRender() {
			public int receiveCellAlignment(int row, int col) {
				if (row >= commKTableModel.getFixedRowCount() && col == 0) {
					return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_LEFT;
				} else {
					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)) {
					return contentBackgroundMap.get(row + "_" + col);
				}
				return null;
			}
		};
		commKTableModel.setCellForegroundRender(cellForegroundRender);
		resultKTable.setModel(commKTableModel);
	}
	
	/**
	 * @Description: 选中专家事件
	 * @author guoyr
	 */
	private void slectExpertAction(final RectangleRadioButton label, final Expert expert) {
		for (Control control : load_composite.getChildren()) {
			control.dispose();
		}
		for (Control control : load_composite.getChildren()) {
			control.dispose();
		}
		if (null != curExpert && !curExpert.isDisposed()) {
			curExpert.setSelection(false);
		}
		curExpert = label;
		curExpert.setSelection(true);
		if (!applyReEvaluation_link.isDisposed()) {
			applyReEvaluation_link.setVisible(false);
		}
		StringBuilder msg = new StringBuilder();
		if (null != expert) {
			String expertUserId = expert.getUserId();
			ExpertEvaluationStep expertEvaluationStep = expertEvaluationStepService.getExpertEvaluationStep(tenderProject.getId(), evaluationStep.getId(), expertUserId);
			if (null != expertEvaluationStep) {
				msg.append("[");
				msg.append(evaluationStep.getStepName());
				msg.append("]评标开始时间：");
				msg.append(null != expertEvaluationStep.getStartTime() ? DateUtils.getTimestampFormat(expertEvaluationStep.getStartTime()) : "");
				msg.append("---");
				if (ObjectUtils.equals(expertEvaluationStep.getStepStatus(), CommonEnum.COMPLETE)) {
					msg.append("评标结束时间：");
					msg.append(null != expertEvaluationStep.getEndTime() ? DateUtils.getTimestampFormat(expertEvaluationStep.getEndTime()) : "");
					
//					long useTime = expertEvaluationStep.getEndTime().getTime() - expertEvaluationStep.getStartTime().getTime();
//					if(useTime > 0){
//						msg.append("   用时：");
//						msg.append(DateUtils.timeSpan(useTime));
//					}
					
					if (!applyReEvaluation_link.isDisposed()) {
						applyReEvaluation_link.setVisible(true);
						applyReEvaluation_link.setData("expertEvaluationStep", expertEvaluationStep);
					}
				} else {
					msg.append("[");
					msg.append(evaluationStep.getStepName());
					msg.append("]进行中...   ");
				}
			} else {
				msg.append("未开始[");
				msg.append(evaluationStep.getStepName());
				msg.append("]！");
			}
			msg.append("双击清标项可以查看详情！");
			
			drawEvaluationTable(expertUserId);
			refreshExpertEvaluationStatus();
		} else {
			if (null != evaluationStep.getStepStatus() && evaluationStep.getStepStatus().equals(CommonEnum.COMPLETE)) {
				msg.append("已完成[");
				msg.append(evaluationStep.getStepName());
				msg.append("]！   ");
			} else if (null != evaluationStep.getStepStatus() && evaluationStep.getStepStatus().equals(CommonEnum.IN_PROGRESS)) {
				msg.append("[");
				msg.append(evaluationStep.getStepName());
				msg.append("]进行中...   ");
			} else {
				msg.append("未开始[");
				msg.append(evaluationStep.getStepName());
				msg.append("]！");
			}
			if (ObjectUtils.equals(evaluationStep.getFilterType(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.FILTER_YES)) {
				msg.append("   评标规则：");
				if (ObjectUtils.equals(evaluationStep.getPassType(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.PASS_MORE)) {
					msg.append("“少数服从多数”");
				} else {
					
					msg.append("“一票否决”；");
				}
				msg.append("  结论为“");
				msg.append(EVAL_UNQUALIFIED);
				msg.append("”的");
				msg.append(supplierName);
				msg.append("不能入围下一评标环节！");
			} else {
				msg.append("评标规则：所有");
				msg.append(supplierName);
				msg.append("全部入围下一评标环节！");
			}
			drawEvaluationResultTable();
		}
		message_label.setText(msg.toString());
		if (null != expert) {
			curExpert.setData(expert.getId());
		} else {
			curExpert.setData(null);
		}
		load_composite.layout();
	}
	
	/**
	 * @Description: 刷新专家评标进度
	 * @author guoyr
	 */
	public void refreshExpertEvaluationStatus() {
		// 只有显示全部专家时才刷新专家的评标完成状态
		if (null != this.CURRENT_EXPERT) {
			return;
		}
		List<ExpertEvaluationStep> expertEvaluationStepList = expertEvaluationStepService.getEvaluationStepListByStepId(tenderProject.getId(), evaluationStep.getId());
		if (null != expertEvaluationStepList && expertEvaluationStepList.size() > 0 && null != expertEvaluationBtnMap) {
			boolean complate = expertList.size() == expertEvaluationStepList.size();
			boolean refresh = false;
			for (ExpertEvaluationStep expertEvaluationStep : expertEvaluationStepList) {
				RectangleRadioButton expertStepBtn = expertEvaluationBtnMap.get(expertEvaluationStep.getUserId());
				if (null != expertStepBtn && !expertStepBtn.isDisposed()) {
					if (ObjectUtils.equals(expertEvaluationStep.getStepStatus(), CommonEnum.COMPLETE)) {
						if (null == expertStepBtn.getImage()) {
							expertStepBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/green/ok.png"));
							refresh = true;
						}
					} else {
						complate = false;
						if (null != expertStepBtn.getImage()) {
							expertStepBtn.setImage(null);
							refresh = true;
						}
					}
				}
			}
			RectangleRadioButton totalExpertBtn = expertEvaluationBtnMap.get("totalExpert");
			if (null != totalExpertBtn && !totalExpertBtn.isDisposed()) {
				if (complate) {
					if (null == totalExpertBtn.getImage()) {
						totalExpertBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/green/ok.png"));
						refresh = true;
					}
				} else {
					if (null != totalExpertBtn.getImage()) {
						totalExpertBtn.setImage(null);
						refresh = true;
					}
				}
			}
			if (refresh) {
				if (null != expertEvaluationListener && complate) {
					expertEvaluationListener.refresh();
				}
				expert_list_composite.layout();
			}
		}
	}
	
	/**
	 * @Description: 查看工程量清单
	 * @author guoyr
	 */
	public void ViewBoqAction() {
		ViewBoqDialog dialog = new ViewBoqDialog(getShell(), tenderProject, null, evaluationStep);
		dialog.open();
	}
	
	/**
	 * @Description: 申请回退
	 * @author guoyr
	 */
	private void applyReEvaluationAction() {
		if (applyReEvaluation_link.getEnabled()) {
			if (null != applyReEvaluation_link.getData("expertEvaluationStep")) {
				ExpertEvaluationStep expertEvaluationStep = (ExpertEvaluationStep) applyReEvaluation_link.getData("expertEvaluationStep");
				if (null != expertEvaluationStep) {
					ApplyReEvaluationDialog applyReEvaluationDialog = new ApplyReEvaluationDialog(getShell(), evaluationStep, expertEvaluationStep);
					applyReEvaluationDialog.open();
				}
			}
		}
	}
	
	public void addExpertEvaluationListener(ExpertEvaluationListener expertEvaluationListener) {
		this.expertEvaluationListener = expertEvaluationListener;
	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
