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

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
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.custom.ScrolledComposite;
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 org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
import com.gx.obe.action.bean.Action;
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.BidFileOpinionDialog;
import com.gx.obe.evaluation.step.listener.ExpertEvaluationListener;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.DateUtils;
import com.gx.obe.util.utils.ListSortUtils;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.NumericChineseUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.StringUtils;
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.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.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.TextWrapCellRenderer;

/**
 * @Description:
 * @author guoyr
 */
public class RecommendRankEvaluationResultComposite extends Composite {
	
	private TenderProjectEntity tenderProjectEntity;
	private TenderProjectRuleEntity tenderProjectRuleEntity;
	private EvaluationStep evaluationStep;
	private Expert CURRENT_EXPERT;
	private ImageResolver imageResolver = new ImageResolver();
	private SupplierService supplierService = new SupplierService();
	private ExpertService expertService;
	private ExpertEvaluationResultService expertEvaluationResultService;
//	private ExpertEvaluationStepService expertEvaluationStepService ;
	private SupplierEvaluationStepService supplierEvaluationStepService;
	private List<Expert> expertList;
	private List<Supplier> supplierList;
	private boolean isShowObjectiveDialog = false;// 是否显示客观项评审意见信息。
	private String hiddenResultTable;
	private String supplierName;
	private Map<String, Color> contentBackgroundMap = new HashMap<String, Color>();
	private Composite expert_list_composite;
	private Composite load_composite;
	private RectangleRadioButton curExpert;
	private Label message_label;
	private Composite tabItem_composite;
	private Map<String, RectangleRadioButton> expertEvaluationBtnMap;
	private ExpertEvaluationListener expertEvaluationListener;
	private ELink applyReEvaluation_link;
	private Composite message_composite;
	private int recommendCount;
	private HashMap<Integer, Supplier> supplierMap;
	private Map<String, ExpertEvaluationResult> expertEvaluationResultMap;
	private ELink viewSupplierResponse_btn;
	{
		LogUtils.logClass(RecommendRankEvaluationResultComposite.class);
	}
	
