package cn.hmsoft.skill.helper;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.log.LogHelper;
import cn.hmsoft.skill.data.model.school.SchoolReg;
import cn.hmsoft.skill.data.model.std.StdReg;

/*****************************
 * 获取考生信息类
 * 
 * @author zxq
 * @create 2018-10-30 09:08:26
 * @version 5.0.0
 * @email: revisit@126.com
 * @Company: www.hmsoft.cn
 */
public abstract class StdFetchHelper {

	private static StdFetchHelper fetch;
	protected static Map<Integer, Map<String, String>> paramMap = null;

	public static StdReg fetch(SchoolReg school, StdReg std) {
		if (fetch == null) {
			synchronized (StdFetchHelper.class) {
				if (fetch == null) {
					String class_name = "cn.hmsoft.skill.helper.fetchor.Fetchor" + SkillParamHelper.GobalProvinceId;
					try {
						fetch = (StdFetchHelper) Class.forName(class_name).newInstance();
					} catch (Exception e) {
						LogHelper.error(e);
					}
					if (fetch == null) {
						return null;
					}
					paramMap = new HashMap<Integer, Map<String, String>>();
				}

			}
		}
		

		if (!paramMap.containsKey(school.getSchool_id())) {
			synchronized (StdFetchHelper.class) {
				if (!paramMap.containsKey(school.getSchool_id())) {
					paramMap.put(school.getSchool_id(), StringHelper.toMap(school.getSocket_param()));
				}
			}
		}
		return fetch.fetchStd(school, std);
	}

	protected abstract StdReg fetchStd(SchoolReg school, StdReg std);

	/************************
	 * 获取参数
	 * 
	 * @param param_name
	 * @param default_value
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected <T> T getParamValue(Integer school_id, String param_name, T default_value) {
		if (!paramMap.containsKey(school_id))
			return default_value;
		if (!paramMap.get(school_id).containsKey(param_name))
			return default_value;
		try {
			return (T) paramMap.get(school_id).get(param_name);
		} catch (Exception e) {
			return default_value;
		}
	}

	/********************************
	 * 调用WebService
	 */
	protected String callWebService(String url, String xmlns, String requestBody) {
		try {
			URL wsUrl = new URL(url);
			HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection();
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
			OutputStream os = conn.getOutputStream();

			// 请求体//xmlns:hbea=\"http://hbea.edu.cn/
			String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" "
					+ (StringHelper.isEmpty(xmlns) ? "" : xmlns.trim()) + "><soapenv:Header/><soapenv:Body>"
					+ (StringHelper.isEmpty(requestBody) ? "" : requestBody.trim())
					+ "</soapenv:Body></soapenv:Envelope>";

			os.write(soap.getBytes());
			os.flush();

			InputStream is = conn.getInputStream();

			byte[] b = new byte[1024];
			int len = 0;
			String s = "";
			while ((len = is.read(b)) != -1) {
				String ss = new String(b, 0, len, "UTF-8");
				s += ss;
			}

			is.close();
			os.close();
			conn.disconnect();
			return s;
		} catch (Exception e) {
			// e.printStackTrace();
			return null;
		}
	}
}
