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

import java.io.File;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.nebula.widgets.grid.Grid;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.nebula.widgets.grid.GridItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.listener.BorderLineListener;
import org.eclipse.swt.core.listener.RoundBorderLineListener;
import org.eclipse.swt.core.listener.VerifyCardIdListener;
import org.eclipse.swt.core.listener.VerifyPhoneListener;
import org.eclipse.swt.core.listener.VerifyRequiredListener;
import org.eclipse.swt.core.utils.ValidateUtils;
import org.eclipse.swt.core.widgets.ELink;
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.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
import com.gx.obe.WebserviceConstants;
import com.gx.obe.business.entity.SignatureFile;
import com.gx.obe.business.entity.SignatureUser;
import com.gx.obe.business.http.HttpResponse;
import com.gx.obe.business.http.HttpUrl;
import com.gx.obe.business.http.HttpUtils;
import com.gx.obe.common.file.download.DownloadFileUtill;
import com.gx.obe.common.file.enumeration.FileConstants;
import com.gx.obe.common.file.service.SignatureFileService;
import com.gx.obe.common.file.service.SignatureUserService;
import com.gx.obe.common.file.utils.FileMd5Util;
import com.gx.obe.common.widget.button.EvalLeftButton;
import com.gx.obe.common.widget.button.EvaluationButton;
import com.gx.obe.common.widget.composite.TipsComposite;
import com.gx.obe.common.widget.dialog.ProgressDialog;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.components.core.enumeration.ExpertEnum;
import com.gx.obe.config.utils.PropertiesUtils;
import com.gx.obe.evaluation.step.dialog.ExpertAvoidDialog;
import com.gx.obe.evaluation.step.dialog.ShowSignNodeDialog;
import com.gx.obe.evaluation.step.listener.ExpertEvaluationListener;
import com.gx.obe.evaluation.step.report.DisciplineDataSet;
import com.gx.obe.http.json.JsonUtil;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.auth.AuthUser;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.expert.Expert;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.ReportFileService;
import com.gx.obe.web.service.SupplierService;
import com.gx.obe.web.service.UserService;
import com.gx.obe.web.service.expert.ExpertEvaluationStepService;
import com.gx.obe.web.service.expert.ExpertService;
import com.gx.pdf.utils.transfer.TransferUtils;
import com.gx.utils.ActionLogUtils;
import com.gx.utils.NowTimeUtils;
import com.report.word.WordDataSet;
import com.report.word.WordGenerateAdapter;
import com.report.word.WordTemplate;
import com.report.word.WordTemplateReportUtil;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;


/**
 * @Description: 专家签到
 * @author guoyr
 */
public class ExpertSignEvaluationComposite extends Composite {

	private TenderProjectEntity tenderProject;
	private EvaluationStep evaluationStep;
	private ExpertEvaluationListener expertEvaluationListener;
	private Composite left_tool_composite;
	private SupplierService supplierService = new SupplierService();
	private ExpertEvaluationStepService expertEvaluationStepService ;;
	private Grid grid;
	private Text userNameText;
	private Text idCardText;
	private Text professionalTitleText;
	private Text specialtyText;
	private Text companyText;
	private Text phoneText;
	private EvaluationButton saveBtn;
	private Button read_btn;
	private EvaluationButton avoid_btn;
	private ELink evaluavtion_note_label;
	private UserService userService;
	private CLabel supplier_title;
	private ProgressDialog synchShellInfo;
//	private Expert CURRENT_EXPERT;
	private Composite composite_4;
	private Composite button_composite;
	private SignatureFileService signatureFileService = new SignatureFileService();
	private SignatureUserService signatureUserService = new SignatureUserService();
	boolean signatureSelftCommitment = false;
	boolean hasSignatrueCommitment = false;
	private TipsComposite tipsComposite;
	private ExpertService expertService = new ExpertService();
	
