package cn.hmsoft.skill.helper;

import java.io.File;
import java.time.LocalDate;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.hmsoft.application.SpringHelper;
import cn.hmsoft.skill.data.dao.std.StdMaterialDao;
import cn.hmsoft.skill.data.model.cf.enrol.CfEnrolMaterial;
import  cn.hmsoft.skill.helper.ArtParamHelper;
import cn.hmsoft.frame.constants.FrameStatus;
import cn.hmsoft.frame.exception.BusinessException;
import cn.hmsoft.helper.LocalDateHelper;
import cn.hmsoft.helper.RandomHelper;

/*********************
 * 报考材料辅助类
 * 
 * @author zxq
 * @create 2019-08-23 16:38:30
 * @version 2.0.0
 * @email: revisit@126.com
 * @Company: www.hmsoft.cn
 */
public class EnrolMaterialHelper {
	private EnrolMaterialHelper() {

	}

	public static String getEnrolMaterialLocalPath() {
		return ArtParamHelper.EnrolMaterialLocalPath
				+ (ArtParamHelper.EnrolMaterialLocalPath.endsWith(File.separator) ? "" : File.separator);
	}

	public static File generateEnrolMaterialFile(String material_type, String cert_id, String suffix) {
		return new File(getEnrolMaterialLocalPath() + generateMaterialName(material_type, cert_id) + "." + suffix);
	}

	/******************
	 * 根据材料类型生成文件名称
	 * 
	 * @param material_type
	 *            文件类型
	 * @param cert_id
	 *            证件号码
	 */
	public static String generateMaterialName(String material_type, String cert_id) {
		return LocalDateHelper.formatNoSplit(LocalDate.now()) + File.separator + material_type + "_" + cert_id + "_"
				+ RandomHelper.getRandomValue(8);
	}
	
	/******************
	 * 根据材料类型生成文件名称
	 */
	public static String generateMaterialSubjectName(String material_type, String cert_id, String subject_id) {
		return LocalDateHelper.formatNoSplit(LocalDate.now()) + File.separator + material_type + "_" + cert_id + "_"
				+ subject_id + "_" + RandomHelper.getRandomValue(8);
	}

	/**************************
	 * 考生报考材料临时文件
	 */
	public static File generateMaterialTempFile() {
		File f = new File(
				getEnrolMaterialLocalPath() + "temp" + File.separator + "art_" + RandomHelper.getRandomValue(16));
		if (!f.getParentFile().exists()) {
			f.getParentFile().mkdirs();
		}
		return f;
	}

	/**************************
	 * 考生报考材料临时文件
	 */
	public static File generateMaterialTempFileBySuffix(String suffix) {
		File f = new File(getEnrolMaterialLocalPath() + "temp" + File.separator + "art_"
				+ RandomHelper.getRandomValue(16) + suffix);
		if (!f.getParentFile().exists()) {
			f.getParentFile().mkdirs();
		}
		return f;
	}

	/**************************
	 * 考生报考材料临时文件
	 */
	public static File generateMaterialTempFile(String file_name) {
		File f = new File(getEnrolMaterialLocalPath() + "temp" + File.separator + file_name);
		if (!f.getParentFile().exists()) {
			f.getParentFile().mkdirs();
		}
		return f;
	}

	/**************************
	 * 考生报考材料临时目录
	 */
	public static File generateMaterialTempPath() {
		File f = new File(getEnrolMaterialLocalPath() + "temp" + File.separator);
		if (!f.exists()) {
			f.mkdirs();
		}
		return f;
	}

	private static EnrolMaterialHelper helper = null;
	private static List<CfEnrolMaterial> GobalMaterialArray = null;
	private static Map<String, CfEnrolMaterial> GobalMaterialMap = null;

	public static EnrolMaterialHelper getInstance() {
		return getInstance(false);

	}

	public static EnrolMaterialHelper getInstance(boolean refresh) {
		if (helper == null || refresh) {
			synchronized (EnrolMaterialHelper.class) {
				GobalMaterialArray = SpringHelper.AppGobalDao.list(CfEnrolMaterial.class, "material_status",
						FrameStatus.Active.toString());
				GobalMaterialMap = new LinkedHashMap<String, CfEnrolMaterial>();
				for (CfEnrolMaterial m : GobalMaterialArray)
					GobalMaterialMap.put(m.getMaterial_type(), m);
				helper = new EnrolMaterialHelper();
			}
		}
		return helper;
	}

	/********************************
	 * 验证考生材料是否齐全
	 */
	public boolean checkStdMaterial(int std_id, String business_type) {
		List<Map<String, Object>> materialArray = SpringHelper.getBean(StdMaterialDao.class).listAllMaterial(std_id);
		return checkStdMaterial(materialArray, business_type);
	}

	/********************************
	 * 验证考生材料是否齐全
	 */
	public boolean checkStdMaterial(List<Map<String, Object>> materialArray, String business_type) {
		if (business_type == null)
			return true;
		for (CfEnrolMaterial conf : GobalMaterialArray) {
			if (business_type.equals(conf.getUpload_business()) || business_type.equals(conf.getAudit_business())) {
				Map<String, Object> curMaterial = null;
				for (Map<String, Object> m : materialArray) {
					if (conf.getMaterial_type().equals(m.get("material_type").toString())) {
						curMaterial = m;
						break;
					}
				}
				if (curMaterial == null || !curMaterial.containsKey("material_id"))
					throw new BusinessException("请在【我的->报考材料】界面上传【" + conf.getMaterial_name() + "】，再进行此业务操作！");
				if (business_type.equals(conf.getAudit_business())) {
					// 需要验证
					if (curMaterial.get("verify_status") == null
							|| !FrameStatus.Active.toString().equals(curMaterial.get("verify_status").toString()))
						throw new BusinessException(
								"【" + conf.getMaterial_name() + "】报考材料尚未审核，由于考生人数过多，审核需要一定时间，请耐心等待！");
				}
			}
		}

		return true;
	}

}
