package com.gx.obe.management.thread;

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.business.enumeration.ExpertEnum;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.XmlUtils;
import com.gx.obe.web.entity.auth.AuthUser;
import com.gx.obe.web.entity.expert.Expert;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.expert.ExpertService;

/**
 * @Description: 导出专家线程
 * @author guoyr
 */
public class ExporExpertThread implements Runnable {
	
	private ExpertService expertService ;
	private Thread thread;
	private TenderProjectEntity tenderProjectEntity;
	private String saveFilePath;
	private CallBack callBack;
	
	public ExporExpertThread(TenderProjectEntity tenderProjectEntity, String saveFilePath, CallBack callBack){
		this.tenderProjectEntity = tenderProjectEntity;
		this.saveFilePath = saveFilePath;
		this.callBack = callBack;
		expertService = new ExpertService();
	}
	
	public void start(){
		thread = new Thread(this);
		thread.setName("导出专家线程");
		thread.start();
	}
	
	public void stop(){
		thread = null;
	}
	
	public void run() {
		List<Expert> expertList = expertService.getExpertList(tenderProjectEntity.getId(), ExpertEnum.ALL);
		if(null != expertList && expertList.size() > 0){
			// 创建Document对象
			Document expertDoc = DocumentHelper.createDocument();
			// 创建根节点
			Element expertElement = expertDoc.addElement("downWorkGmember");
			// 根节点添加属性
			expertElement.addAttribute("xmlns", "");
			for(Expert expert : expertList){
				createExpertItem(expertElement, expert);
			}
			// 创建xml文件
			if(XmlUtils.createXmlFile(expertDoc, saveFilePath, "UTF-8")){
				callBackMsg("√导出专家成功！");
			}else {
				callBackMsg("×导出专家失败！");
			}
		}else {
			callBackMsg("×未检测到评标专家！");
		}
		callBackMsg("导出专家结束！");
		if(null != callBack){
			callBack.after();
		}
	}
	
	/**
	 * @Description: 创建专家<memberInfo>标签
	 * @author guoyr
	 * @param expertElement
	 * @param expert
	 */
	private void createExpertItem(Element expertsElement, Expert expert){
		String userName = "";
		String professionalTitle = "";
		String speciality = "";
		String phone = "";
		String company = "";
		String idCard = "";
		AuthUser user = expert.getUser();
		if(null != user){
			userName = ObjectUtils.getObjString(user.getUserName());
			professionalTitle = ObjectUtils.getObjString(user.getProfessionalTitle());
			speciality = ObjectUtils.getObjString(user.getSpecialty());
			phone = ObjectUtils.getObjString(user.getPhone());
			company = ObjectUtils.getObjString(user.getCompany());
			idCard = ObjectUtils.getObjString(user.getUserIdcard());
		}
		callBackMsg("正在创建【"+userName+"】专家标签...");
		Element expertElement = expertsElement.addElement("memberInfo");
		expertElement.addElement("workgmId").setText("");
		expertElement.addElement("subProjCode").setText("");
		// 姓名
		expertElement.addElement("workgmName").setText(userName);
		// 职称
		expertElement.addElement("workgmDuty").setText(professionalTitle);
		// 专业
		expertElement.addElement("workgmSpeciality").setText(speciality);
		// 电话
		expertElement.addElement("workgmLinkerPhone").setText(phone);
		// 单位
		expertElement.addElement("workgmOrgName").setText(company);
		// 是否组长
		expertElement.addElement("isLeader").setText(ObjectUtils.getObjString(expert.getIsLeader()));
		// 评审成员类型
		expertElement.addElement("isAmount").setText(ObjectUtils.getObjString(expert.getExpertType()));
		// 身份证
		expertElement.addElement("idCard").setText(idCard);
		expertElement.addElement("tel").setText("");
	}
	
	private void callBackMsg(String msg){
		if(null != callBack){
			callBack.callBackString(msg);
		}
	}
}
