package com.gx.obe.management.thread;

import java.util.Map;

import org.eclipse.swt.core.CallBack;

import com.gx.obe.WebserviceConstants;
import com.gx.obe.business.enumeration.ExpertEnum;
import com.gx.obe.business.http.HttpResponse;
import com.gx.obe.business.http.HttpUrl;
import com.gx.obe.business.http.HttpUtils;
import com.gx.obe.config.ConfigConstants;
import com.gx.obe.config.utils.PropertiesUtils;
import com.gx.obe.util.utils.MD5Utils;
import com.gx.obe.util.utils.ObjectUtils;
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.utils.ActionLogUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @Description: 更新评标专家线程
 * @author wangxiang
 */
public class DownloadExpertRemoveHasButSelectThread extends Thread {

	private CallBack callBack;
	private TenderProjectEntity tenderProject;
	private Map<String, Expert> expertMap;
	public DownloadExpertRemoveHasButSelectThread(TenderProjectEntity tenderProject, String webserviceAddress, Map<String, Expert> expertMap, CallBack callBack) {
		this.callBack = callBack;
		this.tenderProject = tenderProject;
		this.expertMap = expertMap;
	}
	
	@Override
	public void run() {
		String method = PropertiesUtils.getWebserviceProperty(WebserviceConstants.DOWNLOAD_EXPERT_INFO, "tenderProjectInfoHS.do?getExpertList");
		HttpUrl httpUrl = HttpUtils.getProjectHttpUrl(tenderProject, method);
		httpUrl.setAttribute("PhaseType",  tenderProject.getPhaseType());
		callBack.callBackString("正在获取评标专家,请稍后...");
		boolean flag = resolveXml(tenderProject, tenderProject, HttpUtils.doPost(httpUrl));
		ActionLogUtils.addServerLog(tenderProject.getId(), "更新专家", httpUrl, flag);
		callBack.callBackString("评标专家获取结束！");
		
		if(null != callBack){
			callBack.after();
		}
	}
	/**
	 * @Description: 解析返回的xml
	 * @author guoyr
	 * @param xmlStr
	 * @return
	 */
	private boolean resolveXml(TenderProjectEntity tenderProject, TenderProjectEntity childProject, HttpResponse httpResponse){
		if(null == httpResponse || !httpResponse.getStatus()){
			if(null != callBack && null != httpResponse && null != httpResponse.getMsg()){
				callBack.callBack(false, "消息："+ httpResponse.getMsg());
			}
			return false;
		}
		
		JSONArray expertListArray = httpResponse.getDataJSONArray("expertList");
		if(null != expertListArray){
			Object[] expertJsonList =  expertListArray.toArray();
			
			if(null != expertJsonList && expertJsonList.length > 0){
				for(Object object : expertJsonList){
					JSONObject expertJson = (JSONObject)object;
					AuthUser user = new AuthUser();
					
					String account = (String)expertJson.get("account");
					if(null != expertMap.get(account)){
						continue;
					}
					
					user.setUserAccount(account);
					user.setSpecialty((String)expertJson.get("specialty"));
					user.setGender((String)expertJson.get("gender"));
					user.setUserName((String)expertJson.get("userName"));
					user.setUserIdcard((String)expertJson.get("idCard"));
					user.setPhone((String)expertJson.get("phone"));
					user.setProfessionalTitle((String)expertJson.get("professionalTitle"));
					if(null != expertJson.get("passWord") && !"".equals(expertJson.get("passWord"))){
						user.setUserPassword((String)expertJson.get("passWord"));
					}else{
						user.setUserPassword(MD5Utils.getMD5String(PropertiesUtils.getProperty(ConfigConstants.EXPERT_DEFAULT_PASSWORD, "1")));
					}
					
					user.setCompany((String)expertJson.get("company"));
					Expert expert = new Expert();
					expert.setUser(user);
					if(null != expertJson.get("isLeader") && "01".equals(expertJson.get("isLeader"))){
						expert.setIsLeader(ExpertEnum.LEADER_YES);
					}
					String type = ObjectUtils.getObjString(expertJson.get("type"));
					int busType = type.contains("01") ? ExpertEnum.EXPERT_BUS : 0;
					int tecType = type.contains("02") ? ExpertEnum.EXPERT_TEC : 0;
					int priceType = type.contains("03") ? ExpertEnum.EXPERT_PRICE : 0;
					expert.setEvalBidType(busType | tecType | priceType);
					
					callBack.callBack(true, expert);
				}
				return true;
			}
		}
		return false;
	}
}