	{
		LogUtils.logClass(ExpertSignEvaluationComposite.class);
	}
	static{
		/**
		 * 第二层验证
		 * **/
		try {
			com.gx.security.CertValidator.getInstance().validateAuthorization();
		} catch (com.gx.security.CertificateException e) {
			throw new com.gx.security.SecurityRuntimeException(e);
		}
	}
	/**
	 * @param parent
	 * @param style
	 */
	public ExpertSignEvaluationComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(2, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
			}
		});
		this.tenderProject = (TenderProjectEntity)parent.getData("tenderProjectEntity");
		this.evaluationStep = (EvaluationStep)parent.getData("evaluationStep");
		
		left_tool_composite = new Composite(this, SWT.NONE);
		left_tool_composite.setBackground(EvalLeftButton.BG);
		left_tool_composite.setBackgroundMode(SWT.INHERIT_FORCE);
		left_tool_composite.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1));
		GridLayout gl_left_tool_composite = new GridLayout(1, false);
		left_tool_composite.setLayout(gl_left_tool_composite);
		
		EvalLeftButton goto_back = new EvalLeftButton(left_tool_composite, SWT.NONE);
		goto_back.setLayoutData(new GridData(SWT.CENTER, 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() {
			@Override
			public void mouseUp(MouseEvent e) {
				goBackAction();
			}
		});
		
		EvalLeftButton refresh_evaluation = new EvalLeftButton(left_tool_composite, SWT.NONE);
		refresh_evaluation.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		refresh_evaluation.setImage(ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/refresh.png"));
		refresh_evaluation.setToolTipText(Messages.Button_refresh);
		
		refresh_evaluation.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				refreshTenderInfo();
			}
		});
		
		Composite right_composite = new Composite(this, SWT.NONE);
		GridLayout gl_right_composite = new GridLayout(1, false);
		gl_right_composite.marginHeight = 10;
		gl_right_composite.marginWidth = 80;
		right_composite.setLayout(gl_right_composite);
		right_composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Composite supplier_composite = new Composite(right_composite, SWT.NONE);
//		supplier_composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_supplier_composite = new GridLayout(1, false);
		gl_supplier_composite.marginHeight = 1;
		gl_supplier_composite.marginWidth = 1;
		supplier_composite.setLayout(gl_supplier_composite);
		GridData gd_supplier_composite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_supplier_composite.heightHint = 100;
		supplier_composite.setLayoutData(gd_supplier_composite);
		
		supplier_title = new CLabel(supplier_composite, SWT.NONE);
		supplier_title.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false, 1, 1));
		supplier_title.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GRAY));
		supplier_title.setFont(SWTResourceManager.getFont("微软雅黑", 15, SWT.NORMAL));
		supplier_title.setText(Messages.Supplier);
		
		Composite composite_3 = new Composite(supplier_composite, SWT.NONE);
		composite_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		composite_3.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite_3.addPaintListener(new RoundBorderLineListener(ColorConstants.BORDER_LINE_COLOR));
		GridLayout gl_composite_3 = new GridLayout(1, false);
		gl_composite_3.marginHeight = 1;
		gl_composite_3.marginWidth = 1;
		composite_3.setLayout(gl_composite_3);
		
		grid = new Grid(composite_3, SWT.V_SCROLL);
		grid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		grid.setHeaderVisible(true);
		
		GridColumn gridColumn_1 = new GridColumn(grid, SWT.CENTER);
		gridColumn_1.setText(Messages.Num);
		gridColumn_1.setWidth(50);
		gridColumn_1.setMaxmumWidth(80);
		
		GridColumn gridColumn_2 = new GridColumn(grid, SWT.NONE);
		gridColumn_2.setText(Messages.Name);
		gridColumn_2.setWidth(250);
		
