<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="java.time.LocalDate" %>
<%@ page import="java.time.format.DateTimeFormatter" %>
<%@ page import="java.time.temporal.TemporalAdjusters" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%

    User user = HrmUserVarify.getUser(request, response);
    if(user==null) {
        response.sendRedirect("/login/Login.jsp");
        return;
    }

    BaseBean bb = new BaseBean();
    RecordSet rs = new RecordSet();
    String cjrbm = request.getParameter("cjrbm");
    String pbyf = request.getParameter("pbyf");
    String yg = request.getParameter("yg");

    String startdate = "";
    String enddate = "";
    String sfdd = "1";
    if(StringUtils.isNotBlank(pbyf) && pbyf.length() == 7){

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate dateTime = LocalDate.parse(pbyf+"-01", formatter);
        LocalDate firstDayOfMonth = dateTime.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate lastDayOfMonth = dateTime.with(TemporalAdjusters.lastDayOfMonth());
        startdate = firstDayOfMonth.toString();
        enddate = lastDayOfMonth.toString();

        if(StringUtils.isNotBlank(cjrbm)) {
            String sql =" select ydsxrq " +
                    " from uf_ygddxx " +
                    " where left(ydsxrq,7) = '"+pbyf+"' "+
                    " and xm ="+ yg +
                    " and ydhbm="+cjrbm+
                    " and xm in( select id from hrmresource  where status in(0,1,2,3) and jobtitle not in(select id from hrmjobtitles where jobtitlename in('营运经理'))) " +
                    " order by id desc ";
            out.print("sql:"+sql+"</br>");
            rs.executeQuery(sql);
            if(rs.next()){
                String ydsxrq = Util.null2String(rs.getString("ydsxrq"));
                out.print("ydsxrq:"+ydsxrq+"</br>");
                if(StringUtils.isNotBlank(ydsxrq)){
                    startdate = ydsxrq;
                    sfdd = "0";
                }
            }

            sql =" select convert(varchar,dateadd(day,-1,ydsxrq), 23) as ydsxrq " +
                    " from uf_ygddxx " +
                    " where left(ydsxrq,7) = '"+pbyf+"' "+
                    " and xm = "+ yg +
                    " and bm= "+ cjrbm+
                    " and ydsxrq > '"+startdate +"'"+
                    " and xm in( select id from hrmresource  where status in(0,1,2,3) and jobtitle not in(select id from hrmjobtitles where jobtitlename in('营运经理'))) " +
                    " order by id desc ";
            rs.executeQuery(sql);
            out.print("sql:"+sql+"</br>");
            if(rs.next()){
                String ydsxrq = Util.null2String(rs.getString("ydsxrq"));
                out.print("ydsxrq:"+ydsxrq+"</br>");
                if(StringUtils.isNotBlank(ydsxrq)){
                    enddate = ydsxrq;
                    sfdd = "0";
                }
            }
        }
    }

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("startdate",startdate);
    jsonObject.put("enddate",enddate);
    jsonObject.put("sfdd",sfdd);

%>
<%=jsonObject.toJSONString() %>



