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

import java.io.File;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.utils.FileDialogUtils;
import org.eclipse.swt.core.utils.WidgetUtils;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.core.widgets.ImageButton;
import org.eclipse.swt.core.widgets.RectangleRadioButton;
import org.eclipse.swt.custom.StackLayout;
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.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 com.gx.obe.ColorConstants;
import com.gx.obe.action.task.TaskListener;
import com.gx.obe.business.entity.ExpertEvaluationStep;
import com.gx.obe.business.entity.SupplierEvaluationFactor;
import com.gx.obe.business.enumeration.TaskEnum;
import com.gx.obe.common.file.enumeration.FileConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.button.EvalLeftButton;
import com.gx.obe.common.widget.button.EvaluationButton;
import com.gx.obe.common.widget.dialog.MessageViewDialog;
import com.gx.obe.common.widget.dialog.ProgressDialog;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.components.core.enumeration.EvaluationFactorEnum;
import com.gx.obe.components.core.enumeration.EvaluationStepEnum;
import com.gx.obe.components.core.enumeration.TenderProjectEnum;
import com.gx.obe.evaluation.step.composite.FactorOpinionComposite;
import com.gx.obe.evaluation.step.composite.PriceEditAndResultComposite;
import com.gx.obe.evaluation.step.dataset.ClearCommentsDataSet;
import com.gx.obe.evaluation.step.dialog.AuditDialog;
import com.gx.obe.evaluation.step.listener.ExpertEvaluationListener;
import com.gx.obe.evaluation.step.thread.ImportSupplierFactorOpinionThread;
import com.gx.obe.evaluation.step.thread.ParsingSupplierSelfOpinionThread;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.util.utils.UuidUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.Task;
import com.gx.obe.web.entity.auth.AuthUser;
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.tender.TenderProjectEntity;
import com.gx.obe.web.service.SupplierEvaluationFactorService;
import com.gx.obe.web.service.SupplierService;
import com.gx.obe.web.service.TaskService;
import com.gx.obe.web.service.evaluation.EvaluationFactorService;
import com.gx.obe.web.service.evaluation.EvaluationGroupService;
import com.gx.obe.web.service.evaluation.EvaluationStepService;
import com.gx.obe.web.service.expert.ExpertEvaluationStepService;
import com.gx.obe.web.service.expert.ExpertService;
import com.gx.utils.ActionLogUtils;
import com.gx.utils.NowTimeUtils;
import com.report.excel.ExcelGenerateAdapter;
import com.report.excel.ExcelReportUtil;
import com.report.excel.ReportDataSet;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

import im.IMClientAdapter;

/**
 * @Description: 清标评审界面
 * @author chenxw
 */
public class PreEvaluationComposite extends Composite implements FactorOpinionComposite.OpinionListener {
	
