package com.kingdee.shr.affair.web.handler;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;


import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.base.permission.UserInfo;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.org.PositionFactory;
import com.kingdee.eas.basedata.org.PositionInfo;
import com.kingdee.eas.basedata.person.Genders;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.affair.EmpEnrollBizBillEntryInfo;
import com.kingdee.eas.hr.affair.EmpEnrollBizBillFactory;
import com.kingdee.eas.hr.affair.EmpEnrollBizBillInfo;
import com.kingdee.eas.hr.base.AffairActionReasonCollection;
import com.kingdee.eas.hr.base.AffairActionReasonFactory;
import com.kingdee.eas.hr.base.ApproveTypeEnum;
import com.kingdee.eas.hr.base.EmployeeTypeCollection;
import com.kingdee.eas.hr.base.EmployeeTypeFactory;
import com.kingdee.eas.hr.base.HRBillStateEnum;
import com.kingdee.eas.hr.base.HRBizDefineCollection;
import com.kingdee.eas.hr.base.HRBizDefineFactory;
import com.kingdee.eas.hr.base.VariationReasonCollection;
import com.kingdee.eas.hr.base.VariationReasonFactory;
import com.kingdee.eas.hr.base.util.HRUtilExtend;
import com.kingdee.eas.hr.emp.CredentialsTypeFactory;
import com.kingdee.eas.hr.emp.EmployeeException;
import com.kingdee.eas.hr.emp.web.util.PassPortCommonUtil;
import com.kingdee.eas.util.app.ContextUtil;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.base.syssetting.app.io.fileImport.ImportException;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;
import com.kingdee.shr.preentry.FileStateEnum;
import com.kingdee.shr.preentry.PreEntryInfo;
import com.kingdee.shr.preentry.PreEntryPersonInfo;
import com.kingdee.shr.preentry.SubmitEffectSSCFacadeFactory;
import com.kingdee.shr.affair.web.handler.EmpPreEntryEditHandler;
import com.kingdee.shr.affair.web.handler.util.EmployeeUtil;

/**
 * 预入职单
 * @author issuser
 *
 */
public class EmpPreEntryEditHandlerEx extends EmpPreEntryEditHandler {
	
	private static Logger logger =
	        Logger.getLogger("com.kingdee.shr.affair.web.handler.EmpPreEntryEditHandlerEx");

	@Override
	protected void beforeSave(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo model) throws SHRWebException {
		PreEntryInfo billInfo = (PreEntryInfo)model;
		if (billInfo.getTalent().getPassportNO() != null) {
			PassPortCommonUtil.checkPassPortContainsChinese(billInfo.getTalent().getPassportNO());
		}

		try {
			Context ctx = SHRContext.getInstance().getContext();
			HRUtilExtend.checkGlobalRoamingExists(ctx, billInfo.getGlobalRoaming());
		} catch (EmployeeException var6) {
			throw new ShrWebBizException(var6.getMessage());
		} catch (BOSException var7) {
			var7.printStackTrace();
		}
		
		super.beforeSave(request, response, model);
		if (billInfo.getBillState() == null) {
			billInfo.setBillState(HRBillStateEnum.SAVED);
		}

		if (billInfo.getTalent() != null && billInfo.getTalent().getFileState() == null) {
			billInfo.getTalent().setFileState(FileStateEnum.TempStorage);
		}
		try {
			if( ObjectUtils.isEmpty(billInfo.get("empNumber"))  ){
				EmployeeUtil employeeUtil = new EmployeeUtil();
				int fnumber = employeeUtil.getFnumber();
				billInfo.put("empNumber",fnumber);
			}
			
		} catch (BOSException e) {
			throw new RuntimeException(e);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}

	}
		
	@Override
	protected void verifyModel(HttpServletRequest request,
			HttpServletResponse response, CoreBaseInfo model)
			throws SHRWebException {
		PreEntryInfo preEntryInfo = (PreEntryInfo) model;
		try {
			EmployeeUtil employeeUtil = new EmployeeUtil();
			PreEntryPersonInfo prePersonInfo = preEntryInfo.getTalent();
			String fid = model.getString("id");
			//再入职校验
			employeeUtil.checkIsReEntry(prePersonInfo.getIdCardNO(), preEntryInfo.getName(), prePersonInfo.getBirthday());
			//用户名唯一校验
			Boolean isUniqueCheck = employeeUtil.checkUserName(
					preEntryInfo.get("userName").toString(),fid,null);
			if( !isUniqueCheck){
				throw new ImportException("This user name："+ preEntryInfo.get("userName") + "already exists"); 
			}
			//工作邮箱唯一校验
			Boolean emailCheck = employeeUtil.checkEmail(preEntryInfo.get("workemail").toString(),fid,null);
			if( !emailCheck){
				throw new ImportException("The work email："+ preEntryInfo.get("userName") + "already exists"); 
			}
		} catch (BOSException e1) {
			e1.printStackTrace();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		super.verifyModel(request, response, model);
	}
	
	/**
	 * 再入职校验
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 * @throws SHRWebException
	 */
	public String checkIdEntityAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
		//身份证号
		String idCardNo = request.getParameter("idCardNo");
		//出生日期
		String birthday = request.getParameter("birthday");
		//名称
		String name = request.getParameter("name");
		logger.error("身份证号:"+idCardNo);
		logger.error("出生日期:"+birthday+",名称："+name);
		Context ctx = SHRContext.getInstance().getContext();
		try {
			if(StringUtils.isNotBlank(idCardNo)) {
				String checkSql = "select FIDCardNO from t_bd_person where FIDCardNO = '" + idCardNo + "'";
				logger.error("查询身份证SQL："+checkSql);
				IRowSet rs = DbUtil.executeQuery(ctx, checkSql);
				if(rs.next()){
					modelMap.put("resultMessage", "Employee is re-entry employee, ID number：" + rs.getString("FIDCardNO"));
				}
			}
			
			if(StringUtils.isNotBlank(birthday) && StringUtils.isNotBlank(name)) {
				String checkSql = "select FPASSPORTNO from t_bd_person where to_char(FBirthday,'yyyy-MM-dd') = '" + birthday + "' and UPPER(FName_L1) = UPPER('"+ name +"')";
				logger.error("查询证件SQL："+checkSql);
				IRowSet rs = DbUtil.executeQuery(ctx, checkSql);
				if(rs.next()){
					modelMap.put("resultMessage", "Employee is re-entry employee, passport number：" + rs.getString("FPASSPORTNO"));
				}
			}
		} catch (BOSException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		JSONUtils.writeJson(response, modelMap);
		return null;
	}
	
}