package com.gx.obe.management.thread;

import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.widgets.Display;

import com.gx.obe.common.file.enumeration.FileConstants;
import com.gx.obe.common.file.utils.ParsingXmlUtils;
import com.gx.obe.thread.CreateEvaluationStepThread;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.XmlUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.SupplierService;
import com.gx.obe.web.service.evaluation.EvaluationFactorService;

/**
 * @Description: 自动解析并导入招标或投标文件下的指标xml
 * @author guoyr
 */
public class EvaluationFactorXmlResolverThread implements Runnable {
	
	private SupplierService supplierService = new SupplierService();
	private EvaluationFactorService evaluationFactorService = new EvaluationFactorService();
	private Thread thread;
	private TenderProjectEntity tenderProjectEntity;
	private CallBack callBack;
	private Logger LOG = Logger.getLogger(EvaluationFactorXmlResolverThread.class);
	private int sorNo = 1;
	
	{
		LogUtils.logClass(EvaluationFactorXmlResolverThread.class);
	}
	
	/**
	 * 无参构造方法
	 */
	public EvaluationFactorXmlResolverThread(){}
	
	/** 有参构造方法
	 * @param tenderProjectEntity 项目信息
	 * @param callBack 回调函数
	 */
	public EvaluationFactorXmlResolverThread(TenderProjectEntity tenderProjectEntity, CallBack callBack){
		this.tenderProjectEntity = tenderProjectEntity;
		this.callBack = callBack;
	}
	
	/**
	 * @Description: 开启线程
	 * @author guoyr
	 */
	public void start(){
		thread = new Thread(this);
		thread.start();
	}
	
	/**
	 * @Description: 停止线程
	 * @author guoyr
	 */
	public void stop(){
		thread = null;
	}
	
	public void run() {
		if (null != callBack) {
			final boolean isImport = importLocalEvalFactor();
			Display.getDefault().asyncExec(new Runnable() {
				public void run() {
					// 导入本地评审指标
					callBack.callBackBoolean(isImport);
				}
			});
		}
	}
	
	/**
	 * @Description: 导入本地评审指标（招标文件或投标文件中的指标）
	 * @author guoyr
	 */
	private boolean importLocalEvalFactor(){
		callBack.callBackString("正在解析评审指标，请稍后......");
		boolean isImport = false;
		// 获得评审指标Document对象
		Document factorDoc = getZbOrTbFactorDoc();
		List<EvaluationFactor> factorList = null;
		final StringBuilder msg = new StringBuilder();
		try {
			factorList = ParsingXmlUtils.parsingEvaluationFactorXML(factorDoc, tenderProjectEntity);
		} catch (Exception e) {
			LOG.error("解析指标文件失败", e);
			msg.append("解析【" + tenderProjectEntity.getTenderName() +"】指标文件失败！");
			callBack.callBackString("解析指标文件失败！");
		}
		if (null != factorList) {
			if(factorList.size() > 0){
				// 批量保存
				if(evaluationFactorService.saveDownloadEvaluationFactor(tenderProjectEntity.getId(), factorList) > 0){
					isImport = true;
					LOG.debug(factorList.size() +"个评审指标导入成功！");
					msg.append(factorList.size()+"个评审指标导入成功！\r\n");
					callBack.callBackString(factorList.size() +"个评审指标导入成功！");
					
					callBack.callBackString("正在创建评标步骤！");
					CreateEvaluationStepThread createEvaluationStepThread = new CreateEvaluationStepThread(tenderProjectEntity, null, new CallBack(){
						public void callBack(final boolean flag, final Object message) {
							if(flag){
								callBack.callBackString("评标步骤创建完成！");
							}else {
								callBack.callBackString("评标步骤创建失败！" + (String)message);
							}
						}
					});
					createEvaluationStepThread.start();
					createEvaluationStepThread.join();
				}else {
					LOG.debug("评审指标导入失败！");
					msg.append("评审指标导入失败！\r\n");
					callBack.callBackString("评审指标导入失败！");
				}
			}else {
				LOG.debug("未检索到【" + tenderProjectEntity.getTenderName() +"】有可导入的评审指标！");
				msg.append("未检索到【" + tenderProjectEntity.getTenderName() +"】有可导入的评审指标！");
				callBack.callBackString("未检索到可导入的评审指标！");
			}
		}
//		Display.getDefault().syncExec(new Runnable(){
//			public void run() {
//				msg.append("评审指标读取结束。\r\n");
//				MessageDialog.openRightBottomMessage(msg.toString(), 5000);
//			}
//		});
		return isImport;
	}
	
	/**
	 * @Description: 获得评审指标Document对象
	 * @author guoyr
	 * @return
	 */
	private Document getZbOrTbFactorDoc(){
		// 获得招标文件的指标Document
		Document factorDoc = XmlUtils.getXmlDocument(FileConstants.getBiddingFilePath(tenderProjectEntity.getId()) + FileConstants.PSZB_XML);
		// 未找到招标文件的指标，则获得投标文件的指标。
		if(null == factorDoc){
			// 获得投标人集合
			List<Supplier> supplierList = supplierService.getSupplierOpenBidList(tenderProjectEntity.getId());
			if(null != supplierList && supplierList.size() > 0){
				// 遍历投标人信息
				for(Supplier supplier : supplierList){
					// 获得投标人投标文件下的指标路径
					String supplierFactorXmlPath = FileConstants.getBidFilePath(tenderProjectEntity.getId(), supplier.getId()) + FileConstants.PSZB_XML;
					// 获得投标文件的指标Document
					factorDoc = XmlUtils.getXmlDocument(supplierFactorXmlPath);
					if(null != factorDoc){
						break;
					}
				}
			}
		}
		return factorDoc;
	}
}