	private SupplierService supplierService = new SupplierService();
	private EvaluationStepService evaluationStepService = new EvaluationStepService();
	private EvaluationGroupService evaluationGroupService = new EvaluationGroupService();
	private ExpertEvaluationStepService expertEvaluationStepService = new ExpertEvaluationStepService();
	private EvaluationFactorService evaluationFactorService = new EvaluationFactorService();
	private SupplierEvaluationFactorService supplierEvaluationFactorService = new SupplierEvaluationFactorService();
	private TaskService taskService = new TaskService();
	private ExpertService expertService = new ExpertService();
	private TenderProjectEntity curTender;
//	private ProjectRule curRule;
	private EvaluationStep curStep;
	private AuthUser curUser;
	private ExpertEvaluationListener expertEvaluationListener;
	private Composite buttonComposite;
	private EvaluationButton submit_result_btn;
	private Composite opinionResultLink;
	private Map<String, List<String>> supplierIdListEvalBidTypeMap;
	private Composite switchComposite;
	private ImageButton curButton;
	private StackLayout stackLayout = new StackLayout();
	private FactorOpinionComposite busOpinionComposite;
	private FactorOpinionComposite tesOpinionComposite;
	private PriceEditAndResultComposite priceEditAndResultComposite;
	private Composite evaluationTableComposite;
	private Composite left_tool_composite;
	private boolean ishideLeft;
	private ExpertEvaluationStep expertEvaluationStep;
	private Composite tipsComposite;
	private StyledText evaluationTipsText;
	private Composite topComposite;
	private String evalBidType;
	private boolean isEdit;
	private ESepator switchSep;
	private List<EvaluationFactor> factorList;
	private HashMap<String, Map<String, String>> allSupplierOpinionMap;
	private ELink supplierSelfOpinionLink;
	private ExecutorService httpExecutorService;
	private CallBack callBack;
	private boolean isReview;
	private TaskListener taskListener;
	
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public PreEvaluationComposite(Composite parent, int style) {
		super(parent, style);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		setBackgroundMode(SWT.INHERIT_FORCE);
		GridLayout gridLayout = new GridLayout(2, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		
		this.curTender = (TenderProjectEntity) parent.getData("tenderProjectEntity");
		this.curStep = (EvaluationStep) parent.getData("evaluationStep");
		this.curUser = (AuthUser) parent.getData("curUser");
//		this.curRule = (ProjectRule) parent.getData("projectRule");
		this.ishideLeft = null != parent.getData("hideLeft");
		
		left_tool_composite = new Composite(this, SWT.NONE);
		left_tool_composite.setBackground(EvalLeftButton.BG);
		GridData gd_left_tool_composite = new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1);
		gd_left_tool_composite.exclude = ishideLeft;
		left_tool_composite.setLayoutData(gd_left_tool_composite);
		GridLayout gl_left_tool_composite = new GridLayout(1, false);
		gl_left_tool_composite.verticalSpacing = 2;
		left_tool_composite.setLayout(gl_left_tool_composite);
		left_tool_composite.setVisible(!ishideLeft);
		
		EvalLeftButton goto_back = new EvalLeftButton(left_tool_composite, SWT.NONE);
		goto_back.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		goto_back.setImage(ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/back.png"));
		goto_back.setToolTipText(Messages.Button_return);
		goto_back.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				goBackAction();
			}
		});
		
		EvalLeftButton refresh_evaluation = new EvalLeftButton(left_tool_composite, SWT.NONE);
		refresh_evaluation.setImage(ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/refresh.png"));
		refresh_evaluation.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		refresh_evaluation.setToolTipText(Messages.Button_refresh);
		refresh_evaluation.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				saveOpinionAction();
				refreshAction();
			}
		});
		
		ESepator sep = new ESepator(left_tool_composite, SWT.NONE);
		sep.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		Composite right_composite = new Composite(this, 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 contentComposite = new Composite(right_composite, SWT.NONE);
		contentComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_composite_5 = new GridLayout(1, false);
		gl_composite_5.verticalSpacing = 0;
		gl_composite_5.marginHeight = 0;
		gl_composite_5.marginWidth = 0;
		contentComposite.setLayout(gl_composite_5);
		
		tipsComposite = new Composite(contentComposite, SWT.NONE);
		GridLayout gl_tipsComposite = new GridLayout(1, 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));
		
		evaluationTipsText = new StyledText(tipsComposite, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.READ_ONLY);
		GridData gd_evaluation_factor_memo = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_evaluation_factor_memo.heightHint = 77;
		evaluationTipsText.setLayoutData(gd_evaluation_factor_memo);
		evaluationTipsText.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		evaluationTipsText.setLeftMargin(10);
		evaluationTipsText.setTopMargin(5);
		
		ESepator tipsSep = new ESepator(contentComposite, SWT.HORIZONTAL);
		tipsSep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		tipsSep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		topComposite = new Composite(contentComposite, SWT.NONE);
		topComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		switchComposite = new Composite(topComposite, SWT.NONE);
		switchComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		opinionResultLink = new Composite(topComposite, SWT.NONE);
		opinionResultLink.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1));
		opinionResultLink.setLayout(new GridLayout(3, false));
		
		ELink exportOpinionResultLink = new ELink(opinionResultLink, SWT.NONE);
		exportOpinionResultLink.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				exportOpinionResultAction();
			}
		});
		exportOpinionResultLink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/export.png"));
		exportOpinionResultLink.setText(MessageFormat.format(Messages.Button_exportFormat, Messages.BidFileComments));
		
		ELink importOpinionResultLink = new ELink(opinionResultLink, SWT.NONE);
		importOpinionResultLink.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				importOpinionResultAction();
			}
		});
		importOpinionResultLink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/import.png"));
		importOpinionResultLink.setText(MessageFormat.format(Messages.Button_importFormat, Messages.BidFileComments));
		
		supplierSelfOpinionLink = new ELink(opinionResultLink, SWT.NONE);
		supplierSelfOpinionLink.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				getOpinionAction();
			}
		});
		supplierSelfOpinionLink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/download.png"));
		supplierSelfOpinionLink.setText(Messages.Button_getSupplierSelfOpinion);
		
		switchSep = new ESepator(contentComposite, SWT.HORIZONTAL);
		switchSep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		switchSep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		evaluationTableComposite = new Composite(contentComposite, SWT.NONE);
		evaluationTableComposite.setLayout(stackLayout);
		evaluationTableComposite.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));
		
		buttonComposite = new Composite(right_composite, SWT.NONE);
		buttonComposite.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		GridLayout gl_button_composite = new GridLayout(1, false);
		buttonComposite.setLayout(gl_button_composite);
		
		submit_result_btn = new EvaluationButton(buttonComposite, SWT.NONE | EButton.CSS_GREEN);
		submit_result_btn.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
		submit_result_btn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		submit_result_btn.setText(Messages.Button_submitEvaluationResult);
		submit_result_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/white/submit.png"));
		submit_result_btn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				submitEvaluationResultAction();
			}
		});
		
		init();
	}
	
	private void init() {
		expertEvaluationStep = expertEvaluationStepService.getExpertEvaluationStep(curTender.getId(), curStep.getId(), curUser.getId());
		if (null == expertEvaluationStep || null == expertEvaluationStep.getStepStatus() || expertEvaluationStep.getStepStatus().equals(CommonEnum.NOT_START)) {
			ActionLogUtils.addServerLog(curTender.getId(), "进入" + curStep.getStepName(), Constants.USER.getUserName());
			expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.IN_PROGRESS, curTender.getId(), curStep, curUser.getId());
			expertEvaluationStep = expertEvaluationStepService.getExpertEvaluationStep(curTender.getId(), curStep.getId(), curUser.getId());
		}
		isEdit = !CommonEnum.IN_MANAGER_REVIEW.equals(expertEvaluationStep.getStepStatus());
		
		isReview = expertEvaluationStep.getStepStatus().equals(CommonEnum.IN_PROGRESS) || expertEvaluationStep.getStepStatus().equals(CommonEnum.RE_OPERATOR);
		
		if(!TenderProjectEnum.REMOTE.equals(curTender.getRemoteStatus())) {
			WidgetUtils.setControlVisible(supplierSelfOpinionLink, false);
		}
		
		if (isReview) {
			submit_result_btn.setText(Messages.Button_submitAndApproval);
			String applyReason = expertEvaluationStep.getApplyReason();
			if (StringUtils.isEmpty(applyReason)) {
				tipsComposite.dispose();
			} else {
				evaluationTipsText.setText(Messages.Title_reBackReason + "：".concat(applyReason));
				StyleRange styleRange = new StyleRange();
				styleRange.start = 0;
				styleRange.length = Messages.Title_reBackReason.length() + 1;
				styleRange.foreground = SWTResourceManager.getColor(SWT.COLOR_BLUE);
				styleRange.fontStyle = SWT.BOLD;
				evaluationTipsText.setStyleRange(styleRange);
			}
		} else {
			submit_result_btn.setText(MessageFormat.format(Messages.Button_approvalFormat, curStep.getStepName()));
			tipsComposite.dispose();
		}
		refreshAction();
	}
	
	/**
	 * @Description: 刷新事件
	 * @author chenxw
	 */
	private void refreshAction() {
		for (Control control : switchComposite.getChildren()) {
			control.dispose();
		}
		
		factorList = new ArrayList<EvaluationFactor>();
		Integer factorType = curStep.getEvaluationParamByKey(com.gx.obe.components.core.enumeration.EvaluationStepEnum.FACTOR_TYPE, Integer.class);
		if (null == factorType) {
			factorType = com.gx.obe.components.core.enumeration.EvaluationStepEnum.FACTOR_COMPLIANCE | com.gx.obe.components.core.enumeration.EvaluationStepEnum.FACTOR_SCORE;
		}
		if ((factorType & com.gx.obe.components.core.enumeration.EvaluationStepEnum.FACTOR_COMPLIANCE) != 0) {
			List<EvaluationStep> complianceEvaluationStepList = evaluationStepService.getEvaluationStepListByType(curTender.getId(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.COMPLIANCE);
			for (EvaluationStep evaluationStep : complianceEvaluationStepList) {
				List<EvaluationFactor> evaluationStepFactorTreeList = evaluationFactorService.getEvaluationStepFactorTreeList(curTender.getId(), evaluationStep);
				factorList.addAll(evaluationStepFactorTreeList);
			}
		}
		if ((factorType & com.gx.obe.components.core.enumeration.EvaluationStepEnum.FACTOR_SCORE) != 0) {
			List<EvaluationStep> detailEvaluationStepList = evaluationStepService.getEvaluationStepListByType(curTender.getId(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.DETAIL);
			for (EvaluationStep evaluationStep : detailEvaluationStepList) {
				List<EvaluationFactor> evaluationStepFactorTreeList = evaluationFactorService.getEvaluationStepFactorTreeList(curTender.getId(), evaluationStep);
				factorList.addAll(evaluationStepFactorTreeList);
			}
		}
		
		refreshEvaluationAction();
		
		switchComposite.layout();
	}
	
	private void refreshEvaluationAction() {
		supplierIdListEvalBidTypeMap = evaluationGroupService.getSupplierIdListEvalBidTypeMapByUserId(curTender.getId(), curUser.getId(), curStep.getId());
		
		if (supplierIdListEvalBidTypeMap.containsKey(EvaluationFactorEnum.EVAL_BID_BUS)) {
			busOpinionComposite = new FactorOpinionComposite(evaluationTableComposite, SWT.NONE);
			busOpinionComposite.addOpinionListener(this);
			List<String> supplierIdList = supplierIdListEvalBidTypeMap.get(EvaluationFactorEnum.EVAL_BID_BUS);
			busOpinionComposite.refresh(curTender, curStep, EvaluationFactorEnum.EVAL_BID_BUS, supplierIdList, factorList, isEdit);
			createImageButton(Messages.Business, EvaluationFactorEnum.EVAL_BID_BUS);
		}
		if (supplierIdListEvalBidTypeMap.containsKey(EvaluationFactorEnum.EVAL_BID_TEC)) {
			tesOpinionComposite = new FactorOpinionComposite(evaluationTableComposite, SWT.NONE);
			tesOpinionComposite.addOpinionListener(this);
			List<String> supplierIdList = supplierIdListEvalBidTypeMap.get(EvaluationFactorEnum.EVAL_BID_TEC);
			tesOpinionComposite.refresh(curTender, curStep, EvaluationFactorEnum.EVAL_BID_TEC, supplierIdList, factorList, isEdit);
			createImageButton(Messages.Technology, EvaluationFactorEnum.EVAL_BID_TEC);
		}
		if (supplierIdListEvalBidTypeMap.containsKey(EvaluationFactorEnum.EVAL_BID_PRICE)) {
			priceEditAndResultComposite = new PriceEditAndResultComposite(evaluationTableComposite, SWT.NONE);
			priceEditAndResultComposite.refresh(curTender, supplierService.getEvaluationStepSupplierList(curTender.getId(), curStep), isEdit);
			createImageButton(Messages.QuotedPrice, EvaluationFactorEnum.EVAL_BID_PRICE);
		}
		Control[] children = switchComposite.getChildren();
		if (children.length == 0) {
			((GridData) switchComposite.getLayoutData()).exclude = true;
		} else {
			if (children.length == 1) {
				((GridData) switchComposite.getLayoutData()).exclude = true;
			} else {
				GridLayout gl_switchComposite = new GridLayout(children.length, false);
				gl_switchComposite.marginHeight = 0;
				gl_switchComposite.marginWidth = 0;
				switchComposite.setLayout(gl_switchComposite);
			}
			curButton = (ImageButton) children[0];
			curButton.notifyListeners(SWT.MouseUp, null);
		}
		
		((GridData) opinionResultLink.getLayoutData()).exclude = EvaluationFactorEnum.EVAL_BID_PRICE.equals(evalBidType);
		
		boolean switchExclude = ((GridData) switchComposite.getLayoutData()).exclude;
		boolean importExclude = ((GridData) opinionResultLink.getLayoutData()).exclude;
		
		((GridData) topComposite.getLayoutData()).exclude = switchExclude & importExclude;
		((GridData) switchSep.getLayoutData()).exclude = switchExclude & importExclude;
		
		GridLayout gl_topComposite = new GridLayout((switchExclude ? 0 : 1) + (importExclude ? 0 : 1), false);
		gl_topComposite.marginHeight = 0;
		gl_topComposite.marginWidth = 0;
		topComposite.setLayout(gl_topComposite);
	}
	
	/**
	 * @Description: 导出清标评议意见
	 * @author chenxw
	 */
	private void exportOpinionResultAction() {
		File templateFile = FileConstants.getResourceFile("ClearBidCommentContent.xlsx");
		if (!templateFile.exists()) return;
		String directorySavePath = FileDialogUtils.getDirectorySavePath(Messages.Tips_selectExportDic, "[" + curTender.getTenderNo() + "]" + curTender.getTenderName());
		if (StringUtils.isEmpty(directorySavePath)) return;
		String fileName = "";
		if (evalBidType.equals(EvaluationFactorEnum.EVAL_BID_BUS)) {
			fileName = Messages.BidFileComments+"-"+Messages.Business+".xlsx";
		} else if (evalBidType.equals(EvaluationFactorEnum.EVAL_BID_TEC)) {
			fileName = Messages.BidFileComments+"-"+Messages.Technology+".xlsx";
		}
		directorySavePath = directorySavePath.concat(File.separator).concat(fileName);
		saveOpinionAction();
		ProgressDialog synchShell = new ProgressDialog(getShell(), MessageFormat.format(Messages.Button_exportFormat, Messages.BidFileComments));
		getExcelReportThread(templateFile, directorySavePath, synchShell).start();
		synchShell.open();
	}
	
	/**
	 * @Description: 获取导出报表线程
	 * @author chenxw
	 * @param templateFile
	 * @param directorySavePath
	 * @param synchShell
	 * @return
	 */
	private Thread getExcelReportThread(final File templateFile, final String directorySavePath, final ProgressDialog synchShell) {
		return new Thread(new Runnable() {
			public void run() {
				ExcelReportUtil.generate(templateFile.getAbsolutePath(), directorySavePath, new ExcelGenerateAdapter() {
					public List<ReportDataSet> initReportDataSetList(String dataSetName, Map<String, String> paramMap, Map<String, String> enumMap) {
						ClearCommentsDataSet dataSet = new ClearCommentsDataSet();
						dataSet.init(curTender);
						List<Supplier> supplierList = null;
						List<EvaluationFactor> factorList = null;
						if (evalBidType.equals(EvaluationFactorEnum.EVAL_BID_BUS)) {
							supplierList = busOpinionComposite.getSupplierList();
							factorList = busOpinionComposite.getEvaluationFactorList();
						} else if (evalBidType.equals(EvaluationFactorEnum.EVAL_BID_TEC)) {
							supplierList = tesOpinionComposite.getSupplierList();
							factorList = tesOpinionComposite.getEvaluationFactorList();
						}
						dataSet.setSupplierList(supplierList);
						dataSet.setEvaluationFactorList(factorList);
						return dataSet.getDateSetList(paramMap, enumMap);
					}
					
					public void afterGenerate(String reportPath) {
						Display.getDefault().syncExec(new Runnable() {
							public void run() {
								if (null != synchShell) {
									synchShell.close();
								}
								MessageDialog.openShortMessage(Messages.Tips_exportSuccess);
							}
						});
					}
				});
			}
		});
	}
	
	/**
	 * @Description: 导入清标评议意见
	 * @author chenxw
	 */
	protected void importOpinionResultAction() {
		String excelFilePath = FileDialogUtils.getFileNames(SWT.MULTI, MessageFormat.format(Messages.Button_importFormat, Messages.BidFileComments), new String[] { "*.xls;*.xlsx" }, new String[] { "excel" });
		if (StringUtils.isEmpty(excelFilePath)) return;
		saveOpinionAction();
		MessageViewDialog dialog = new MessageViewDialog(getShell(), -1);
		dialog.setTitle(MessageFormat.format(Messages.Button_importFormat, Messages.BidFileComments));
		
		ImportSupplierFactorOpinionThread thread = new ImportSupplierFactorOpinionThread(curTender.getId(), excelFilePath, dialog.getCallBack());
		List<Supplier> supplierList = null;
		List<EvaluationFactor> factorList = null;
		if (evalBidType.equals(EvaluationFactorEnum.EVAL_BID_BUS)) {
			supplierList = busOpinionComposite.getSupplierList();
			factorList = busOpinionComposite.getEvaluationFactorList();
		} else if (evalBidType.equals(EvaluationFactorEnum.EVAL_BID_TEC)) {
			supplierList = tesOpinionComposite.getSupplierList();
			factorList = tesOpinionComposite.getEvaluationFactorList();
		}
		thread.setSupplierList(supplierList);
		thread.setEvaluationFactorList(factorList);
		thread.start();
		
		dialog.open();
		
		refreshAction();
	}
	
	/**
	 * @Description: 获取供应商自评意见
	 * @author wangx
	 */
	private void getOpinionAction(){
		List<Supplier> supplierList = null;
		List<EvaluationFactor> factorList = null;
		if (evalBidType.equals(EvaluationFactorEnum.EVAL_BID_BUS)) {
			supplierList = busOpinionComposite.getSupplierList();
			factorList = busOpinionComposite.getEvaluationFactorList();
		} else if (evalBidType.equals(EvaluationFactorEnum.EVAL_BID_TEC)) {
			supplierList = tesOpinionComposite.getSupplierList();
			factorList = tesOpinionComposite.getEvaluationFactorList();
		}
		
		allSupplierOpinionMap =  new HashMap<String,Map<String,String>>();
		List<SupplierEvaluationFactor> supplierEvaluationFactorList = supplierEvaluationFactorService.getEntityListByTenderId(curTender.getId());
		Map<String,SupplierEvaluationFactor>  supplierEvaluationFactorMap = new  HashMap<String,SupplierEvaluationFactor>();
		if(null != supplierEvaluationFactorList && supplierEvaluationFactorList.size() > 0){
			for(SupplierEvaluationFactor supplierEvaluationFactor : supplierEvaluationFactorList){
				supplierEvaluationFactorMap.put(supplierEvaluationFactor.getSupplierId().concat(supplierEvaluationFactor.getFactorCode()), supplierEvaluationFactor);
			}                                                           
		}
		
		if(null == supplierList || null == factorList|| supplierList.size() == 0  || factorList.size() == 0){
			return;
		}
		
		MessageViewDialog dialog = new MessageViewDialog(getShell(), -1);
		httpExecutorService = Executors.newFixedThreadPool(supplierList.size() > 5 ? 5 : supplierList.size());
		dialog.setTitle(Messages.Button_getSupplierSelfOpinion);
		callBack = dialog.getCallBack();
		final CountDownLatch cdl = new CountDownLatch(supplierList.size());
		for (Supplier supplier : supplierList) {
			ParsingSupplierSelfOpinionThread task = new ParsingSupplierSelfOpinionThread(curTender,supplierEvaluationFactorMap,allSupplierOpinionMap,supplier,factorList, cdl, callBack);
			httpExecutorService.submit(task);
		}
		httpExecutorService.shutdown();
		new Thread(new Runnable() {
			public void run() {
				try {
					cdl.await();
				} catch (InterruptedException e) {}
				Display.getDefault().syncExec(new Runnable() {
					public void run() {
						if (supplierSelfOpinionLink.isDisposed()) {
							return;
						}
						callBack.callBackString(Messages.Tips_operateSuccess);
						refreshAction();
					}
				});
			}
		}).start();
		
		dialog.open();
	}
	
	
	/**
	 * @Description: 获得按钮
	 * @author chenxw
	 * @param name
	 * @param evalBidType
	 * @return
	 */
	private ImageButton createImageButton(String name, final String _evalBidType) {
		final RectangleRadioButton button = new RectangleRadioButton(switchComposite, SWT.NONE);
		button.setText(name);
		button.setLeftMargin(20);
		button.setRightMargin(20);
		button.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				curButton.setSelection(false);
				curButton = button;
				curButton.setSelection(true);
				evalBidType = _evalBidType;
				if (evalBidType.equals(EvaluationFactorEnum.EVAL_BID_BUS)) {
					stackLayout.topControl = busOpinionComposite;
				} else if (evalBidType.equals(EvaluationFactorEnum.EVAL_BID_TEC)) {
					stackLayout.topControl = tesOpinionComposite;
				} else if (evalBidType.equals(EvaluationFactorEnum.EVAL_BID_PRICE)) {
					stackLayout.topControl = priceEditAndResultComposite;
				}
				evaluationTableComposite.layout();
			}
		});
		return button;
	}
	
	/**
	 * @Description: 修改意见
	 * @author chenxw
	 */
	public void editOpinionAction() {
		if (!isReview) {
			setButtonStatus(true);
			return;
		}
		boolean isNullBusOpinion = false;
		if (null != busOpinionComposite && !busOpinionComposite.isDisposed()) {
			isNullBusOpinion = busOpinionComposite.isNullFactorOpinion();
		}
		boolean isNullTesOpinion = false;
		if (null != tesOpinionComposite && !tesOpinionComposite.isDisposed()) {
			isNullTesOpinion = tesOpinionComposite.isNullFactorOpinion();
		}
		setButtonStatus(!isNullBusOpinion && !isNullTesOpinion);
	}
	
	/**
	 * @Description: 评审完成
	 * @author chenxw
	 */
	private void submitEvaluationResultAction() {
		if (!submit_result_btn.getEnabled()) {
			return;
		}
		if (null != busOpinionComposite && !busOpinionComposite.isDisposed()) {
			if (busOpinionComposite.isNullFactorOpinion()) {
				return;
			}
		}
		if (null != tesOpinionComposite && !tesOpinionComposite.isDisposed()) {
			if (tesOpinionComposite.isNullFactorOpinion()) {
				return;
			}
		}
		setButtonStatus(false);
		
		boolean isSuccess = false;
		
		if (expertEvaluationStep.getStepStatus().equals(CommonEnum.IN_PROGRESS) || expertEvaluationStep.getStepStatus().equals(CommonEnum.RE_OPERATOR)) {
			if (CommonEnum.YES.equals(curStep.getEvaluationParamByKey(EvaluationStepEnum.USE_AUDIT_PRE_EVAL))) {
				isSuccess = isSubmitSuccess();
			} else {
				isSuccess = isNotAuditSubmitSuccess();
			}
		} else if (expertEvaluationStep.getStepStatus().equals(CommonEnum.IN_EXPERT_REVIEW)) {
			isSuccess = isExpertReviewSuccess();
		} else if (expertEvaluationStep.getStepStatus().equals(CommonEnum.IN_MANAGER_REVIEW)) {
			isSuccess = isManagerReviewSuccess();
		}
		
		if (isSuccess) {
			if (null != taskListener) {
				taskListener.completeTask();
			}
			goBackAction();
		} else {
			setButtonStatus(true);
		}
	}
	
	/**
	 * @Description: 是否提交成功
	 * @author chenxw
	 * @return
	 */
	private boolean isSubmitSuccess() {
		//个人不控制进度
		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 false;
		}
		expertEvaluationStep.setApplyReason(null);
		expertEvaluationStepService.updateAssignProperty(expertEvaluationStep, new String[] { "applyReason" });
		expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.IN_EXPERT_REVIEW, curTender.getId(), curStep, curUser.getId());
		curStep.setStepStatus(CommonEnum.IN_PROGRESS);
		evaluationStepService.updateAssignProperty(curStep, new String[] { "stepStatus" });
		createTask(TaskEnum.PreEvaluation.EXPERT_EEVIEW);
		return true;
	}
	
	/**
	 * @Description: 是否审核成功
	 * @author chenxw
	 * @return
	 */
	private boolean isExpertReviewSuccess() {
		AuditDialog auditDialog = new AuditDialog(getShell());
		auditDialog.setTitle(submit_result_btn.getText());
		AuditDialog.AuditListener listener = new AuditDialog.AuditListener() {
			private boolean isSuccess = false;
			
			public void audit(boolean isAdopt, String message) {
				if (isAdopt) {
					expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.IN_MANAGER_REVIEW, curTender.getId(), curStep, curUser.getId());
					createTask(TaskEnum.PreEvaluation.MANAGER_REVIEW);
				} else {
					expertEvaluationStep.setStepStatus(CommonEnum.IN_PROGRESS);
					expertEvaluationStep.setApplyReason(message);
					expertEvaluationStepService.updateAssignProperty(expertEvaluationStep, new String[] { "stepStatus", "applyReason" });
				}
				isSuccess = true;
			}
			
			public boolean isSuccess() {
				return isSuccess;
			}
		};
		auditDialog.addAuditListener(listener);
		auditDialog.open();
		return listener.isSuccess();
	}
	
	/**
	 * @Description: 生成专家组长审核待办
	 * @author chenxw
	 */
	private void createTask(TaskEnum.PreEvaluation preEvaluation) {
		Task task = new Task();
		task.setId(UuidUtils.getUUID());
		task.setTenderId(curTender.getId());
		task.setInitiatorUserId(Constants.USER.getId());
		task.setTaskType(TaskEnum.PRE_EVALUATION.getKey());
		task.addParam("tenderId", curTender.getId());
		task.addParam("stepId", curStep.getId());
		task.addParam("userId", curUser.getId());
		task.setState(TaskEnum.STATE.NOT_STARTED.getKey());
		task.setInitiationTime(NowTimeUtils.getNowDate());
		switch (preEvaluation) {
			case EXPERT_EEVIEW:
				Expert expertLeader = expertService.getExpertLeader(curTender.getId());
				if (expertLeader == null) {
					return;
				}
				task.setExecutorUserId(expertLeader.getUserId());
				task.setModeType(TaskEnum.PreEvaluation.EXPERT_EEVIEW.getKey());
				task.addParam("hideLeft", "true");
				taskService.insert(task);
				IMClientAdapter.getInstance().sendNoticeAction("MY_TASK", "", expertLeader.getUserId());
				break;
			case MANAGER_REVIEW:
				task.setExecutorUserId(curTender.getUserId());
				task.setModeType(TaskEnum.PreEvaluation.MANAGER_REVIEW.getKey());
				taskService.insert(task);
				IMClientAdapter.getInstance().sendNoticeAction("MY_TASK", "", curTender.getUserId());
				break;
		}
	}
	
	/**
	 * @Description: 是否不审核提交成功
	 * @author chenxw
	 * @return
	 */
	private boolean isNotAuditSubmitSuccess() {
		//个人不控制进度
		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 false;
		}
		expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.COMPLETE, curTender.getId(), curStep, curUser.getId());
		expertEvaluationStep = expertEvaluationStepService.getExpertEvaluationStep(curTender.getId(), curStep.getId(), curUser.getId());
		int evaluationComplateCount = expertEvaluationStepService.getExpertEvaluationComplateCount(curTender.getId(), curStep.getId());
		int evaluationGroupUserCount = evaluationGroupService.getEvaluationGroupUserCount(curTender.getId(), curStep.getId());
		if (evaluationComplateCount >= evaluationGroupUserCount) {
			return expertEvaluationStepService.submitPreEvaluationResult(curTender.getId(), curStep);
		}
		return true;
	}
	
	
	/**
	 * @Description: 是否审核成功
	 * @author chenxw
	 * @return
	 */
	private boolean isManagerReviewSuccess() {
		AuditDialog auditDialog = new AuditDialog(getShell());
		auditDialog.setTitle(submit_result_btn.getText());
		AuditDialog.AuditListener listener = new AuditDialog.AuditListener() {
			private boolean isSuccess = false;
			
			public void audit(boolean isAdopt, String message) {
				if (isAdopt) {
					expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.COMPLETE, curTender.getId(), curStep, curUser.getId());
					expertEvaluationStep = expertEvaluationStepService.getExpertEvaluationStep(curTender.getId(), curStep.getId(), curUser.getId());
					int evaluationComplateCount = expertEvaluationStepService.getExpertEvaluationComplateCount(curTender.getId(), curStep.getId());
					int evaluationGroupUserCount = evaluationGroupService.getEvaluationGroupUserCount(curTender.getId(), curStep.getId());
					if (evaluationComplateCount >= evaluationGroupUserCount) {
						expertEvaluationStepService.submitPreEvaluationResult(curTender.getId(), curStep);
					}
				} else {
					expertEvaluationStep.setStepStatus(CommonEnum.IN_PROGRESS);
					expertEvaluationStep.setApplyReason(message);
					expertEvaluationStepService.updateAssignProperty(expertEvaluationStep, new String[] { "stepStatus", "applyReason" });
				}
				isSuccess = true;
			}
			
			public boolean isSuccess() {
				return isSuccess;
			}
		};
		auditDialog.addAuditListener(listener);
		auditDialog.open();
		return listener.isSuccess();
	}
	
	/**
	 * @Description: 设置按钮的状态
	 * @author chenxw
	 * @param enable
	 */
	private void setButtonStatus(boolean enable) {
		if (buttonComposite.isDisposed()) {
			return;
		}
		for (Control control : buttonComposite.getChildren()) {
			if (!control.isDisposed() && control instanceof ImageButton) {
				((ImageButton) control).setEnabled(enable);
			}
		}
	}
	
	/**
	 * @Description: 返回
	 * @author chenxw
	 */
	private void goBackAction() {
		saveOpinionAction();
		if (null != expertEvaluationListener) {
			expertEvaluationListener.complete();
		}
		if (null != taskListener) {
			taskListener.returnMyTask();
		}
	}
	
	/**
	 * @Description: 保存评议意见
	 * @author chenxw
	 */
	private void saveOpinionAction() {
		if (null != busOpinionComposite) {
			busOpinionComposite.saveOpinionAction(false);
		}
		if (null != tesOpinionComposite) {
			tesOpinionComposite.saveOpinionAction(false);
		}
	}
	
	public void addExpertEvaluationListener(ExpertEvaluationListener expertEvaluationListener) {
		this.expertEvaluationListener = expertEvaluationListener;
	}
	
	public void addTaskListener(TaskListener taskListener) {
		this.taskListener = taskListener;
	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	
}
