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

import java.io.File;
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
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.BigButton;
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.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;

import com.gx.obe.ColorConstants;
import com.gx.obe.action.bean.Action;
import com.gx.obe.business.entity.BidPriceRound;
import com.gx.obe.business.entity.MenuFunction;
import com.gx.obe.business.entity.SupplierEvaluationStep;
import com.gx.obe.business.enumeration.MenuEnum;
import com.gx.obe.common.file.enumeration.FileConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.dialog.MessageViewDialog;
import com.gx.obe.common.widget.listener.SelectSupplierListener;
import com.gx.obe.common.widget.utils.MenuFunctionUtils;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.components.core.enumeration.BidPriceEnum;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.components.core.enumeration.TenderProjectEnum;
import com.gx.obe.config.ConfigConstants;
import com.gx.obe.evaluation.step.composite.BidPriceResutlTableComposite;
import com.gx.obe.evaluation.step.composite.MultiBidPriceResultDetailComposite;
import com.gx.obe.evaluation.step.composite.RemoteMultiBidPriceInProgressComposite;
import com.gx.obe.evaluation.step.dialog.ApplyReBidPriceDialog;
import com.gx.obe.evaluation.step.dialog.EndRoundPriceConfirmDialog;
import com.gx.obe.evaluation.step.dialog.ViewMultiRoundPriceDialog;
import com.gx.obe.evaluation.step.listener.ExpertEvaluationListener;
import com.gx.obe.evaluation.step.listener.ReBidPriceCompoleteListener;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.util.utils.FileUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.RuntimeUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.util.utils.XmlUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.auth.Menu;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.expert.Expert;
import com.gx.obe.web.entity.price.BidPriceHisResult;
import com.gx.obe.web.entity.price.BidPriceResult;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;
import com.gx.obe.web.service.BidPriceHisResultService;
import com.gx.obe.web.service.BidPriceResultService;
import com.gx.obe.web.service.BidPriceRoundStatusService;
import com.gx.obe.web.service.MenuService;
import com.gx.obe.web.service.ReBidPriceRoundService;
import com.gx.obe.web.service.SupplierEvaluationStepService;
import com.gx.obe.web.service.SupplierService;
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.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

