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

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.listener.LoadingAdatper;
import org.eclipse.swt.core.listener.RoundBorderLineListener;
import org.eclipse.swt.core.listener.ScrollMessageListener;
import org.eclipse.swt.core.utils.WidgetUtils;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.ScrolledComposite;
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.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;

import com.gx.obe.ColorConstants;
import com.gx.obe.business.entity.BidPriceRound;
import com.gx.obe.common.widget.button.CountDownLabel;
import com.gx.obe.common.widget.composite.PageNumComposite;
import com.gx.obe.common.widget.composite.ReBidPriceCountComposite;
import com.gx.obe.common.widget.composite.ReBidPriceSupplierComposite;
import com.gx.obe.common.widget.listener.PageListener;
import com.gx.obe.common.widget.shell.ProgressMessageDialog;
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.evaluation.step.listener.ReBidPriceCompoleteListener;
import com.gx.obe.evaluation.step.listener.RemoteMultyRoundBidPriceListener;
import com.gx.obe.evaluation.step.thread.RefreshRemoteSupplierMultyBidPriceResutlThread;
import com.gx.obe.message.Messages;
import com.gx.obe.util.thread.CountTimeThread;
import com.gx.obe.util.utils.DateUtils;
import com.gx.obe.web.entity.Page;
import com.gx.obe.web.entity.Supplier;
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.ReBidPriceRoundService;
import com.gx.obe.web.service.SupplierService;
import com.gx.utils.NowTimeUtils;
import com.swtdesigner.SWTResourceManager;

public class RemoteMultiBidPriceInProgressComposite extends Composite {
	private Composite supplierComposite;
	private TenderProjectEntity tenderProjectEntity;
	private ReBidPriceRoundService reBidPriceRoundService;
	private BidPriceResultService bidPriceResultService;
	private BidPriceHisResultService bidPriceHisResultService;
	private Composite showComposite;
	private Composite savecomposite;
	private EButton finishButton;
	private Composite totalComposite;
	private List<Supplier> supplierList;
	private String SUPPLIER_NAME = "供应商";
//	private EButton sendMessageBtn;
	private Composite countComposite;
	private ReBidPriceCountComposite totalCountComposite;
	private ReBidPriceCountComposite bidPriceCountComposite;
	private Label endTimeLable;
	private Composite allSupplierCom;
// 	private ImageResolver imageResolver = new ImageResolver();
	private PageNumComposite pageComposite;
	protected int PAGE_NO = 1;
	protected boolean isEnd = false;
	protected RefreshRemoteSupplierMultyBidPriceResutlThread downLoadRebidPriceThread;
	private Composite composite_2;
	protected int PAGE_SIZE = 10;
	private static Date lastDate ;
	private Map<String, ReBidPriceSupplierComposite> reBidPriceSupplierCompositeMap;
	private int totalComplateCount = 0;
	private BidPriceRound bidPriceRound;
	private ReBidPriceCompoleteListener compoleteLisener;
	private Map<String, String> hasReBidPriceSupplierMap = new HashMap<String, String>();
	private Boolean result;
	private CountDownLabel countDownLabel;
	private ScrolledComposite scrolledComposite;
	private CountTimeThread countTimeThread;
	private Composite messageComposite;
	private ScrollMessageListener scrollMessageListener;
	private SupplierService supplierService = new SupplierService();
	private BidPriceRoundStatusService bidPriceRoundStatusService = new BidPriceRoundStatusService();


	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public RemoteMultiBidPriceInProgressComposite(Composite parent, int style) {
		super(parent, style);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		gridLayout.verticalSpacing = 0;
		setLayout(gridLayout);
		setBackgroundMode(SWT.INHERIT_FORCE);

		showComposite = new Composite(this, SWT.NONE);
		showComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_showComposite = new GridLayout(1, false);
		gl_showComposite.marginWidth = 0;
		gl_showComposite.horizontalSpacing = 0;
		gl_showComposite.verticalSpacing = 0;
		gl_showComposite.marginHeight = 0;
		showComposite.setLayout(gl_showComposite);
		
		totalComposite = new Composite(showComposite, SWT.NONE);
		GridLayout gl_totalComposite = new GridLayout(3, false);
		gl_totalComposite.verticalSpacing = 0;
		gl_totalComposite.marginHeight = 0;
		gl_totalComposite.marginWidth = 0;
		totalComposite.setLayout(gl_totalComposite);
		totalComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		supplierComposite = new Composite(totalComposite, SWT.NONE);
		supplierComposite.setBackground(ColorConstants.BORDER_LINE_COLOR);
		GridLayout gl_supplierComposite = new GridLayout(1, false);
		gl_supplierComposite.verticalSpacing = 0;
		gl_supplierComposite.horizontalSpacing = 0;
		gl_supplierComposite.marginHeight = 1;
		gl_supplierComposite.marginWidth = 1;
		supplierComposite.setLayout(gl_supplierComposite);
		supplierComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		messageComposite = new Composite(supplierComposite, SWT.NONE);
		messageComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_messageComposite = new GridLayout(1, false);
		gl_messageComposite.marginWidth = 0;
		gl_messageComposite.marginHeight = 0;
		messageComposite.setLayout(gl_messageComposite);
		GridData gd_messageComposite = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		gd_messageComposite.heightHint = 20;
		messageComposite.setLayoutData(gd_messageComposite);
		
		scrolledComposite = new ScrolledComposite(supplierComposite, SWT.V_SCROLL);
		scrolledComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		
		allSupplierCom = new Composite(scrolledComposite, SWT.NONE);
		GridLayout gl_allSupplierCom = new GridLayout(1, false);
		gl_allSupplierCom.marginTop = 5;
		gl_allSupplierCom.marginWidth = 8;
		gl_allSupplierCom.marginHeight = 0;
		allSupplierCom.setLayout(gl_allSupplierCom);
		scrolledComposite.setContent(allSupplierCom);
		scrolledComposite.setMinSize(allSupplierCom.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		
		pageComposite = new PageNumComposite(supplierComposite, SWT.NONE);
		pageComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		pageComposite.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		pageComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		ESepator fact_tab = new ESepator(totalComposite, SWT.NONE);
		fact_tab.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1));
		fact_tab.setLineWidth(2);
		fact_tab.setBackground(ColorConstants.SKIN_BG);
		