	private ExpertEvaluationStepService expertEvaluationStepService = new ExpertEvaluationStepService();
	
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public RecommendRankEvaluationResultComposite(Composite parent, Action action, int style) {
		super(parent, style);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				imageResolver.disposite();
				if (null != expertList) {
					expertList.clear();
				}
				contentBackgroundMap.clear();
			}
		});
		this.tenderProjectRuleEntity = action.getTenderProjectRule();
		this.tenderProjectEntity = action.getTenderProject();
		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);
		
		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);
		
		message_composite = new Composite(this, SWT.NONE);
		GridLayout gl_composite_6 = new GridLayout(5, 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.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
		applyReEvaluation_link.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/hand.png"));
		applyReEvaluation_link.setText(Messages.Button_appllyForBack);
		applyReEvaluation_link.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				applyReEvaluationAction();
			}
		});
		
		viewSupplierResponse_btn = new ELink(message_composite, SWT.NONE);
		viewSupplierResponse_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/view.png"));
		viewSupplierResponse_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				viewSupplierOpinionAndResponse(false);
			}
		});
		viewSupplierResponse_btn.setText("查看投标响应");
		
		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() {
		expertEvaluationResultService = new ExpertEvaluationResultService();
		expertService = new ExpertService();
		supplierEvaluationStepService = new SupplierEvaluationStepService();
		supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProjectEntity.getTenderType());
		
		recommendCount = evaluationStep.getEvaluationParamByKey(com.gx.obe.components.core.enumeration.EvaluationStepEnum.RECOMMEND_COUNT, Integer.class, 3);
		if (null == tenderProjectRuleEntity) tenderProjectRuleEntity = new TenderProjectRuleEntity();
		supplierList = supplierService.getEvaluationStepSupplierList(tenderProjectEntity.getId(), evaluationStep);
		
		expertList = expertService.getEvaluationStepExpertList(tenderProjectEntity.getId(), evaluationStep);
		if (null != CURRENT_EXPERT && Constants.USER.getId().equals(CURRENT_EXPERT.getUserId())) {
			applyReEvaluation_link.setText(MessageFormat.format(Messages.Button_appllyForBackFormat, 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(Messages.Expert + (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
	 */
	public void refreshExpertEvaluationStatus() {
		// 只有显示全部专家时才刷新专家的评标完成状态
		if (null != this.CURRENT_EXPERT) {
			return;
		}
		List<ExpertEvaluationStep> expertEvaluationStepList = expertEvaluationStepService.getEvaluationStepListByStepId(tenderProjectEntity.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
	 * @param userId
	 */
	private void initEvaluationResult(String userId) {
		// 评审当前评标专家所有的评标结果
		List<ExpertEvaluationResult> queryExpertEvaluationResultList = expertEvaluationResultService.getExpertStepAllSupplierEvaluationResultList(tenderProjectEntity.getId(), evaluationStep.getId(), userId);
		contentBackgroundMap.clear();
		
		if (null != supplierMap) {
			supplierMap.clear();
		} else {
			supplierMap = new HashMap<Integer, Supplier>();
		}
		if (null != expertEvaluationResultMap) {
			expertEvaluationResultMap.clear();
		} else {
			expertEvaluationResultMap = new HashMap<String, ExpertEvaluationResult>();
		}
		
		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) {
				expertEvaluationResultMap.put(supplier.getId(), expertEvaluationResult);
				int sortNo = expertEvaluationResult.getSortNoIntValue();
				if (sortNo != 0) {
					supplierMap.put(sortNo, supplier);
				}
			}
		}
	}
	
	/**
	 * @Description: 实现评审汇总表格
	 * @author guoyr
	 */
	private void drawEvaluationResultTable() {
		
		List<SupplierEvaluationStep> querySupplierEvaluationStepList = null;
		if (ObjectUtils.equals(evaluationStep.getStepStatus(), CommonEnum.COMPLETE)) {
			querySupplierEvaluationStepList = supplierEvaluationStepService.getSupplierEvaluationStepByTenderIdAndStepId(tenderProjectEntity.getId(), evaluationStep.getId());
		}
		
		List<ExpertEvaluationResult> queryExpertEvaluationResultList = expertEvaluationResultService.getExpertStepAllSupplierEvaluationResultList(tenderProjectEntity.getId(), evaluationStep.getId(), null);
		
		final String[] title = new String[recommendCount + 2];
		title[0] = MessageFormat.format(Messages.NameFormat, com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProjectEntity.getTenderType()));
		int i = 1;
		while (i <= recommendCount) {
			title[i] = "第" + NumericChineseUtils.getChinese(i) + "名得票数量";
			i++;
		}
		title[i] = "最终排名";
		
		final HashMap<String, Integer> supplierRankMap = new HashMap<String, Integer>();
		final HashMap<String, Map<Integer, Integer>> supplierRankVoteMap = new HashMap<String, Map<Integer, Integer>>();
		for (Supplier supplier : supplierList) {
			if (null != querySupplierEvaluationStepList && querySupplierEvaluationStepList.size() > 0) {
				for (SupplierEvaluationStep evaluationStep : querySupplierEvaluationStepList) {
					if (StringUtils.equals(supplier.getId(), evaluationStep.getSupplierId())) {
						supplierRankMap.put(supplier.getId(), evaluationStep.getSortNo());
						break;
					}
				}
			}
			Map<Integer, Integer> supplierExpertVoteMap = new HashMap<Integer, Integer>();
			if (null != queryExpertEvaluationResultList && queryExpertEvaluationResultList.size() > 0) {
				for (ExpertEvaluationResult expertEvaluationResult : queryExpertEvaluationResultList) {
					if (StringUtils.equals(supplier.getId(), expertEvaluationResult.getSupplierId())) {
						int sortNo = expertEvaluationResult.getSortNoIntValue();
						int value = supplierExpertVoteMap.containsKey(sortNo) ? supplierExpertVoteMap.get(sortNo) : 0;
						supplierExpertVoteMap.put(expertEvaluationResult.getSortNoIntValue(), ++value);
					}
				}
			}
			supplierRankVoteMap.put(supplier.getId(), supplierExpertVoteMap);
		}
		
		if (supplierRankMap.size() == 0) {
			ListSortUtils<Supplier> listSortUtils = new ListSortUtils<Supplier>(supplierList, new Comparator<Supplier>() {
				public int compare(Supplier behindSupplier, Supplier frontSupplier) {
					Map<Integer, Integer> frontRankMap = supplierRankVoteMap.get(frontSupplier.getId());
					Map<Integer, Integer> behindRankMap = supplierRankVoteMap.get(behindSupplier.getId());
					return compareMap(frontRankMap, behindRankMap, 1, recommendCount);
				}
			});
			
			for (Supplier supplier : supplierList) {
				supplierRankMap.put(supplier.getId(), listSortUtils.getSortNo(supplier));
			}
		}
		
		Collections.sort(supplierList, new Comparator<Supplier>() {
			public int compare(Supplier behindSupplier, Supplier frontSupplier) {
				if (supplierRankMap.containsKey(frontSupplier.getId()) && supplierRankMap.containsKey(behindSupplier.getId())) {
					int frontSortNo = supplierRankMap.get(frontSupplier.getId());
					int behindSortNo = supplierRankMap.get(behindSupplier.getId());
					return behindSortNo - frontSortNo;
				}
				return 0;
			}
		});
		
		for (Control control : load_composite.getChildren()) {
			control.dispose();
		}
		
		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 commonKtableCModel = new CommonKtableCModel(resultKTable, 1, 1, supplierList.size(), title.length - 1);
		// 设置列宽
		commonKtableCModel.setColumnMaxWidth(0, 200);
		commonKtableCModel.setColumnMinWidth(0, 150);
		for (int j = 0; j < supplierList.size(); j++) {
			commonKtableCModel.setRowHeight(j, Constants.ROW_HIGHT);
		}
		CellContentRender contentRender = new CellContentRender() {
			public Object receiveContent(int row, int col) {
				if (row < commonKtableCModel.getFixedHeaderRowCount()) {
					return title[col];
				} else {
					Supplier supplier = supplierList.get(row - commonKtableCModel.getFixedHeaderRowCount());
					if (null != supplier) {
						if (col == 0) {
							return supplier.getSupplierName();
						} else if (col <= recommendCount) {
							if (supplierRankVoteMap.containsKey(supplier.getId())) {
								Map<Integer, Integer> map = supplierRankVoteMap.get(supplier.getId());
								if (map.containsKey(col)) {
									return map.get(col);
								}
							}
						} else {
							if (supplierRankMap.containsKey(supplier.getId())) {
								return supplierRankMap.get(supplier.getId());
							}
						}
					}
					return "";
				}
			}
		};
		commonKtableCModel.setCellContentRender(contentRender);
		// 设置单元格渲染器
		CellRender cellRender = new CellRender() {
			public Object receiveCellRender(int row, int col) {
				if (commonKtableCModel.isFixedCell(col, row)) {
					return new FixedWrapCellRenderer();
				} else {
					return new TextWrapCellRenderer();
				}
			}
		};
		commonKtableCModel.setCellRender(cellRender);
		
		// 编辑单元格对齐方式
		CellAlignmentRender cellAlignmentRender = new CellAlignmentRender() {
			public int receiveCellAlignment(int row, int col) {
				if (row >= commonKtableCModel.getFixedHeaderColumnCount() && col == 0) {
					return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_LEFT;
				}
				return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
			}
		};
		commonKtableCModel.setCellAlignmentRender(cellAlignmentRender);
		
		// 设置单元格前景颜色
		CellForegroundRender cellForegroundRender = new CellForegroundRender() {
			public Color receiveCellForeground(int row, int col) {
				return null;
			}
		};
		commonKtableCModel.setCellForegroundRender(cellForegroundRender);
		resultKTable.setModel(commonKtableCModel);
	}
	
	/**
	 * @Description: 选中专家事件
	 * @author guoyr
	 */
	private void slectExpertAction(final RectangleRadioButton label, final Expert expert) {
		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) {
			ExpertEvaluationStep expertEvaluationStep = expertEvaluationStepService.getExpertEvaluationStep(tenderProjectEntity.getId(), evaluationStep.getId(), expert.getUserId());
			if (null != expertEvaluationStep) {
				msg.append("[");
				msg.append(evaluationStep.getStepName());
				msg.append("]");
				msg.append(Messages.EvalStartTime);
				msg.append("：");
				msg.append(null != expertEvaluationStep.getStartTime() ? DateUtils.getTimestampFormat(expertEvaluationStep.getStartTime()) : "");
				msg.append("---");
				if (ObjectUtils.equals(expertEvaluationStep.getStepStatus(), CommonEnum.COMPLETE)) {
					msg.append(Messages.EvalEndTime);
					msg.append("：");
					msg.append(null != expertEvaluationStep.getEndTime() ? DateUtils.getTimestampFormat(expertEvaluationStep.getEndTime()) : "");
					if (!applyReEvaluation_link.isDisposed()) {
						applyReEvaluation_link.setVisible(true);
						applyReEvaluation_link.setData("expertEvaluationStep", expertEvaluationStep);
					}
				} else {
					msg.append(MessageFormat.format(Messages.Tips_progressIn, evaluationStep.getStepName()));
				}
			} else {
				msg.append(MessageFormat.format(Messages.Tips_progressNotStart, evaluationStep.getStepName()));
//				msg.append("点击“切换坐标”，可切换指标坐标！");
			}
			
			initEvaluationResult(expert.getUserId());
			drawRankEvaluationTable(expert.getUserId());
			refreshExpertEvaluationStatus();
		} else {
			if (null != evaluationStep.getStepStatus() && evaluationStep.getStepStatus().equals(CommonEnum.COMPLETE)) {
				msg.append(MessageFormat.format(Messages.Tips_progressComplate, evaluationStep.getStepName()));
			} else if (null != evaluationStep.getStepStatus() && evaluationStep.getStepStatus().equals(CommonEnum.IN_PROGRESS)) {
				msg.append(MessageFormat.format(Messages.Tips_progressIn, evaluationStep.getStepName()));
			} else {
				msg.append(MessageFormat.format(Messages.Tips_progressNotStart, evaluationStep.getStepName()));
			}
			drawEvaluationResultTable();
		}
		message_label.setText(msg.toString());
		if (null != expert) {
			curExpert.setData(expert.getId());
		} else {
			curExpert.setData(null);
		}
		message_composite.layout();
		load_composite.layout();
	}
	
	private void drawRankEvaluationTable(String userId) {
		for (Control control : load_composite.getChildren()) {
			control.dispose();
		}
		
		ScrolledComposite scrolledComposite = new ScrolledComposite(load_composite, SWT.H_SCROLL | SWT.V_SCROLL);
		scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		
		Composite composite = new Composite(scrolledComposite, SWT.NONE);
		composite.setBackground(ColorConstants.BORDER_LINE_COLOR);
		GridLayout gl_composite = new GridLayout(2, false);
		gl_composite.verticalSpacing = 1;
		gl_composite.horizontalSpacing = 1;
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		drawRankEvaluationTitle(composite);
		for (int i = 1; i <= recommendCount; i++) {
			Supplier supplier = supplierMap.get(i);
			String supplierOpinion = null;
			if (null != supplier) {
				ExpertEvaluationResult expertEvaluationResult = expertEvaluationResultMap.get(supplier.getId());
				supplierOpinion = null != expertEvaluationResult ? expertEvaluationResult.getSupplierOpinion() : null;
			}
			drawRankEvaluationTableTd(composite, i, supplier, supplierOpinion);
		}
		
		Label empty = new Label(composite, SWT.NONE);
		empty.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
		empty.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		scrolledComposite.setContent(composite);
		scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
	}
	
	private void drawRankEvaluationTitle(Composite composite) {
		Composite rankTitleComposite = new Composite(composite, SWT.NONE);
		rankTitleComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		rankTitleComposite.setLayout(new GridLayout(1, false));
		GridData gd_rankTitleComposite = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
		gd_rankTitleComposite.widthHint = 200;
		rankTitleComposite.setLayoutData(gd_rankTitleComposite);
		
		Label rankTitleLabel = new Label(rankTitleComposite, SWT.NONE);
		rankTitleLabel.setFont(SWTResourceManager.getFont("微软雅黑", 11, SWT.NORMAL));
		rankTitleLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
		rankTitleLabel.setText(Messages.SortNo);
		
		Composite supplierTitleComposite = new Composite(composite, SWT.NONE);
		supplierTitleComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		supplierTitleComposite.setLayout(new GridLayout(2, false));
		GridData gd_supplierTitleComposite = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		supplierTitleComposite.setLayoutData(gd_supplierTitleComposite);
		
		Label supplierTitleLabel = new Label(supplierTitleComposite, SWT.NONE);
		supplierTitleLabel.setFont(SWTResourceManager.getFont("微软雅黑", 11, SWT.NORMAL));
		supplierTitleLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
		supplierTitleLabel.setText(supplierName);
		
	}
	
	private void drawRankEvaluationTableTd(Composite composite, int sortNo, Supplier supplier, String supplierOpinion) {
		Composite rankTitleComposite = new Composite(composite, SWT.NONE);
		rankTitleComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		rankTitleComposite.setLayout(new GridLayout(1, false));
		GridData gd_rankTitleComposite = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
		gd_rankTitleComposite.widthHint = 200;
		gd_rankTitleComposite.heightHint = 50;
		rankTitleComposite.setLayoutData(gd_rankTitleComposite);
		
		Label rankTitleLabel = new Label(rankTitleComposite, SWT.NONE);
		rankTitleLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		rankTitleLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
		rankTitleLabel.setText("第" + NumericChineseUtils.getChinese(sortNo) + "名");
		
		Composite supplierTitleComposite = new Composite(composite, SWT.NONE);
		supplierTitleComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		supplierTitleComposite.setLayout(new GridLayout(2, false));
		GridData gd_supplierTitleComposite = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		supplierTitleComposite.setLayoutData(gd_supplierTitleComposite);
		
		Label supplierTitleLabel = new Label(supplierTitleComposite, SWT.NONE);
		supplierTitleLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		supplierTitleLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, true, 1, 1));
		supplierTitleLabel.setText(null != supplier ? supplier.getSupplierName() : "");
		if (null != supplier) {
			Text supplierOpinionText = new Text(supplierTitleComposite, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.READ_ONLY);
			supplierOpinionText.setForeground(ColorConstants.NOTE_TIPS_COLOR);
			GridData gd_text = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
			gd_text.heightHint = 40;
			gd_text.widthHint = 300;
			supplierOpinionText.setLayoutData(gd_text);
			supplierOpinionText.setText("评审意见：" + (null != supplierOpinion ? supplierOpinion : ""));
		}
		
	}
	
	/**
	 * @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 boolean isShowObjectiveDialog() {
		return isShowObjectiveDialog;
	}
	
	public void addExpertEvaluationListener(ExpertEvaluationListener expertEvaluationListener) {
		this.expertEvaluationListener = expertEvaluationListener;
	}
	
	protected int compareMap(Map<Integer, Integer> frontRankMap, Map<Integer, Integer> behindRankMap, int index, int recommendCount) {
		if (index > recommendCount) {
			return 0;
		}
		int frontCount = frontRankMap.containsKey(index) ? frontRankMap.get(index) : 0;
		int behindCount = behindRankMap.containsKey(index) ? behindRankMap.get(index) : 0;
		if (frontCount == behindCount) {
			return compareMap(frontRankMap, behindRankMap, ++index, recommendCount);
		}
		return frontCount - behindCount;
	}
	
	/**
	 * @Description: 查看（编辑）投标人评议意见或投标响应
	 * @author guoyr
	 * @param isViewSupplierOpinion true:查看评议意见
	 */
	private void viewSupplierOpinionAndResponse(boolean isViewSupplierOpinion) {
		BidFileOpinionDialog dialog = new BidFileOpinionDialog(getShell(), tenderProjectEntity, evaluationStep, supplierList, null, null, isViewSupplierOpinion, false);
		dialog.open();
	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
