package com.gx.obe.evaluation.step.dialog;

import java.util.Date;
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.utils.GridUtils;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.EDialog;
import org.eclipse.swt.core.widgets.ELabel;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.CLabel;
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.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

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.I18NEnum;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.components.core.enumeration.TenderProjectEnum;
import com.gx.obe.evaluation.step.listener.ImportEvaluationResultListener;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.DateUtils;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.NumberFormatUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.EvaluationFactorResult;
import com.gx.obe.web.entity.Page;
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.tender.TenderProjectEntity;
import com.gx.obe.web.service.SupplierService;
import com.gx.obe.web.service.TenderProjectService;
import com.gx.obe.web.service.evaluation.EvaluationFactorResultService;
import com.gx.obe.web.vo.TenderProjectVo;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 导入专家的历史打分
 * @author guoyr
 */
public class ImportHistoryScoreResultDialog extends EDialog {

	private String supplierName;
	private Grid projectGrid;
	private Grid supplierGrid;
	private Grid factorGrid;
	private TenderProjectEntity tenderProject;
	private EvaluationStep evaluationStep;
	private EButton selectAllBtn;
	private ELabel messageLabel;
	private EButton selectBallAll;
	private EButton importScortBtn;
	private ImageResolver imageResolver = new ImageResolver();
	private Image projectImage ;
	private Image singleProjectImage ;
	private Image subPackageImage;
	private List<Supplier> supplierList;
	private List<EvaluationFactor> evaluationFactorList;
	private ImportEvaluationResultListener callBack;
//	private List<TenderProjectEntity> tenderProjectList = new ArrayList<TenderProjectEntity>();
	private SupplierService supplierService;
	private EvaluationFactorResultService evaluationFactorResultService;
	private TenderProjectService tenderProjectService;
	private Label tipSecondLabel;
	private Image phaseTypeImage;
	
	{
		LogUtils.logClass(ImportHistoryScoreResultDialog.class);
	}
	
