<%@ 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.common.DateUtil" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="com.engine.kq.biz.KQGroupMemberComInfo" %>
<%@ page import="com.engine.kq.biz.KQGroupComInfo" %>
<%@ page import="com.engine.kq.biz.KQGroupBiz" %>
<%@ page import="java.time.LocalDate" %>
<%@ page import="java.time.format.DateTimeFormatter" %>
<%@ page import="java.time.DayOfWeek" %>
<%@ page import="java.time.temporal.TemporalAdjusters" %>
<%@ page import="java.util.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%

    String[] workSchedule = {"09:00-11:00","11:00-16:00","16:00-20:00","20:00-21:30"};
    User user = HrmUserVarify.getUser(request, response);
    if(user==null) {
        response.sendRedirect("/login/Login.jsp");
        return;
    }
    int userid = user.getUID();
    BaseBean bb = new BaseBean();
    RecordSet rs = new RecordSet();
    String pbrq = request.getParameter("pbrq");
    String bp = request.getParameter("bp");



    String dateType = "0";
    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    LocalDate currentDate = LocalDate.parse(pbrq, df);

    DayOfWeek dayOfWeek = currentDate.getDayOfWeek();
    int weekValue = dayOfWeek.getValue();

    LocalDate lastDayOfMonth = currentDate.with(TemporalAdjusters.lastDayOfMonth()); // 获取本月最后一天
    LocalDate startDayOfMonth = currentDate.with(TemporalAdjusters.firstDayOfMonth()); // 获取本月最后一天
    String monthStartDate = startDayOfMonth.toString();
    String monthEndDate = lastDayOfMonth.toString();
    Map<String,String> holidayMap = getHolidayByGroupId(userid+"",monthStartDate,monthEndDate);

    String changeType = "" ;
    if(holidayMap.containsKey(currentDate.toString())){
        changeType = holidayMap.get(currentDate.toString());
        out.print("changeType:"+changeType+"</br>");
    }

    if("2".equals(changeType)){
        dateType = "0";
    }else if("1".equals(changeType) || "3".equals(changeType)){
        dateType = "1";
    }else if(weekValue == 6 || weekValue  == 7){
        dateType = "1";
    }

    out.print("dateType:"+dateType+"</br>");

    Map<String,Integer> defaultScheduleMap = new HashMap<String,Integer>();
    if(StringUtils.isNotBlank(bp)){
        String sql = " select dysjd,pzrs from uf_pbjcsj where bprs = ? and pblx = ?" ;
        out.print("sql:"+sql+"</br>");
        rs.executeQuery(sql,new Object[]{bp,dateType});
        while (rs.next()){
            int dysd = Util.getIntValue(Util.null2String(rs.getString("dysjd")));
            int pzrs = Util.getIntValue(Util.null2String(rs.getString("pzrs")));

            String sjd = "";
            if(dysd < workSchedule.length){
                sjd = workSchedule[dysd];
                if(StringUtils.isNotBlank(sjd)){
                    defaultScheduleMap.put(sjd,pzrs);
                }
            }
        }
    }

%>


