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

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.eas.common.EASBizException;
import com.kingdee.eas.custom.ats.AtsLeaveBillDetailSameFactory;
import com.kingdee.eas.custom.ats.AtsLeaveBillDetailSameInfo;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.ats.AtsLeaveBillCollection;
import com.kingdee.eas.hr.ats.AtsLeaveBillFactory;
import com.kingdee.eas.hr.ats.AtsLeaveBillInfo;
import com.kingdee.eas.hr.ats.HolidayPolicyFactory;
import com.kingdee.eas.hr.ats.HolidayPolicyInfo;
import com.kingdee.eas.hr.ats.HolidayTypeFactory;
import com.kingdee.eas.hr.ats.HolidayTypeInfo;
import com.kingdee.eas.hr.ats.HolidayTypeUnitEnum;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.ats.web.handler.util.AtsLeaveBillOperationUtil;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;

public class AtsLeaveBillEditHandlerEx extends AtsLeaveBillEditHandler {
	
	private static final Logger logger = LoggerFactory.getLogger(AtsLeaveBillEditHandlerEx.class);

	/**
	 * 获取假期信息(根据假期制度)
	 * @param request
	 * @param response
	 * @param modelMap
	 * @throws EASBizException
	 * @throws BOSException
	 * @throws SHRWebException
	 */
	public void getLeaveDataAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws EASBizException, BOSException, SHRWebException {
		Context ctx = SHRContext.getInstance().getContext();
		String policyId = request.getParameter("policyId");
		HolidayPolicyInfo policyInfo = HolidayPolicyFactory.getLocalInstance(ctx).getHolidayPolicyInfo(new ObjectUuidPK(policyId));
		HolidayTypeInfo holidayInfo = null;
		if(policyInfo != null && policyInfo.getHolidayType() != null) {
			String holidayTypeId = policyInfo.getHolidayType().getId().toString();
			holidayInfo = HolidayTypeFactory.getLocalInstance(ctx).getHolidayTypeInfo(new ObjectUuidPK(holidayTypeId));
		}
        JSONUtils.writeJson(response, holidayInfo);
	}
	
	/**
	 * 根据假期制度获取假期类型单位
	 * @param request
	 * @param response
	 * @param modelMap
	 * @throws EASBizException
	 * @throws BOSException
	 * @throws SHRWebException
	 */
	public void getLeaveUnitAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws EASBizException, BOSException, SHRWebException {
		Context ctx = SHRContext.getInstance().getContext();
		String policyId = request.getParameter("policyId");
		HolidayPolicyInfo policyInfo = HolidayPolicyFactory.getLocalInstance(ctx).getHolidayPolicyInfo(new ObjectUuidPK(policyId));
		HolidayTypeUnitEnum unit = policyInfo.getUnit();
		modelMap.put("leaveUnit", unit.getValue());
        JSONUtils.writeJson(response, modelMap);
	}
	
	
	/**
	 * 获取假期信息(根据假期类型)
	 * @param request
	 * @param response
	 * @param modelMap
	 * @throws EASBizException
	 * @throws BOSException
	 * @throws SHRWebException
	 */
	public void getLeaveDataByTypeAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws EASBizException, BOSException, SHRWebException {
		Context ctx = SHRContext.getInstance().getContext();
		String policyId = request.getParameter("policyId");
		System.out.print("获取假期信息(根据假期类型)：" + policyId);
		HolidayTypeInfo holidayInfo = HolidayTypeFactory.getLocalInstance(ctx).getHolidayTypeInfo(new ObjectUuidPK(policyId));
        JSONUtils.writeJson(response, holidayInfo);
		
	}
	
	/**
	 * 校验参数查询
	 * @param request
	 * @param response
	 * @param modelMap
	 * @throws EASBizException
	 * @throws BOSException
	 * @throws SQLException 
	 * @throws SHRWebException
	 */
	public void customerCheckParamAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws BOSException, SQLException, SHRWebException, EASBizException {
		Context ctx = SHRContext.getInstance().getContext();
		String personId = request.getParameter("personId");
		Map<String,String> returnMap = new HashMap<String,String>();
//		String personSql = "SELECT b.fnumber FROM t_bd_person a  " + 
//				" left join CT_MP_WorkerCategory b on a.CFWorkercategoryID = b.fid " +
//				" where a.fid = '" + personId + "'";
		String personSql = "SELECT e.fnumber FROM T_BD_Person a "
				+ "left join (select b.fpersonid,c.CFWorkercategoryID from (SELECT FPERSONID,max(FEFFDT) as maxDate FROM T_HR_EmpOrgRelation where fassignType = '1' group by FPERSONID) b "
				+ "left join T_HR_EmpOrgRelation c on b.fpersonid = c.fpersonid and b.maxdate=c.FEFFDT and c.fassignType = '1') d on a.fid=d.fpersonid "
				+ "left join CT_MP_WorkerCategory e on e.fid=d.CFWorkercategoryID "
				+ "where a.fid='"+personId+"'";
		
		System.out.print("员工类别查询：" + personSql);
		IRowSet rs = DbUtil.executeQuery(ctx, personSql);
		if(rs.next()) {
			returnMap.put("personType", rs.getString("fnumber"));
		}
		 JSONUtils.writeJson(response, returnMap);
	}
	
	/**
	 * 请假单提交工作流保存数据至新建的拆分表
	 */
	@Override
	protected void afterSubmit(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo model)
			throws SHRWebException {
		logger.info("AtsLeaveBillEditHandlerEx.....afterSubmit...start");
		// TODO 自动生成的方法存根
				super.afterSubmit(request, response, model);
		try {
			Context ctx = SHRContext.getInstance().getContext();
			AtsLeaveBillInfo info=(AtsLeaveBillInfo)model;
			logger.info("AtsLeaveBillEditHandlerEx.....number...",info.getNumber());
			
			logger.info("AtsLeaveBillEditHandlerEx.....BillID...",info.getId().toString());
			 //先删除拆分数据
			if(info.getId()!=null ) {
				String billFormatter = "('" + info.getId().toString() + "')";
				AtsLeaveBillOperationUtil.deleteInfo(ctx, billFormatter);
			}
			//审批中的数据存入拆分表
			AtsLeaveBillOperationUtil.saveInfo(ctx, info);
			logger.info("AtsLeaveBillEditHandlerEx.....afterSubmit...end");
		}catch(Exception e) {
			
		}
		
	}
	
	
}
