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

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.nebula.widgets.grid.Grid;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.nebula.widgets.grid.GridItem;
import org.eclipse.nebula.widgets.grid.listener.GridSelectionListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.core.widgets.ImageButton;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.I18N;
import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.components.core.enumeration.EvaluationFactorEnum;
import com.gx.obe.config.utils.PropertiesUtils;
import com.gx.obe.evaluation.step.dialog.ViewEvaluationResultDialog;
import com.gx.obe.evaluation.step.listener.AiEvaluationListener;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.util.utils.CollectionUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.SortUtils;
import com.gx.obe.web.entity.EvaluationFactorResult;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
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.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 智能评审
 * @author chenxw
 */
public class AiEvaluationComposite extends EvaluationComposite implements AiEvaluationListener {
	
	private Composite factorGridComposite;
	private Grid factorGrid;
	private ImageButton hideOrShowFactor_btn;
	private StyledText evaluation_factor_memo;
	private Composite factorMemoComposite;
	private Composite tipsComposite;
	private ImageButton preFactorBtn;
	private ImageButton nextFactorBtn;
	private EButton submit_result_btn;
	private static boolean iKnowTips = false;
	private Image menuImage = imageResolver.loadImage(IconsActivator.PLUGIN_ID, "icons/tree_item.png");
	private Image menuOpenImage = imageResolver.loadImage(IconsActivator.PLUGIN_ID, "icons/tree_parent.png");
	private EvaluationFactor curFactor;
	private List<EvaluationFactor> factorList;
	private List<Supplier> supplierList;
	private Map<String, Map<String, EvaluationFactorResult>> factorResultSupplierMapFactorMap;
	private Composite supplierListComposite;
	private AiEvaluationSupplierComposite aiEvaluationSupplierComposite;
	private Map<String, ExpertEvaluationResult> resultSupplierMap;
	private EvaluationStep noComplateFilterStep = null;
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public AiEvaluationComposite(Composite parent, int style) {
		super(parent, style);
		
		Composite right_composite = new Composite(parentComposite, SWT.NONE);
		GridLayout gl_right_composite = new GridLayout(1, false);
		gl_right_composite.marginHeight = 0;
		gl_right_composite.marginWidth = 0;
		gl_right_composite.verticalSpacing = 0;
		gl_right_composite.horizontalSpacing = 0;
		right_composite.setLayout(gl_right_composite);
		right_composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Composite evaluationTableComposite = new Composite(right_composite, SWT.NONE);
		GridLayout gl_evaluationTableComposite = new GridLayout(5, false);
		gl_evaluationTableComposite.verticalSpacing = 0;
		gl_evaluationTableComposite.horizontalSpacing = 0;
		gl_evaluationTableComposite.marginHeight = 0;
		gl_evaluationTableComposite.marginWidth = 0;
		evaluationTableComposite.setLayout(gl_evaluationTableComposite);
		evaluationTableComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		factorGridComposite = new Composite(evaluationTableComposite, SWT.NONE);
		GridLayout gl_factorGridComposite = new GridLayout(2, false);
		gl_factorGridComposite.verticalSpacing = 0;
		gl_factorGridComposite.horizontalSpacing = 0;
		gl_factorGridComposite.marginHeight = 0;
		gl_factorGridComposite.marginWidth = 0;
		factorGridComposite.setLayout(gl_factorGridComposite);
		GridData gd_factorComposite = new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1);
		gd_factorComposite.widthHint = 250;
		factorGridComposite.setLayoutData(gd_factorComposite);
		
		factorGrid = new Grid(factorGridComposite, SWT.H_SCROLL | SWT.V_SCROLL);
		factorGrid.addListener(SWT.Selection, new GridSelectionListener() {
			public void leftMouseButtonDown(GridItem item) {
				selectionFactorAction();
			}
		});
		factorGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		factorGrid.setAutoHeight(true);
		factorGrid.setAutoWidth(true);
		factorGrid.setItemHeight(Constants.ROW_HIGHT);
		factorGrid.setTreeLinesVisible(true);
		factorGrid.setShowEvenBackground(true);
		
		GridColumn gridColumn = new GridColumn(factorGrid, SWT.NONE);
		gridColumn.setShowTreeTogglePic(true);
		gridColumn.setTree(true);
		gridColumn.setTreeIndent(10);
		Image treeExpandPic = ImageResolver.RESOLVER_INSTANCE.loadImage(IconsActivator.PLUGIN_ID, "icons/tree_expend.png");
		Image treeClosePic = ImageResolver.RESOLVER_INSTANCE.loadImage(IconsActivator.PLUGIN_ID, "icons/tree_close.png");
		gridColumn.setTreeTogglePic(treeExpandPic, treeClosePic);
		