	/**
	 * @param parentShell
	 * @param tenderProjectEntity
	 * @param evaluationStep
	 * @param supplierList
	 * @param evaluationFactorList
	 * @param callBack
	 */
	public ImportHistoryScoreResultDialog(Shell parentShell, TenderProjectEntity tenderProject, EvaluationStep evaluationStep, List<Supplier> supplierList, List<EvaluationFactor> evaluationFactorList, ImportEvaluationResultListener callBack) {
		super(parentShell);
		this.tenderProject = tenderProject;
		this.evaluationStep = evaluationStep;
		this.supplierList = supplierList;
		this.callBack = callBack;
		this.evaluationFactorList = evaluationFactorList;
		supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProject.getTenderType());
	}

	/** 
	 * @Description: 
	 * @param parent
	 * @return
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout gl_container = new GridLayout(1, false);
		gl_container.verticalSpacing = 0;
		gl_container.horizontalSpacing = 0;
		gl_container.marginWidth = 0;
		gl_container.marginHeight = 0;
		container.setLayout(gl_container);
		container.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				imageResolver.disposite();
			}
		});
		Composite topComposite = new Composite(container, SWT.NONE);
		topComposite.setLayout(new GridLayout(1, false));
		topComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		tipSecondLabel = new Label(topComposite, SWT.WRAP);
		tipSecondLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		tipSecondLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		tipSecondLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		tipSecondLabel.setText("");
		
		ESepator label_top = new ESepator(container, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_top.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		label_top.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		
		Composite composite_1 = new Composite(container, SWT.NONE);
		GridLayout gl_composite_1 = new GridLayout(3, false);
		gl_composite_1.verticalSpacing = 0;
		gl_composite_1.horizontalSpacing = 0;
		gl_composite_1.marginWidth = 0;
		gl_composite_1.marginHeight = 0;
		composite_1.setLayout(gl_composite_1);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Composite composite = new Composite(composite_1, SWT.NONE);
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.verticalSpacing = 0;
		gl_composite.horizontalSpacing = 0;
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		composite.setLayout(gl_composite);
		GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_composite.heightHint = 250;
		composite.setLayoutData(gd_composite);
		
		Composite composite_5 = new Composite(composite, SWT.NONE);
		composite_5.setLayout(new GridLayout(1, false));
		composite_5.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		CLabel label = new CLabel(composite_5, SWT.NONE);
		label.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
//		label.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/television.png"));
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		label.setFont(SWTResourceManager.getFont("宋体", 10, SWT.BOLD));
		label.setText("1、选择要导入的历史评审项目");
		
		ESepator sep = new ESepator(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
		sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		sep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		projectGrid = new Grid(composite, SWT.V_SCROLL);
		projectGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		projectGrid.setHeaderVisible(true);
		
		GridColumn gridColumn_1 = new GridColumn(projectGrid, SWT.NONE);
		gridColumn_1.setTree(true);
		gridColumn_1.setText("编号");
		gridColumn_1.setWidth(150);
		
		GridColumn gridColumn = new GridColumn(projectGrid, SWT.NONE);
		gridColumn.setText(Messages.Name);
		gridColumn.setWidth(200);
		gridColumn.setWordWrap(true);
		
		ESepator label_3 = new ESepator(composite_1, SWT.SEPARATOR | SWT.VERTICAL);
		label_3.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 3));
		label_3.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		Composite composite_2 = new Composite(composite_1, SWT.NONE);
		GridLayout gl_composite_2 = new GridLayout(1, false);
		gl_composite_2.verticalSpacing = 0;
		gl_composite_2.horizontalSpacing = 0;
		gl_composite_2.marginHeight = 0;
		gl_composite_2.marginWidth = 0;
		composite_2.setLayout(gl_composite_2);
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 3));
		
		Composite composite_9 = new Composite(composite_2, SWT.NONE);
		composite_9.setLayout(new GridLayout(1, false));
		composite_9.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		CLabel label_2 = new CLabel(composite_9, SWT.NONE);
		label_2.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		label_2.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
//		label_2.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/television.png"));
		label_2.setFont(SWTResourceManager.getFont("宋体", 10, SWT.BOLD));
		label_2.setText("3、选择要导入历史结果的评审指标");
		
		ESepator sep3 = new ESepator(composite_2, SWT.SEPARATOR | SWT.HORIZONTAL);
		sep3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		sep3.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		factorGrid = new Grid(composite_2, SWT.H_SCROLL | SWT.V_SCROLL);
		factorGrid.setHeaderVisible(true);
		factorGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		GridColumn gridColumn_4 = new GridColumn(factorGrid, SWT.CHECK);
		gridColumn_4.setTree(true);
		gridColumn_4.setText(Messages.EvalFactor);
		gridColumn_4.setWidth(200);
		gridColumn_4.setWordWrap(true);
		
		GridColumn gridColumn_5 = new GridColumn(factorGrid, SWT.RIGHT);
		gridColumn_5.setText("历史结果");
		gridColumn_5.setWidth(50);
		
		GridColumn gridColumn_3 = new GridColumn(factorGrid, SWT.CENTER);
		gridColumn_3.setText("备注");
		gridColumn_3.setWidth(60);
		gridColumn_3.setWordWrap(true);
		
		ESepator label_5 = new ESepator(composite_1, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_5.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		label_5.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		Composite composite_3 = new Composite(composite_1, SWT.NONE);
		GridLayout gl_composite_3 = new GridLayout(1, false);
		gl_composite_3.verticalSpacing = 0;
		gl_composite_3.horizontalSpacing = 0;
		gl_composite_3.marginHeight = 0;
		gl_composite_3.marginWidth = 0;
		composite_3.setLayout(gl_composite_3);
		GridData gd_composite_3 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_composite_3.heightHint = 230;
		composite_3.setLayoutData(gd_composite_3);
		
		Composite composite_7 = new Composite(composite_3, SWT.NONE);
		composite_7.setLayout(new GridLayout(1, false));
		composite_7.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		CLabel label_1 = new CLabel(composite_7, SWT.NONE);
		label_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
//		label_1.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/television.png"));
		label_1.setFont(SWTResourceManager.getFont("宋体", 10, SWT.BOLD));
		label_1.setText("2、选择要导入历史结果的"+supplierName);
		
		ESepator sep2 = new ESepator(composite_3, SWT.SEPARATOR | SWT.HORIZONTAL);
		sep2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		sep2.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		supplierGrid = new Grid(composite_3, SWT.V_SCROLL);
		supplierGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		GridColumn gridColumn_2 = new GridColumn(supplierGrid, SWT.NONE);
		gridColumn_2.setText(supplierName);
		gridColumn_2.setWidth(300);
		gridColumn_2.setWordWrap(true);
		
		ESepator label_4 = new ESepator(composite_1, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_4.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		label_4.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		Composite composite_4 = new Composite(container, SWT.NONE);
		composite_4.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		GridLayout gl_composite_4 = new GridLayout(5, false);
		composite_4.setLayout(gl_composite_4);
		
		messageLabel = new ELabel(composite_4, SWT.WRAP);
		messageLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		messageLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		messageLabel.setText("每次只能选择一个相同的"+supplierName+"并为其导入拥有相同评审指标的分值！");
		
		selectAllBtn = new EButton(composite_4, SWT.NONE);
		GridData gd_selectAllBtn = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		selectAllBtn.setLayoutData(gd_selectAllBtn);
		selectAllBtn.setText(Messages.Button_selectAll);
		selectAllBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				selectAllHistoryResultAction(true);
			}
		});
		
		selectBallAll = new EButton(composite_4, SWT.NONE);
		GridData gd_button = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		selectBallAll.setLayoutData(gd_button);
		selectBallAll.setText(Messages.Button_selectInvert);
		selectBallAll.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				selectAllHistoryResultAction(false);
			}
		});
		
		importScortBtn = new EButton(composite_4, SWT.NONE);
		importScortBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				importHistoryResultAction();
			}
		});
		importScortBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/download.png"));
		importScortBtn.setText("导入历史结果");
		
		EButton cancle_btn = new EButton(composite_4, SWT.NONE);
		cancle_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/close.png"));
		cancle_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				getShell().close();
			}
		});
		GridData gd_button_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		cancle_btn.setLayoutData(gd_button_1);
		cancle_btn.setText(Messages.Button_close);

		init();
		return container;
	}

	/**
	 * @Description: 界面数据初始化
	 * @author guoyr
	 */
	private void init(){
		supplierService = new SupplierService();
		evaluationFactorResultService = new EvaluationFactorResultService();
		tenderProjectService = new TenderProjectService();
		projectImage = imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/project_parent.png");
		subPackageImage = imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/project_package.png");
		singleProjectImage = imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/project_project.png");
		projectGrid.setItemHeight(Constants.ROW_HIGHT);
		supplierGrid.setItemHeight(Constants.ROW_HIGHT);
		factorGrid.setItemHeight(Constants.ROW_HIGHT);
		factorGrid.setAutoHeight(true);
		this.setTitle("从您已评审过的项目中导入相同"+ supplierName +"的历史评审结果");
		tipSecondLabel.setText("1、选择要导入的历史评审项目  〉〉 2、选择要导入历史结果的"+supplierName+" 〉〉 3、选择要导入分值的评审指标 〉〉 4、点击“导入历史结果”");
		refreshProjectGrid();
		// 添加项目表格的选中事件和右键菜单事件
		projectGrid.addListener(SWT.Selection, new GridSelectionListener(){
			@Override
			public void leftMouseButtonDown(GridItem item) {
				refreshSupplierGrid();
			}
		});
		supplierGrid.addListener(SWT.Selection, new GridSelectionListener(){
			@Override
			public void leftMouseButtonDown(GridItem item) {
				refreshFactorResultGrid();
			}
		});
		
	}
	
	/**
	 * @Description: 刷新投标项目列表
	 * @author guoyr
	 */
	private void refreshProjectGrid() {
		projectGrid.removeAll();
		if(null != tenderProject.getParentId()) {
			loadSubPackage(tenderProject.getParentId(), null);
		}else {
			// 只加载近三个月的
			Date startTime = DateUtils.getBeginDayOfMonth(-3);
//			Date endTime = DateUtils.getEndDayOfMonth(-3);
			Date endTime = DateUtils.getDayEnd();
			final Page<TenderProjectVo> projectPages = tenderProjectService.getEvaluationProjectPageList(Constants.USER.getId(), Constants.USER.getUserType(), null, null, startTime, endTime, 1, 9999);
			Display.getDefault().syncExec(new Runnable(){
				public void run(){
					if(projectGrid.isDisposed())return;
					if(null != projectPages && null != projectPages.getRecords()){
						for(TenderProjectVo tenderProjectVo : projectPages.getRecords()){
							if(tenderProject.getId().equals(tenderProjectVo.getTenderId())) {
								continue;
							}
							GridItem parentItem = new GridItem(projectGrid, SWT.NONE);
							parentItem.setData("tenderId", tenderProjectVo.getTenderId());
							parentItem.setText(0, tenderProjectVo.getTenderNo());
							parentItem.setText(1, tenderProjectVo.getTenderName());
							parentItem.setToolTipText(0, parentItem.getText(0));
							parentItem.setToolTipText(1, parentItem.getText(1));
							if(null != tenderProjectVo.getPhaseType() && tenderProjectVo.getPhaseType().equals(TenderProjectEnum.PHASE_TYPE_PRE)){
								if(null == phaseTypeImage){
									phaseTypeImage = imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/phase_type.png");
								}
								parentItem.setImage(1, phaseTypeImage);
							}
							if(CommonEnum.YES.equals(tenderProjectVo.getIsPack())) {
								parentItem.setData("IS_PACK", CommonEnum.YES);
								parentItem.setImage(0,projectImage);
							}else {
								parentItem.setImage(0,singleProjectImage);
							}
						}
					}
				}
			});	
		}
		if(projectGrid.getItemCount() > 0){
			if(projectGrid.getItem(0).getItemCount() > 0){
				projectGrid.setSelection(1);
			}else {
				projectGrid.setSelection(0);
			}
		}else {
			GridItem parentItem = new GridItem(projectGrid, SWT.NONE);
			parentItem.setColumnSpan(0, 2);
			parentItem.setText(0, "未检索到您近三月的历史评审项目！");
		}
		refreshSupplierGrid();
	}
	
	private void loadSubPackage(String parentId, GridItem parentItem) {
		if(null != parentItem) {
			for(GridItem item : parentItem.getItems()) {
				item.dispose();
			}
		}
		List<TenderProjectEntity> tenderProjectList = tenderProjectService.getSubPackageList(parentId);
		if(null != tenderProjectList) {
			for(TenderProjectEntity tenderPack : tenderProjectList){
				if(tenderProject.getId().equals(tenderPack.getId())) {
					continue;
				}
				GridItem gridItem =  null;
				if(null != parentItem) {
					gridItem = new GridItem(parentItem, SWT.NONE);
				}else {
					gridItem = new GridItem(projectGrid, SWT.NONE);
				}
				
				gridItem.setData("tenderId", tenderPack.getId());
				gridItem.setText(0, tenderPack.getTenderNo());
				gridItem.setText(1, tenderPack.getTenderName());
				gridItem.setToolTipText(0, gridItem.getText(0));
				gridItem.setToolTipText(1, gridItem.getText(1));
				if(null != tenderPack.getPhaseType() && tenderPack.getPhaseType().equals(TenderProjectEnum.PHASE_TYPE_PRE)){
					if(null == phaseTypeImage){
						phaseTypeImage = imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/phase_type.png");
					}
					gridItem.setImage(1, phaseTypeImage);
				}
			}
		}
	}
	/**
	 * @Description: 刷新投标人列表
	 * @author guoyr
	 */
	private void refreshSupplierGrid(){
		supplierGrid.removeAll();
		if(projectGrid.getSelectionCount() > 0){
			int selectIndex = projectGrid.getSelectionIndex();
			GridItem selectGridItem = projectGrid.getSelection()[0];
			String tenderId = (String)selectGridItem.getData("tenderId");
			if(null != tenderId){
				String IS_PACK = (String)selectGridItem.getData("IS_PACK");
				if(CommonEnum.YES.equals(IS_PACK)) {
					if(selectGridItem.isExpanded()) {
						selectGridItem.setExpanded(false);
					}else {
						selectGridItem.setExpanded(true);
						loadSubPackage(tenderId, selectGridItem);
					}
					if(projectGrid.getItemCount() > selectIndex) {
						projectGrid.setSelection(selectIndex + 1);
					}
					refreshSupplierGrid();
					return;
				}
				List<Supplier> selectSupplierList = supplierService.getSupplierList(tenderId);
				if(null != selectSupplierList && null != supplierList){
					for(Supplier selectSupplier : selectSupplierList){
						for(Supplier supplier : supplierList){
							if(ObjectUtils.equals(selectSupplier.getSupplierName().trim(), supplier.getSupplierName().trim())){
								GridItem gridItem = new GridItem(supplierGrid, SWT.NONE);
								gridItem.setData("supplier", selectSupplier);
								gridItem.setData("importSupplierId", supplier.getId());
								gridItem.setText(0, ObjectUtils.getObjString(supplier.getSupplierName()));
								gridItem.setToolTipText(0, gridItem.getText(0));
							}
						}
					}
				}
			}
		}
		
		if(supplierGrid.getItemCount() > 0){
			supplierGrid.setSelection(0);
		}else {
			GridItem gridItem = new GridItem(supplierGrid, SWT.NONE);
			gridItem.setText(0, "未检索到与当前项目相同的"+ supplierName);
		}
		refreshFactorResultGrid();
	}
	
	/**
	 * @Description: 刷新投标人评标结果
	 * @author guoyr
	 */
	private void refreshFactorResultGrid(){
		factorGrid.removeAll();
		List<EvaluationFactorResult> evaluationFactorResultList = null;
		if(supplierGrid.getSelectionCount() > 0 && projectGrid.getSelectionCount() > 0){
			Supplier supplier = (Supplier)supplierGrid.getSelection()[0].getData("supplier");
			String tenderId= (String)projectGrid.getSelection()[0].getData("tenderId");
			if(null != supplier && null != tenderId){
				// 评审当前评标专家所有的评标结果
//				QueryWrapper queryWrapper = new QueryWrapper(EvaluationFactorResult.class);
//				queryWrapper.setWhereParam("tenderId", selectTenderProject.getId());
//				queryWrapper.setWhereParam("supplierId", supplier.getId());
//				queryWrapper.setWhereParam("userId", Constants.USER.getId());
//				evaluationFactorResultList = evaluationFactorResultService.queryList(queryWrapper, EvaluationFactorResult.class);
				evaluationFactorResultList = evaluationFactorResultService.getExpertEvaluationFactorResult(tenderId, null, Constants.USER.getId(), supplier.getId());
			}
		}
		int equalsResultCount = 0;
		if(null != evaluationFactorList && evaluationFactorList.size() > 0){
			for(EvaluationFactor evaluationFactor : evaluationFactorList){
				
				GridItem gridItem = new GridItem(factorGrid, SWT.NONE);
				gridItem.setText(0, evaluationFactor.getFactorName());
				gridItem.setToolTipText(0, evaluationFactor.getFactorName());
				gridItem.setChecked(0, false);
				gridItem.setCheckVisible(0, false);
				if(null != evaluationFactorResultList && evaluationFactorResultList.size() > 0){
					for(EvaluationFactorResult evaluationFactorResult : evaluationFactorResultList){
						if(null != evaluationFactor && ObjectUtils.equals(evaluationFactorResult.getFactorName().trim(), evaluationFactor.getFactorName().trim())){
							if(evaluationStep.getStepType().equals(com.gx.obe.components.core.enumeration.EvaluationStepEnum.COMPLIANCE) ){
								if(null != evaluationFactorResult.getEvaluationResult()){
									gridItem.setText(1,  evaluationFactorResult.getEvalPassTypeCN(evaluationFactor.getEvalContent()));
									gridItem.setData("evaluationFactorId", evaluationFactor.getId());
									gridItem.setData("evaluationFactorResult", evaluationFactorResult);
									gridItem.setText(2, "可以导入！");
									gridItem.setChecked(0, true);
									gridItem.setCheckVisible(0, true);
									equalsResultCount ++;
								}
							}else {
								if(null != evaluationFactorResult.getEvaluationScore()){
									gridItem.setText(1, NumberFormatUtils.format(evaluationFactorResult.getEvaluationScore(), 5));
									if(null != evaluationFactor.getFactorMaxScore() && evaluationFactorResult.getEvaluationScore().compareTo(evaluationFactor.getFactorMaxScore()) > 0){
										gridItem.setText(2,"超过最大评分范围，不能导入！");
									}else if(null != evaluationFactor.getFactorMinScore() && evaluationFactorResult.getEvaluationScore().compareTo(evaluationFactor.getFactorMinScore()) < 0){
										gridItem.setData("checked", "0");
										gridItem.setText(2, "小于最小评分范围，不能导入！");
									}else {
										gridItem.setData("evaluationFactorId", evaluationFactor.getId());
										gridItem.setData("evaluationFactorResult", evaluationFactorResult);
										gridItem.setText(2, "可以导入！");
										gridItem.setChecked(0, true);
										gridItem.setCheckVisible(0, true);
										equalsResultCount ++;
										break;
									}
									
								}
							}
						}
					}
				}
			}
		}
		if(factorGrid.getItemCount() > 0){
			if(equalsResultCount > 0){
				selectBallAll.setEnabled(true);
				selectAllBtn.setEnabled(true);
				importScortBtn.setEnabled(true);
			}else{
				selectBallAll.setEnabled(false);
				selectAllBtn.setEnabled(false);
				importScortBtn.setEnabled(false);
			}
		}else {
			GridItem gridItem = new GridItem(factorGrid, SWT.NONE);
			gridItem.setColumnSpan(0, 3);
			gridItem.setText(0, "未检索到可导入的历史结果！");
			gridItem.setCheckVisible(0, false);
			selectBallAll.setEnabled(false);
			selectAllBtn.setEnabled(false);
			importScortBtn.setEnabled(false);
		}
	}
	
	/**
	 * @Description: 全选、反选
	 * @author guoyr
	 * @param selectAll
	 */
	private void selectAllHistoryResultAction(boolean selectAll){
		if(selectAll){
			GridUtils.checkAll(factorGrid, 0, true);
		}else {
			GridUtils.checkAllBack(factorGrid, 0);
		}
	}

	/**
	 * @Description: 导入历史打分
	 * @author guoyr
	 */
	private void importHistoryResultAction(){
		if(importScortBtn.getEnabled()){
			Map<String, Map<String, EvaluationFactorResult>> importResultMap = new HashMap<String, Map<String, EvaluationFactorResult>>();
			List<GridItem> gridItemList = GridUtils.getCheckedItems(factorGrid, 0, true);
			if(null != gridItemList && gridItemList.size() > 0){
				Map<String, EvaluationFactorResult> supplierEvalResultMap = new HashMap<String, EvaluationFactorResult>();
				String importSupplierId = (String)supplierGrid.getSelection()[0].getData("importSupplierId");
				for(GridItem gridItem : gridItemList){
					supplierEvalResultMap.put((String)gridItem.getData("evaluationFactorId"), (EvaluationFactorResult)gridItem.getData("evaluationFactorResult"));
				}
				importResultMap.put(importSupplierId, supplierEvalResultMap);
				if(null != callBack){
					callBack.importEvalResult(importResultMap);
					supplierGrid.getSelection()[0].setImage(0, ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/ok.png"));
				}
				MessageDialog.openShortMessage("已导入！");
			}else {
				MessageDialog.openWarning(getShell(), "请选择要导入历史评审结果的评审指标！");
			}
		}
	}
	@Override
	protected Point getInitialSize() {
		return new Point(867, 565);
	}
}
