package cn.hmsoft.skill.control.std;

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.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.exception.BusinessException;
import cn.hmsoft.frame.thread.FrameThreadHelper;
import cn.hmsoft.frame.util.FrameIdCardUtil;
import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.helper.excel.ExcelReaderHelper;
import cn.hmsoft.skill.data.dao.std.StdLogDao;
import cn.hmsoft.skill.data.dao.std.StdLoginDao;
import cn.hmsoft.skill.data.dao.std.StdMaterialDao;
import cn.hmsoft.skill.data.dao.std.StdRegDao;
import cn.hmsoft.skill.data.model.std.StdReg;
import cn.hmsoft.skill.service.std.StdRegService;

@RestController
public class StdRegControl extends FrameControl {

	@Autowired
	private StdRegDao stdRegDao;
	@Autowired
	private StdLogDao stdLogDao;//业务日志
	@Autowired
	private StdLoginDao stdLoginDao;//登录日志
	@Autowired
	private StdRegService stdRegService;
	@Autowired
	private StdMaterialDao daoMaterial;
	
	@RequestMapping("std/reg/page")
	public Ajax page(String query, Integer limit, Integer start, String order, String type) {
		if (StringHelper.isEmpty(order))
			order = "reg_time";
		return new Ajax(this.stdRegDao.pageStdReg(this.getFrameOptr().getOrg_id(), start, limit, 
				query, getQueryOrder(order, type)));
	}

	@RequestMapping("std/reg/delete")
	public Ajax delete(int std_id) {
		this.stdRegService.delete(std_id, this.getFrameOptr());
		return new Ajax();
	}

	@RequestMapping("std/reg/save")
	public Ajax save(StdReg std) {
		this.stdRegService.save(std, this.getFrameOptr());
		return new Ajax();
	}

	@RequestMapping("std/reg/info")
	public Ajax find(int std_id) {
		StdReg std = this.stdRegDao.find(std_id);
		Map<String, Object> map = new HashMap<String, Object>();
		if (std != null) {
			map.put("StdReg", std);
			map.put("MaterialArray", this.daoMaterial.listAllMaterial(std.getStd_id()));
		}
		return new Ajax(map);
	}
	
	@RequestMapping("std/reg/login/page")
	public Ajax pageLogin(int std_id, Integer limit, Integer start) {
		return new Ajax(this.stdLoginDao.pageLogin(std_id, start, limit));
	}

	@RequestMapping("std/reg/log/page")
	public Ajax pageLog(int std_id, Integer limit, Integer start) {
		return new Ajax(this.stdLogDao.pageLog(std_id, start, limit));
	}

	@CrossOrigin
	@RequestMapping("std/reg/batchReg")
	public Ajax batchReg(MultipartFile file) throws Exception {
		final FrameOptr optr = this.getFrameOptr();
		List<List<String>> values = ExcelReaderHelper.readSheet(file.getOriginalFilename(), file.getInputStream(), 1, 10);
		if (values.size() > 20) {
			// 超过100条用 线程模式
			FrameThread thread = FrameThreadHelper.createThread("注册-批量注册", "一共有【" + values.size() + "】条考生注册信息需要处理",
					FrameThreadCallbackType.Swal, "一共导入了【" + values.size() + "】条注册信息", this.getFrameOptr());
			new Thread(() -> {
				this.stdRegService.batchReg(thread, values, optr);
			}).start();
			return new Ajax(thread);
		} else {
			this.stdRegService.batchReg(null, values, optr);
		}
		return new Ajax();
	}
	
	@RequestMapping("std/reg/create")
	public Ajax create(StdReg std) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (std.getCert_type().equals("IdCard") && !FrameIdCardUtil.validateIdCard18(std.getCert_id())) {
				throw new BusinessException("身份证验证失败!");
			}
			//判断密码是否为空，如果为空，则取身份证后六位。
			if (StringHelper.isEmpty(std.getStd_pass())) {
				std.setStd_pass(std.getCert_id().substring(std.getCert_id().length()-6, std.getCert_id().length()));
			}
			std.setFrom_school(this.getFrameOptr().getOrg_id());
			StdReg stdReg =	this.stdRegService.create(std);
			map.put("StdReg", stdReg);
		} catch(Exception e) {
			throw new BusinessException(e.getMessage());
		}
		return new Ajax(map);
	}
	

	/**
	 * 缴费
	 * @param std_id
	 * @return
	 */
	@RequestMapping("std/reg/pay")
	public Ajax stdPayment(Integer std_id) {
		this.stdRegService.stdPaymentUpdate(std_id);
		return new Ajax();
	}
	
	/**
	 * 重置密码
	 * @param std_id
	 * @return
	 */
	@RequestMapping("std/reg/password")
	public Ajax stdUpdatePassword(Integer std_id) {
		this.stdRegService.stdUpdatePassword(std_id);
		return new Ajax();
	}
}
