package cn.hmsoft.skill.enrol.control;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.exception.web.LoginTimeoutException;
import cn.hmsoft.frame.exception.BusinessException;
import cn.hmsoft.frame.util.FrameAssertUtil;
import cn.hmsoft.frame.util.FrameDictUtil;
import cn.hmsoft.frame.util.FrameRandomCodeUtil;
import cn.hmsoft.helper.excel.Excel2007Writer;
import cn.hmsoft.helper.pdf.ItextPdfHelper;
import cn.hmsoft.helper.pdf.PdfService;
import cn.hmsoft.skill.constants.SkillLogType;
import cn.hmsoft.skill.constants.SkillLoginError;
import cn.hmsoft.skill.constants.StdMateialType;
import cn.hmsoft.skill.data.dao.school.SchoolAspectDao;
import cn.hmsoft.skill.data.dao.school.SchoolLogDao;
import cn.hmsoft.skill.data.dao.std.StdDataDao;
import cn.hmsoft.skill.data.dao.std.StdEnrolSubjectDao;
import cn.hmsoft.skill.data.dao.std.StdRegDao;
import cn.hmsoft.skill.data.model.school.SchoolReg;
import cn.hmsoft.skill.data.model.std.StdData;
import cn.hmsoft.skill.data.model.std.StdSchool;
import cn.hmsoft.skill.enrol.service.EnrolStdEnrolService;
import cn.hmsoft.skill.helper.SkillMateialHelper;
import cn.hmsoft.skill.helper.SkillParamHelper;
import cn.hmsoft.skill.school.service.SchoolArrangeService;

@RestController
public class EnrolStdSchoolControl extends AjaxControl {
	@Autowired
	private StdRegDao daoStd;
	@Autowired
	private SchoolAspectDao daoAspect;
	@Autowired
	private SchoolLogDao daoLog;
	@Autowired
	private EnrolStdEnrolService serverEnrol;
	@Autowired
	private PdfService pdfService;
	@Autowired
	private StdDataDao stdDataDao;
	
	protected StdSchool getStdSchool() {
		StdSchool s = null;
		try {
			s = (StdSchool) getSession().getAttribute(SkillParamHelper.SessionName_StdSchool);
		} catch (Exception e) {
			throw new LoginTimeoutException();
		}
		if (s == null)
			throw new LoginTimeoutException();
		return s;
	}
	
	public SchoolReg getSchoolReg() {
		try {
			return (SchoolReg) this.getSession().getAttribute(SkillParamHelper.SessionName_SchoolReg);
		} catch (Exception e) {
			e.printStackTrace();
			throw new LoginTimeoutException();
		}
	}
	

	/**
	 * 
	 * */
	@RequestMapping("/enrol/school/login")
	public Ajax schoollogin(String school_name, String school_pwd, Integer school_id, String valid_code) {
		FrameAssertUtil.checkParam(school_name, school_pwd, school_id);
		Ajax ajax = new Ajax();
		ajax.setSuccess(false);
		
		// 判断是否开启验登录验证
		if (SkillParamHelper.StdLoginWithVaildCode && !SkillParamHelper.AppDebug) {
			// 为了后续注册，这里不要清除验证码信息
			boolean flag = FrameRandomCodeUtil.validRandomCode(this.getRequest(), valid_code, false);
			if (!flag) {
				ajax.setErrorCode(SkillLoginError.CodeNotVaild);
				return ajax;
			}
		}
		
		StdSchool school = daoStd.findBySql(StdSchool.class,"select * from std_school where std_school=? and school_reg_id=?",school_name,school_id);
		
		FrameAssertUtil.isNotNull(school, "请确认学校名称！");
		if (!school.getStd_school_pwd().equals(school_pwd))
			throw new BusinessException("请确认密码！");
	
		school.setStd_school_pwd(null);
		this.getSession().setAttribute(SkillParamHelper.SessionName_StdSchool, school);
		
		SchoolReg schoolreg = daoStd.find(SchoolReg.class,school_id);
		this.getSession().setAttribute(SkillParamHelper.SessionName_SchoolReg, schoolreg);
		return new Ajax();
	}
	