		countComposite = new Composite(totalComposite, SWT.NONE);
		GridLayout gl_countComposite = new GridLayout(1, false);
		gl_countComposite.marginWidth = 0;
		gl_countComposite.marginHeight = 0;
		gl_countComposite.horizontalSpacing = 0;
		countComposite.setLayout(gl_countComposite);
		GridData gd_countComposite = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1);
		gd_countComposite.widthHint = 250;
		countComposite.setLayoutData(gd_countComposite);
		
		composite_2 = new Composite(countComposite, SWT.NONE);
		composite_2.setBackgroundMode(SWT.INHERIT_FORCE);
		composite_2.setBackground(SWTResourceManager.getColor(242, 242, 242));
		composite_2.setBackgroundMode(SWT.INHERIT_FORCE);
//		composite_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_GRAY));
		composite_2.addPaintListener(new RoundBorderLineListener(ColorConstants.BORDER_LINE_COLOR));
		
		GridLayout gl_composite_2 = new GridLayout(1, false);
		gl_composite_2.verticalSpacing = 5;
		composite_2.setLayout(gl_composite_2);
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		
		Label deallineTitle = new Label(composite_2, SWT.NONE);
		deallineTitle.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
		deallineTitle.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		deallineTitle.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		deallineTitle.setText(Messages.Deadline);
		
		endTimeLable = new Label(composite_2, SWT.WRAP);
		endTimeLable.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		endTimeLable.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
		endTimeLable.setForeground(ColorConstants.FONT_BLACK_COLOR);
		
		Label countDownTitle = new Label(composite_2, SWT.NONE);
		countDownTitle.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
		countDownTitle.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		countDownTitle.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		countDownTitle.setText(Messages.CountdownOnBoard);
		
		countDownLabel = new CountDownLabel(composite_2, SWT.WRAP);
		countDownLabel.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		GridData gd_countDownLabel = new GridData(SWT.CENTER, SWT.FILL, false, false, 1, 1);
		gd_countDownLabel.widthHint = 200;
		gd_countDownLabel.heightHint = 40;
		countDownLabel.setLayoutData(gd_countDownLabel);
		countDownLabel.setForeground(ColorConstants.FONT_BLACK_COLOR);
		
		
		totalCountComposite = new ReBidPriceCountComposite(countComposite, SWT.NONE);
		GridData gd_totalCountComposite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_totalCountComposite.heightHint = 100;
		totalCountComposite.setLayoutData(gd_totalCountComposite);
		
		bidPriceCountComposite = new ReBidPriceCountComposite(countComposite, SWT.NONE);
		GridData gd_bidPriceCountComposite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_bidPriceCountComposite.heightHint = 100;
		bidPriceCountComposite.setLayoutData(gd_bidPriceCountComposite);
		

		savecomposite = new Composite(showComposite, SWT.NONE);
		savecomposite.setBackgroundMode(SWT.INHERIT_FORCE);
		savecomposite.setLayout(new GridLayout(2, false));
		savecomposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 3, 1));

		finishButton = new EButton(savecomposite, SWT.NONE | EButton.CSS_BLUE);
		GridData gd_finishButton = new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1);
		gd_finishButton.heightHint = 30;
		finishButton.setLayoutData(gd_finishButton);
		finishButton.setFont(SWTResourceManager.getFont("微软雅黑", 11, SWT.NORMAL));
		finishButton.setLeftMargin(10);
		finishButton.setRightMargin(10);
		finishButton.setText("");
		finishButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				saveReBidPriceRoundAction();
			}
		});
		
		init();
	}

	private void init() {
		bidPriceResultService = new BidPriceResultService();
		reBidPriceRoundService = new ReBidPriceRoundService();
		reBidPriceSupplierCompositeMap = new HashMap<String, ReBidPriceSupplierComposite>();
		totalCountComposite.addStatusName(Messages.Opening_reBidPriceTotal, Messages.Opening_counts);
		bidPriceCountComposite.addStatusName(Messages.Opening_reBidPriceComplate, Messages.Opening_counts);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent arg0) {
				close();
			}
		});
		messageComposite.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		messageComposite.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		scrollMessageListener = new ScrollMessageListener(messageComposite);
		scrollMessageListener.setScrollRight(true);
		messageComposite.addPaintListener(scrollMessageListener);
	}
	
	public void refresh(TenderProjectEntity tenderProjectEntity, TenderProjectRuleEntity tenderProjectRuleEntity,final List<Supplier> supplierList, BidPriceRound bidPriceRound,  String userId) {
		this.tenderProjectEntity = tenderProjectEntity;
		this.supplierList = supplierList;
		this.bidPriceRound = bidPriceRound;
		this.result = (Boolean) this.getParent().getData("result");
		
		SUPPLIER_NAME = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProjectEntity.getTenderType());
		if(bidPriceRound.getRoundNo() != 0 && bidPriceRound.getRoundNo() !=1 ) {
			List<Supplier> roundSupplierList = supplierService.getPrivceRoundSupplierList(bidPriceRound.getId());
			if(null != roundSupplierList && roundSupplierList.size() > 0) {
				this.supplierList = roundSupplierList;
			}
  		}
		lastDate = bidPriceRound.getRoundEndTime();
		if(null == lastDate){
			lastDate = NowTimeUtils.getNowDate();
		}
		endTimeLable.setText(DateUtils.getTimestampFormat(lastDate));
		startCountTimeThread(lastDate);
		finishButton.setEnabled(false);
		finishButton.setText(MessageFormat.format(Messages.CompleteFormat, bidPriceRound.getRoundName()));
		totalCountComposite.setCountValue(0, this.supplierList.size());
		if(Constants.USER.getId().equals(userId)){
			refreshRemoteSupplierMultyBidPriceResult(true, true);
		}else{
			WidgetUtils.setControlVisible(savecomposite, false);
			refreshRemoteSupplierMultyBidPriceResult(false, false);
		}
		refreshAction();
		showSupplierPage(PAGE_NO);
	}
	
	/**
	 * @Description: 刷新供应商远程报价
	 * @author guoyr 
	 */
	private void refreshRemoteSupplierMultyBidPriceResult(boolean download, boolean loop) {
		if(null != downLoadRebidPriceThread){
			downLoadRebidPriceThread.stop();
		}
		final Runnable refreshHasReBidPriceSupplierRunnable = new Runnable(){
			public void run(){
				if(!bidPriceCountComposite.isDisposed()){
					bidPriceCountComposite.setCountValue(0, totalComplateCount);
				}
			}
		};
		downLoadRebidPriceThread = new RefreshRemoteSupplierMultyBidPriceResutlThread(tenderProjectEntity, supplierList, download, loop, this.bidPriceRound,new RemoteMultyRoundBidPriceListener(){
			public void refresh(String supplierId, final boolean complateFlag, final int complateCount) {
				if(complateFlag){
					hasReBidPriceSupplierMap.put(supplierId, "1");
					if(complateCount > totalComplateCount){
						totalComplateCount = complateCount;
						Display.getDefault().syncExec(refreshHasReBidPriceSupplierRunnable);
					}
				}
				final ReBidPriceSupplierComposite reBidPriceSupplierComposite = reBidPriceSupplierCompositeMap.get(supplierId);
				if(null != reBidPriceSupplierComposite){
					Display.getDefault().syncExec(new  Runnable() {
						public void run() {
							if(!reBidPriceSupplierComposite.isDisposed()){
								reBidPriceSupplierComposite.setReBidPriceStatues(complateFlag);
							}
							if(complateCount > 0) {
								compoleteLisener.hasComplete();
							}
						}
					});
				}
			}

			public void showMessage(String message) {
				scrollMessageListener.showMessage(message);
			}

			public void complate() {
				Display.getDefault().syncExec(new Runnable(){
					public void run(){
						if(!finishButton.isDisposed()){
							finishButton.setEnabled(true);
							// 当前报价完成时自动完成
							bidPriceRound.setRoundStatus(com.gx.obe.components.core.enumeration.EvaluationStepEnum.COMPOLETE);
							if(reBidPriceRoundService.completeCurrentRound(bidPriceRound)){
//								if(reBidPriceRoundService.updateAssignProperty(bidPriceRound, new String[]{"id","roundStatus"})){
								MessageDialog.openInformation(getShell(), MessageFormat.format(Messages.Tips_complateRoundBidPrice, bidPriceRound.getRoundName()));
								WidgetUtils.setControlVisible(savecomposite, false);
								compoleteLisener.complete();
							}
						}
					}
				});
			}
		});
		downLoadRebidPriceThread.start();
//	belongCalendar()
		
		
		
	}
	
	/**
	 * @Description: 换页
	 * @author wx 
	 */
	private void refreshAction(){
		pageComposite.setVisible(false);
		((GridData)pageComposite.getLayoutData()).exclude = true;
		supplierComposite.layout();
		pageComposite.setMaxPageCount(10);
		pageComposite.setBorderColor(SWTResourceManager.getColor(204, 204, 204));
		pageComposite.setForeground(SWTResourceManager.getColor(204, 204, 204));
		pageComposite.setSelectionBgColor(SWTResourceManager.getColor(24, 112, 221));
		pageComposite.setSelectionForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		pageComposite.addPageListener(new PageListener(){
			public void changed(int pageNo, int pageSize) {
				showSupplierPage(pageNo);
			}
		});
	}
	
	/**
	 * @Description: 展示供应商
	 * @author wx 
	 */
	private void showSupplierPage(int curPage){
		this.PAGE_NO = curPage;
		new Thread(new Runnable(){
			public void run() {
				
				Display.getDefault().syncExec(new Runnable(){
					public void run(){
						if(!supplierComposite.isDisposed()){
							// 先计算每页能显示多条数据
							GridLayout gridLayout = ((GridLayout)allSupplierCom.getLayout());
							int supplierListHeight = scrolledComposite.getBounds().height - gridLayout.marginHeight * 2 - gridLayout.marginTop - gridLayout.marginBottom;
							PAGE_SIZE = supplierListHeight / (35 + gridLayout.verticalSpacing);
							if(null != supplierList && supplierList.size() > PAGE_SIZE && !pageComposite.getVisible()){
								supplierListHeight -= Math.max(pageComposite.getBounds().height, 31);
								PAGE_SIZE = supplierListHeight / (35 + gridLayout.verticalSpacing);
							}
						}
					}
				});
				
				final Page<Supplier> supplierPage = new Page<Supplier>();
				if(null != supplierList){
					List<Supplier> supplierPageList = new ArrayList<Supplier>();
					int totalCount = 0;
					int pageStart = (PAGE_NO-1) * PAGE_SIZE ;
					if(supplierList.size() < pageStart){
						pageStart = 0;
						PAGE_NO = 1;
					}
					for(Supplier supplier : supplierList){
						if(totalCount >= pageStart && totalCount < pageStart + PAGE_SIZE){
							supplierPageList.add(supplier);
						} 
						totalCount ++;
					}
					supplierPage.setRecords(supplierPageList);
					supplierPage.setCurrent(PAGE_NO);
					supplierPage.setSize(PAGE_SIZE);
					supplierPage.setTotal(totalCount);
				}
				reBidPriceSupplierCompositeMap.clear();
				Display.getDefault().syncExec(new Runnable(){
					public void run() {
						if(!supplierComposite.isDisposed()){
							for(Control control : allSupplierCom.getChildren()){
								control.dispose();
							}
							int totalHeight = 0;
							int width = allSupplierCom.getBounds().width;
							if(null != supplierPage && null != supplierPage.getRecords() && supplierPage.getSize() > 0){
								for(Supplier supplier : supplierPage.getRecords()){
									ReBidPriceSupplierComposite reBidPriceSupplierComposite = new ReBidPriceSupplierComposite(allSupplierCom,supplier,SWT.NONE);
									reBidPriceSupplierComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
									totalHeight += reBidPriceSupplierComposite.computeSize(width, SWT.DEFAULT).y;//supplierComposite.getRowHeight();
									if(hasReBidPriceSupplierMap.containsKey(supplier.getId())){
										reBidPriceSupplierComposite.setReBidPriceStatues(true);
									}
									reBidPriceSupplierCompositeMap.put(supplier.getId(), reBidPriceSupplierComposite);
								}
							}
							if(supplierPage.getPages() > 1){
								pageComposite.refreshPages(supplierPage);
								((GridData)pageComposite.getLayoutData()).exclude = false;
								pageComposite.setVisible(true);
								totalHeight += pageComposite.computeSize(width, SWT.DEFAULT).y;
							}else {
								((GridData)pageComposite.getLayoutData()).exclude = true;
								pageComposite.setVisible(false);
							}
							
							GridLayout gridLayout = ((GridLayout)allSupplierCom.getLayout());
							int supplierListHeight = scrolledComposite.getBounds().height - gridLayout.marginHeight * 2 - gridLayout.marginTop - gridLayout.marginBottom;
							
							int surplusHeight = supplierListHeight - supplierPage.getSize() * gridLayout.verticalSpacing - totalHeight;
							if(surplusHeight > 0){
								int perHeight = surplusHeight/supplierPage.getSize();
								for(Entry<String, ReBidPriceSupplierComposite> supplierRemoteListner : reBidPriceSupplierCompositeMap.entrySet()){
									ReBidPriceSupplierComposite remoteSupplier = supplierRemoteListner.getValue();
									if(null != remoteSupplier && !remoteSupplier.isDisposed()){
										int height = remoteSupplier.computeSize(width, SWT.DEFAULT).y + perHeight;
										((GridData)remoteSupplier.getLayoutData()).heightHint = height > 70 ? 70 : height;
									}
								}
							}
							
						}
						supplierComposite.layout();
						allSupplierCom.layout();
						scrolledComposite.setMinSize(allSupplierCom.computeSize(SWT.DEFAULT, SWT.DEFAULT));
					}
				});
			}
		}).start();
	}
	

	public void close() {
		if(null != downLoadRebidPriceThread){
			downLoadRebidPriceThread.stop();
			downLoadRebidPriceThread = null;
		}
		if(null != countTimeThread){
			countTimeThread.stop();
			countTimeThread = null;
		}
//		if(null != imageResolver){
//			imageResolver.disposite();
//		}
	}

	/**
	 * 判断当前时间是否在to之内
	 *
	 * @param to   结束日期
	 * @return
	 */
	public static boolean belongCalendar() {
		Calendar date = Calendar.getInstance();
		date.setTime(DateUtils.now());

		Calendar before = Calendar.getInstance();
		before.setTime(lastDate);

		if (date.before(before)) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * @Description: 完成轮次报价
	 * @author wangxiang
	 */
	private void saveReBidPriceRoundAction() {
		if(!finishButton.isEnabled()){
			return;
		}
		if (!MessageDialog.openConfirm(getShell(), Messages.Dialog_warmTips, MessageFormat.format(Messages.Tips_confirmOperate, finishButton.getText()), new String[] {Messages.Button_confirm, Messages.Button_cancel})) {
			return;
		}
		
		if(null != bidPriceRound){
			bidPriceRound.setRoundStatus(com.gx.obe.components.core.enumeration.EvaluationStepEnum.COMPOLETE);
//			if(reBidPriceRoundService.updateAssignProperty(bidPriceRound, new String[]{"id","roundStatus"})){
			if(reBidPriceRoundService.completeCurrentRound(bidPriceRound)){
				WidgetUtils.setControlVisible(savecomposite, false);
				compoleteLisener.complete();
			}
		}
	}
	
	/**
	 * @Description: 启动倒计时线程
	 * @author guoyr
	 */
	private void startCountTimeThread(final Date countDownTime){
		if(null != countTimeThread){
			countTimeThread.stop();
			countTimeThread = null;
		}
//		countDownLabel.setTimeLength(6);
		countTimeThread = new CountTimeThread(NowTimeUtils.getNowDate() , countDownTime, new CountTimeThread.CountTime(6) {
			public void countTime(String[] countDownTime) {
				countDownLabel.refreshCountDown(countDownTime);
			}
			public void finish(){
				final LoadingAdatper loadingAdatper = new LoadingAdatper() {
					public void finish(boolean flag) {
						if(!valComplateBidPrice(bidPriceRound.getId())){
							close();//停止远程获取报价信息
							final int size = bidPriceRoundStatusService.getNotCompletePriceNumber(bidPriceRound.getId());
							Display.getDefault().syncExec(new Runnable(){
								public void run(){
									//查询未报价的数量
									if(MessageDialog.openConfirm(getShell(), Messages.Dialog_warmTips, MessageFormat.format("报价截止时间已到，还有{0}家供应商未完成报价，请选择处理方式" ,size ) , new String[]{"沿用上一轮报价", "确认放弃报价"})) {
										bidPriceResultService.updateSupplierIdNotRecordedBidPrice(tenderProjectEntity.getId(),bidPriceRound.getId());
									}else {
										bidPriceRoundStatusService.updateCompletePriceStatus(bidPriceRound.getId(), BidPriceEnum.PRICE_STATUS_OK);
									}
									compoleteLisener.complete();
								}
							});
						}
					}
				};
				Display.getDefault().syncExec(new Runnable(){
					public void run(){
						ProgressMessageDialog messageProgress = new ProgressMessageDialog(getShell(), true, -1, loadingAdatper);
						messageProgress.setTitle("正在检测报价完整性。。。");
						new Thread(new Runnable() {
							public void run() {
								downLoadRebidPriceThread.stop();
								downLoadRebidPriceThread.join();
								if(!valComplateBidPrice(bidPriceRound.getId())){
									try {
										Thread.sleep(2000);
									} catch (InterruptedException e) {
									}
									refreshRemoteSupplierMultyBidPriceResult(true,false);
									downLoadRebidPriceThread.join();
								}
								messageProgress.finish(true);
							}
						}).start();
						messageProgress.open();						
					}
				});
				
				
				
			}
		});
		countTimeThread.start();
	}
	
	/**
	 * @Description: 验证是否全部完成报价
	 * @author guoyr
	 * @return 
	 */
	private boolean valComplateBidPrice(String roundId){
//		return bidPriceHisResultService.isBidPriceRoundComplated(tenderProjectEntity.getId(), 0);
		return bidPriceRoundStatusService.isCompletePrice(roundId);
	}
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public void addCompletelisener(ReBidPriceCompoleteListener compoleteLisener) {
		this.compoleteLisener = compoleteLisener;
	}
}
