package cn.hmsoft.skill.helper.fetchor;

import java.io.File;
import java.lang.reflect.Field;
import java.time.LocalDateTime;
import cn.hmsoft.helper.JsonHelper;
import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.helper.http.HttpHelper;
import cn.hmsoft.skill.constants.StdMateialType;
import cn.hmsoft.skill.data.model.school.SchoolReg;
import cn.hmsoft.skill.data.model.std.StdReg;
import cn.hmsoft.skill.helper.SkillMateialHelper;
import cn.hmsoft.skill.helper.StdFetchHelper;

public class Fetchor42 extends StdFetchHelper {

	@Override
	protected StdReg fetchStd(SchoolReg school, StdReg std) {
		String url = this.getParamValue(school.getSchool_id(), "url", "");
		String socket_name = this.getParamValue(school.getSchool_id(), "socket_name", "GetJNGKbyJSON_sfzh");
		// 请求体
		String soap = "<hbea:"+socket_name+"><hbea:xxdm>" + this.getParamValue(school.getSchool_id(), "WebServiceName", "")
				+ "</hbea:xxdm><hbea:pass>" + this.getParamValue(school.getSchool_id(), "WebServicePass", "") + "</hbea:pass><hbea:sfzh>"
				+ std.getCert_id() + "</hbea:sfzh></hbea:"+socket_name+">";

		String returnValue = this.callWebService(url, "xmlns:hbea=\"http://hbea.edu.cn/\"", soap);
		if (returnValue == null)
			return null;
		// <?xml version="1.0" encoding="utf-8"?><soap:Envelope
		// xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
		// xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		// xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetKsInfobyJSON_sfzhResponse
		// xmlns="http://hbea.edu.cn/"><GetKsInfobyJSON_sfzhResult>{"stat":"00","ksh":"16420301170302","xm":"杜葵凡","xbdm":"2","xb":"女","sfzh":"420303199805241720","csrq":"19980524","tkkl":"4","zyfx":"4143","byxxdm":"420301006","byxxmc":"十堰市车城高级中学","txdz":"阳光栖谷3号楼.10楼.5号房","yzbm":"442000","lxdh":"","dhlxr":"","sjr":"杜葵凡","tkfs":"401:230.35;403:223.33;","tkbz":"401:b;403:b;","pic":"http://wsys.hbee.edu.cn:8092/pic/0301/16420301170302.jpeg","sfzpic":"http://wsys.hbee.edu.cn:8092/sfzpic/0301/0301_420303199805241720.jpg","zkzh":""}</GetKsInfobyJSON_sfzhResult></GetKsInfobyJSON_sfzhResponse></soap:Body></soap:Envelope>

		String jsonValue = returnValue.substring(
				returnValue.indexOf("<GetJNGKbyJSON_sfzhResult>") + "<GetJNGKbyJSON_sfzhResult>".length(),
				returnValue.indexOf("</GetJNGKbyJSON_sfzhResult>"));
		// System.out.println(jsonValue);
		Std42 wStd = JsonHelper.toObject(jsonValue.replace("wsys.hbee.edu.cn", "119.97.236.43"), Std42.class);
		if ("00".equals(wStd.getStat())) {
			// 清除所有空格：湖北院特色，必须
			wStd.trimAttr();
			// 找到考生
			// StdReg std = new StdReg();
			// 考生号
			std.setExam_id(wStd.getKsh());
			// 考生姓名
			std.setStd_name(wStd.getXm());
			// 性别
			std.setStd_sex(wStd.getXb());
			// 证件号码
			std.setCert_id(std.getCert_id().toUpperCase());
			
			// 技能高考
			if(StringHelper.isNotEmpty(wStd.getJnkl()))
				std.setStd_type(wStd.getJnkl());

			// 毕业学校代码
			std.setStd_param1(wStd.getByxxdm());

			// 毕业学校
			std.setStd_school(wStd.getByxxmc());

			// 家庭住址
			std.setLink_addr(wStd.getTxdz());


			// 联系电话
			if (std.getStd_mobile1() == null)
				std.setStd_mobile1(wStd.getLxdh());

			// 统考分数
			std.setStd_param2(wStd.getTkfs());

			// 统考备注
			std.setStd_param3(wStd.getTkbz());

			// 大头照照片地址
			std.setStd_param4(wStd.getPic());
			// 身份证照片地址
			std.setStd_param5(wStd.getSfzpic());
			
			

			File stdUpload = null;
			// 已经有照片信息了
			if (StringHelper.isNotEmpty(std.getStd_image()))
				stdUpload = SkillMateialHelper.makeStdImageFile(std.getStd_image(), StdMateialType.StdUpload);
			else
				stdUpload = SkillMateialHelper.makeStdImageFile(std.getCert_id(), StdMateialType.StdUpload, "jpeg");
			if (HttpHelper.download(wStd.getPic(), stdUpload)) {
				std.setStd_image(stdUpload.getName());
			}else {
				//下载不成功，尝试身份证照片
				// 身份证照片
				File cardPhoto = null;
				if (StringHelper.isNotEmpty(std.getStd_image()))
					cardPhoto = SkillMateialHelper.makeStdImageFile(std.getStd_image(), StdMateialType.StdUpload);
				else
					cardPhoto = SkillMateialHelper.makeStdImageFile(std.getCert_id(), StdMateialType.StdUpload, "jpg");
				if (HttpHelper.download(wStd.getSfzpic(), stdUpload)) {
					std.setStd_image(cardPhoto.getName());
				}
			}
			

			// 注册信息
			std.setReg_time(LocalDateTime.now());
			return std;
		} else if ("01".equals(wStd.getStat())) {
			// 未找到考生
		} else if ("03".equals(wStd.getStat())) {
			// 参数错误
		}

		return null;
	}

