<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="com.engine.common.util.ParamUtil" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="javax.ws.rs.core.Context" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="java.time.LocalDate" %>
<%@ page import="java.time.format.DateTimeFormatter" %>
<%@ page import="java.time.temporal.ChronoUnit" %>
<%@ page import="weaver.general.StringUtil" %>
<%@ page import="java.time.LocalDateTime" %>
<%@ page import="weaver.file.Prop" %>
<%@ page import="java.time.DayOfWeek" %>
<%@ page import="java.util.*" %>
<%@ page import="weaver.conn.RecordSetDataSource" %>
<%@ page import="com.engine.custom.corn.Thread.YearReportThread" %>
<%@ page import="com.engine.custom.corn.util.ReportUtil" %>
<%@ page import="java.util.stream.Collectors" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>


<%
    RecordSetDataSource em7rs = new RecordSetDataSource("em7");
    RecordSetDataSource emprs = new RecordSetDataSource("dm_msg");
    RecordSet rs = new RecordSet();
    RecordSet rs2 = new RecordSet();
    RecordSet rs3 = new RecordSet();
    String insertSql = "INSERT INTO uf_personreport( RY, ND, DDCS, TGWZZS, ZSHYWZ1, ZSHYWZ2, ZSHYWZ3, YSXLJFQHYCS, GRHYCYCS, ZJGXWD, GRWDGX,SKTXYSFXS, SKTXCYCS, GCZGW, LJJYZZ, LJJP, JJRCLGW, QNLJCLGW, CGBL, LJSYSLCS, QGZYDF, NDDJLZGYY,LJDJCS, ZAYDYY, LJSYCS, PJMRZXRS, PJMRZXRSZB, LJDLOACS, DLCSCGRSZB, YYGS, GWBLLCXS, SWSPLCXS,JSXXZONGSHU, XWHYCS, SWSPLCGS,FORMMODEID)" +
            "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    // 获取总人数
    String sql = "select count(*) cnt from Hrmresource";
    rs.executeQuery(sql);
    Integer cnt = 0;
    if (rs.next()){
        cnt = rs.getInt("cnt");
    }


    //按照模板插入写死的值
    Map<String, String> map = getConfigRecord();

    String sqlhrm = "select * from HRMRESOURCE a left join uf_personreport b on a.id = b.ry where b.ry is null and accounttype !=  1 and a.workcode is not null and belongto = -1 and a.status in ( 0,1,2,3)";
    rs.executeQuery(sqlhrm);

    //获取节假日日期字符串查询条件
    List<String> allWeekendsOfYear = ReportUtil.getAllWeekendsOfYear(2023).stream()
            .map(s -> "'" + s + "'")
            .collect(Collectors.toList());;
    String weekStr = String.join(",",allWeekendsOfYear);
    String wfids = Prop.getPropValue("yearReport", "gwwfid");
    String slwfid = Prop.getPropValue("yearReport", "slwfid");
    String AffairsWfid = ReportUtil.getAffairsWfid(rs3);
    String updateuserSql = "update uf_personreport set workcode = ? , name = ? , grhycycs = ? ,grwdgx = ? ,jjrclgw = ? ,qnljclgw = ?,cgbl = ? ,ljdloacs = ?,clgwzwrq = ?,grswsps = ?,grcylts = ?,grfsxxrs = ?,grfsxxqlss=?,ljsyslcs = ?,dycclgwrq = ? where ry = ?";


    while (rs.next()){
        String id = rs.getString("id");
        rs2.executeUpdate(insertSql,id,map.get("ND"),map.get("DDCS"),map.get("TGWZZS"),map.get("ZSHYWZ1"),map.get("ZSHYWZ2"),map.get("ZSHYWZ3"),map.get("YSXLJFQHYCS"),map.get("GRHYCYCS"),map.get("ZJGXWD"),map.get("GRWDGX"),map.get("SKTXYSFXS"),map.get("SKTXCYCS"),map.get("GCZGW"),map.get("LJJYZZ"),map.get("LJJP"),map.get("JJRCLGW"),map.get("QNLJCLGW"),map.get("CGBL"),map.get("LJSYSLCS"),map.get("QGZYDF"),map.get("NDDJLZGYY"),map.get("LJDJCS"),map.get("ZAYDYY"),map.get("LJSYCS"),map.get("PJMRZXRS"),map.get("PJMRZXRSZB"),map.get("LJDLOACS"),map.get("DLCSCGRSZB"),map.get("YYGS"),map.get("GWBLLCXS"),map.get("SWSPLCXS"),map.get("JSXXZONGSHU"),map.get("XWHYCS"),map.get("SWSPLCGS"),map.get("FORMMODEID"));
        String workcode = rs.getString("WORKCODE");
        String name = rs.getString("LASTNAME");
        int shareDocCnt = com.wbi.util.Util.getIntValue(ReportUtil.getShareDOCCountbyUserid(rs2, id)+"",0) ;
        int personMeetCount = com.wbi.util.Util.getIntValue(ReportUtil.getMeetCountByUserid(rs2, id)+"",0) ;
        int yearOfficialWfCount = com.wbi.util.Util.getIntValue(ReportUtil.getYearOfficialWfCount(rs2, id, wfids)+"",0) ;
        int holidayOfficialWfCount = com.wbi.util.Util.getIntValue(ReportUtil.getHolidayOfficialWfCount(rs2, id, wfids,weekStr)+"",0) ;
        int officialWfCountPercentage = com.wbi.util.Util.getIntValue(ReportUtil.getOfficialWfCountPercentage(rs2, id, wfids,weekStr,cnt)+"",0) ;
        String lastTimeDate = com.wbi.util.Util.null2String(ReportUtil.getOfficialWflastTimeDate(rs2, id, wfids)) ;
        int loginDayCount = com.wbi.util.Util.getIntValue(ReportUtil.getLoginDayCount(rs2, id)+"",0) ;
        int AffairsWfCount = com.wbi.util.Util.getIntValue(ReportUtil.getYearAffairsWfCount(rs2, id, AffairsWfid)+"",0);
        int MsgCount = com.wbi.util.Util.getIntValue(ReportUtil.getMsgCountByUserid(emprs, id)+"",0);
        int PeopleCount = com.wbi.util.Util.getIntValue(ReportUtil.getMsgPeopleCountByUserid(emprs, id)+"",0);
        int MsgGangHsienCount = com.wbi.util.Util.getIntValue(ReportUtil.getMsgGangHsienCountByUserid(emprs, id)+"",0);
        int BusinessWfCount = com.wbi.util.Util.getIntValue(ReportUtil.getBusinessWfCount(rs2, id,slwfid)+"",0);
        String LastOfficialWfDate = com.wbi.util.Util.null2String(ReportUtil.getLastOfficialWfDate(rs2, id,wfids));
        rs.executeUpdate(updateuserSql,workcode,name,personMeetCount,shareDocCnt,holidayOfficialWfCount,yearOfficialWfCount,officialWfCountPercentage,loginDayCount,lastTimeDate,AffairsWfCount,MsgCount,PeopleCount,MsgGangHsienCount,BusinessWfCount,LastOfficialWfDate,id);
        out.print(workcode);
    }

