<%@ 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="java.time.LocalDate" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="com.engine.kq.biz.KQGroupBiz" %>
<%@ page import="com.engine.kq.biz.KQGroupComInfo" %>
<%@ page import="com.engine.kq.biz.KQGroupMemberComInfo" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="java.time.DayOfWeek" %>
<%@ 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 pbyf = request.getParameter("pbyf");
    String userid = request.getParameter("userid");
    String cjrbm = request.getParameter("cjrbm");

//    List<Map<String,String>> hrmList = new ArrayList<Map<String,String>>();
//    if(StringUtils.isNotBlank(cjrbm)) {
//        String sql =" select id,lastname from hrmresource where departmentid = "+cjrbm +" and status in(0,1,2,3) and jobtitle not in(select id from hrmjobtitles where jobtitlename in('营运经理')) and id not in(select xm from uf_ygddxx where left(ydsxrq,7) = '"+pbyf+"')"+
//                    " union "+
//                    " select id,lastname from hrmresource where status in(0,1,2,3) and id in(select xm from uf_ygddxx where left(ydsxrq,7) = '"+pbyf+"' and ydhbm ="+cjrbm+") and jobtitle not in(select id from hrmjobtitles where jobtitlename in('营运经理')) "+
//                    " union "+
//                    " select id,lastname from hrmresource where status in(0,1,2,3) and id in(select xm from uf_ygddxx where left(ydsxrq,7) = '"+pbyf+"' and ydsxrq >='"+pbyf+"-02' and bm ="+cjrbm+") and jobtitle not in(select id from hrmjobtitles where jobtitlename in('营运经理')) " ;
//
//        hrmList = queryListBySql(sql);
//    }

//    if(hrmList !=null && hrmList.size() > 0){
//        userid = hrmList.get(0).get("id");
//    }

    if(StringUtils.isBlank(userid)){
        userid = user.getUID()+"";
    }

    String activeGroupId = "";
    int daysInMonth = 0;
    Map<String,String> weekMap = new HashMap<String,String>();
    List<Map<String,String>> weekList = new ArrayList<Map<String,String>>();
    if(StringUtils.isNotBlank(pbyf) && pbyf.length() == 7) {
        LocalDate date = LocalDate.parse(pbyf + "-01"); // 获取当前日期
        LocalDate lastDayOfMonth = date.with(TemporalAdjusters.lastDayOfMonth()); // 获取本月最后一天
        LocalDate startDayOfMonth = date.with(TemporalAdjusters.firstDayOfMonth()); // 获取本月最后一天
        String monthStartDate = startDayOfMonth.toString();
        String monthEndDate = lastDayOfMonth.toString();

        activeGroupId = getUserActiveGroupId(userid);
        String fixedScheduleGroupId = "5";

        Map<String,String> holidayMap = getHolidayByGroupId(fixedScheduleGroupId,monthStartDate,monthEndDate);

        int thisMonth = date.getMonthValue(); // 获取当前是哪月
        daysInMonth = date.lengthOfMonth(); // 获取本月有多少天

        for (int i = 1; i <= daysInMonth; i++) {
            LocalDate currentDate = LocalDate.of(date.getYear(), thisMonth, i);
            System.out.println("currentDate:" + currentDate.toString());
            DayOfWeek dayOfWeek = currentDate.getDayOfWeek();
            System.out.println(i + "号是周 " + dayOfWeek.getValue());
            int weekValue = dayOfWeek.getValue();
            String weekDayName = "";
            switch (weekValue) {
                case 1:
                    weekDayName = "周一";
                    break;
                case 2:
                    weekDayName = "周二";
                    break;
                case 3:
                    weekDayName = "周三";
                    break;
                case 4:
                    weekDayName = "周四";
                    break;
                case 5:
                    weekDayName = "周五";
                    break;
                case 6:
                    weekDayName = "周六";
                    break;
                case 7:
                    weekDayName = "周日";
                    break;
            }

            String changeType = "" ;
            if(holidayMap.containsKey(currentDate.toString())){
                changeType = holidayMap.get(currentDate.toString());
            }
            weekMap = new HashMap<String,String>();
            weekMap.put("weekname", weekDayName);
            weekMap.put("changeType", changeType);
            weekMap.put("weekday", i+"");
            weekMap.put("weekdate", currentDate.toString());
            weekList.add(weekMap);
        }
    }

    String serialids = "";
    if(StringUtils.isNotBlank(activeGroupId)){
        String sql = " select serialids from kq_group where id = "+activeGroupId ;
        rs.executeQuery(sql);
        if(rs.next()){
            serialids = Util.null2String(rs.getString("serialids"));
        }
    }
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("data",weekList);
    jsonObject.put("count",daysInMonth);
    jsonObject.put("group",activeGroupId);
    jsonObject.put("serialids",serialids);
%>

<%=jsonObject.toJSONString() %>


<%!

    /***
     *
     * @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;
    }


    public Map<String,String> getHolidayByGroupId(String activeGroupId,String monthStartDate,String monthEndDate){

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

        try {
            if(StringUtils.isNotBlank(monthStartDate) && StringUtils.isNotBlank(monthEndDate)){
                //String activeGroupId = getUserActiveGroupId(userid);
                // bb.writeLog("activeGroupId:"+activeGroupId);
                if(StringUtils.isNotBlank(activeGroupId)){
                    String sql =" select holidayDate,changeType from KQ_HolidaySet\n" +
                            " where groupid = " + activeGroupId+
                            " 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,changeTypename);

//                        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;
    }


    public static List<Map<String,String>> queryListBySql(String sql){
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        List<Map<String,String>> list = new ArrayList<Map<String,String>>();
        try{
            if(StringUtils.isNotBlank(sql)){
                Map<String,String> map = new HashMap<String,String>();
                rs.executeQuery(sql);
                String[] column = rs.getColumnName();
                while (rs.next()){
                    map = new HashMap<String,String>();
                    for(int i=0;i<column.length;i++){
                        String value = rs.getString(column[i]);
                        map.put(column[i],value);
                    }
                    if(!map.isEmpty() && map.size()>0){
                        list.add(map);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            bb.writeLog("queryListBySql-e:"+e);
        }
        return list;
    }

%>


