package cn.hmsoft.skill.enrol.control;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
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.data.dao.FrameParamDao;
import cn.hmsoft.frame.data.model.FrameParam;
import cn.hmsoft.frame.exception.BusinessException;
import cn.hmsoft.frame.util.FrameIdCardUtil;
import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.skill.constants.StdMateialType;
import cn.hmsoft.skill.constants.StdMaterialType;
import cn.hmsoft.skill.data.dao.std.StdRegDao;
import cn.hmsoft.skill.data.model.std.StdMaterial;
import cn.hmsoft.skill.data.model.std.StdReg;
import cn.hmsoft.skill.helper.SkillMateialHelper;
import cn.hmsoft.skill.helper.SkillParamHelper;
import cn.hmsoft.skill.service.std.StdMaterialService;
import cn.hmsoft.skill.service.std.StdRegService;

@RestController
public class EnrolStdRegControl {

	@Autowired
	private StdRegService stdRegService;
	@Autowired
	private StdMaterialService sMaterial;
	@Autowired
	private StdRegDao stdRegDao;
	
	@Autowired
	private FrameParamDao frameParamDao;
	
	@RequestMapping("std/reg/register")
	public Ajax create(StdReg std) {
		Ajax ajax = new Ajax();
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (std.getCert_type().equals("IdCard") && !FrameIdCardUtil.validateIdCard18(std.getCert_id())) {
				//throw new BusinessException("身份证验证失败!");
				ajax.setErrorCode(-1);
				ajax.setErrorMsg("身份证验证失败!");
				return ajax;
			}
			//判断密码是否为空，如果为空，则取身份证后六位。
			if (StringHelper.isEmpty(std.getStd_pass())) {
				std.setStd_pass(std.getCert_id().substring(std.getCert_id().length()-6, std.getCert_id().length()));
			}
			StdReg stdReg = this.stdRegService.create(std);
			map.put("StdReg", stdReg);
		} catch(Exception e) {
			//throw new BusinessException(e.getMessage());
			ajax.setErrorCode(-1);
			ajax.setErrorMsg(e.getMessage());
			return ajax;
		}
		return new Ajax(map);
	}
	
	/**
	 * 身份证照片上传
	 * @param file
	 * @return
	 */
	@RequestMapping("std/reg/card/photo")
	public Ajax uploadIdCard(MultipartFile file,HttpServletRequest request) {
		String std_id = request.getParameter("sId");
		StdMaterial stdMaterial= new StdMaterial();
		Map<String, Object> map = new HashMap<String, Object>();
		StdReg stdReg = this.stdRegDao.find(std_id);
		String originalFilename = file.getOriginalFilename();
        int lastIndexOf = originalFilename.lastIndexOf(".");
        String fileType = originalFilename.substring(lastIndexOf + 1);
        //文件类型判断 jpg,png ,JPEG
        if (fileType.equals("jpg") || fileType.equals("png") || fileType.equals("jpeg") ) {
//        	String idCard =StdMaterialType.IdCard + "_" + stdReg.getCert_id()+"."+ fileType;
//        	FrameParam frameParam = frameParamDao.find(SkillParamHelper.LocalMateialPath);
        	//String path = frameParam.getParam_value()+ File.separator + StdMateialType.StdUpload + File.separator + idCard;
        	
        	File stdUpload = null;
			// 已经有照片信息了   std_param5存储身份证照片
			if (StringHelper.isNotEmpty(stdReg.getStd_param5())) {
				stdUpload = SkillMateialHelper.makeStdImageFile(stdReg.getStd_param5(), StdMateialType.StdUpload);
			}else {
				stdUpload = SkillMateialHelper.makeStdImageFile(stdReg.getCert_id(), StdMateialType.StdUpload ,StdMaterialType.IdCard, "jpeg");
			}
            //判断路径中文件是否存在
            if (!stdUpload.exists()) {
	            try {
	            	stdUpload.getParentFile().mkdirs();//构建文件夹
	            	stdUpload.createNewFile();//构建文件
	            	//保存路径
	                file.transferTo(stdUpload);
	                this.stdRegService.stdUpdateIdcard(stdReg.getStd_id(),stdUpload.getName());
	                
	                //照片信息添加到材料信息表
	                stdMaterial.setStd_id(stdReg.getStd_id());
	                stdMaterial.setMaterial_type(StdMaterialType.IdCard);
	                stdMaterial.setMaterial_file(stdUpload.getPath());
	                stdMaterial.setFile_type(fileType);
	                this.sMaterial.create(stdMaterial);
	                
	                map.put("stdMaterial", stdMaterial);
	                
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
            }
           
        } else {
        	throw new BusinessException("上传的居民身份证照片必须是jpg,png,jpeg格式!");
        }
       
        return new Ajax(map);
	}
	
	/**
	 * 免冠照片上传
	 * @param file
	 * @return
	 */
	@RequestMapping("std/reg/crown/photo")
	public Ajax uploadNoCrownPhoto(MultipartFile file,HttpServletRequest request) {
		String std_id = request.getParameter("stdId");
		StdMaterial stdMaterial= new StdMaterial();
		Map<String, Object> map = new HashMap<String, Object>();
		StdReg stdReg = this.stdRegDao.find(std_id);
//		String path=EnrolMaterialHelper.generateMaterialTempPath().getPath()+File.separator + StdMaterialType.StdUpload;
		String originalFilename = file.getOriginalFilename();
        int lastIndexOf = originalFilename.lastIndexOf(".");
        String fileType = originalFilename.substring(lastIndexOf + 1);
        //文件类型判断 jpg,png ,JPEG
        if (fileType.equals("jpg") || fileType.equals("png") || fileType.equals("jpeg") ) {
        //	String noCrownPhoto =StdMaterialType.NoCrownPhoto + "_" + stdReg.getCert_id()+"."+ fileType;
//        	FrameParam frameParam = frameParamDao.find(SkillParamHelper.LocalMateialPath);
        	File stdUpload = null;
			// 已经有照片信息了   Std_image存储大头免冠照片
			if (StringHelper.isNotEmpty(stdReg.getStd_image())) {
				stdUpload = SkillMateialHelper.makeStdImageFile(stdReg.getStd_image(), StdMateialType.StdUpload);
			}else {
				stdUpload = SkillMateialHelper.makeStdImageFile(stdReg.getCert_id(),StdMateialType.StdUpload , StdMaterialType.NoCrownPhoto, "jpeg");
			}
            //判断路径中文件是否存在
//            if (!stdUpload.exists()) {
	            try {
	            	stdUpload.getParentFile().mkdirs();//构建文件夹
	            	stdUpload.createNewFile();//构建文件
	            	//保存路径
	                file.transferTo(stdUpload);
	               
	                //照片信息添加到考生注册信息表
	                this.stdRegService.stdUpdateNoCrownPhoto(stdReg.getStd_id(), stdUpload.getName());
	                //照片信息添加到材料信息表
	                stdMaterial.setStd_id(stdReg.getStd_id());
	                stdMaterial.setMaterial_type(StdMaterialType.NoCrownPhoto);
	                stdMaterial.setMaterial_file(stdUpload.getPath());
	                stdMaterial.setFile_type(fileType);
	                this.sMaterial.create(stdMaterial);
	                
	                map.put("stdMaterial", stdMaterial);
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
 //           }
           
        } else {
        	throw new BusinessException("上传的面冠证件照必须是jpg,png,jpeg格式!");
        }
	
        return new Ajax(map);
	}
	
	/**
	 * 免冠照片上传
	 * @param file
	 * @return
	 */
	@RequestMapping("std/upload/crown/photo")
	public Ajax stdUploadNoCrownPhoto(MultipartFile file,String std_id) {
		//String std_id = request.getParameter("stdId");
		StdMaterial stdMaterial= new StdMaterial();
		Map<String, Object> map = new HashMap<String, Object>();
		StdReg stdReg = this.stdRegDao.find(std_id);
//		String path=EnrolMaterialHelper.generateMaterialTempPath().getPath()+File.separator + StdMaterialType.StdUpload;
		String originalFilename = file.getOriginalFilename();
        int lastIndexOf = originalFilename.lastIndexOf(".");
        String fileType = originalFilename.substring(lastIndexOf + 1);
        //文件类型判断 jpg,png ,JPEG
        if (fileType.equals("jpg") || fileType.equals("png") || fileType.equals("jpeg") ) {
        //	String noCrownPhoto =StdMaterialType.NoCrownPhoto + "_" + stdReg.getCert_id()+"."+ fileType;
//        	FrameParam frameParam = frameParamDao.find(SkillParamHelper.LocalMateialPath);
        	File stdUpload = null;
			// 已经有照片信息了   Std_image存储大头免冠照片
			if (StringHelper.isNotEmpty(stdReg.getStd_image())) {
				stdUpload = SkillMateialHelper.makeStdImageFile(stdReg.getStd_image(), StdMateialType.StdUpload);
			}else {
				stdUpload = SkillMateialHelper.makeStdImageFile(stdReg.getCert_id(),StdMateialType.StdUpload , StdMaterialType.NoCrownPhoto, "jpeg");
			}
            //判断路径中文件是否存在
 //           if (!stdUpload.exists()) {
	            try {
	            	stdUpload.getParentFile().mkdirs();//构建文件夹
	            	stdUpload.createNewFile();//构建文件
	            	//保存路径
	                file.transferTo(stdUpload);
	               
	                //照片信息添加到考生注册信息表
	                this.stdRegService.stdUpdateNoCrownPhoto(stdReg.getStd_id(), stdUpload.getName());
	                //照片信息添加到材料信息表
	                stdMaterial.setStd_id(stdReg.getStd_id());
	                stdMaterial.setMaterial_type(StdMaterialType.NoCrownPhoto);
	                stdMaterial.setMaterial_file(stdUpload.getPath());
	                stdMaterial.setFile_type(fileType);
	                this.sMaterial.create(stdMaterial);
	                
	                map.put("stdMaterial", stdMaterial);
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
 //           }
           
        } else {
        	throw new BusinessException("上传的面冠证件照必须是jpg,png,jpeg格式!");
        }
	
        return new Ajax(map);
	}
}
