package com.engine.custom.corn.Thread;

import com.engine.custom.corn.util.ReportUtil;
import com.wbi.util.Util;
import org.docx4j.wml.R;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.file.Prop;
import weaver.general.BaseBean;
import weaver.hrm.User;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class YearReportThread implements Runnable {

    private Integer startNum;
    private Integer endNum;
    private final static String INSERT_SQL = "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 sqlhrm = "select * from Hrmresource ORDER BY id LIMIT 100 OFFSET ";
    private RecordSetDataSource em7rs;
    private RecordSetDataSource emprs;
    private RecordSet rs;
    private RecordSet rs2;
    private Map<String, String> map;
    private Integer cnt = 0;
    private BaseBean bb;

    public YearReportThread(Integer startNum, Integer endNum,Map<String, String> map,Integer cnt) {
        this.startNum = startNum;
        this.endNum = endNum;
        this.map = map;
        this.cnt = cnt;
        this.em7rs = new RecordSetDataSource("em7");
        this.emprs = new RecordSetDataSource("dm_msg");
        this.rs = new RecordSet();
        this.rs2 = new RecordSet();
        bb = new BaseBean();
    }

    @Override
    public void run() {
        writeLog("开始结束数"+startNum+"__"+endNum);
        for (int i = startNum; i < endNum; i++) {
            String  sqlhrmWhere = sqlhrm + i*100;
            rs.executeQuery(sqlhrmWhere);
            while (rs.next()){
                String id = rs.getString("id");
                rs2.executeUpdate(INSERT_SQL,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"));
                writeLog(rs2.getExceptionMsg());
            }
        }

        //更新总行的数量
        //消息总数
        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 = 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(rs);
        String updateuserSql = "update uf_personreport set workcode = ? , name = ? , grhycycs = ? ,grwdgx = ? ,jjrclgw = ? ,qnljclgw = ?,cgbl = ? ,ljdloacs = ?,clgwzwrq = ?,grswsps = ?,grcylts = ?,grfsxxrs = ?,grfsxxqlss=?,ljsyslcs = ?,dycclgwrq = ? where ry = ?";
        //开始更新个人的不同的数据
        for (int i = startNum; i < endNum; i++) {
            String  sqlhrmWhere = sqlhrm + i*100;
            rs.executeQuery(sqlhrmWhere);
            while (rs.next()){
                String id = rs.getString("id");
                String workcode = rs.getString("WORKCODE");
                String name = rs.getString("LASTNAME");
                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);
                String LastOfficialWfDate = 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);
            }
        }

    }
    public  void writeLog(String log) {
        bb.writeLog(Thread.currentThread().getName()+"==>YearReportThread:"+log);
    }

}