		ESepator file_right_ver = new ESepator(evaluationTableComposite, SWT.VERTICAL);
		file_right_ver.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		file_right_ver.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, true, 1, 1));
		
		hideOrShowFactor_btn = new ImageButton(evaluationTableComposite, SWT.NONE);
		hideOrShowFactor_btn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				hideOrShowFactorAction();
			}
		});
		hideOrShowFactor_btn.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		hideOrShowFactor_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/left_arrow.png"));
		hideOrShowFactor_btn.setBackgroundColor(SWTResourceManager.getColor(242, 242, 242));
		hideOrShowFactor_btn.setActiveBackgroundColor(SWTResourceManager.getColor(255, 153, 0));
		hideOrShowFactor_btn.setRightMargin(0);
		hideOrShowFactor_btn.setTopMargin(50);
		hideOrShowFactor_btn.setBottomMargin(50);
		hideOrShowFactor_btn.setLeftMargin(0);
		hideOrShowFactor_btn.setData("isHide", false);
		
		ESepator file_left_ver = new ESepator(evaluationTableComposite, SWT.VERTICAL);
		file_left_ver.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		file_left_ver.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, true, 1, 1));
		
		Composite factorComposite = new Composite(evaluationTableComposite, SWT.NONE);
		GridLayout gl_supplierComposite = new GridLayout(1, false);
		gl_supplierComposite.verticalSpacing = 0;
		gl_supplierComposite.horizontalSpacing = 0;
		gl_supplierComposite.marginHeight = 0;
		gl_supplierComposite.marginWidth = 0;
		factorComposite.setLayout(gl_supplierComposite);
		factorComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		factorMemoComposite = new Composite(factorComposite, SWT.NONE);
		GridLayout gl_composite = new GridLayout(2, false);
		gl_composite.verticalSpacing = 0;
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		gl_composite.horizontalSpacing = 0;
		factorMemoComposite.setLayout(gl_composite);
		GridData gd_factorMemoComposite = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_factorMemoComposite.heightHint = 75;
		factorMemoComposite.setLayoutData(gd_factorMemoComposite);
		
		evaluation_factor_memo = new StyledText(factorMemoComposite, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		evaluation_factor_memo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		evaluation_factor_memo.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		
		Composite nextComposite = new Composite(factorMemoComposite, SWT.NONE);
		GridLayout gl_nextComposite = new GridLayout(1, false);
		nextComposite.setLayout(gl_nextComposite);
		nextComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 2));
		
		tipsComposite = new Composite(factorMemoComposite, SWT.NONE);
		GridLayout gl_tipsComposite = new GridLayout(2, false);
		gl_tipsComposite.marginHeight = 0;
		gl_tipsComposite.marginWidth = 0;
		tipsComposite.setLayout(gl_tipsComposite);
		tipsComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		tipsComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_RED));
		
		CLabel evaluationTipsLabel = new CLabel(tipsComposite, SWT.NONE);
		evaluationTipsLabel.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.BOLD));
		evaluationTipsLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		evaluationTipsLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1));
		String message = " 提示：专家评标时应仔细核对投标文件及相关附件，若响应内容与文件中不一致时，可以进行修正，不符合件招标文件要求的资质可进行作废处理！";
		evaluationTipsLabel.setText(PropertiesUtils.getProperty("evaluationTips", message));
		
		CLabel label = new CLabel(tipsComposite, SWT.NONE);
		label.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				closeMessageAction();
			}
		});
		label.setText(Messages.Button_iKnown);
		label.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		label.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		label.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/white/close.png"));
		
		preFactorBtn = new ImageButton(nextComposite, SWT.NONE);
		preFactorBtn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				previewAction();
			}
		});
		preFactorBtn.setAlpha(10);
		preFactorBtn.setTopMargin(5);
		preFactorBtn.setBottomMargin(5);
		preFactorBtn.setLeftMargin(10);
		preFactorBtn.setRightMargin(10);
		preFactorBtn.setText("上一评审指标");
		preFactorBtn.setBackground(ColorConstants.SKIN_BG);
		preFactorBtn.setBorderColor(ColorConstants.SKIN_BG);
		preFactorBtn.setActiveBorderColor(ColorConstants.SKIN_ACTIVE_BG);
		preFactorBtn.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		preFactorBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/arrow_up.png"));
		
		nextFactorBtn = new ImageButton(nextComposite, SWT.NONE);
		nextFactorBtn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				nextAction();
			}
		});
		nextFactorBtn.setAlpha(10);
		nextFactorBtn.setTopMargin(5);
		nextFactorBtn.setBottomMargin(5);
		nextFactorBtn.setLeftMargin(10);
		nextFactorBtn.setRightMargin(10);
		nextFactorBtn.setText("下一评审指标");
		nextFactorBtn.setBackground(ColorConstants.SKIN_BG);
		nextFactorBtn.setBorderColor(ColorConstants.SKIN_BG);
		nextFactorBtn.setActiveBorderColor(ColorConstants.SKIN_ACTIVE_BG);
		nextFactorBtn.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		nextFactorBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/arrow_down.png"));
		
		ESepator sepLabTop = new ESepator(factorComposite, SWT.HORIZONTAL);
		sepLabTop.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		sepLabTop.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		supplierListComposite = new Composite(factorComposite, SWT.NONE);
		GridLayout gl_supplierListComposite = new GridLayout(1, false);
		gl_supplierListComposite.marginHeight = 0;
		gl_supplierListComposite.marginWidth = 0;
		supplierListComposite.setLayout(gl_supplierListComposite);
		supplierListComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		ESepator buttonSep = new ESepator(right_composite, SWT.HORIZONTAL);
		buttonSep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		buttonSep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		Composite buttonComposite = new Composite(right_composite, SWT.NONE);
		GridLayout gl_button_composite = new GridLayout(3, false);
		buttonComposite.setLayout(gl_button_composite);
		buttonComposite.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		
		EButton save_result_btn = new EButton(buttonComposite, SWT.NONE | EButton.CSS_GREEN);
		save_result_btn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				saveEvaluationResultAction();
			}
		});
		GridData gd_save_result_btn = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_save_result_btn.widthHint = 100;
		gd_save_result_btn.heightHint = 35;
		save_result_btn.setLayoutData(gd_save_result_btn);
		save_result_btn.setText(Messages.Button_saveEvaluationResult);
		save_result_btn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		save_result_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/white/save.png"));
		
		EButton view_result_btn = new EButton(buttonComposite, SWT.NONE | EButton.CSS_GREEN);
		view_result_btn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				vieweEvaluationResultAction();
			}
		});
		GridData gd_view_result_btn = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_view_result_btn.widthHint = 100;
		gd_view_result_btn.heightHint = 35;
		view_result_btn.setLayoutData(gd_view_result_btn);
		view_result_btn.setText("查看结果");
		view_result_btn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		view_result_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/white/view.png"));
		
		submit_result_btn = new EButton(buttonComposite, SWT.NONE | EButton.CSS_GREEN);
		submit_result_btn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				submitEvaluationResultAction();
			}
		});
		GridData gd_submit_result_btn = new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1);
		gd_submit_result_btn.widthHint = 100;
		gd_submit_result_btn.heightHint = 35;
		submit_result_btn.setLayoutData(gd_submit_result_btn);
		submit_result_btn.setText(Messages.Button_submitEvaluationResult);
		submit_result_btn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		submit_result_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/white/submit.png"));
		
		init();
	}
	
	/**
	 * @Description: 初始化
	 * @author chenxw
	 */
	protected void init() {
 		super.init();
		refreshAction();
	}
	
	/**
	 * @Description: 刷新
	 * @author chenxw
	 */
	protected void refreshAction() {
		super.refreshAction();
		if (iKnowTips) {
			tipsComposite.dispose();
		}
		getNoComplateFilterStep();
		refreshFactorTree();
	}
	
	/**
	 * @Description: 刷新指标树
	 * @author chenxw
	 */
	private void refreshFactorTree() {
		factorGrid.removeAll();
		List<EvaluationFactor> topFactorList = evaluationFactorService.getExpertEvaluationStepFactorTreeList(curTender.getId(), curStep, curUser.getId());
		if (CollectionUtils.isNotNull(topFactorList)) {
			factorList = new ArrayList<EvaluationFactor>();
			for (EvaluationFactor factor : topFactorList) {
				initChildGridItem(factor, new GridItem(factorGrid, SWT.NONE));
			}
			supplierList = supplierService.getEvaluationStepSupplierList(curTender.getId(), curStep);
			if (CollectionUtils.isNull(supplierList)) {
				supplierList = new ArrayList<Supplier>();
			}
			resultSupplierMap = getResultSupplierMap(supplierList);
			factorResultSupplierMapFactorMap = getFactorResultSupplierMapFactorMap(factorList, supplierList);
			refreshFactorGridImage();
			
			if (factorGrid.getSelectionCount() == 0 && factorGrid.getItemCount() > 0) {
				factorGrid.select(getFactorGridSelection(factorGrid.getItems()));
			}
			selectionFactorAction();
			factorGridOrientate();
		}
		factorGrid.getParent().layout();
	}
	
	/**
	 * @Description: 递归实现指标树
	 * @author chenxw
	 * @param factor
	 * @param gridItem
	 */
	private void initChildGridItem(EvaluationFactor factor, GridItem gridItem) {
		gridItem.setText(0, factor.getFactorName());
		gridItem.setToolTipText(0, factor.getFactorName());
		
		if (CollectionUtils.isNull(factor.getChildFactorList())) {
			gridItem.setData("evaluationFactor", factor);
			gridItem.setImage(0, menuImage);
			factorList.add(factor);
		} else {
			gridItem.setImage(0, menuOpenImage);
			gridItem.setExpanded(true);
			for (EvaluationFactor childFactor : factor.getChildFactorList()) {
				initChildGridItem(childFactor, new GridItem(gridItem, SWT.NONE));
			}
		}
		
		if (null != curFactor && curFactor.getId().equals(factor.getId())) {
			factorGrid.setSelection(new GridItem[] { gridItem });
		}
	}
	
	/**
	 * @Description: 获取指标选择列
	 * @author chenxw
	 * @param gridItems
	 * @return
	 */
	private int getFactorGridSelection(GridItem[] gridItems) {
		for (GridItem item : gridItems) {
			if (item.getItemCount() == 0) {
				return factorGrid.indexOf(item);
			}
			return getFactorGridSelection(item.getItems());
		}
		return 0;
	}
	
	/**
	 * @Description: 初始化评审结果
	 * @author chenxw
	 */
	private Map<String, ExpertEvaluationResult> getResultSupplierMap(List<Supplier> supplierList) {
		Map<String, ExpertEvaluationResult> resultSupplierIdMap = new HashMap<String, ExpertEvaluationResult>();
		List<ExpertEvaluationResult> resultList = expertEvaluationResultService.getExpertStepAllSupplierEvaluationResultList(curTender.getId(), curStep.getId(), curUser.getId());
		for (final Supplier supplier : supplierList) {
			ExpertEvaluationResult result = CollectionUtils.get(resultList, t -> t.getSupplierId().equals(supplier.getId()));
			if (null == result) {
				result = new ExpertEvaluationResult();
				result.setTenderId(curTender.getId());
				result.setStepId(curStep.getId());
				result.setUserId(curUser.getId());
				result.setSupplierId(supplier.getId());
			}
			resultSupplierIdMap.put(supplier.getId(), result);
		}
		return resultSupplierIdMap;
	}
	
	/**
	 * @Description: 初始化评审指标结果
	 * @author chenxw
	 */
	private Map<String, Map<String, EvaluationFactorResult>> getFactorResultSupplierMapFactorMap(List<EvaluationFactor> factorList, List<Supplier> supplierList) {
		Map<String, Map<String, EvaluationFactorResult>> factorResultSupplierMapFactorMap = new HashMap<String, Map<String, EvaluationFactorResult>>();
		List<EvaluationFactorResult> factorResultList = evaluationFactorResultService.getExpertEvaluationFactorResult(curTender.getId(), curStep.getId(), curUser.getId(), null);
		for (final EvaluationFactor evaluationFactor : factorList) {
			Map<String, EvaluationFactorResult> supplierFactorResult = new HashMap<String, EvaluationFactorResult>();
			for (final Supplier supplier : supplierList) {
				EvaluationFactorResult evaluationFactorResult = CollectionUtils.get(factorResultList, t -> {
					return t.getSupplierId().equals(supplier.getId()) && t.getFactorId().equals(evaluationFactor.getId());
				});
				if (null == evaluationFactorResult) {
					evaluationFactorResult = new EvaluationFactorResult();
					evaluationFactorResult.setTenderId(curTender.getId());
					evaluationFactorResult.setUserId(curUser.getId());
					evaluationFactorResult.setStepId(curStep.getId());
					evaluationFactorResult.setSupplierId(supplier.getId());
					evaluationFactorResult.setFactorId(evaluationFactor.getId());
				}
				supplierFactorResult.put(supplier.getId(), evaluationFactorResult);
			}
			factorResultSupplierMapFactorMap.put(evaluationFactor.getId(), supplierFactorResult);
		}
		return factorResultSupplierMapFactorMap;
	}
	
	/**
	 * @Description: 刷新所有指标图片
	 * @author chenxw
	 */
	public void refreshFactorGridImage() {
		submit_result_btn.setEnabled(true);
		for (GridItem gridItem : factorGrid.getItems()) {
			EvaluationFactor factor = (EvaluationFactor) gridItem.getData("evaluationFactor");
			if (null == factor) {
				continue;
			}
			Map<String, EvaluationFactorResult> factorResultSupplierMap = factorResultSupplierMapFactorMap.get(factor.getId());
			Collection<EvaluationFactorResult> results = factorResultSupplierMap.values();
			Collection<EvaluationFactorResult> filterResults = new ArrayList<EvaluationFactorResult>(results);
			CollectionUtils.filter(filterResults, t -> null != t.getEvaluationFinalScore() || null != t.getEvaluationResult());
			if (filterResults.size() == results.size()) {
				gridItem.setImage(0, ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/submit.png"));
			} else if (filterResults.size() == 0) {
				submit_result_btn.setEnabled(false);
				gridItem.setImage(0, ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/question.png"));
			} else {
				submit_result_btn.setEnabled(false);
				gridItem.setImage(0, ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/warning.png"));
			}
		}
		factorGrid.getParent().layout();
	}
	
	/**
	 * @Description: 选择指标事件
	 * @author chenxw
	 */
	private void selectionFactorAction() {
		EvaluationFactor evaluationFactor = null;
		if (factorGrid.getSelectionCount() > 0) {
			evaluationFactor = (EvaluationFactor) factorGrid.getSelection()[0].getData("evaluationFactor");
		}
		if (null == evaluationFactor) {
			return;
		}
		this.curFactor = evaluationFactor;
		
		refreshBtnStatus();
		refreshEvaluationFactorMemo();
		refreshSupplierEvaluationComposite();
	}
	
	/**
	 * @Description: 刷新按钮
	 * @author chenxw
	 */
	private void refreshBtnStatus() {
		int selectIndex = factorGrid.getSelectionIndex();
		preFactorBtn.setEnabled(true);
		nextFactorBtn.setEnabled(true);
		if (selectIndex <= 0) {
			preFactorBtn.setEnabled(false);
		}
		if (selectIndex == factorGrid.getItemCount() - 1) {
			nextFactorBtn.setEnabled(false);
		}
	}
	
	/**
	 * @Description: 刷新评审指标备注
	 * @author chenxw
	 */
	private void refreshEvaluationFactorMemo() {
		StringBuilder evaluationFactorMemo = new StringBuilder();
		if (curStep.getStepType().equals(com.gx.obe.components.core.enumeration.EvaluationStepEnum.DETAIL)) {
			String factorMinScore = NumberFormat.getInstance().format(curFactor.getFactorMinScore());
			String factorMaxScore = NumberFormat.getInstance().format(curFactor.getFactorMaxScore());
			evaluationFactorMemo.append("分值范围：[").append(factorMinScore).append("-").append(factorMaxScore).append("分]");
			if (null != curFactor.getFactorWeight()) {
				evaluationFactorMemo.append("(权重").append(BigDecimalUtils.round(curFactor.getFactorWeight(), 2).toString()).append("%)");
			}
			evaluationFactorMemo.append("   ");
		}
		
		int startIndex = evaluationFactorMemo.length();
		StyleRange factorNameTitleStyleRange = getTitleStyleRange(startIndex, 5);
		
		StyleRange styleFactorNameRange = new StyleRange();
		styleFactorNameRange.start = startIndex + 5;
		styleFactorNameRange.length = curFactor.getFactorName().length();
		styleFactorNameRange.foreground = SWTResourceManager.getColor(20, 20, 20);
		styleFactorNameRange.fontStyle = SWT.NORMAL;
		
		evaluationFactorMemo.append("评审指标：").append(curFactor.getFactorName());
		
		StyleRange factorMemoTitleStyleRange = null;
		StyleRange styleFactorMemoRange = null;
		if (null != curFactor.getMemo() && curFactor.getMemo().length() > 0) {
			evaluationFactorMemo.append("\r\n");
			startIndex = evaluationFactorMemo.length();
			factorMemoTitleStyleRange = getTitleStyleRange(startIndex, 5);
			evaluationFactorMemo.append("评分细则：").append(curFactor.getMemo());
			styleFactorMemoRange = new StyleRange();
			styleFactorMemoRange.start = startIndex + 5;
			styleFactorMemoRange.length = curFactor.getMemo().length();
			styleFactorMemoRange.foreground = SWTResourceManager.getColor(100, 100, 100);
			styleFactorMemoRange.fontStyle = SWT.NORMAL;
		}
		
		StyleRange styleScoreRange = new StyleRange();
		styleScoreRange.start = 5;
		styleScoreRange.length = evaluationFactorMemo.length() - 5;
		styleScoreRange.foreground = SWTResourceManager.getColor(77, 163, 19);
		styleScoreRange.fontStyle = SWT.BOLD;
		
		evaluation_factor_memo.setText(evaluationFactorMemo.toString());
		evaluation_factor_memo.setStyleRange(getTitleStyleRange(0, 5));
		evaluation_factor_memo.setStyleRange(styleScoreRange);
		evaluation_factor_memo.setStyleRange(factorNameTitleStyleRange);
		evaluation_factor_memo.setStyleRange(styleFactorNameRange);
		if (null != factorMemoTitleStyleRange) {
			evaluation_factor_memo.setStyleRange(factorMemoTitleStyleRange);
			evaluation_factor_memo.setStyleRange(styleFactorMemoRange);
		}
	}
	
	/**
	 * @Description: 获取StyleRange
	 * @author chenxw
	 * @param start
	 * @param length
	 * @return
	 */
	private StyleRange getTitleStyleRange(int start, int length) {
		StyleRange styleRange = new StyleRange();
		styleRange.start = start;
		styleRange.length = length;
		styleRange.foreground = SWTResourceManager.getColor(SWT.COLOR_BLUE);
		styleRange.fontStyle = SWT.BOLD;
		return styleRange;
	}
	
	/**
	 * @Description: 刷新供应商评审界面
	 * @author chenxw
	 */
	private void refreshSupplierEvaluationComposite() {
		if (null == aiEvaluationSupplierComposite || aiEvaluationSupplierComposite.isDisposed()) {
			aiEvaluationSupplierComposite = new AiEvaluationSupplierComposite(supplierListComposite, SWT.NONE);
			aiEvaluationSupplierComposite.addAiEvaluationListener(this);
		}
		aiEvaluationSupplierComposite.refresh(curFactor);
	}
	
	/**
	 * @Description: 刷新投标人结构化
	 * @author chenxw
	 * @param supplierId
	 */
	public void refreshSupplierStruct(String supplierId) {
		aiEvaluationSupplierComposite.refreshSupplierStruct(supplierId);
	}
	
	/**
	 * @Description: 刷新投标人结果
	 * @author chenxw
	 * @param supplierId
	 */
	public void refreshSupplierResult(String supplierId) {
		aiEvaluationSupplierComposite.refreshSupplierResult(supplierId);
		refreshFactorGridImage();
	}
	
	/**
	 * @Description: 显示或关闭指标树
	 * @author chenxw
	 */
	private void hideOrShowFactorAction() {
		if ((Boolean) hideOrShowFactor_btn.getData("isHide")) {
			((GridData) factorGridComposite.getLayoutData()).widthHint = 250;
			hideOrShowFactor_btn.setData("isHide", false);
			hideOrShowFactor_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/left_arrow.png"));
		} else {
			((GridData) factorGridComposite.getLayoutData()).widthHint = 0;
			hideOrShowFactor_btn.setData("isHide", true);
			hideOrShowFactor_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/right_arrow.png"));
		}
		factorGridComposite.getParent().layout();
	}
	
	/**
	 * @Description: 关闭提示事件
	 * @author chenxw
	 */
	private void closeMessageAction() {
		iKnowTips = true;
		tipsComposite.dispose();
		factorMemoComposite.layout();
	}
	
	/**
	 * @Description: 查看上一指标
	 * @author chenxw
	 */
	private void previewAction() {
		if (preFactorBtn.getEnabled()) {
			int selectIndex = factorGrid.getSelectionIndex();
			if (selectIndex > 0) {
				factorGrid.setSelection(selectIndex - 1);
				selectionFactorAction();
				factorGridOrientate();
			}
		}
	}
	
	/**
	 * @Description: 查看下一指标
	 * @author chenxw
	 */
	private void nextAction() {
		if (nextFactorBtn.getEnabled()) {
			int selectIndex = factorGrid.getSelectionIndex();
			if (selectIndex < factorGrid.getItemCount()) {
				factorGrid.setSelection(selectIndex + 1);
				selectionFactorAction();
				factorGridOrientate();
			}
		}
	}
	
	/**
	 * @Description: 指标树定位
	 * @author chenxw
	 */
	private void factorGridOrientate() {
		Rectangle rectangle = factorGrid.getBounds();
		factorGrid.scroll(0, 0, 0, 0, rectangle.width, rectangle.height, true);
		factorGrid.getVerticalBar().setSelection(factorGrid.getSelectionIndex());
		factorGrid.getVerticalBar().notifyListeners(SWT.Selection, null);
	}
	
	/**
	 * @Description: 保存评标结果
	 * @author chenxw
	 */
	private void saveEvaluationResultAction() {
		if (expertEvaluationStepService.saveEvaluationResult(curTender.getId(), curUser.getId(), curStep, resultSupplierMap, getFactorResultFactorMapSupplierMap())) {
			MessageDialog.openShortMessage(Messages.Tips_saveEvalResultSuccess);
		} else {
			MessageDialog.openInformation(getShell(), Messages.Dialog_warmTips, Messages.Tips_saveEvalResultFailed);
		}
	}
	
	/**
	 * @Description: 查看评标结果
	 * @author chenxw
	 */
	private void vieweEvaluationResultAction() {
		ViewEvaluationResultDialog viewEvaluationResultDialog = new ViewEvaluationResultDialog(getShell(), curTender, curStep, curExpert);
		viewEvaluationResultDialog.setOperator(SWT.MAX | SWT.CLOSE);
		viewEvaluationResultDialog.setTitle(curStep.getStepName() + "评审结果");
		viewEvaluationResultDialog.open();
	}
	
	/**
	 * @Description: 评审完成
	 * @author chenxw
	 */
	private void submitEvaluationResultAction() {
		if (isNullFactorResult()) {
			 MessageFormat.format(Messages.Tips_existNoEvaluationFactor, curStep.getStepName());
			return;
		}
		if (!expertEvaluationStepService.saveEvaluationResult(curTender.getId(), curUser.getId(), curStep, resultSupplierMap, getFactorResultFactorMapSupplierMap())) {
			MessageDialog.openWarning(getShell(), Messages.Dialog_warmTips, Messages.Tips_saveEvalResultFailed);
			return;
		}
		if (ObjectUtils.equals(curRule.getOrderReview(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.ORDER_SELF_FREE)) {
			List<EvaluationStep> evaluationStepList = evaluationStepService.getNotComplateStepList(curTender.getId(), curStep.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;
		}
		
		String message = MessageFormat.format(Messages.Tips_submitEvaluationResult, curStep.getStepName());
		if (!MessageDialog.openConfirm(getShell(), Messages.Button_submitEvaluationResult, message, new String[] { Messages.Button_confirmComplate, Messages.Button_continueEvaluation})) {
			return;
		}
		if (!expertEvaluationStepService.submitEvaluationResult(curTender.getId(), curRule, curUser.getId(), curStep, supplierList)) {
			MessageDialog.openInformation(getShell(), Messages.Dialog_warmTips, Messages.Tips_submitEvalResultFailed);
			return;
		}
		goBackAction();
	}
	
	/**
	 * @Description: 获得未完成的可能会存在废标的评审节点
	 * @author guoyr 
	 */
	private void getNoComplateFilterStep(){
		List<EvaluationStep> evaluationStepList = evaluationStepService.getNotComplateFilterStepList(curTender.getId(), curStep.getSortNo());
		if (null != evaluationStepList && evaluationStepList.size() > 0) {
			noComplateFilterStep =  evaluationStepList.get(0);
		}else {
			noComplateFilterStep = null;
		}
	}
	
	/**
	 * @Description: 是否存在未评审指标
	 * @author chenxw
	 * @return
	 */
	private boolean isNullFactorResult() {
		for (String factorId : factorResultSupplierMapFactorMap.keySet()) {
			Map<String, EvaluationFactorResult> factorResultSupplierMap = factorResultSupplierMapFactorMap.get(factorId);
			for (String supplierId : factorResultSupplierMap.keySet()) {
				EvaluationFactorResult factorResult = factorResultSupplierMap.get(supplierId);
				if (null == factorResult.getEvaluationFinalScore() && null == factorResult.getEvaluationResult()) return true;
			}
		}
		return false;
	}
	
	/**
	 * @Description: 获取指标评审结果
	 * @author chenxw
	 * @return
	 */
	private Map<String, Map<String, EvaluationFactorResult>> getFactorResultFactorMapSupplierMap() {
		Map<String, Map<String, EvaluationFactorResult>> factorResultFactorMapSupplierMap = new HashMap<String, Map<String, EvaluationFactorResult>>();
		List<EvaluationFactorResult> evaluationFactorResultList = CollectionUtils.mapToArrayList(factorResultSupplierMapFactorMap);
		if (curStep.getStepType().equals(com.gx.obe.components.core.enumeration.EvaluationStepEnum.COMPLIANCE)) {
			refreshResultAccessment(evaluationFactorResultList, factorResultFactorMapSupplierMap);
		} else {
			refreshResultTotalScore(evaluationFactorResultList, factorResultFactorMapSupplierMap);
		}
		return factorResultFactorMapSupplierMap;
	}
	
	/**
	 * @Description: 刷新评审结果（通过、不通过）
	 * @author chenxw
	 * @param factorResultList
	 * @param factorResultFactorMapSupplierMap
	 */
	private void refreshResultAccessment(List<EvaluationFactorResult> factorResultList, Map<String, Map<String, EvaluationFactorResult>> factorResultFactorMapSupplierMap) {
		Integer unqualifiedDeviateClauseCount = curStep.getEvaluationParamByKey(com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED_DEVIATE_CLAUSE_COUNT, Integer.class, 1);
		for (final Supplier supplier : supplierList) {
			String accessment = com.gx.obe.components.core.enumeration.EvaluationStepEnum.QUALIFIED;
			int deviateClauseCount = 0;
			Map<String, EvaluationFactorResult> factorResultFactorMap = new HashMap<String, EvaluationFactorResult>();
			for (final EvaluationFactor evaluationFactor : factorList) {
				EvaluationFactorResult evaluationFactorResult = CollectionUtils.get(factorResultList, t -> {
					return t.getSupplierId().equals(supplier.getId()) && t.getFactorId().equals(evaluationFactor.getId());
				});
				if (EvaluationFactorEnum.UNCONFORM.equals(evaluationFactorResult.getEvaluationResult())) {
					if (EvaluationFactorEnum.CLAUSE_TYPE_DEVIATE.equals(evaluationFactor.getClauseType())) {
						deviateClauseCount++;
					} else {
						accessment = com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED;
					}
				}
				factorResultFactorMap.put(evaluationFactor.getId(), evaluationFactorResult);
			}
			if (deviateClauseCount >= unqualifiedDeviateClauseCount) accessment = com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED;
			ExpertEvaluationResult result = resultSupplierMap.get(supplier.getId());
			result.setAccessment(accessment);
			factorResultFactorMapSupplierMap.put(supplier.getId(), factorResultFactorMap);
		}
	}
	
	/**
	 * @Description: 刷新评审结果总分与排名
	 * @author chenxw
	 * @param factorResultList
	 * @param factorResultFactorMapSupplierMap
	 */
	private void refreshResultTotalScore(List<EvaluationFactorResult> factorResultList, Map<String, Map<String, EvaluationFactorResult>> factorResultFactorMapSupplierMap) {
		List<Double> sortList = new ArrayList<Double>();
		for (final Supplier supplier : supplierList) {
			BigDecimal totalEvaluationScore = null;
			BigDecimal totalEvaluationFinalScore = null;
			Map<String, EvaluationFactorResult> factorResultFactorMap = new HashMap<String, EvaluationFactorResult>();
			for (final EvaluationFactor evaluationFactor : factorList) {
				EvaluationFactorResult evaluationFactorResult = CollectionUtils.get(factorResultList, t -> {
					return t.getSupplierId().equals(supplier.getId()) && t.getFactorId().equals(evaluationFactor.getId());
				});
				totalEvaluationScore = BigDecimalUtils.add(totalEvaluationScore, evaluationFactorResult.getEvaluationScore());
				totalEvaluationFinalScore = BigDecimalUtils.add(totalEvaluationFinalScore, evaluationFactorResult.getEvaluationFinalScore());
				factorResultFactorMap.put(evaluationFactor.getId(), evaluationFactorResult);
			}
			ExpertEvaluationResult result = resultSupplierMap.get(supplier.getId());
			result.setEvaluationScore(totalEvaluationScore);
			result.setEvaluationFinalScore(totalEvaluationFinalScore);
			sortList.add(BigDecimalUtils.doubleValue(totalEvaluationFinalScore, 0));
			factorResultFactorMapSupplierMap.put(supplier.getId(), factorResultFactorMap);
		}
		SortUtils sortUtils = new SortUtils(sortList, true);
		for (String supplierId : resultSupplierMap.keySet()) {
			ExpertEvaluationResult result = resultSupplierMap.get(supplierId);
			result.setSortNo(sortUtils.getSortNo(BigDecimalUtils.doubleValue(result.getEvaluationFinalScore(), 0)));
		}
	}
	
	/**
	 * @Description: 选择供应商事件
	 * @author chenxw
	 * @param curSupplier
	 */
	public void selectingSupplierAction(Supplier curSupplier) {
		aiEvaluationSupplierComposite.selectingSupplierAction(curSupplier);
	}
	
	/**
	 * @Description: 获取当前标段
	 * @author chenxw
	 * @return
	 */
	public TenderProjectEntity getCurTender() {
		return curTender;
	}
	
	/**
	 * @Description: 获取当前步骤
	 * @author chenxw
	 * @return
	 */
	public EvaluationStep getCurStep() {
		return curStep;
	}
	
	/**
	 * @Description: 获取当前专家
	 * @author chenxw
	 * @return
	 */
	public Expert getCurExpert() {
		return curExpert;
	}
	
	/**
	 * @Description: 获取供应商列表
	 * @author chenxw
	 * @return
	 */
	public List<Supplier> getSupplierList() {
		return supplierList;
	}
	
	/**
	 * @Description: 获取评审指标结果
	 * @author chenxw
	 * @return
	 */
	public Map<String, EvaluationFactorResult> getFactorResultMap() {
		return factorResultSupplierMapFactorMap.get(curFactor.getId());
	}
	
	/**
	 * @Description: 获取专家评审结果
	 * @author chenxw
	 * @return
	 */
	public Map<String, ExpertEvaluationResult> getResultSupplierMap() {
		return resultSupplierMap;
	}
	
	/**
	 * @Description: 获取投标人名称
	 * @author chenxw
	 * @return
	 */
	public String getSupplierName() {
		return I18N.getString(I18NEnum.SUPPLIERNAME, curTender.getTenderType());
	}
	
	/**
	 * @Description: 获取得分小数位数
	 * @author chenxw
	 * @return
	 */
	public int getScoreRound() {
		return ObjectUtils.getIntValue(curStep.getEvaluationParamByKey(com.gx.obe.components.core.enumeration.EvaluationStepEnum.EXPERT_SCORE_ROUND), 2);
	}
	
	protected void checkSubclass() {}
	
}
