package com.gx.obe.management.thread;

import java.text.MessageFormat;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.eclipse.swt.core.CallBack;


import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.XmlUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.SupplierService;

/**
 * @Description: 导出投标人
 * @author guoyr
 */
public class ExportSupplierThread implements Runnable {
	
	private SupplierService supplierService = new SupplierService();
	private Thread thread;
	private TenderProjectEntity tenderProjectEntity;
	private String saveFilePath;
	private CallBack callBack;
	private String supplierName;
	
	
	/**有参构造函数
	 * @param tenderProjectEntity
	 * @param saveFilePath
	 * @param callBack
	 */
	public ExportSupplierThread(TenderProjectEntity tenderProjectEntity, String saveFilePath, CallBack callBack){
		this.tenderProjectEntity = tenderProjectEntity;
		this.saveFilePath = saveFilePath;
		this.callBack = callBack;
		supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProjectEntity.getTenderType());
	}
	
	/**
	 * @Description: 开启线程
	 * @author guoyr
	 */
	public void start(){
		thread = new Thread(this);
		thread.start();
	}
	
	/**
	 * @Description: 停止线程
	 * @author guoyr
	 */
	public void stop(){
		thread = null;
	}
	
	public void run() {
		List<Supplier> supplierList = supplierService.getSupplierList(tenderProjectEntity.getId());
		if(null != supplierList && supplierList.size() > 0){
			// 创建Document对象
			Document supplierDoc = DocumentHelper.createDocument();
			// 创建根节点
			Element suppliersElement = supplierDoc.addElement("supplierList");
			// 根节点添加属性
			suppliersElement.addAttribute("xmlns", "");
			// 遍历投标人
			for(Supplier supplier : supplierList){
				// 创建投标人标签
				createSupplierItem(suppliersElement, supplier);
			}
			
			// 创建xml文件
			if(XmlUtils.createXmlFile(supplierDoc, saveFilePath, "UTF-8")){
				callBackMsg("√"+Messages.Tips_exportSuccess);
			}else {
				callBackMsg("×"+Messages.Tips_exportFail);
			}
		}else {
			callBackMsg("×" + Messages.Tips_noExportableData);
		}
		callBackMsg(Messages.Tips_exportFinished);
		if(null != callBack){
			callBack.after();
		}
	}

	/**
	 * @Description: 组装要导出的供应商信息
	 * @author guoyr
	 * @param suppliersElement
	 * @param supplier 
	 */
	private void createSupplierItem(Element suppliersElement, Supplier supplier){
		String name = ObjectUtils.getObjString(supplier.getSupplierName());
		callBackMsg("正在导出["+name+"]...");
		Element supplierElement = suppliersElement.addElement("ecpTenderApplyRec");
		supplierElement.addElement("supplierName").setText(name);// 投标人名称
		supplierElement.addElement("orgCode").setText(ObjectUtils.getObjString(supplier.getOrgCode()));// 投标人编号
		supplierElement.addElement("linker").setText(ObjectUtils.getObjString(supplier.getLinker()));// 联系人
		supplierElement.addElement("idCard").setText(ObjectUtils.getObjString(supplier.getIdCard()));// 身份证号
		supplierElement.addElement("linkerTel").setText(ObjectUtils.getObjString(supplier.getLinkerTel()));// 联系电话
		supplierElement.addElement("address").setText(ObjectUtils.getObjString(supplier.getAddress()));// 联系地址
		supplierElement.addElement("zipCode").setText(ObjectUtils.getObjString(supplier.getZipcode()));// 邮编
		supplierElement.addElement("memo").setText(ObjectUtils.getObjString(supplier.getMemo()));// 备注
		callBackMsg("√"+MessageFormat.format(Messages.Tips_exportSuccessFormat, name));
	}
	
	private void callBackMsg(String msg){
		if(null != callBack){
			callBack.callBackString(msg);
		}
	}
}
