package cn.hmsoft.skill.control.std;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import cn.hmsoft.application.web.Ajax;
import cn.hmsoft.frame.constants.FrameThreadCallbackType;
import cn.hmsoft.frame.control.FrameControl;
import cn.hmsoft.frame.data.model.FrameOptr;
import cn.hmsoft.frame.data.model.FrameThread;
import cn.hmsoft.frame.thread.FrameThreadHelper;
import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.helper.excel.ExcelReaderHelper;
import cn.hmsoft.skill.data.dao.school.AreaRecruitDao;
import cn.hmsoft.skill.data.dao.school.DepartmentMajorDao;
import cn.hmsoft.skill.data.dao.school.ProvinceBatchDao;
import cn.hmsoft.skill.data.dao.school.SchoolAreaDao;
import cn.hmsoft.skill.data.dao.school.SchoolRegDao;
import cn.hmsoft.skill.data.dao.std.StdAdmissionDao;
import cn.hmsoft.skill.data.dao.std.StdScoreSegmentDao;
import cn.hmsoft.skill.data.model.school.SchoolReg;
import cn.hmsoft.skill.school.service.SchoolAreaRecruitService;
import cn.hmsoft.skill.school.service.SchoolDepartmentMajorService;
import cn.hmsoft.skill.school.service.SchoolProvinceBatchService;
import cn.hmsoft.skill.service.std.StdAdmissionService;

@RestController
public class StdAdmissionControl extends FrameControl {

	@Autowired
	private StdAdmissionDao stdAdmissionDao;
	@Autowired
	private StdAdmissionService stdAdmissionService;
	@Autowired
	private SchoolAreaDao schoolAreaDao;
	@Autowired
	private DepartmentMajorDao departmentMajorDao;
	@Autowired
	private ProvinceBatchDao provinceBatchDao;
	@Autowired
	private SchoolProvinceBatchService schoolProvinceBatchService;
	@Autowired
	private SchoolDepartmentMajorService schoolDepartmentMajorService;
	@Autowired
	private StdScoreSegmentDao stdScoreSegmentDao;
	@Autowired
	private SchoolAreaRecruitService schoolAreaRecruitService;
	@Autowired
	private AreaRecruitDao areaRecruitDao;
	
	@Autowired
	private SchoolRegDao daoSchool;
	
	/**
	 * 查询录取数据
	 * @param region 省份
	 * @param nation 名族
	 * @param department 系(院)
	 * @param major 专业
	 * @param sex 性别
	 * @param filing_unit 投档单位名称
	 * @param graduation_type 毕业类别
	 * @param examination_type 考试类型
	 * @param batch 批次
	 * @param families 科类
	 * @param planning_nature 计划性质
	 * @param query
	 * @param limit
	 * @param start
	 * @param order
	 * @param type
	 * @return
	 */
	@RequestMapping("std/admission/page")
	public Ajax page(String region, String nation,String department,String major,String sex,String filing_unit,String graduation_type,String examination_type,String batch,String families,String planning_nature,String isReported,String query, Integer limit, Integer start, String order, String type) {
		if (StringHelper.isEmpty(order))
			order = "no";
		return new Ajax(this.stdAdmissionDao.pageStdAdmission(region,nation,department,major,sex,filing_unit,graduation_type,examination_type,batch,families,planning_nature,isReported,this.getFrameOptr().getOrg_id(), start, limit, 
				query, getQueryOrder(order, type)));
	}
	
	/**
	 *       查询各学校录取报到人数数据
	 * @param province 省份
	 * @param area 市州
	 * @param region 地区
	 * @param school_name 学校
	 * @param query
	 * @param limit
	 * @param start
	 * @param order
	 * @param type
	 * @return
	 */
	@RequestMapping("std/schoolAdmission/page")
	public Ajax page(String year,String province,String area,String region, String school_name,String query, Integer limit, Integer start, String order, String type) {
		if (StringHelper.isEmpty(order))
			order = "area";
			type = "DESC";
		return new Ajax(this.schoolAreaDao.pageSchoolAdmission(year,province,area,region,school_name,this.getFrameOptr().getOrg_id(), start, limit, 
				query, getQueryOrder(order, type)));
	}
	