//		GridColumn gridColumn_3 = new GridColumn(grid, SWT.CENTER);
//		gridColumn_3.setText(Messages.OrgCode);
//		gridColumn_3.setWidth(100);
//		
//		GridColumn gridColumn = new GridColumn(grid, SWT.NONE);
//		gridColumn.setText(Messages.Address);
//		gridColumn.setWidth(250);
//		gridColumn.setMinimumWidth(250);
		
		
		Composite composite_2 = new Composite(right_composite, SWT.NONE);
		composite_2.setBackgroundMode(SWT.INHERIT_FORCE);
		composite_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridData gd_composite_2 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_composite_2.heightHint = 100;
		composite_2.setLayoutData(gd_composite_2);
		composite_2.setLayout(new GridLayout(1, false));
		composite_2.addPaintListener(new RoundBorderLineListener(ColorConstants.BORDER_LINE_COLOR));
		
		CLabel expert_info = new CLabel(composite_2, SWT.NONE);
		expert_info.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false, 1, 1));
		expert_info.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GRAY));
		expert_info.setFont(SWTResourceManager.getFont("微软雅黑", 15, SWT.NORMAL));
		expert_info.setText(Messages.Title_expertInfo);
		
		Composite expert_info_composite = new Composite(composite_2, SWT.NONE);
		expert_info_composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_expert_info_composite = new GridLayout(4, false);
		gl_expert_info_composite.marginWidth = 30;
		gl_expert_info_composite.verticalSpacing = 5;
		gl_expert_info_composite.marginHeight = 5;
		expert_info_composite.setLayout(gl_expert_info_composite);
		
		CLabel label_3 = new CLabel(expert_info_composite, SWT.NONE);
		label_3.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		label_3.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label_3.setLeftMargin(50);
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, true, 1, 1));
		label_3.setText(Messages.FullName+"：");
		
		userNameText = new Text(expert_info_composite, SWT.NONE);
		userNameText.setTopIndex(2);
		userNameText.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		GridData gd_userNameText = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_userNameText.heightHint = 25;
		gd_userNameText.widthHint = 200;
		userNameText.setLayoutData(gd_userNameText);
		userNameText.addPaintListener(new BorderLineListener(ColorConstants.BORDER_LINE_COLOR, false, true, false, false));
		userNameText.addVerifyListener(new VerifyRequiredListener(userNameText, MessageFormat.format(Messages.Tips_required, Messages.FullName)));
		userNameText.setTextLimit(20);
		
		CLabel label_4 = new CLabel(expert_info_composite, SWT.NONE);
		label_4.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		label_4.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label_4.setLeftMargin(50);
		label_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_4.setText(Messages.IDCard+"：");
		
		idCardText = new Text(expert_info_composite, SWT.NONE);
		idCardText.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		GridData gd_idCardText = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_idCardText.heightHint = 25;
		gd_idCardText.widthHint = 200;
		idCardText.setLayoutData(gd_idCardText);
		idCardText.addPaintListener(new BorderLineListener(ColorConstants.BORDER_LINE_COLOR, false, true, false, false));
		idCardText.addVerifyListener(new VerifyRequiredListener(idCardText,MessageFormat.format(Messages.Tips_required, Messages.IDCard)));
		idCardText.addVerifyListener(new VerifyCardIdListener(MessageFormat.format(Messages.Tips_pleaseEnterTheCorrect, Messages.IDCard)));
		
		CLabel label_5 = new CLabel(expert_info_composite, SWT.NONE);
		label_5.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		label_5.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, true, 1, 1));
		label_5.setText(Messages.ProfessionalTitle+"：");
		
		professionalTitleText = new Text(expert_info_composite, SWT.NONE);
		professionalTitleText.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		GridData gd_professionalTitleText = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_professionalTitleText.heightHint = 25;
		professionalTitleText.setLayoutData(gd_professionalTitleText);
		professionalTitleText.addPaintListener(new BorderLineListener(ColorConstants.BORDER_LINE_COLOR, false, true, false, false));
		professionalTitleText.setTextLimit(50);
		
		CLabel label_6 = new CLabel(expert_info_composite, SWT.NONE);
		label_6.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		label_6.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_6.setText(Messages.Specialty+"：");
		
		specialtyText = new Text(expert_info_composite, SWT.NONE);
		specialtyText.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		GridData gd_specialtyText = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_specialtyText.heightHint = 25;
		specialtyText.setLayoutData(gd_specialtyText);
		specialtyText.setTextLimit(100);
		specialtyText.addPaintListener(new BorderLineListener(ColorConstants.BORDER_LINE_COLOR, false, true, false, false));
		
		CLabel label_7 = new CLabel(expert_info_composite, SWT.NONE);
		label_7.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label_7.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		label_7.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, true, 1, 1));
		label_7.setText(Messages.WorkCompany+"：");
		
		companyText = new Text(expert_info_composite, SWT.NONE);
		companyText.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		GridData gd_companyText = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_companyText.heightHint = 25;
		companyText.setLayoutData(gd_companyText);
		companyText.setTextLimit(100);
		companyText.addPaintListener(new BorderLineListener(ColorConstants.BORDER_LINE_COLOR, false, true, false, false));
		
		CLabel label_8 = new CLabel(expert_info_composite, SWT.NONE);
		label_8.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		label_8.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label_8.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_8.setText(Messages.LinkerTel+"：");
		
		phoneText = new Text(expert_info_composite, SWT.NONE);
		phoneText.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		GridData gd_phoneText = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_phoneText.heightHint = 25;
		phoneText.setLayoutData(gd_phoneText);
		phoneText.addVerifyListener(new VerifyPhoneListener(MessageFormat.format(Messages.Tips_pleaseEnterTheValid, Messages.LinkerTel)));
		phoneText.addPaintListener(new BorderLineListener(ColorConstants.BORDER_LINE_COLOR, false, true, false, false));
		phoneText.setTextLimit(20);
		
		tipsComposite = new TipsComposite(right_composite, SWT.NONE);
		tipsComposite.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		tipsComposite.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		tipsComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		composite_4 = new Composite(right_composite, SWT.NONE);
		GridLayout gl_composite_4 = new GridLayout(2, false);
		gl_composite_4.horizontalSpacing = 25;
		composite_4.setLayout(gl_composite_4);
		composite_4.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		
		evaluavtion_note_label = new ELink(composite_4, SWT.NONE);
		evaluavtion_note_label.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				evaluavtionNote();
			}
		});
		evaluavtion_note_label.setImage(ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/book_24.png"));
		evaluavtion_note_label.setText(Messages.Button_evaluationCommitment);
		
		read_btn = new Button(composite_4, SWT.CHECK);
		read_btn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				checkHasSignatrueCommitment(true);
			}
		});
		read_btn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		read_btn.setText(Messages.Tips_evaluationCommitment);
		
		button_composite = new Composite(right_composite, SWT.NONE);
		GridLayout gl_button_composite = new GridLayout(2, false);
		gl_button_composite.marginHeight = 1;
		gl_button_composite.marginWidth = 1;
		button_composite.setLayout(gl_button_composite);
		button_composite.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false, 1, 1));
		
		saveBtn = new EvaluationButton(button_composite, SWT.NONE);
		saveBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				signAction();
			}
		});
		saveBtn.setText(Messages.Button_expertSignIn);
		
		avoid_btn = new EvaluationButton(button_composite, SWT.NONE);
		avoid_btn.setBackgroundColor(SWTResourceManager.getColor(SWT.COLOR_RED));
		avoid_btn.setText(Messages.Button_expertAvoid);
		avoid_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				avoidAction();
			}
		});
		
		init();
	}

	private void evaluavtionNote(){
		if(!evaluavtion_note_label.getEnabled()){
			return ;
		}
		if(userNameText.getText().trim().length() > 0){
			Constants.USER.setUserName(userNameText.getText().trim());
		}
		if(idCardText.getText().trim().length() > 0){
			Constants.USER.setUserIdcard(idCardText.getText().trim());
		}else if(signatureSelftCommitment){
			MessageDialog.openShortMessage(Messages.Tips_inputExpertInfoFirst);
			return;
		}
		openFile();
	}
	
	/**
	 * @Description: 打开签章界面
	 * @author wangxiang
	 * @param file
	 * @param fileName 
	 */
	private void openFile(){
		File myCommitmentFile = getMyCommitment();
		if(null == myCommitmentFile || !myCommitmentFile.exists()) {
			//模板生成承诺书
			creatExpertNodePdf();
		}
		ShowSignNodeDialog dialog = new ShowSignNodeDialog(getShell(),tenderProject, signatureSelftCommitment);
		dialog.setTitle(Messages.Button_evaluationCommitment);
		dialog.open();
		checkHasSignatrueCommitment(false);
	}
	
	
	/**
	 * @Description: 获得流转签章中评标承诺书
	 * @author guoyr
	 * @return 
	 */
	private SignatureFile getMySignatureFile() {
		List<SignatureFile> signatureFileList = signatureFileService.getSignatureFileList(tenderProject.getId(),FileConstants.REPORT_FILE_FOLDER);
		SignatureFile mySignatureFile = null;
		if(null != signatureFileList && signatureFileList.size() > 0) {
			for(SignatureFile signatureFile : signatureFileList) {
				if((signatureFile.getFileName().contains("承诺书") || signatureFile.getFileName().contains("ommitment"))) {
					if(signatureSelftCommitment) {
						if(signatureFile.getFileName().contains(Constants.USER.getUserName())) {
							return signatureFile;
						}
					}else {
						mySignatureFile = signatureFile;
					}
				}
			}
		}
		return mySignatureFile;
	}
	
	/**
	 * @Description: 初始化界面数据
	 * @author guoyr
	 */
	private void init(){
		supplier_title.setText(com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProject.getTenderType()));
		expertEvaluationStepService = new ExpertEvaluationStepService();
		userService = new UserService();
		grid.setItemHeight(30);
		
		checkHasSignatrueCommitment(false);
		listSupplier();
		initExpertInfo();
	}
	
	/**
	 * @Description: 获得我的承诺书
	 * @author guoyr
	 * @return 
	 */
	private File getMyCommitment() {
		File myCommentFile = null;
		final File myCommitmentDir = new File(FileConstants.getReportFilePath(tenderProject.getId()).concat(File.separator).concat("commitment"));
		if(myCommitmentDir.exists()) {
			File[] myCommentFiles = myCommitmentDir.listFiles();
			if(null != myCommentFiles) {
				for(File _myCommentFile : myCommentFiles) {
					if(_myCommentFile.isFile()) {
						if(_myCommentFile.getName().endsWith("pdf")) {
							if(signatureSelftCommitment) {
								if((_myCommentFile.getName().contains("承诺书") || _myCommentFile.getName().contains("ommitment")) && _myCommentFile.getName().contains(Constants.USER.getUserName())) {
									return _myCommentFile;
								}
							}else {
								if(_myCommentFile.getName().contains("承诺书") || _myCommentFile.getName().contains("ommitment")) {
									myCommentFile = _myCommentFile;
								}
							}
						}
					}
				}
			}
		}
		return myCommentFile;
	}
	
	/**
	 * @Description: 
	 * @author guoyr 
	 */
	private void checkHasSignatrueCommitment(boolean showTips) {
		
		read_btn.setEnabled(false);
		signatureSelftCommitment = CommonEnum.YES.equals(evaluationStep.getEvaluationParamByKey("signatureSelftCommitment"));
		new Thread(new Runnable() {
			public void run() {
				hasSignatrueCommitment = false;
				SignatureFile mySignatureFile = getMySignatureFile();
				// 如果在流转签章中找到我的评标承诺书，则下载下来
				if (null != mySignatureFile) {
					String fileURL = "/".concat(tenderProject.getId()).concat("/").concat(FileConstants.REPORT_FILE_FOLDER).concat("/").concat(mySignatureFile.getFileName());
					File signaturePdfFile = new File(FileConstants.getReportFilePath(tenderProject.getId()).concat(File.separator).concat("commitment").concat(File.separator).concat(mySignatureFile.getFileName()));
					boolean downloadNewFile = false;
					if(signaturePdfFile.exists()) {
						ReportFileService reportFileService  = new ReportFileService();
						if (!FileMd5Util.getFileMD5(signaturePdfFile).equals(reportFileService.getFileMD5(fileURL))) {
							signaturePdfFile.delete();
							downloadNewFile = true;
						}
					}else {
						downloadNewFile = true;
					}
					if(downloadNewFile) {
						Display.getDefault().syncExec(new Runnable() {
							public void run() {
								if(!read_btn.isDisposed()) {
									DownloadFileUtill.startDownloadFileSync(fileURL, mySignatureFile.getFileName(), signaturePdfFile.getParentFile().getAbsolutePath(), "正在获取承诺书");
								}
							}
						});
					}
					hasSignatrueCommitment = hasSignatrueCommitment(mySignatureFile);
				}
				Display.getDefault().syncExec(new Runnable() {
					public void run() {
						if(!read_btn.isDisposed()) {
							read_btn.setEnabled(true);
							if(signatureSelftCommitment) {
								read_btn.setSelection(hasSignatrueCommitment);
								if(showTips && !hasSignatrueCommitment) {
									MessageDialog.openShortMessage(Messages.Tips_noSignCommitment);
								}
							}else {
								if(hasSignatrueCommitment) {
									read_btn.setSelection(true);
								}
							}
							saveBtn.setEnabled(read_btn.getSelection());
						}
					}
				});
			}
		}).start();
		
	}
	/**
	 * @Description: 生成评标须知PDF
	 * @author wangx
	 */
	private void creatExpertNodePdf(){
		//承诺书模板
		final File templateFile = FileConstants.getResourceFile(Constants.EXPERT_COMMITMENT);
		if(templateFile.exists()){
			Display.getDefault().syncExec(new Runnable() {
				public void run() {
					synchShellInfo = new ProgressDialog(getShell(), MessageFormat.format(Messages.Tips_generatingFormat, Messages.Button_evaluationCommitment));
				}
			});
			
			Thread creatPdf = new Thread(new Runnable() {
				public void run() {
					String filePath = templateFile.getAbsolutePath(); 
					final File reportFile = new File(FileConstants.getReportFilePath(tenderProject.getId()).concat(File.separator).concat("commitment").concat(File.separator).concat(Messages.Button_evaluationCommitment+"_"+Constants.USER.getUserName()).concat(templateFile.getName().substring(templateFile.getName().indexOf("."))));
					if(reportFile.exists()){
						reportFile.delete();
					}
					final DisciplineDataSet reportDataSet = new DisciplineDataSet();
					reportDataSet.init(tenderProject, null);
					final WordDataSet disciplineDataSet = reportDataSet.getWordDataSet();
					
					WordTemplateReportUtil.generate(filePath, reportFile.getAbsolutePath(), new WordGenerateAdapter() {
						
						public WordDataSet initDataSet(WordTemplate wordTemplate) {
							return disciplineDataSet;
						}
						
						public void onGenerate(String reportPath) {
						}
						
						public void afterGenerate(boolean generate, final String reportPath) {
							if (generate) {
								File myDocFile = new File(reportPath);
								if(myDocFile.exists()) {
									File pdfFile = TransferUtils.convert2PDF(myDocFile.getAbsolutePath());
									if(null != pdfFile && pdfFile.exists()){
										myDocFile.delete();
										if(signatureSelftCommitment) {
											createSignatrueCommitment(pdfFile);
										}
									}
								}
							}
							Display.getDefault().syncExec(new Runnable() {
								public void run() {
									if(!read_btn.isDisposed()) {
										if(null != synchShellInfo){
											synchShellInfo.close();
										}
										read_btn.setEnabled(true);
										saveBtn.setEnabled(read_btn.getSelection());
									}
								}
							});
						}
					});
				}
			});
			creatPdf.start();
			Display.getDefault().syncExec(new Runnable() {
				public void run() {
					synchShellInfo.open();
				}
			});
		
		}
	}
	
	private void createSignatrueCommitment(File pdfFile) {
		List<File> fileList = new ArrayList<File>();
		fileList.add(pdfFile);
		List<AuthUser> userList = new ArrayList<AuthUser>();
		userList.add(Constants.USER);
		signatureFileService.saveBatchSignatureFile(tenderProject.getId(),FileConstants.REPORT_FILE_FOLDER, fileList, userList).size();
	
	}
	
	/**
	 * @Description: 返回
	 * @author guoyr 
	 */
	private void goBackAction(){
		if(null != expertEvaluationListener){
			expertEvaluationListener.complete();
		}
	}
	/**
	 * @Description: 刷新项目信息
	 * @author guoyr
	 */
	private void refreshTenderInfo(){
		listSupplier();
		initExpertInfo();
	}
	
	/**
	 * @Description: 我已阅读
	 * @author guoyr 
	 * @param nodeSignatureFile 
	 */
	private boolean hasSignatrueCommitment(SignatureFile nodeSignatureFile){
		boolean hasSignatrueCommitment = false;
		if(null != nodeSignatureFile) {
			List<SignatureUser> signatureUserList = signatureUserService.getSignatureUserBySignatureId(nodeSignatureFile.getId());
			if(null != signatureUserList) {
				for(SignatureUser signatureUser : signatureUserList) {
					if (signatureUser.getUserId().equals(Constants.USER.getId()) && CommonEnum.YES.equals(signatureUser.getSignatureType())) {
						hasSignatrueCommitment = true;
					}
				}
			}
		}
		
		return hasSignatrueCommitment;
	}
	/**
	 * @Description: 列出供应商
	 * @author guoyr 
	 */
	private void listSupplier(){
		grid.removeAll();
		List<Supplier> supplierList = supplierService.getOpenBidSuccessSupplier(tenderProject.getId());
		int supplierCount = 1;
		if(null != supplierList && supplierList.size() > 0 ){
			for(Supplier supplier : supplierList){
				GridItem item = new GridItem(grid, SWT.NONE);
				item.setText(0, supplierCount+"");
				item.setText(1, supplier.getSupplierName());
//				item.setText(2, ObjectUtils.getObjString(supplier.getOrgCode()));
//				item.setText(3, ObjectUtils.getObjString(supplier.getAddress()));
				supplierCount++;
			}
		}
	}
	
	/**
	 * @Description: 初始化专家的信息
	 * @author guoyr
	 */
	private void initExpertInfo() {
		AuthUser user = Constants.USER;
		userNameText.setText(ObjectUtils.getObjString(user.getUserName()));
		idCardText.setText(ObjectUtils.getObjString(user.getUserIdcard()));
		phoneText.setText(ObjectUtils.getObjString(user.getPhone()));
		professionalTitleText.setText(ObjectUtils.getObjString(user.getProfessionalTitle()));
		specialtyText.setText(ObjectUtils.getObjString(user.getSpecialty()));
		companyText.setText(ObjectUtils.getObjString(user.getCompany()));
		tipsComposite.addTips(ObjectUtils.getObjString(evaluationStep.getStepMemo(), Messages.Tips_expertSignStep));
	}
	
	/**
	 * @Description: 签到事件
	 * @author guoyr
	 */
	private void signAction(){
		if(saveBtn.getEnabled()){
			if(!ValidateUtils.checkInput(userNameText.getParent()))return;
			if(submitExpertSignEvaluationn(ExpertEnum.SIGN, null)){
				goBackAction();
			}else {
				MessageDialog.openError(getShell(), Messages.Tips_operateFail);
			}
			//上传第一个签到时间
			List<Expert> expertList = expertService.getExpertListByTenderId(tenderProject.getId());
			int i=0;
			for(Expert expert : expertList){
				if(expert.getSignTime()!=null) {
					i++;
				}
			}
			if(i==0) {
				HttpUrl httpUrl = HttpUtils.getProjectHttpUrl(tenderProject, PropertiesUtils.getWebserviceProperty(WebserviceConstants.PUSH_DATE_TO_VEDIO_PLAT_FORM, ""));
				httpUrl.setName("上传第一个专家签到时间");
				httpUrl.setAttribute("pushType", "01");
				httpUrl.setAttribute("tenderNo", tenderProject.getTenderNo());
				httpUrl.setAttribute("stageType", "");
				httpUrl.setAttribute("stageId", tenderProject.getStageId());
				httpUrl.setAttribute("tenderNoNumber", tenderProject.getTenderNumber());
				httpUrl.setAttribute("dateTime", NowTimeUtils.getDateTime());
				
				HttpResponse httpResponse = HttpUtils.doPost(httpUrl);
				httpResponse.getStatus();
			}
		}
	}
	

	/**
	 * @Description: 回避事件
	 * @author guoyr
	 */
	private void avoidAction(){
		if(avoid_btn.getEnabled()){
			if(!ValidateUtils.checkInput(userNameText.getParent()))return;
			ExpertAvoidDialog dialog = new ExpertAvoidDialog(getShell(), Constants.USER );
			dialog.setTitle(Messages.Button_expertAvoid);
			dialog.open();
			if(dialog.isAvoidYes()){
				if(submitExpertSignEvaluationn(ExpertEnum.AVOID, dialog.getAvoidReason())){
					goBackAction();
				}else {
					MessageDialog.openError(getShell(), Messages.Tips_operateFail);
				}
			}
		}
	}
	
	/**
	 * @Description: 保存签到结果
	 * @author guoyr
	 * @param signType
	 * @param signResult
	 * @return
	 */
	private boolean submitExpertSignEvaluationn(String signType, String signResult){
		boolean signFlag = expertEvaluationStepService.submitExpertSignEvaluation(tenderProject.getId(), Constants.USER.getId(), evaluationStep, signType, signResult);
		if(signFlag){
			List<String> updatePropertyList = new ArrayList<String>();
			AuthUser user = Constants.USER;
			updatePropertyList.add("id");
			if(userNameText.getText().trim().length() > 0){
				user.setUserName(userNameText.getText().trim());
				Constants.USER.setUserName(userNameText.getText().trim());
				updatePropertyList.add("userName");
			}
			if(idCardText.getText().trim().length() > 0){
				user.setUserIdcard(idCardText.getText().trim());
				Constants.USER.setUserIdcard(idCardText.getText().trim());
				updatePropertyList.add("userIdcard");
			}
			if(phoneText.getText().trim().length() > 0){
				user.setPhone(phoneText.getText().trim());
				Constants.USER.setPhone(phoneText.getText().trim());
				updatePropertyList.add("phone");
			}
			if(professionalTitleText.getText().trim().length() > 0){
				user.setProfessionalTitle(professionalTitleText.getText().trim());
				updatePropertyList.add("professionalTitle");
			}
			if(specialtyText.getText().trim().length() > 0){
				user.setSpecialty(specialtyText.getText().trim());
				updatePropertyList.add("specialty");
			}
			if(companyText.getText().trim().length() > 0){
				user.setCompany(companyText.getText().trim());
				updatePropertyList.add("company");
			}
			if(updatePropertyList.size() > 0){
				ActionLogUtils.addServerLog(tenderProject.getId(), "专家签到", JsonUtil.jsonObjectToStr(user));
				userService.updateAssignProperty(user, updatePropertyList.toArray(new String[updatePropertyList.size()]));
			}
		}else {
			return false;
		}
		return true;
		
	}
	
	
	public void addExpertEvaluationListener(ExpertEvaluationListener expertEvaluationListener){
		this.expertEvaluationListener = expertEvaluationListener;
	}
	@Override
	protected void checkSubclass() {
	}
	
}
