package com.kingdee.shr.customer.gtiit.handler;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.base.param.IParamControl;
import com.kingdee.eas.base.param.ParamControlFactory;
import com.kingdee.eas.basedata.org.HROrgUnitCollection;
import com.kingdee.eas.basedata.org.HROrgUnitFactory;
import com.kingdee.eas.basedata.org.HROrgUnitInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.hr.emp.ContractTempletCollection;
import com.kingdee.eas.hr.emp.ContractTempletFactory;
import com.kingdee.eas.hr.emp.ContractTempletInfo;
import com.kingdee.eas.hr.emp.ContractTypeCollection;
import com.kingdee.eas.hr.emp.ContractTypeFactory;
import com.kingdee.eas.hr.emp.ContractTypeInfo;
import com.kingdee.eas.hr.emp.LabContractFirstPartyCollection;
import com.kingdee.eas.hr.emp.LabContractFirstPartyFactory;
import com.kingdee.eas.hr.emp.LabContractFirstPartyInfo;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.ats.web.handler.AtsOverTimeBillEditHandler;
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;
import com.kingdee.shr.customer.gtiit.util.BaseUtil;
import com.kingdee.shr.customer.gtiit.util.DateTimeUtils;

import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * @description（类描述）:单人加班单表单Handler
 * @author（创建人）:
 * @createDate（创建时间）: 10:28
 * @updateUser（修改人）:
 * @updateDate（修改时间）:
 * @updateRemark（修改备注）:
 */
public class AtsOverTimeBillEditHandlerEx extends AtsOverTimeBillEditHandler {

	private static Logger logger =
	        Logger.getLogger("com.kingdee.shr.customer.gtiit.handler.AtsOverTimeBillEditHandlerEx");
	
	public void workMultipleAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
		Context ctx = SHRContext.getInstance().getContext();
		String otDate = request.getParameter("otDate");
		logger.error("加班日期："+otDate);
		String sql = "SELECT b.CFWORKMULTIPLE FROM T_HR_ATS_LegalHoliday a left join T_HR_ATS_LegalHolidayItem b on a.fid = b.FGROUPID where a.FSTATE = '1' and b.FSTARTDATE <= '"+otDate+"' and b.FENDDATE >= '"+otDate+"'";
		logger.error("查询加班类型SQL："+sql);
		Map<String, Map<String, String>> overTimeTypeMap = BaseUtil.getOverTimeType(ctx);
		Map<String, String> returnMap = new HashMap<String, String>();
		try {
			IRowSet iRowSet = DbUtil.executeQuery(ctx, sql);
			while (iRowSet.next()){
				if(iRowSet.getString("CFWORKMULTIPLE").equals("1")){
					//工作日加班
					returnMap = overTimeTypeMap.get("001");
				}else if(iRowSet.getString("CFWORKMULTIPLE").equals("2")){
					//休息日加班
					returnMap = overTimeTypeMap.get("002");
				}else if(iRowSet.getString("CFWORKMULTIPLE").equals("3")){
					//法定节假日加班
					returnMap = overTimeTypeMap.get("003");
				}
			}
		} catch (BOSException e) {
			e.printStackTrace();
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
		JSONUtils.writeJson(response, returnMap);
	}


	public void jobTypeAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
		Calendar calendar = Calendar.getInstance();
		Context ctx = SHRContext.getInstance().getContext();
		Map<String, Map<String, String>> overTimeTypeMap = BaseUtil.getOverTimeType(ctx);
		Map<String, String> returnMap = new HashMap<String, String>();
		String otDate = request.getParameter("otDate");
		logger.error("加班日期:"+otDate);
		Date parseDate = DateTimeUtils.parseDate(otDate, "yyyy-MM-dd");
		calendar.setTime(parseDate);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if(dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY) {
			//休息日加班
        	returnMap = overTimeTypeMap.get("002");
        }else {
			//工作日加班
        	returnMap = overTimeTypeMap.get("001");
        }
        JSONUtils.writeJson(response, returnMap);
	}
}
