package com.engine.custom.corn;
import com.engine.custom.corn.Thread.YearReportThread;
import com.engine.custom.corn.util.ReportUtil;
import com.icbc.api.internal.apache.http.impl.cookie.S;
import com.wbi.util.Util;
import org.docx4j.wml.R;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.file.Prop;
import weaver.interfaces.schedule.BaseCronJob;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.DayOfWeek;
import java.util.*;
import java.util.stream.Collectors;

public class YearReportCorn extends BaseCronJob {

    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(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    public void execute() {

        RecordSetDataSource em7rs = new RecordSetDataSource("em7");
        RecordSetDataSource emprs = new RecordSetDataSource("dm_msg");
        RecordSet rs = new RecordSet();
        RecordSet rs2 = new RecordSet();
        RecordSet rs3 = new RecordSet();
        deleteHistoryData(rs2);
        // 获取总人数
        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();
        int pageTotle = (int) Math.ceil(cnt/100);
        String sqlhrm = "select * from Hrmresource ORDER BY id LIMIT 100 OFFSET ";
        int ThreadNum = (int) Math.ceil(pageTotle/5);
        for (int i = 0; i < 5; i++) {
            System.out.println(i*ThreadNum+"======"+(((i+1)*ThreadNum)-1));
            if (i==4){
                new Thread(new YearReportThread(i * ThreadNum,pageTotle,map,cnt)).start();
            }else{
                new Thread(new YearReportThread(i * ThreadNum, (((i + 1) * ThreadNum) - 1),map,cnt)).start();
            }
        }








        // for (int i = 0; i < pageTotle; i++) {
        //   String  sqlhrmWhere = sqlhrm + i*100;
        //   rs.executeQuery(sqlhrmWhere);
        //   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"));
        //   }
        // }
        //
        // //更新总行的数量
        // //消息总数
        // int msgCount = ReportUtil.getMsgCount();
        // //分享文档总数
        // int ShareDOCCount = ReportUtil.getShareDOCCount();
        // //会议总次数
        // int meetCount = ReportUtil.getMeetCount();
        //
        // String updateTotalSql = "update uf_personreport set jsxxzongshu = ? ,zjgxwd = ? ,ysxljfqhycs = ? where ry != 1";
        // rs.executeUpdate(updateTotalSql,msgCount,ShareDOCCount,meetCount);
        // //获取节假日日期字符串查询条件
        // List<String> allWeekendsOfYear = 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 =  getAffairsWfid(rs);
        // String updateuserSql = "update uf_personreport set grhycycs = ? ,grwdgx = ? ,jjrclgw = ? ,qnljclgw = ?,cgbl = ? ,ljdloacs = ?,clgwzwrq = ?,grswsps = ?,grcylts = ?,grfsxxrs = ?,grfsxxqlss=?,ljsyslcs = ? where ry = ?";
        // //开始更新个人的不同的数据
        // for (int i = 0; i < pageTotle; i++) {
        //     String  sqlhrmWhere = sqlhrm + i*100;
        //     rs.executeQuery(sqlhrmWhere);
        //     while (rs.next()){
        //         String id = rs.getString("id");
        //         int shareDocCnt = Util.getIntValue(ReportUtil.getShareDOCCountbyUserid(rs2, id)+"",0) ;
        //         int personMeetCount = Util.getIntValue(ReportUtil.getMeetCountByUserid(rs2, id)+"",0) ;
        //         int yearOfficialWfCount = Util.getIntValue(ReportUtil.getYearOfficialWfCount(rs2, id, wfids)+"",0) ;
        //         int holidayOfficialWfCount = Util.getIntValue(ReportUtil.getHolidayOfficialWfCount(rs2, id, wfids,weekStr)+"",0) ;
        //         int officialWfCountPercentage = Util.getIntValue(ReportUtil.getOfficialWfCountPercentage(rs2, id, wfids,weekStr,cnt)+"",0) ;
        //         String lastTimeDate = Util.null2String(ReportUtil.getOfficialWflastTimeDate(rs2, id, wfids)) ;
        //         int loginDayCount = Util.getIntValue(ReportUtil.getLoginDayCount(rs2, id)+"",0) ;
        //         int AffairsWfCount = Util.getIntValue(ReportUtil.getYearAffairsWfCount(rs2, id, AffairsWfid)+"",0);
        //         int MsgCount = Util.getIntValue(ReportUtil.getMsgCountByUserid(emprs, id)+"",0);
        //         int PeopleCount = Util.getIntValue(ReportUtil.getMsgPeopleCountByUserid(emprs, id)+"",0);
        //         int MsgGangHsienCount = Util.getIntValue(ReportUtil.getMsgGangHsienCountByUserid(emprs, id)+"",0);
        //         int BusinessWfCount = Util.getIntValue(ReportUtil.getBusinessWfCount(rs2, id,slwfid)+"",0);
        //         rs.executeUpdate(updateuserSql,personMeetCount,shareDocCnt,holidayOfficialWfCount,yearOfficialWfCount,officialWfCountPercentage,loginDayCount,lastTimeDate,AffairsWfCount,MsgCount,PeopleCount,MsgGangHsienCount,BusinessWfCount,id);
        //     }
        // }

    }

    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(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", Util.null2String(recordSet.getString("RY")));
        map.put("ND", Util.null2String(recordSet.getString("ND")));
        map.put("DDCS", Util.null2String(recordSet.getString("DDCS")));
        map.put("TGWZZS", Util.null2String(recordSet.getString("TGWZZS")));
        map.put("ZSHYWZ1", Util.null2String(recordSet.getString("ZSHYWZ1")));
        map.put("ZSHYWZ2", Util.null2String(recordSet.getString("ZSHYWZ2")));
        map.put("ZSHYWZ3", Util.null2String(recordSet.getString("ZSHYWZ3")));
        map.put("YSXLJFQHYCS", Util.null2String(recordSet.getString("YSXLJFQHYCS")));
        map.put("GRHYCYCS", Util.null2String(recordSet.getString("GRHYCYCS")));
        map.put("ZJGXWD", Util.null2String(recordSet.getString("ZJGXWD")));
        map.put("GRWDGX", Util.null2String(recordSet.getString("GRWDGX")));
        map.put("SKTXYSFXS", Util.null2String(recordSet.getString("SKTXYSFXS")));
        map.put("SKTXCYCS", Util.null2String(recordSet.getString("SKTXCYCS")));
        map.put("GCZGW", Util.null2String(recordSet.getString("GCZGW")));
        map.put("LJJYZZ", Util.null2String(recordSet.getString("LJJYZZ")));
        map.put("LJJP", Util.null2String(recordSet.getString("LJJP")));
        map.put("JJRCLGW", Util.null2String(recordSet.getString("JJRCLGW")));
        map.put("QNLJCLGW", Util.null2String(recordSet.getString("QNLJCLGW")));
        map.put("CGBL", Util.null2String(recordSet.getString("CGBL")));
        map.put("LJSYSLCS", Util.null2String(recordSet.getString("LJSYSLCS")));
        map.put("QGZYDF", Util.null2String(recordSet.getString("QGZYDF")));
        map.put("NDDJLZGYY", Util.null2String(recordSet.getString("NDDJLZGYY")));
        map.put("LJDJCS", Util.null2String(recordSet.getString("LJDJCS")));
        map.put("ZAYDYY", Util.null2String(recordSet.getString("ZAYDYY")));
        map.put("LJSYCS", Util.null2String(recordSet.getString("LJSYCS")));
        map.put("PJMRZXRS", Util.null2String(recordSet.getString("PJMRZXRS")));
        map.put("PJMRZXRSZB", Util.null2String(recordSet.getString("PJMRZXRSZB")));
        map.put("LJDLOACS", Util.null2String(recordSet.getString("LJDLOACS")));
        map.put("DLCSCGRSZB", Util.null2String(recordSet.getString("DLCSCGRSZB")));
        map.put("FORMMODEID", Util.null2String(recordSet.getString("FORMMODEID")));
        map.put("MODEDATACREATER", Util.null2String(recordSet.getString("MODEDATACREATER")));
        map.put("MODEDATACREATERTYPE", Util.null2String(recordSet.getString("MODEDATACREATERTYPE")));
        map.put("MODEDATACREATEDATE", Util.null2String(recordSet.getString("MODEDATACREATEDATE")));
        map.put("MODEDATACREATETIME", Util.null2String(recordSet.getString("MODEDATACREATETIME")));
        map.put("MODEDATAMODIFIER", Util.null2String(recordSet.getString("MODEDATAMODIFIER")));
        map.put("MODEDATAMODIFYDATETIME", Util.null2String(recordSet.getString("MODEDATAMODIFYDATETIME")));
        map.put("FORM_BIZ_ID", Util.null2String(recordSet.getString("FORM_BIZ_ID")));
        map.put("MODEUUID", Util.null2String(recordSet.getString("MODEUUID")));
        map.put("YYGS", Util.null2String(recordSet.getString("YYGS")));
        map.put("GWBLLCXS", Util.null2String(recordSet.getString("GWBLLCXS")));
        map.put("SWSPLCXS", Util.null2String(recordSet.getString("SWSPLCXS")));
        map.put("JSXXZONGSHU", Util.null2String(recordSet.getString("JSXXZONGSHU")));
        map.put("XWHYCS", Util.null2String(recordSet.getString("XWHYCS")));
        map.put("SWSPLCGS", Util.null2String(recordSet.getString("SWSPLCGS")));
        return map;

    }



    public static void main(String[] args) {
        int ceil = (int)Math.ceil(201 / 5);

        for (int i = 0; i < 5; i++) {
            System.out.println(i*ceil+"======"+(((i+1)*ceil)-1));
        }
    }


    private void deleteHistoryData(RecordSet recordSet) {
        recordSet.executeUpdate("delete uf_personreport where ry != 1");
    }
}