<%!

    public String getWeekTypeByDate(String pbrq,String userid){

        String dateType = "0";
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate currentDate = LocalDate.parse(pbrq, df);

        DayOfWeek dayOfWeek = currentDate.getDayOfWeek();
        int weekValue = dayOfWeek.getValue();

        LocalDate lastDayOfMonth = currentDate.with(TemporalAdjusters.lastDayOfMonth()); // 获取本月最后一天
        LocalDate startDayOfMonth = currentDate.with(TemporalAdjusters.firstDayOfMonth()); // 获取本月最后一天
        String monthStartDate = startDayOfMonth.toString();
        String monthEndDate = lastDayOfMonth.toString();
        Map<String,String> holidayMap = getHolidayByGroupId(userid,monthStartDate,monthEndDate);

        String changeType = "" ;
        if(holidayMap.containsKey(currentDate.toString())){
            changeType = holidayMap.get(currentDate.toString());
        }

        if("1".equals(changeType) || "3".equals(changeType)){
            dateType = "1";
        }else if(weekValue == 6 || weekValue  == 7){
            dateType = "1";
        }else if("2".equals(changeType)){
            dateType = "0";
        }
        return dateType;
    }

    /***
     *
     * @param userid
     * @param monthStartDate
     * @param monthEndDate
     * @return
     */
    public Map<String,String> getHolidayByGroupId(String userid,String monthStartDate,String monthEndDate){

        Map<String,String> holidayMap = new HashMap<String,String>();
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();

        try {
            if(StringUtils.isNotBlank(userid) && StringUtils.isNotBlank(monthStartDate) && StringUtils.isNotBlank(monthEndDate)){
//                String activeGroupId = getUserActiveGroupId(userid);
//                bb.writeLog("activeGroupId:"+activeGroupId);

                String fixedScheduleGroupId = "5";
                if(StringUtils.isNotBlank(fixedScheduleGroupId)){
                    String sql =" select holidayDate,changeType from KQ_HolidaySet\n" +
                            " where groupid = " + fixedScheduleGroupId+
                            " and holidayDate >='"+monthStartDate+"'\n" +
                            " and holidayDate <='"+monthEndDate+"' " ;
                    rs.execute(sql);
                    while (rs.next()){
                        String holidayDate = Util.null2String(rs.getString("holidayDate"));
                        String changeType = Util.null2String(rs.getString("changeType"));
//                        String changeTypename = "" ;
//                        if("1".equals(changeType)){ //1-公众假日 2-调配工作日 3-调配休息日
//                            changeTypename += "公众假日";
//                        }else if("2".equals(changeType)){
//                            changeTypename += "调配工作日";
//                        }else if("3".equals(changeType)){
//                            changeTypename += "调配休息日";
//                        }

                        holidayMap.put(holidayDate,changeType);

//                        if(weekMap.containsKey(holidayDate)){
//                            String holidayDate_new = weekMap.get(holidayDate);
//                            if("1".equals(changeType)){ //1-公众假日 2-调配工作日 3-调配休息日
//                                holidayDate_new += "[公众假日]";
//                            }else if("1".equals(changeType)){
//                                holidayDate_new += "[调配工作日]";
//                            }else if("2".equals(changeType)){
//                                holidayDate_new += "[调配休息日]";
//                            }
//                            weekMap.put(holidayDate,holidayDate_new);
//                        }
                    }
                }
            }
        }catch (Exception e){

        }
        return holidayMap;
    }

    /***
     *
     * @param userid
     * @return
     */
    public String getUserActiveGroupId(String userid){
        BaseBean bb = new BaseBean();

        KQGroupMemberComInfo kqGroupMemberComInfo = null;
        KQGroupComInfo kqGroupComInfo = null ;
        String activeGroupId = "";
        try{
            kqGroupMemberComInfo = new KQGroupMemberComInfo();
            kqGroupComInfo = new KQGroupComInfo();

            Map<String, List<String>> groupMemberMap = getUserGroup();

            String activeGroup = "";
            if(groupMemberMap.containsKey(userid)){
                List<String> groupIds = groupMemberMap.get(userid);
                bb.writeLog("groupIds:"+groupIds.size());

                for(String i : groupIds){
                    String isDelete = kqGroupComInfo.getIsdelete(i);
                    if(!"1".equals(isDelete)) {
                        String currentGroupId = Util.null2String(kqGroupMemberComInfo.getKQGroupId(userid, DateUtil.getCurrentDate()));
                        if (i.equals(currentGroupId)) {
                            activeGroupId =  i;
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            bb.writeLog(e);
        }
        return activeGroupId;
    }


    /***
     *
     * @return
     */
    public Map<String, List<String>> getUserGroup(){
        BaseBean bb = new BaseBean();
        Map<String, List<String>> groupMemberMap = new HashMap<>();
        RecordSet rs = new RecordSet();
        String viewScope = "3";
        try {
            KQGroupBiz kqGroupBiz = new KQGroupBiz();
            Map<String, Object> groupParams = new HashMap<String, Object>();
            if("3".equals(viewScope)){
                groupParams.put("isNoAccount", "1");
            }
            rs.executeQuery(kqGroupBiz.getGroupMemberSql(groupParams));
            while (rs.next()) {
                String id = rs.getString("resourceid");
                String gid = rs.getString("groupid");
                if (!groupMemberMap.containsKey(id))
                    groupMemberMap.put(id, new ArrayList<>());
                groupMemberMap.get(id).add(gid);
            }
        } catch (Exception e) {
        }
        return groupMemberMap;
    }


%>