%>
<%!



    private String getAffairsWfid(RecordSet rs) {
        String swspwftype = Prop.getPropValue("yearReport", "swspwftype");
        rs.executeQuery("select id  from workflow_base where workflowtype in ( "+swspwftype+" )");
        ArrayList<String> wfids = new ArrayList<>();
        while (rs.next()){
            wfids.add(com.wbi.util.Util.null2String(rs.getString("id")));
        }
        return String.join(",",wfids);
    }


    /**
     * 获取指定年的休息日(双休情况下)
     * @param year
     * @return
     */
    private static List<String> getAllWeekendsOfYear(int year) {
        //将本年的周六周日加到list中
        List<String> weekendDates = new ArrayList<>();
        LocalDate date = LocalDate.of(year, 1, 1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        while (date.getYear() == year) {
            if (date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY) {
                weekendDates.add(date.format(formatter));
            }
            date = date.plusDays(1);
        }
        //查询库里的节假日设置
        RecordSet recordSet = new RecordSet();
        // String groupid = Util.null2String(Prop.getPropValue("year_report_set", "groupid"),"24") ;
        String groupid = "24" ;
        recordSet.executeQuery("select changeType ,holidayDate from KQ_HolidaySet where groupid = ?",groupid);
        //去掉调配工作日,加上公众假日和调配休息日
        List<String> WorkDates = new ArrayList<>();
        List<String> holidayDates = new ArrayList<>();
        while (recordSet.next()){
            String changeType = recordSet.getString("changeType");
            String holidayDate = recordSet.getString("holidayDate");
            if ("2".equals(changeType)){
                WorkDates.add(holidayDate);
            }else if("1".equals(changeType) || "3".equals(changeType)){
                holidayDates.add(holidayDate);
            }
        }
        weekendDates.removeAll(WorkDates);
        HashSet<String> set = new HashSet<>();
        set.addAll(weekendDates);
        set.addAll(holidayDates);
        return new ArrayList<>(set);
    }

    private Map<String, String> getConfigRecord(){
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery("select * from uf_personreport where ry = 1");
        recordSet.next();
        HashMap<String,String> map = new HashMap<>();
        map.put("RY", com.wbi.util.Util.null2String(recordSet.getString("RY")));
        map.put("ND", com.wbi.util.Util.null2String(recordSet.getString("ND")));
        map.put("DDCS", com.wbi.util.Util.null2String(recordSet.getString("DDCS")));
        map.put("TGWZZS", com.wbi.util.Util.null2String(recordSet.getString("TGWZZS")));
        map.put("ZSHYWZ1", com.wbi.util.Util.null2String(recordSet.getString("ZSHYWZ1")));
        map.put("ZSHYWZ2", com.wbi.util.Util.null2String(recordSet.getString("ZSHYWZ2")));
        map.put("ZSHYWZ3", com.wbi.util.Util.null2String(recordSet.getString("ZSHYWZ3")));
        map.put("YSXLJFQHYCS", com.wbi.util.Util.null2String(recordSet.getString("YSXLJFQHYCS")));
        map.put("GRHYCYCS", com.wbi.util.Util.null2String(recordSet.getString("GRHYCYCS")));
        map.put("ZJGXWD", com.wbi.util.Util.null2String(recordSet.getString("ZJGXWD")));
        map.put("GRWDGX", com.wbi.util.Util.null2String(recordSet.getString("GRWDGX")));
        map.put("SKTXYSFXS", com.wbi.util.Util.null2String(recordSet.getString("SKTXYSFXS")));
        map.put("SKTXCYCS", com.wbi.util.Util.null2String(recordSet.getString("SKTXCYCS")));
        map.put("GCZGW", com.wbi.util.Util.null2String(recordSet.getString("GCZGW")));
        map.put("LJJYZZ", com.wbi.util.Util.null2String(recordSet.getString("LJJYZZ")));
        map.put("LJJP", com.wbi.util.Util.null2String(recordSet.getString("LJJP")));
        map.put("JJRCLGW", com.wbi.util.Util.null2String(recordSet.getString("JJRCLGW")));
        map.put("QNLJCLGW", com.wbi.util.Util.null2String(recordSet.getString("QNLJCLGW")));
        map.put("CGBL", com.wbi.util.Util.null2String(recordSet.getString("CGBL")));
        map.put("LJSYSLCS", com.wbi.util.Util.null2String(recordSet.getString("LJSYSLCS")));
        map.put("QGZYDF", com.wbi.util.Util.null2String(recordSet.getString("QGZYDF")));
        map.put("NDDJLZGYY", com.wbi.util.Util.null2String(recordSet.getString("NDDJLZGYY")));
        map.put("LJDJCS", com.wbi.util.Util.null2String(recordSet.getString("LJDJCS")));
        map.put("ZAYDYY", com.wbi.util.Util.null2String(recordSet.getString("ZAYDYY")));
        map.put("LJSYCS", com.wbi.util.Util.null2String(recordSet.getString("LJSYCS")));
        map.put("PJMRZXRS", com.wbi.util.Util.null2String(recordSet.getString("PJMRZXRS")));
        map.put("PJMRZXRSZB", com.wbi.util.Util.null2String(recordSet.getString("PJMRZXRSZB")));
        map.put("LJDLOACS", com.wbi.util.Util.null2String(recordSet.getString("LJDLOACS")));
        map.put("DLCSCGRSZB", com.wbi.util.Util.null2String(recordSet.getString("DLCSCGRSZB")));
        map.put("FORMMODEID", com.wbi.util.Util.null2String(recordSet.getString("FORMMODEID")));
        map.put("MODEDATACREATER", com.wbi.util.Util.null2String(recordSet.getString("MODEDATACREATER")));
        map.put("MODEDATACREATERTYPE", com.wbi.util.Util.null2String(recordSet.getString("MODEDATACREATERTYPE")));
        map.put("MODEDATACREATEDATE", com.wbi.util.Util.null2String(recordSet.getString("MODEDATACREATEDATE")));
        map.put("MODEDATACREATETIME", com.wbi.util.Util.null2String(recordSet.getString("MODEDATACREATETIME")));
        map.put("MODEDATAMODIFIER", com.wbi.util.Util.null2String(recordSet.getString("MODEDATAMODIFIER")));
        map.put("MODEDATAMODIFYDATETIME", com.wbi.util.Util.null2String(recordSet.getString("MODEDATAMODIFYDATETIME")));
        map.put("FORM_BIZ_ID", com.wbi.util.Util.null2String(recordSet.getString("FORM_BIZ_ID")));
        map.put("MODEUUID", com.wbi.util.Util.null2String(recordSet.getString("MODEUUID")));
        map.put("YYGS", com.wbi.util.Util.null2String(recordSet.getString("YYGS")));
        map.put("GWBLLCXS", com.wbi.util.Util.null2String(recordSet.getString("GWBLLCXS")));
        map.put("SWSPLCXS", com.wbi.util.Util.null2String(recordSet.getString("SWSPLCXS")));
        map.put("JSXXZONGSHU", com.wbi.util.Util.null2String(recordSet.getString("JSXXZONGSHU")));
        map.put("XWHYCS", com.wbi.util.Util.null2String(recordSet.getString("XWHYCS")));
        map.put("SWSPLCGS", com.wbi.util.Util.null2String(recordSet.getString("SWSPLCGS")));
        return map;

    }
%>