	/**
	 *  查询分省分批次录取报到情况
	 * @param province 省份
	 * @param batch 批次
	 * @param query
	 * @param limit
	 * @param start
	 * @param order
	 * @param type
	 * @return
	 */
	@RequestMapping("std/provinceBatch/page")
	public Ajax page(String year,String province,String batch,String query, Integer limit, Integer start, String order, String type) {
		if (StringHelper.isEmpty(order))
			order = "province";
			type = "DESC";
		return new Ajax(this.provinceBatchDao.pageProvinceBatch(year,province,batch,this.getFrameOptr().getOrg_id(), start, limit, 
				query, getQueryOrder(order, type)));
	}

	
	
	/**
	 *       查询各学校录取报到人数数据
	 * @param department 院系
	 * @param major 专业
	 * @param query
	 * @param limit
	 * @param start
	 * @param order
	 * @param type
	 * @return
	 */
	@RequestMapping("std/departmentMajor/majorPage")
	public Ajax majorPage(String year,String department,String major,String query, Integer limit, Integer start, String order, String type) {
		if (StringHelper.isEmpty(order))
			order = "department";
			type = "DESC";
		return new Ajax(this.departmentMajorDao.pageDepartmentMajor(year,department,major,this.getFrameOptr().getOrg_id(), start, limit, 
				query, getQueryOrder(order, type)));
	}
	
	/**
	 *      查询分省分批次录取统计表
	 * @param province 省份
	 * @param batch 批次
	 * @param query
	 * @param limit
	 * @param start
	 * @param order
	 * @param type
	 * @return
	 */
	@RequestMapping("std/provinceBatchSex/page")
	public Ajax provinceBatchSex(String year,String province,String batch,String query, Integer limit, Integer start, String order, String type) {
		if (StringHelper.isEmpty(order))
			order = "province";
			type = "DESC";
		return new Ajax(this.provinceBatchDao.provinceBatchSex(year,province,batch,this.getFrameOptr().getOrg_id(), start, limit, 
				query, getQueryOrder(order, type)));
	}

	
	/**
	 *       查询生源分专业分科类生源质量情况统计表
	 * @param department 院系
	 * @param major 专业
	 * @param query
	 * @param limit
	 * @param start
	 * @param order
	 * @param type
	 * @return
	 */
	@RequestMapping("std/quality/qualityPage")
	public Ajax qualityPage(String year,String department,String major,String query, Integer limit, Integer start, String order, String type) {
//		if (StringHelper.isEmpty(order))
//			order = "department";
//			type = "ASC";
		return new Ajax(this.departmentMajorDao.pageDepartmentMajorQuality(year,department,major,this.getFrameOptr().getOrg_id(), start, limit, 
				query, getQueryOrder(order, type)));
	}
	
	/**
	 *  分省分批次分科类生源质量情况统计表
	 * @param province 省份
	 * @param batch 批次
	 * @param query
	 * @param limit
	 * @param start
	 * @param order
	 * @param type
	 * @return
	 */
	@RequestMapping("std/batchQuality/batchQualityPage")
	public Ajax batchQualityPage(String year,String province,String batch,String query, Integer limit, Integer start, String order, String type) {
//		if (StringHelper.isEmpty(order))
//			order = "province";
//			type = "DESC";
		return new Ajax(this.provinceBatchDao.pageProvinceBatchQuality(year,province,batch,this.getFrameOptr().getOrg_id(), start, limit, 
				query, getQueryOrder(order, type)));
	}
	
	/**
	 *  普高考生十分一段统计表
	 * @param province 省份
	 * @param query
	 * @param limit
	 * @param start
	 * @param order
	 * @param type
	 * @return
	 */
	@RequestMapping("std/segment/pageScoreSegment")
	public Ajax pageScoreSegment(String year,String province,String batch,String query, Integer limit, Integer start, String order, String type) {
		return new Ajax(this.stdScoreSegmentDao.pageScoreSegment(year,province,batch,this.getFrameOptr().getOrg_id(), start, limit, 
				query, getQueryOrder(order, type)));
	}
	
	
	/**
	 *       分片区生源建设人数统计表
	 * @param 
	 * @param area 市州
	 * @param query
	 * @param limit
	 * @param start
	 * @param order
	 * @param type
	 * @return
	 */
	@RequestMapping("std/recruit/findRecruitPage")
	public Ajax findRecruitPage(String year,String area,String query, Integer limit, Integer start, String order, String type) {
		if (StringHelper.isEmpty(order))
			order = "area";
			type = "DESC";
		return new Ajax(this.areaRecruitDao.pageAreaRecruit(year,area,this.getFrameOptr().getOrg_id(), start, limit, 
				query, getQueryOrder(order, type)));
	}
	
