schedule4util.jsp 10.9 KB
<%@ page import="com.engine.kq.biz.KQTimesArrayComInfo" %>
<%@ page import="com.engine.kq.biz.chain.shiftinfo.ShiftInfoBean" %>
<%@ 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 language="java" contentType="text/html; charset=UTF-8" %>
<%

    try {
        String userids = Util.null2String(request.getParameter("userids"));
        String fromdatetime = Util.null2String(request.getParameter("fromdatetime"));
        String mimilliseconds = Util.null2String(request.getParameter("mimilliseconds"));
        if(userids.length() == 0){
            out.println("param userids is null:<br/>");
            return ;
        }
        if(fromdatetime.length() == 0){
            out.println("param fromdatetime is null:<br/>");
            return ;
        }
        if(mimilliseconds.length() == 0){
            out.println("param mimilliseconds is null:<br/>");
            return ;
        }
        int userid = Util.getIntValue(userids);
        long mimillisecond = Long.parseLong(mimilliseconds);

        //最大循环次数是7次
        int max_day = 14;
        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();
                    //如果有休息时间的话,只能是一次打卡,把工作时段需要拆分长两段
                    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;
                            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();
                    //如果有休息时间的话,只能是一次打卡,把工作时段需要拆分长两段
                    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.print(curdate+":arrayCnt:"+ arrayCnt+":totalCnt:"+ totalCnt+":mimillisecond:"+mimillisecond+"::"+(totalCnt >= mimillisecond) +"<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);
            }
        }
        if(needDateTime.length() > 0){
            needDateTime += ":"+timeSS;
            out.print("结果:"+LocalDateTime.parse(needDateTime, fullFormatter).toInstant(ZoneOffset.of("+8")).toEpochMilli()+"<br/>");
        }else{
            out.print("结果:"+0L+"<br/>");
        }

    } catch (Exception e) {
        e.printStackTrace();
        out.print(e.getMessage());
    }

%>