OvertimeTest4KQ.jsp 11.2 KB
<%@ page import="com.engine.kq.biz.KQTimesArrayComInfo" %>
<%@ page import="com.engine.kq.biz.chain.shiftinfo.ShiftInfoBean" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ page import="com.engine.kq.util.KQDurationCalculatorUtil" %>
<%@ page import="java.time.LocalDate" %>
<%@ page import="java.time.LocalDateTime" %>
<%@ page import="java.time.ZoneOffset" %>
<%@ page import="java.time.format.DateTimeFormatter" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Arrays" %>
<%@ page import="java.util.List" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="com.alibaba.fastjson.JSON" %>
<%@ page import="com.engine.hrm.biz.ScheduleUtil4Flow" %>
<%


    try{
        int userid = Util.getIntValue(request.getParameter("userid"),0);
        //最大循环次数是30次
        int max_day = Util.getIntValue(request.getParameter("max_day"),30);
        String fromdatetime = request.getParameter("fromdatetime");
        long mimillisecond = Util.getIntValue(request.getParameter("mimillisecond"),0);

        long time2 = new ScheduleUtil4Flow().workingTimeAdd(userid, fromdatetime, mimillisecond);
        out.println("new ScheduleUtil4Flow().workingTimeAdd()接口返回===fromdatetime:"+fromdatetime
                +":mimillisecond:"+mimillisecond+":time2:"+time2+"<br/>");

        KQTimesArrayComInfo kqTimesArrayComInfo = new KQTimesArrayComInfo();
        DateTimeFormatter fullFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter timeMinFormatter = DateTimeFormatter.ofPattern("HH:mm");
        DateTimeFormatter ssFormatter = DateTimeFormatter.ofPattern("ss");

        LocalDateTime localDateTime = LocalDateTime.parse(fromdatetime, fullFormatter);
        String timeMin = localDateTime.toLocalTime().format(timeMinFormatter);
        //秒
        String timeSS = localDateTime.toLocalTime().format(ssFormatter);
        int firstDayTimeIndex = kqTimesArrayComInfo.getArrayindexByTimes(timeMin);
        LocalDate curLocalDate =localDateTime.toLocalDate();
        long totalCnt = 0L;
        long tmp_totalCnt = 0L;
        String needDateTime = "";
        //负数表示往前推
        if(mimillisecond < 0){
            mimillisecond = Math.abs(mimillisecond);
            for(int i = 0 ; i < max_day ; i++){
                String curdate = curLocalDate.format(dateFormatter);
                if(needDateTime.length() > 0){
                    break;
                }
                ShiftInfoBean shiftInfoBean = KQDurationCalculatorUtil
                        .getWorkTime(""+userid, curdate,true,false);
                if(shiftInfoBean != null){

                    int restStartIndex = -1;
                    int restEndIndex = -1;
                    List<int[]> restIndex = shiftInfoBean.getRestIndex();
                    for(int r = 0 ; r < restIndex.size();r++){
                        int[] rests = restIndex.get(r);
                        if(rests != null && rests.length == 2){
                            restStartIndex = rests[0];
                            restEndIndex = rests[1];
                        }
                    }
                    List<int[]> workIndex = shiftInfoBean.getWorkIndex();
                    out.println(curdate+"===workIndex:"+ JSON.toJSONString(workIndex) +"<br/>");
                    //如果有休息时间的话,只能是一次打卡,把工作时段需要拆分长两段
                    if(restStartIndex > -1 && restEndIndex > -1){
                        List<int[]> tmpWorkIndex = new ArrayList<>();
                        int[] tmpWroks = workIndex.get(0);
                        int[] combined0 = new int[2];
                        combined0[0] = tmpWroks[0];
                        combined0[1] = restStartIndex;
                        int[] combined1 = new int[2];
                        combined1[0] = restEndIndex;
                        combined1[1] = tmpWroks[1];
                        tmpWorkIndex.add(combined0);
                        tmpWorkIndex.add(combined1);
                        workIndex = tmpWorkIndex;
                    }
                    for(int j = workIndex.size()-1 ; j >= 0;j--){
                        int[] initArrays = kqTimesArrayComInfo.getInitArr();
                        int[] works = workIndex.get(j);
                        if(works != null && works.length == 2){
                            Arrays.fill(initArrays, works[0], works[1], 0);
                            int arrayCnt = 0;
                            if(i == 0){
                                arrayCnt = kqTimesArrayComInfo.getCnt(initArrays, 0,firstDayTimeIndex,0);
                                arrayCnt = arrayCnt*60*1000;
                            }else{
                                arrayCnt = kqTimesArrayComInfo.getCnt(initArrays, 0,1440,0);
                                arrayCnt = arrayCnt*60*1000;
                            }
                            totalCnt += arrayCnt;
                            out.println(curdate+"===totalCnt:"+ totalCnt+"<br/>");
                            if(totalCnt >= mimillisecond){
                                long betweenArray =  mimillisecond-tmp_totalCnt;
                                int betweenMins = (int) (betweenArray/(1000*60));
                                if(i == 0){
                                    if(firstDayTimeIndex >= works[0] && firstDayTimeIndex <= works[1]){
                                        int needMin = firstDayTimeIndex-betweenMins;
                                        needDateTime = shiftInfoBean.getSplitDate()+" "+kqTimesArrayComInfo.getTimesByArrayindex(needMin);
                                    }else{
                                        int needMin = works[1]-betweenMins;
                                        needDateTime = shiftInfoBean.getSplitDate()+" "+kqTimesArrayComInfo.getTimesByArrayindex(needMin);
                                    }
                                }else{
                                    int needMin = works[1]-betweenMins;
                                    needDateTime = shiftInfoBean.getSplitDate()+" "+kqTimesArrayComInfo.getTimesByArrayindex(needMin);
                                }
                                break;
                            }
                            tmp_totalCnt = totalCnt;
                        }
                    }
                }
                curLocalDate =curLocalDate.minusDays(1);
            }

        }else{
            for(int i = 0 ; i < max_day ; i++){
                String curdate = curLocalDate.format(dateFormatter);
                if(needDateTime.length() > 0){
                    break;
                }
                ShiftInfoBean shiftInfoBean = KQDurationCalculatorUtil.getWorkTime(""+userid, curdate,true,false);
                if(shiftInfoBean != null){
                    int restStartIndex = -1;
                    int restEndIndex = -1;
                    List<int[]> restIndex = shiftInfoBean.getRestIndex();
                    for(int r = 0 ; r < restIndex.size();r++){
                        int[] rests = restIndex.get(r);
                        if(rests != null && rests.length == 2){
                            restStartIndex = rests[0];
                            restEndIndex = rests[1];
                        }
                    }
                    List<int[]> workIndex = shiftInfoBean.getWorkIndex();
                    out.println(curdate+"===workIndex:"+ JSON.toJSONString(workIndex) +"<br/>");
                    //如果有休息时间的话,只能是一次打卡,把工作时段需要拆分长两段
                    if(restStartIndex > -1 && restEndIndex > -1){
                        List<int[]> tmpWorkIndex = new ArrayList<>();
                        int[] tmpWroks = workIndex.get(0);
                        int[] combined0 = new int[2];
                        combined0[0] = tmpWroks[0];
                        combined0[1] = restStartIndex;
                        int[] combined1 = new int[2];
                        combined1[0] = restEndIndex;
                        combined1[1] = tmpWroks[1];
                        tmpWorkIndex.add(combined0);
                        tmpWorkIndex.add(combined1);
                        workIndex = tmpWorkIndex;
                    }
                    for(int j = 0 ; j < workIndex.size();j++){
                        int[] initArrays = kqTimesArrayComInfo.getInitArr();
                        int[] works = workIndex.get(j);
                        if(works != null && works.length == 2){
                            Arrays.fill(initArrays, works[0], works[1], 0);
                            int arrayCnt = 0;
                            if(i == 0){
                                arrayCnt = kqTimesArrayComInfo.getCnt(initArrays, firstDayTimeIndex,1440,0);
                                arrayCnt = arrayCnt*60*1000;
                            }else{
                                arrayCnt = kqTimesArrayComInfo.getCnt(initArrays, 0,1440,0);
                                arrayCnt = arrayCnt*60*1000;
                            }
                            totalCnt += arrayCnt;
                            out.println(curdate+"===totalCnt:"+ totalCnt+"<br/>");
                            if(totalCnt >= mimillisecond){
                                long betweenArray =  mimillisecond-tmp_totalCnt;
                                int betweenMins = (int) (betweenArray/(1000*60));
                                if(i == 0){
                                    if(firstDayTimeIndex >= works[0] && firstDayTimeIndex <= works[1]){
                                        int needMin = firstDayTimeIndex+betweenMins;
                                        needDateTime = shiftInfoBean.getSplitDate()+" "+kqTimesArrayComInfo.getTimesByArrayindex(needMin);
                                    }else{
                                        int needMin = works[0]+betweenMins;
                                        needDateTime = shiftInfoBean.getSplitDate()+" "+kqTimesArrayComInfo.getTimesByArrayindex(needMin);
                                    }
                                }else{
                                    int needMin = works[0]+betweenMins;
                                    needDateTime = shiftInfoBean.getSplitDate()+" "+kqTimesArrayComInfo.getTimesByArrayindex(needMin);
                                }
                                break;
                            }
                            tmp_totalCnt = totalCnt;
                        }
                    }
                }
                curLocalDate =curLocalDate.plusDays(1);
            }
        }

        long result = 0;
        out.println("new ScheduleUtil4Flow().workingTimeAdd()接口返回===needDateTime:"+needDateTime+"<br/>");
        if(needDateTime.length() > 0){
            needDateTime += ":"+timeSS;
            result = LocalDateTime.parse(needDateTime, fullFormatter).toInstant(ZoneOffset.of("+8")).toEpochMilli();
        }else{
            result = 0L;
        }
    }catch (Exception e){
        out.println("new ScheduleUtil4Flow().workingTimeAdd()接口返回===Exception:"+e.getLocalizedMessage()+"<br/>");
        e.printStackTrace();

    }

%>