public class RemoteMultiRoundsPriceComposite extends Composite {
	private TenderProjectEntity tenderProjectEntity;
	private EvaluationStep evaluationStep;
	private ExpertEvaluationStepService expertEvaluationStepService ;
	private SupplierService supplierService = new SupplierService();
	private EvaluationStepService evaluationStepService = new EvaluationStepService();
	private ExpertService expertService ;
	private ReBidPriceRoundService reBidPriceRoundService ;
	private BidPriceResultService bidPriceResultService;
	private TenderProjectRuleEntity tenderProjectRuleEntity;
	private Expert CURRENT_EXPERT;
	private ExpertEvaluationListener expertEvaluationListener;
	private Composite showComposite;
	private Composite roundComposite;
	private ELink addRoundElink;
	private ELink endRoundElink;
	private ELink exportFilesBtn;
	private MenuFunctionUtils menuFunctionUtils = new MenuFunctionUtils();
	private List<Supplier> supplierList;
	private Composite savecomposite;
	private BigButton finishButton;
	private ReBidPriceCompoleteListener compoleteLisener;
	private boolean result = false;
	private BidPriceHisResultService bidPriceHisResultService;
	private Integer allowMaxRound;
	private Label tips;
	private BigDecimal minScore;
	private Action projectAction;
	private SupplierEvaluationStepService supplierEvaluationStepService;
	private Composite roundListComposite;
	private int maxRound = 1;
	private ImageButton curRoundButton;
	private String SUPPLIER_NAME;
	private MultiBidPriceResultDetailComposite showDetailComposite;
	private BidPriceResutlTableComposite bidPriceResutlTableComposite;
	private SelectSupplierListener selectSupplierListener;
	private BidPriceRound curbidPriceRound;
	private ELink viewResultElink;
	private BidPriceRoundStatusService bidPriceRoundStatusService;
	
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public RemoteMultiRoundsPriceComposite(Composite parent, Action projectAction, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		gridLayout.verticalSpacing = 5;
		setLayout(gridLayout);
		this.projectAction = projectAction;
		roundComposite = new Composite(this, SWT.NONE);
		GridLayout gl_roundComposite = new GridLayout(6, false);
		gl_roundComposite.verticalSpacing = 0;
		gl_roundComposite.marginHeight = 0;
		gl_roundComposite.marginWidth = 0;
		roundComposite.setLayout(gl_roundComposite);
		roundComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		

		roundListComposite = new Composite(roundComposite, SWT.NONE);
		roundListComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		RowLayout rl_roundListComposite = new RowLayout(SWT.HORIZONTAL);
		rl_roundListComposite.marginBottom = 0;
		roundListComposite.setLayout(rl_roundListComposite);
		
		
		addRoundElink = new ELink(roundComposite, SWT.RIGHT);
		addRoundElink.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				addRoundAction();
			}
		});
		addRoundElink.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		addRoundElink.setText(Messages.Button_startReBidPrice);
		addRoundElink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/copy.png"));
		menuFunctionUtils.addFunction(addRoundElink, "ADD_REBIDPRICE");
		
		endRoundElink = new ELink(roundComposite, SWT.RIGHT);
		endRoundElink.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				endRoundAction();
			}
		});
		endRoundElink.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		endRoundElink.setText(Messages.Button_endCurRound);
		endRoundElink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/copy.png"));
		menuFunctionUtils.addFunction(endRoundElink, "ALLOW_END_ROUND");
		
		tips = new Label(roundComposite, SWT.RIGHT);
		tips.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		tips.setText("");
		
		exportFilesBtn = new ELink(roundComposite, SWT.NONE);
		exportFilesBtn.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		exportFilesBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				exportReBidPriceFilesAction();
			}
		});
		exportFilesBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/export.png"));
		exportFilesBtn.setText("一键导出多轮报价文件");
		menuFunctionUtils.addFunction(exportFilesBtn, "EXPORT_ROUND_FILES");
		
		viewResultElink = new ELink(roundComposite, SWT.RIGHT);
		viewResultElink.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				viewResultAction();
			}
		});
		viewResultElink.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, true, 1, 1));
		viewResultElink.setText(Messages.Button_viewResult);
		viewResultElink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/view.png"));
		menuFunctionUtils.addFunction(viewResultElink, "VIEW_RESULT");
		
		
		ESepator subLine = new ESepator(roundComposite, SWT.NONE);
		subLine.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 5, 1));
		subLine.setBackground(ColorConstants.SKIN_BG);
		subLine.setLineWidth(2);
		
		showComposite = new Composite(this, SWT.NONE);
		GridLayout gl_showComposite = new GridLayout(1, false);
		gl_showComposite.marginHeight = 0;
		gl_showComposite.marginWidth = 0;
		gl_showComposite.verticalSpacing = 1;
		gl_showComposite.horizontalSpacing = 1;
		showComposite.setLayout(gl_showComposite);
		GridData gd_showComposite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_showComposite.heightHint = 50;
		showComposite.setLayoutData(gd_showComposite);
		
		showDetailComposite = new MultiBidPriceResultDetailComposite(this, SWT.NONE);
		GridData gd_showDetailComposite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_showDetailComposite.heightHint = 50;
		showDetailComposite.setLayoutData(gd_showDetailComposite);
		
		savecomposite = new Composite(this, SWT.NONE);
		savecomposite.setBackgroundMode(SWT.INHERIT_FORCE);
		savecomposite.setLayout(new GridLayout(1, false));
		savecomposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 3, 1));

		finishButton = new BigButton(savecomposite, SWT.NONE);
		finishButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
		finishButton.setText(Messages.Button_submitEvaluationResult);
		finishButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				completeMultiRoundAction();
			}
		});
 	
		init();
	}

	/**
	 * @Description: 初始化
	 * @author wangxiang
	 */
	private void init() {
		bidPriceHisResultService = new BidPriceHisResultService();
		expertEvaluationStepService = new ExpertEvaluationStepService();
		expertService = new ExpertService();
		reBidPriceRoundService = new ReBidPriceRoundService();
		bidPriceResultService = new BidPriceResultService();
		bidPriceRoundStatusService = new BidPriceRoundStatusService();
		supplierEvaluationStepService = new SupplierEvaluationStepService();
		tenderProjectEntity = projectAction.getTenderProject();
		tenderProjectRuleEntity = projectAction.getTenderProjectRule();
		evaluationStep = projectAction.getEvaluationStep();
		this.CURRENT_EXPERT = expertService.getEvaluationStepExpert(tenderProjectEntity.getId(), evaluationStep);
		
		SUPPLIER_NAME = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProjectEntity.getTenderType());
		this.result = ObjectUtils.getObjBoolean(this.getParent().getData("result"));
		if(null != CURRENT_EXPERT && !Constants.USER.getId().equals(CURRENT_EXPERT.getUserId())){
			this.result = true;
		}
		allowMaxRound = evaluationStep.getEvaluationParamByKey(ConfigConstants.MAX_ROUND, Integer.class,3);
		if(allowMaxRound > 0){
			tips.setText(MessageFormat.format(Messages.Tips_exceedMaxBidPriceRound, allowMaxRound));
		}
		finishButton.setText(MessageFormat.format(Messages.CompleteFormat, evaluationStep.getStepName()));
		compoleteLisener = new ReBidPriceCompoleteListener(){
			public void complete() {
				refreshBidPriceRound();
			}

			public void hasComplete() {
				endRoundElink.setEnabled(true);
			}
		};
		selectSupplierListener = new SelectSupplierListener(){
			public void selectSupplier(Supplier supplier) {
				if(null != supplier && null != curbidPriceRound){
					WidgetUtils.setControlVisible(showDetailComposite, true);
					showDetailComposite.refresh(tenderProjectEntity, supplier, maxRound, curbidPriceRound, true);
				}
				layout();
			}
			
		};
		endRoundElink.setEnabled(false);
		Menu menu = new MenuService().getMenutByMenuCode(MenuEnum.ROUND_BID_PIRCE);
		menuFunctionUtils.initFunctionAuthority(menu);
		
		MenuFunction.SHOW.equals(menuFunctionUtils.getAuthority("USE_OLD_ROUND"));
		
		refreshSuppliers();
		refreshBidPriceRound();
			
		if(null != CURRENT_EXPERT && Constants.USER.getId().equals(CURRENT_EXPERT.getUserId()) && !CommonEnum.COMPLETE.equals(evaluationStep.getStepStatus())){
			if(CommonEnum.COMPLETE.equals(evaluationStep.getStepStatus()) || result){
				addRoundElink.setEnabled(false);
			}else{
				// 修改状态为正在进行中
				ActionLogUtils.addServerLog(tenderProjectEntity.getId(), "进入" + evaluationStep.getStepName(), Constants.USER.getUserName());
				expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.IN_PROGRESS, tenderProjectEntity.getId(), evaluationStep, CURRENT_EXPERT.getUserId());
			}
		}else {
			WidgetUtils.setControlVisible(savecomposite, false);
			WidgetUtils.setControlVisible(viewResultElink, false);
		}
		
	}
	private void refreshSuppliers() {
		//获得步骤中的最低值
		minScore = evaluationStep.getEvaluationParamByKey(com.gx.obe.components.core.enumeration.EvaluationStepEnum.EVALUATION_MIN_SCORE, BigDecimal.class);
		List<Supplier> tempSupplierList = supplierService.getEvaluationStepSupplierList(tenderProjectEntity.getId(), evaluationStep);
		if(null == tempSupplierList){
			tempSupplierList = new ArrayList<Supplier>();
		}
		if(null == supplierList){
			supplierList =  new ArrayList<Supplier>();
		}
		if(null != minScore && minScore.compareTo(BigDecimal.ZERO) > 0){
			//查询供应商所有步骤的得分总和
			for(int i = 0; i < tempSupplierList.size(); i++ ){
				BigDecimal score = BigDecimal.ZERO;
				Supplier supplier = supplierList.get(i);
				List<SupplierEvaluationStep> supplierEvaluationStepList = supplierEvaluationStepService.getSupplierEvaluationStepByTenderIdAndSupplierId(tenderProjectEntity.getId(), supplier.getId());
				if(null != supplierEvaluationStepList && supplierEvaluationStepList.size() > 0){
					for (SupplierEvaluationStep supplierEvaluationStep : supplierEvaluationStepList) {
						score = BigDecimalUtils.add(score, supplierEvaluationStep.getEvaluationScore());
					}
				}
				if(score.compareTo(minScore) >= 0){
					supplierList.add(supplier);
				}
			}
		}else {
			supplierList = tempSupplierList;
		}
	}
	private ImageButton createRoundButton(final BidPriceRound bidPriceRound){
		final ImageButton roundButton = new RectangleRadioButton(roundListComposite, SWT.NONE);
		if(bidPriceRound.getRoundNo() == 1) {
			roundButton.setText(Messages.OriginalBidPrice);
		}else {
			roundButton.setText(ObjectUtils.getObjString(bidPriceRound.getRoundName()));
		}
		roundButton.setData("bidPriceRound", bidPriceRound);
		roundButton.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		roundButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				if(null != curRoundButton && !curRoundButton.isDisposed()){
					curRoundButton.setSelection(false);
				}
				curRoundButton = roundButton;
				curRoundButton.setSelection(true);
				loadRoundBidPriceResult();
			}
		});
		return roundButton;
	}
	
	private void refreshBidPriceRound() {
		addRoundElink.setEnabled(false);
		finishButton.setEnabled(false);
		endRoundElink.setEnabled(false);
		WidgetUtils.setControlVisible(savecomposite, false);
		
		for(Control control : roundListComposite.getChildren()){
			control.dispose();
		}
		List<BidPriceRound> bidPriceRoundList = reBidPriceRoundService.getBidPriceRoundList(tenderProjectEntity.getId());
		if(null == bidPriceRoundList || bidPriceRoundList.size() == 0){
			BidPriceRound lastBidPriceRound = new BidPriceRound();
			lastBidPriceRound.setRoundName(Messages.OriginalBidPrice);
			lastBidPriceRound.setRoundNo(0);
			bidPriceRoundList.add(lastBidPriceRound);
		}
		if(null != bidPriceRoundList && bidPriceRoundList.size() > 0){
			maxRound = bidPriceRoundList.size();
//			maxRound =  reBidPriceRoundService.getMaxRound(tenderProjectEntity.getId());
			for(BidPriceRound bidPriceRound : bidPriceRoundList){
				curRoundButton = createRoundButton(bidPriceRound);
			}
			curbidPriceRound = (BidPriceRound)curRoundButton.getData("bidPriceRound");
			if(null != CURRENT_EXPERT && Constants.USER.getId().equals(CURRENT_EXPERT.getUserId()) && valComplateBidPrice(curbidPriceRound.getId())){
				addRoundElink.setEnabled(true);
				finishButton.setEnabled(true);
				WidgetUtils.setControlVisible(savecomposite, true);
				savecomposite.layout();
			}
		}
		curRoundButton.notifyListeners(SWT.MouseUp, null);
		roundListComposite.layout();
		roundComposite.layout();
		this.layout();
	}
	
	/**
	 * @Description: 验证是否全部完成报价
	 * @author guoyr
	 * @return 
	 */
	private boolean valComplateBidPrice(String roundId){
		if(null == roundId) {
			return true;
		}
		return bidPriceRoundStatusService.isCompletePrice(roundId);
	}
	/**
	 * 加载轮次报价
	 * @param roundBidPriceButton 
	 */
	private void loadRoundBidPriceResult() {
		if(null == curRoundButton || curRoundButton.isDisposed())return;
		curbidPriceRound = (BidPriceRound)curRoundButton.getData("bidPriceRound");
		int round = curbidPriceRound.getRoundNo();
//		if(curbidPriceRound.getRoundNo() == maxRound){
//			round = 0;
//		}
		WidgetUtils.setControlVisible(viewResultElink, false);
		//只有当最后一轮报价没完成时进入报价进行中界面
		if((curbidPriceRound.getRoundNo() == maxRound) && !valComplateBidPrice(curbidPriceRound.getId())){
			WidgetUtils.setControlVisible(showDetailComposite, false);
			//新报价界面
			for(Control control : showComposite.getChildren()){
				control.dispose();
			}
			showComposite.setData("result",result);
			RemoteMultiBidPriceInProgressComposite reBidPriceComposite  = new RemoteMultiBidPriceInProgressComposite(showComposite, SWT.NONE);
			reBidPriceComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
			reBidPriceComposite.addCompletelisener(compoleteLisener);
			reBidPriceComposite.refresh(tenderProjectEntity, tenderProjectRuleEntity,supplierList, curbidPriceRound, CURRENT_EXPERT.getUserId());	
			if(!MenuFunction.SHOW.equals(menuFunctionUtils.getAuthority((String)viewResultElink.getData(Menu.FUNCTION)))){
				WidgetUtils.setControlVisible(viewResultElink, true);
			}
		}else{
			WidgetUtils.setControlVisible(showDetailComposite, true);
			if(null == bidPriceResutlTableComposite || bidPriceResutlTableComposite.isDisposed()){
				for(Control control : showComposite.getChildren()){
					control.dispose();
				}
				bidPriceResutlTableComposite  = new BidPriceResutlTableComposite(showComposite, SWT.NONE);
				bidPriceResutlTableComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
				bidPriceResutlTableComposite.addSelectSupplierListener(selectSupplierListener);
			}
			bidPriceResutlTableComposite.refreshBidPriceResultTable(tenderProjectEntity, supplierList, round,curbidPriceRound);	
		}
		roundComposite.layout();
		showComposite.layout();
		showComposite.getParent().layout();
		layout();
	}

	/**
	 * @Description: 添加新一轮报价
	 * @author wangxiang 
	 */
	private void addRoundAction() {
		if(!addRoundElink.isEnabled()){
			return;
		}
		if(!TenderProjectEnum.REMOTE.equals(tenderProjectEntity.getRemoteStatus())){
			MessageDialog.openInformation(getShell(), Messages.Tips_localTenderReBidPrice);
			return;
		}
		maxRound = reBidPriceRoundService.getMaxRound(tenderProjectEntity.getId());
		if(maxRound + 1 > allowMaxRound){
			MessageDialog.openInformation(getShell(), MessageFormat.format(Messages.Tips_exceedMaxBidPriceRound, allowMaxRound));
			return;
		}
		
		//申请添加
		ApplyReBidPriceDialog reBidPriceEvaluationStepDialog = new ApplyReBidPriceDialog(getShell(), tenderProjectEntity,tenderProjectRuleEntity, maxRound + 1, menuFunctionUtils, evaluationStep ,new CallBack() {
			public void callBack(boolean bool,final Object object) {
				if(bool){
					Display.getDefault().syncExec(new Runnable() {
						public void run() {
							if(!roundListComposite.isDisposed()){
								refreshBidPriceRound();
							}
						}
					});
				}
			}
		});
		reBidPriceEvaluationStepDialog.setTitle(addRoundElink.getText());
		reBidPriceEvaluationStepDialog.open();
	}
	
	/**
	 * @Description: 提前结束当前伦次报价
	 * @author wangxiang 
	 */
	private void endRoundAction(){
		if(!endRoundElink.isEnabled()) return;
		int secendMaxRound = reBidPriceRoundService.getMaxRound(tenderProjectEntity.getId()) - 1;
		if(secendMaxRound < 0){
			MessageDialog.openInformation(getShell(), "没有进行二次报价！");
			return;
		}
		
		EndRoundPriceConfirmDialog reEvaluationConfirmDialog = new EndRoundPriceConfirmDialog(getShell(), curbidPriceRound);
		reEvaluationConfirmDialog.open();
		if(!reEvaluationConfirmDialog.isConfirm()){
			return;
		}
		
		curbidPriceRound.setRoundOpinion(reEvaluationConfirmDialog.getOpinion());
		
		boolean allowUseOldBidPrice = MenuFunction.SHOW.equals(menuFunctionUtils.getAuthority("USE_OLD_ROUND"));
				
		List<BidPriceResult> bidPriceResultList = bidPriceResultService.getBidPriceResultList(tenderProjectEntity.getId(), null);
		if(null != bidPriceResultList && bidPriceResultList.size() > 0){
			if(MessageDialog.openConfirm(getShell(), "确认采用上一轮报价", "还有供应商未完成当前轮次报价，是否对该供应商采用上一轮报价？", new String[] { Messages.Button_confirm, "不采用" }) && allowUseOldBidPrice){
				bidPriceResultService.updateSupplierIdNotRecordedBidPrice(tenderProjectEntity.getId(),curbidPriceRound.getId());
			}else {
				bidPriceRoundStatusService.updateCompletePriceStatus(curbidPriceRound.getId(), BidPriceEnum.PRICE_STATUS_OK);
				curbidPriceRound.setRoundStatus(com.gx.obe.components.core.enumeration.EvaluationStepEnum.COMPOLETE);
				reBidPriceRoundService.completeCurrentRound(curbidPriceRound);
			}
		}
		
		WidgetUtils.setControlVisible(savecomposite, false);
		compoleteLisener.complete();
	}
	
	private void viewResultAction(){
		if(viewResultElink.getVisible() && viewResultElink.getEnabled() && null != curbidPriceRound){
			ViewMultiRoundPriceDialog viewMultiRoundPriceDialog = new ViewMultiRoundPriceDialog(getShell(), tenderProjectEntity, supplierList, curbidPriceRound.getRoundNo(),curbidPriceRound);
			viewMultiRoundPriceDialog.setTitle(curbidPriceRound.getRoundName());
			viewMultiRoundPriceDialog.open();
		}
	}

	
	/**
	 * @Description: 完成多轮报价步骤
	 * @author wangxiang
	 */
	private void completeMultiRoundAction() {
		int secendMaxRound = reBidPriceRoundService.getMaxRound(tenderProjectEntity.getId()) - 1;
		if(secendMaxRound < 0){
			MessageDialog.openInformation(getShell(), "没有进行二次报价！");
			return;
		}
		if (null != finishButton && !finishButton.isDisposed() && finishButton.isEnabled()) {
			// 检查是否存在当前评标步骤之前未完成的步骤
			boolean isFreeProgress = ObjectUtils.equals(tenderProjectRuleEntity.getOrderReview(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.ORDER_SELF_FREE);
			if (isFreeProgress) {
				List<EvaluationStep> evaluationStepList = evaluationStepService.getNotComplateStepList(tenderProjectEntity.getId(), evaluationStep.getSortNo());
				if (null != evaluationStepList && evaluationStepList.size() > 0) {
					MessageDialog.openWarning(getShell(), Messages.Dialog_warmTips, MessageFormat.format(Messages.Tips_saveAllComplate, evaluationStepList.get(0).getStepName()));
					return ;
				}
			}
			String message = MessageFormat.format(Messages.Tips_submitEvaluationResult, evaluationStep.getStepName());
			if (!MessageDialog.openConfirm(getShell(), "确认是否完成", message, new String[] {Messages.Button_confirmComplate, Messages.Button_continueEvaluation})) {
				return;
			}
			
			boolean allowUseOldBidPrice = MenuFunction.SHOW.equals(menuFunctionUtils.getAuthority("USE_OLD_ROUND"));
			
			//查询有哪些供应商未完成报价 查询供应商报价为null
			List<BidPriceResult> bidPriceResultList = bidPriceResultService.getTenderIdAndBidPrice(tenderProjectEntity.getId(), null);
			if(null != bidPriceResultList && bidPriceResultList.size() > 0){
				if(MessageDialog.openConfirm(getShell(), "确认采用上一轮报价", "还有供应商未完成当前轮次报价，是否对该供应商采用上一轮报价？", new String[] { Messages.Button_confirm, "不采用" }) && allowUseOldBidPrice){
					for(BidPriceResult bidPriceResult : bidPriceResultList){
						BidPriceHisResult bidPriceHisResult = bidPriceHisResultService.getBidPriceHisResultByTenderId(tenderProjectEntity.getId(), bidPriceResult.getSupplierId(), secendMaxRound);
						bidPriceResult.setBidPrice(bidPriceHisResult.getBidPrice());
					}
					bidPriceResultService.batchSaveOrUpdate(bidPriceResultList, new String[]{"id","bidPrice"});
				}
			}
			
			boolean isover = false;
			boolean leaderOver = false;
			if(evaluationStep.getRoleType().equals(com.gx.obe.components.core.enumeration.EvaluationStepEnum.OPERATOR)){
				isover = expertEvaluationStepService.submitMultiPriceResult(tenderProjectEntity.getId(), CURRENT_EXPERT.getUserId(), evaluationStep);
			}else if(evaluationStep.getRoleType().equals(com.gx.obe.components.core.enumeration.EvaluationStepEnum.EXPERT_LEADER)){
				leaderOver = true;
				isover =expertEvaluationStepService.submitMultiPriceResult(tenderProjectEntity.getId(), CURRENT_EXPERT.getUserId(), evaluationStep);
				expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.COMPLETE, tenderProjectEntity.getId(), evaluationStep, CURRENT_EXPERT.getUserId());
			}
			if (isover) {
				evaluationStep.setStepStatus(CommonEnum.COMPLETE);
				evaluationStepService.updateAssignProperty(evaluationStep, new String[]{"stepStatus"});
				savecomposite.setVisible(false);
				((GridData)savecomposite.getLayoutData()).exclude = true;
				addRoundElink.setEnabled(false);
				if(leaderOver){
					goBackAction();
				}
			} else {
				MessageDialog.openInformation(getShell(), "结果保存失败！");
			}
		}
	}
	
	/**
	 * @Description:一键导出多轮报价文件
	 * @author wangxiang
	 */
	private void exportReBidPriceFilesAction(){
		final String filesPath = FileConstants.getReBidPriceFilePath(tenderProjectEntity.getId());
		final File file = new File(filesPath);
		if (!file.exists() || file.listFiles().length <= 0 ) {
			MessageDialog.openError(getShell(), "当前没有多轮报价文件！");
			return;
		}
		final String selectDir = FileDialogUtils.getDirectorySavePath("请选择导出目录", "[" + tenderProjectEntity.getTenderNo() + "]" + tenderProjectEntity.getTenderName());
		if (!StringUtils.isNotEmpty(selectDir)) {
			return;
		}
		
		final MessageViewDialog dialog = new MessageViewDialog(getShell(), SWT.NONE);
		
		//创建供应商Map
		final Map<String,String>  supplierMap = new HashMap<String,String>();
		if(null != supplierList && supplierList.size() > 0){
			for(Supplier supplier : supplierList){
				supplierMap.put(supplier.getOrgCode(), supplier.getSupplierName());
			}
		}
		
		new Thread(new Runnable() {
			public void run() {
				final String saveDir = selectDir + FileUtils.filterFileName("[" + tenderProjectEntity.getTenderNo() + "]") + StringUtils.filenameFilter(tenderProjectEntity.getTenderName());
				dialog.showMessage("正在导出，请稍后。。。。。。");
				String[] filelist = file.list();
				String roundfileName = null;
				String supplierFileName = null;
				if (null != filelist && filelist.length > 0) {
					for (int i = 0; i < filelist.length; i++) {
						//轮次文件夹
						File roundfile = new File(filesPath.concat(File.separator) + filelist[i]);
						if(roundfile.isDirectory()){
							String[] supplierFileList = roundfile.list();
							roundfileName = roundfile.getName();
							String roundfileNametemp[] = roundfileName.split("\\\\");
							roundfileName=roundfileNametemp[roundfileNametemp.length-1];
							if (null != supplierFileList && supplierFileList.length > 0) {
								for(int j = 0; j < supplierFileList.length; j++){
									//供应商文件夹
									File supplierfile = new File(roundfile.getAbsolutePath().concat(File.separator) + supplierFileList[j]);
									if(supplierfile.isDirectory()){
										String[] reBidPriceFfileList = supplierfile.list();
										supplierFileName = supplierfile.getName();
										String supplierNametemp[] = supplierFileName.split("\\\\");
										supplierFileName = supplierNametemp[supplierNametemp.length-1];
										supplierFileName = supplierMap.get(supplierFileName);
										if (null != reBidPriceFfileList && reBidPriceFfileList.length > 0) {
											//解析index文件
											File indexFile = new File(supplierfile.getAbsolutePath().concat(File.separator).concat(FileConstants.BID_FILE_INDEX_XML));
											Map<String,String> fileMap = parsingReBidPriceXml(indexFile);
											for(int k = 0; k < reBidPriceFfileList.length; k++){
												//多轮报价文件
												File reBidPriceFile = new File(supplierfile.getAbsolutePath().concat(File.separator) + reBidPriceFfileList[k]);
												String fileName = reBidPriceFile.getName();
												if(FileConstants.BID_FILE_INDEX_XML.equals(fileName) || FileConstants.BID_PRICE_XML.equals(fileName)){
													continue;
												}
												String temp[] = fileName.split("\\\\");
												fileName = temp[temp.length-1];
												fileName = fileName.substring(0,fileName.lastIndexOf("."));//去后缀
												fileName = fileMap.get(fileName);
												if(null != fileName && FileUtils.copy(reBidPriceFile.getAbsolutePath(), saveDir.concat(File.separator).concat(roundfileName).concat(File.separator).concat(supplierFileName).concat(File.separator).concat(fileName))){
													dialog.showMessage(fileName+"导出完成");
												}
											}
										}
									}
								}
							}
						}
					}
				}
				
				File file = new File(saveDir);
				if (file.exists()) {
					Display.getDefault().syncExec(new Runnable() {
						public void run() {
							dialog.showMessage("导出完毕！");
							dialog.close();
							if (MessageDialog.openConfirm(getShell(), "导出成功，是否打开目录？")) {
								RuntimeUtils.openDirector(saveDir);
							}
						}
					});
				} else {
					Display.getDefault().syncExec(new Runnable() {
						public void run() {
							MessageDialog.openError(getShell(), "没有可以导出的多轮报价文件！");
							dialog.close();
						}
					});
				}
			}
		}).start();
		dialog.open();
	}
	
	/**
	 * 加载报价明细附件
	 */
	private Map<String,String>  parsingReBidPriceXml(File xmlFile){
		Map<String,String> fileMap = new HashMap<String,String>();
		if (xmlFile.exists() && xmlFile.isFile()) {
			Document document = XmlUtils.getXmlDocument(xmlFile.getAbsolutePath());
			if (null != document) {
				Element rootElement = document.getRootElement();
				List<?> objectList = rootElement.elements("fileItem");
				if (null != objectList && objectList.size() > 0) {
					for (Object object : objectList) {
						Element fileItem = (Element) object;
						String fileId = fileItem.elementTextTrim("fileId");
						String fileName = fileItem.elementTextTrim("fileName");
						String endStr = fileName.substring(fileName.lastIndexOf(".") + 1);
						fileMap.put(fileId, fileName);
					}
				}
			}
		}
		return fileMap;
	}

	private void goBackAction() {
		if (null != expertEvaluationListener) {
			expertEvaluationListener.complete();
		}
	}
	
	public void addExpertEvaluationListener(ExpertEvaluationListener expertEvaluationListener) {
		this.expertEvaluationListener = expertEvaluationListener;
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
