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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.ats.AtsOverTimeBillEntryCollection;
import com.kingdee.eas.hr.ats.AtsOverTimeBillEntryInfo;
import com.kingdee.eas.hr.ats.AtsOverTimeBillInfo;
import com.kingdee.eas.hr.ats.AttendFileStateEnum;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.ats.web.handler.PerAtsOverTimeBillBatchEditHandler;
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.customer.gtiit.util.BaseUtil;
import com.kingdee.shr.customer.gtiit.util.DateTimeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;
import org.springframework.util.LinkedMultiValueMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.*;

public class PerAtsOverTimeBillBatchEditHandlerExEx extends PerAtsOverTimeBillBatchEditHandlerEx {
	private static Logger logger = Logger
			.getLogger("com.kingdee.shr.customer.gtiit.handler.PerAtsOverTimeBillBatchEditHandlerExEx");
	@Override
	protected void verifyModel(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo model)
			throws SHRWebException {
		super.verifyModel(request, response, model);
		//增加一位员工每个自然月只能提一张加班单校验
		checkOnlyTakeOneBill(request, response, model);
	}
	/**
	 * 一位员工每个自然月只能提一张加班单校验
	 * @param request
	 * @param response
	 * @param model
	 */
	protected void checkOnlyTakeOneBill(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo model) throws ShrWebBizException {
		Context ctx = SHRContext.getInstance().getContext();
		try {
			AtsOverTimeBillInfo billInfo = (AtsOverTimeBillInfo) model;
			AtsOverTimeBillEntryCollection entries = billInfo.getEntries();
			StringBuilder sql = new StringBuilder(" select e.fid from T_HR_ATS_OVERTIMEBILL b ");
			sql.append(" left join T_HR_ATS_OVERTIMEBILLentry e on b.fid= e.fbillid ");
			sql.append(" where e.fpersonid=? and b.fbillstate='3' and e.fotdate>={ts ?} and e.fotdate<={ts ?} ");
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			for (int i = 0; i < entries.size(); i++) {
				AtsOverTimeBillEntryInfo ats = entries.get(i);
				Date otdate = ats.getOtDate();
				PersonInfo personInfo = ats.getPerson();
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(otdate);
				calendar.set(Calendar.DAY_OF_MONTH, 1);
				String fotdate1 = sf.format(calendar.getTime());
				calendar.add(Calendar.MONTH, 1);
				calendar.add(Calendar.DAY_OF_MONTH, -1);
				String fotdate2 = sf.format(calendar.getTime());
				IRowSet rs = DbUtil.executeQuery(ctx, sql.toString(), new String[]{personInfo.getId().toString(), fotdate1, fotdate2});
				if(rs.size()>0){
					throw new ShrWebBizException("Each employee can apply for overtime only once per natural month.Employee ("+personInfo.getName()+") has already applied.");
				}
			}
		}catch (BOSException e){
			e.printStackTrace();
		}
	}


}