	/**
	 *      湖北省近三年分地区招生情况统计表
	 * @param query
	 * @param limit
	 * @param start
	 * @param order
	 * @param type
	 * @return
	 */
	@RequestMapping("std/three/pageThreeYear")
	public Ajax pageThreeYear(String query, Integer limit, Integer start, String order, String type) {
		return new Ajax(this.areaRecruitDao.pageThreeYear(this.getFrameOptr().getOrg_id(), start, limit, 
				query, getQueryOrder(order, type)));
	}
	
	
	/**
	 * 录取数据批量导入
	 * @param file
	 * @return
	 * @throws Exception
	 */
	@CrossOrigin
	@RequestMapping("std/admission/batchAdmission")
	public Ajax batchAdmission(MultipartFile file) throws Exception {
		final FrameOptr optr = this.getFrameOptr();
		List<List<String>> values = ExcelReaderHelper.readSheet(file.getOriginalFilename(), file.getInputStream(), 1,
				45);
		// 超过100条用 线程模式
		FrameThread thread = FrameThreadHelper.createThread("录取-批量导入", "一共有【" + values.size() + "】条考生录取信息需要处理",
				FrameThreadCallbackType.Swal, "一共导入了【" + values.size() + "】条注册信息", this.getFrameOptr());
		new Thread(() -> {
			this.stdAdmissionService.batchAdmission(thread, values, optr);
		}).start();
		return new Ajax(thread);
	}
	
	/**
	 * 导入省份批次计划数
	 * @param file
	 * @return
	 * @throws Exception
	 */
	@CrossOrigin
	@RequestMapping("std/batch/batchRegion")
	public Ajax batchRegion(MultipartFile file) throws Exception {
		final FrameOptr optr = this.getFrameOptr();
		List<List<String>> values = ExcelReaderHelper.readSheet(file.getOriginalFilename(), file.getInputStream(), 1,
				4);
		// 超过100条用 线程模式
		FrameThread thread = FrameThreadHelper.createThread("录取-批量导入", "一共有【" + values.size() + "】条考生录取信息需要处理",
				FrameThreadCallbackType.Swal, "一共导入了【" + values.size() + "】条注册信息", this.getFrameOptr());
		new Thread(() -> {
			this.schoolProvinceBatchService.batchRegion(thread, values, optr);
		}).start();
		return new Ajax(thread);
	}
	
	/**
	 * 导入院系专业计划数据
	 * @param file
	 * @return
	 * @throws Exception
	 */
	@CrossOrigin
	@RequestMapping("std/major/batchMajor")
	public Ajax batchMajor(MultipartFile file) throws Exception {
		final FrameOptr optr = this.getFrameOptr();
		List<List<String>> values = ExcelReaderHelper.readSheet(file.getOriginalFilename(), file.getInputStream(), 1,
				4);
		// 超过100条用 线程模式
		FrameThread thread = FrameThreadHelper.createThread("录取-批量导入", "一共有【" + values.size() + "】条考生录取信息需要处理",
				FrameThreadCallbackType.Swal, "一共导入了【" + values.size() + "】条注册信息", this.getFrameOptr());
		new Thread(() -> {
			this.schoolDepartmentMajorService.batchMajor(thread, values, optr);
		}).start();
		return new Ajax(thread);
	}
	
	/**
	 * 导入市州招生任务数据
	 * @param file
	 * @return
	 * @throws Exception
	 */
	@CrossOrigin
	@RequestMapping("std/recruit/batchRecruit")
	public Ajax batchRecruit(MultipartFile file) throws Exception {
		final FrameOptr optr = this.getFrameOptr();
		List<List<String>> values = ExcelReaderHelper.readSheet(file.getOriginalFilename(), file.getInputStream(), 1,
				4);
		// 超过100条用 线程模式
		FrameThread thread = FrameThreadHelper.createThread("录取-批量导入", "一共有【" + values.size() + "】条市州招生任务数据需要处理",
				FrameThreadCallbackType.Swal, "一共导入了【" + values.size() + "】条市州招生任务数据信息", this.getFrameOptr());
		new Thread(() -> {
			this.schoolAreaRecruitService.batchRecruit(thread, values, optr);
		}).start();
		return new Ajax(thread);
	}

}