	class Std42 {
		/****************
		 * {"stat":"00","ksh":"16420301170302","xm":"杜葵凡","xbdm":"2","xb":"女","sfzh":"420303199805241720","csrq":"19980524",
		 * "tkkl":"4","zyfx":"4143","byxxdm":"420301006","byxxmc":"十堰市车城高级中学","txdz":"阳光栖谷3号楼.10楼.5号房","yzbm":"442000",
		 * "lxdh":"","dhlxr":"","sjr":"杜葵凡","tkfs":"401:230.35;403:223.33;","tkbz":"401:b;403:b;",
		 * "pic":"http://wsys.hbee.edu.cn:8092/pic/0301/16420301170302.jpeg","sfzpic":"http://wsys.hbee.edu.cn:8092/sfzpic/0301/0301_420303199805241720.jpg","zkzh":""}
		 */
		// 返回状态 ，00 表示找到考生
		private String stat;
		// 考生号
		private String ksh;
		// 考生姓名
		private String xm;
		// 性别代码 1，男，2 女
		private String xbdm;
		// 性别 男 女
		private String xb;
		// 身份证号
		private String sfzh;
		// 出生日期 19900101格式
		private String csrq;
		// 统考类型 1，是美术，2是音乐，3是舞蹈，4是戏剧影视编导 8 技能高考
		private String tkkl;
		// 专业方向 41-播音主持，42-广播电视编导，43-表演，44-服装表演
		private String zyfx;
		// 毕业学校代码
		private String byxxdm;
		// 毕业学校名称
		private String byxxmc;
		// 通讯地址
		private String txdz;
		// 邮政编码
		private String yzbm;
		// 联系电话
		private String lxdh;
		// 电话联系人
		private String dhlxr;
		// 收件人
		private String sjr;
		// 统考分数
		private String tkfs;
		// 统考备注
		private String tkbz;
		// 大头照照片地址
		private String pic;
		private String zkzh;
		// 身份证照片地址
		private String sfzpic;
		// 技能高考科目类型
		private String jnkl;

		public String getStat() {
			return stat;
		}

		public void setStat(String stat) {
			this.stat = stat;
		}

		public String getKsh() {
			return ksh;
		}

		public void setKsh(String ksh) {
			this.ksh = ksh;
		}

		public String getXm() {
			return xm;
		}

		public void setXm(String xm) {
			this.xm = xm;
		}

		public String getXbdm() {
			return xbdm;
		}

		public void setXbdm(String xbdm) {
			this.xbdm = xbdm;
		}

		public String getXb() {
			return xb;
		}

		public void setXb(String xb) {
			this.xb = xb;
		}

		public String getSfzh() {
			return sfzh;
		}

		public void setSfzh(String sfzh) {
			this.sfzh = sfzh;
		}

		public String getCsrq() {
			return csrq;
		}

		public void setCsrq(String csrq) {
			this.csrq = csrq;
		}

		public String getTkkl() {
			return tkkl;
		}

		public void setTkkl(String tkkl) {
			this.tkkl = tkkl;
		}

		public String getByxxdm() {
			return byxxdm;
		}

		public void setByxxdm(String byxxdm) {
			this.byxxdm = byxxdm;
		}

		public String getByxxmc() {
			return byxxmc;
		}

		public void setByxxmc(String byxxmc) {
			this.byxxmc = byxxmc;
		}

		public String getTxdz() {
			return txdz;
		}

		public void setTxdz(String txdz) {
			this.txdz = txdz;
		}

		public String getYzbm() {
			return yzbm;
		}

		public void setYzbm(String yzbm) {
			this.yzbm = yzbm;
		}

		public String getLxdh() {
			return lxdh;
		}

		public void setLxdh(String lxdh) {
			this.lxdh = lxdh;
		}

		public String getDhlxr() {
			return dhlxr;
		}

		public void setDhlxr(String dhlxr) {
			this.dhlxr = dhlxr;
		}

		public String getSjr() {
			return sjr;
		}

		public void setSjr(String sjr) {
			this.sjr = sjr;
		}

		public String getTkfs() {
			return tkfs;
		}

		public void setTkfs(String tkfs) {
			this.tkfs = tkfs;
		}

		public String getTkbz() {
			return tkbz;
		}

		public void setTkbz(String tkbz) {
			this.tkbz = tkbz;
		}

		public String getPic() {
			return pic;
		}

		public void setPic(String pic) {
			this.pic = pic;
		}

		public String getZkzh() {
			return zkzh;
		}

		public void setZkzh(String zkzh) {
			this.zkzh = zkzh;
		}

		public String getSfzpic() {
			return sfzpic;
		}

		public void setSfzpic(String sfzpic) {
			this.sfzpic = sfzpic;
		}

		public String getZyfx() {
			return zyfx;
		}

		public void setZyfx(String zyfx) {
			this.zyfx = zyfx;
		}
		

		public String getJnkl() {
			return jnkl;
		}

		public void setJnkl(String jnkl) {
			this.jnkl = jnkl;
		}

		public void trimAttr() {
			for (Field f : this.getClass().getDeclaredFields()) {
				try {
					f.setAccessible(true);
					Object value = f.get(this);
					if (value != null) {
						f.set(this, value.toString().trim());

					}
				} catch (Exception e) {

				}
			}
		}
	}

}