	@RequestMapping("/enrol/school/info/init")
	public Ajax init() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("StdSchool", getStdSchool());
		return new Ajax(map);
	}
	
	/***
	 * 考生准考证下载
	 * @throws Exception
	 */
	@RequestMapping("/enrol/school/ticket/down")
	public void schoolTicket() throws Exception {

		StdSchool stdSchool = getStdSchool();
		SchoolReg schoolReg = getSchoolReg();

		File dest = SkillMateialHelper.makeStdImageFile(StdMateialType.StdTicket,
				"T" + schoolReg.getSchool_code() + stdSchool.getStd_school_code() + ".pdf");

		if (dest.exists())
			dest.delete();
		
		serverEnrol.stdSchoolMergePdf(stdSchool.getStd_school_code(), getSchoolReg().getSchool_id());
			
		this.downloadFile(dest, SkillParamHelper.CurrentYear + schoolReg.getSchool_name() + stdSchool.getStd_school() + "-考生准考证.pdf");

	}

	/**
	 * 登出
	 */
	@RequestMapping("/enrol/school/logout")
	public void logout() {
		this.getRequest().getSession().invalidate();
	}
	
	@RequestMapping("/enrol/school/student/download")
	public void downloadStudent() {
		SchoolReg school = getSchoolReg();
		StdSchool stdSchool = getStdSchool();
		List<Map<String, Object>> list = this.daoAspect.listAspectSchoolStudent(school.getSchool_id(),stdSchool.getStd_school_code());
		Map<String, Integer> vMap = Excel2007Writer.createTitleMap("#", 10);
		vMap.put("报考专业", 15);
		vMap.put("毕业学校", 30);
		vMap.put("考生姓名", 10);
		vMap.put("准考证号", 10);
		vMap.put("考生号", 14);
		if(school.getSchool_id()!=8190) {
		vMap.put("身份证号", 18);
		vMap.put("性别", 6);
		vMap.put("联系电话1", 12);
		vMap.put("联系电话2", 12);
		vMap.put("通讯地址", 40);
		}
		vMap.put("报考时间", 20);
		if(school.getSchool_id()!=8190) {
		vMap.put("缴费渠道", 10);
		}
		vMap.put("缴费时间", 20);

		List<List<Object>> values = new ArrayList<List<Object>>();

		int index = 1;
		for (Map<String, Object> map : list) {
			List<Object> array = new ArrayList<Object>();
			array.add(++index);
			array.add(map.get("aspect_name"));
			array.add(map.get("std_school"));
			array.add(map.get("std_name"));
			array.add(map.get("ticket_num"));
			array.add(map.get("exam_id"));
			if(school.getSchool_id()!=8190) {
			array.add(map.get("cert_id"));
			array.add(map.get("std_sex"));
			array.add(map.get("std_mobile1"));
			array.add(map.get("std_mobile2"));
			array.add(map.get("link_addr"));
			}
			array.add(map.get("enrol_time") == null ? null : map.get("enrol_time"));
			if(school.getSchool_id()!=8190) {
			array.add(map.get("channel_id") == null
					? (map.get("pay_status") == null ? "" : (map.get("bill_id") == null ? "未缴费" : "未缴费"))
					: (FrameDictUtil.getFrameDictText("StdPayChannel", map.get("channel_id").toString())));
			}
			array.add(map.get("pay_time") == null ? null : map.get("pay_time"));

			values.add(array);

		}

		this.daoLog.log(school.getSchool_id(), SkillLogType.DownladStd.toString(), values.size() + "");
		Excel2007Writer.downloadAsExcel(this.getResponse(), school.getSchool_name() + "-考生报考缴费数据明细", values,
				vMap);
	}
	
	@RequestMapping("/enrol/school/student/studentdata")
	public void getStudent() throws Exception {
		String aspect_name ="作曲-器乐曲写作";
		 List<StdData> lstStdData = this.stdDataDao.listStd(aspect_name);
		for(int i=0;i<=lstStdData.size();i++) {
			String cert_id =lstStdData.get(i).getCert_id() ;
			String path = "E:\\0405---南艺答卷\\答卷\\作曲-器乐曲写作\\"+cert_id;
			File std = new File(path);
			int page_width =595 ;
			int page_height = 842;
//			PdfService pdf = new PdfService();
			this.pdfService.makePdf(path, ItextPdfHelper
					.getPageSizeByMillimeter(page_width, page_height));
			File pdfFolder = new File("E:\\0405---南艺答卷\\pdf\\作曲-器乐曲写作-答卷");
			this.pdfService.makePdf(std, pdfFolder, ItextPdfHelper
					.getPageSizeByMillimeter(page_width, page_height));
			
		}
		
	}
}
