package cn.hmsoft.skill.enrol.control;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.hmsoft.application.web.Ajax;
import cn.hmsoft.application.web.AjaxControl;
import cn.hmsoft.frame.exception.BusinessException;
import cn.hmsoft.frame.util.FrameAssertUtil;
import cn.hmsoft.frame.util.FrameIdCardUtil;
import cn.hmsoft.frame.util.FrameRandomCodeUtil;
import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.skill.constants.StdMateialType;
import cn.hmsoft.skill.data.dao.std.StdRegDao;
import cn.hmsoft.skill.data.model.school.SchoolReg;
import cn.hmsoft.skill.data.model.std.StdReg;
import cn.hmsoft.skill.enrol.service.EnrolStdRegService;
import cn.hmsoft.skill.helper.SkillParamHelper;
import cn.hmsoft.skill.helper.StdFetchHelper;

/************************
 * 考生获取信息，从考试院获取数据
 * 
 * @author zxq
 * @create 2018-10-30 08:23:19
 * @version 5.0.0
 * @email: revisit@126.com
 * @Company: www.hmsoft.cn
 */
@RestController
public class EnrolStdFetchControl extends AjaxControl {

	@Autowired
	private StdRegDao daoStd;
	@Autowired
	private EnrolStdRegService sStdReg;

	/***************************
	 * 考生注册
	 * 
	 * @param cert_id 证件号码
	 * @param exam_id 考生号
	 * @param valid_code 验证码
	 * @param refresh 是否强制刷新
	 * @return
	 */
	@RequestMapping("/enrol/std/fetch")
	public Ajax fetch(String cert_id, String exam_id, String valid_code, String std_name,int school_id, boolean refresh) {
		FrameAssertUtil.checkParam(cert_id, exam_id);
		if (!FrameIdCardUtil.validateIdCard18(cert_id)) {
			throw new BusinessException("身份证验证失败，请检查");
		}

		// 判断是否开启验登录验证
		if (SkillParamHelper.StdLoginWithVaildCode) {
			// 为了后续注册，这里不要清除验证码信息
			boolean flag = FrameRandomCodeUtil.validRandomCode(this.getRequest(), valid_code, false);
			if (!flag) {
				throw new BusinessException("验证失败，请重新进行操作验证！");
			}
		}

		// 获取考生类型 --18422802800131
		SchoolReg school = this.daoStd.find(SchoolReg.class,school_id);
		FrameAssertUtil.isNotNull(school, "未找到提交的学校信息!");
		// 首先找到是否存在考生
		StdReg std = this.daoStd.find("cert_id",cert_id);
		if (std != null) {
			if (!refresh)
				// 如果已经存在了，请不强制刷新，则提示错误信息
				throw new BusinessException("该证件号对应的考生信息系统已经存在，无需再次从考试院进行获取！");
		} else {
			std = new StdReg();
			std.setCert_id(cert_id);
			std.setStd_name(std_name);
			std.setExam_id(exam_id);
		}
		// 皮卡丘，go
		StdReg transStd = StdFetchHelper.fetch(school, std);
		if (transStd == null || transStd.getReg_time() == null) {
			throw new BusinessException("无法从考试院获取数据，请确定身份证和高考报名号稍后再试！");
		}
		if (StringHelper.isEmpty(transStd.getCert_type())) {
			transStd.setCert_type("IdCard");
		}
		if (StringHelper.isEmpty(transStd.getStd_province())) {
			transStd.setStd_province("湖北省");
		}

		// 获取到了
		transStd.setFrom_school(school.getSchool_id());
		this.sStdReg.stdFormRemote(school, transStd);
		if(transStd.getStd_image()!=null) {
			transStd.setStd_image(SkillParamHelper.MateialWebPath + "/" +StdMateialType.StdUpload +std.getStd_image());
		}
		return new Ajax(transStd);
	}

}
