<%@ page import="com.engine.kq.biz.KQWorkTime" %>
<%@ page import="com.engine.kq.entity.WorkTimeEntity" %>
<%@ page import="java.text.DecimalFormat" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="com.engine.kq.entity.TimeScopeEntity" %>
<%@ page import="java.util.List" %>
<%@ page import="java.time.temporal.ChronoUnit" %>
<%@ page import="java.time.LocalDate" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%
    RecordSet rs = new RecordSet();
    String userid = request.getParameter("userid");
//    String kq_date = request.getParameter("kq_date");
    String leaveid = request.getParameter("leaveid");
    double leavetime = Double.parseDouble(request.getParameter("leavetime"));
    String qjksrq = request.getParameter("qjksrq");
    String qjjsrq = request.getParameter("qjjsrq");
    String qjkssj = request.getParameter("qjkssj");
    String qjjssj = request.getParameter("qjjssj");
    out.print("userid:"+userid+"</br>");
    out.print("leaveid:"+leaveid+"</br>");
    out.print("leavetime:"+leavetime+"</br>");
    out.print("qjksrq:"+qjksrq+"</br>");
    out.print("qjjsrq:"+qjjsrq+"</br>");
    out.print("qjkssj:"+qjkssj+"</br>");
    out.print("qjjssj:"+qjjssj+"</br>");


    int total_workMins =0;

    String workHour = String.valueOf(leavetime);
    String minimumUnit = "";
    String sql = " select minimumUnit from kq_LeaveRules where (isDelete is null or isDelete!=1) and id = "+leaveid;
    rs.executeQuery(sql);
    if(rs.next()){
        minimumUnit = Util.null2String(rs.getString("minimumUnit"));
    }

    out.print("minimumUnit:"+minimumUnit+"</br>");

    if("3".equals(minimumUnit)){
        if(qjksrq.equals(qjjsrq)){
            out.print("qjksrq:"+qjksrq+"</br>");
            KQWorkTime kqWorkTime = new KQWorkTime();
            WorkTimeEntity workTime = kqWorkTime.getWorkTime(userid, qjksrq);
            if(workTime !=null){
                int workMins = workTime.getWorkMins();

                String beginTime = "";
                String endTime = "" ;
                List<TimeScopeEntity> timeScopeEntityList =  workTime.getWorkTime();
                for(TimeScopeEntity timeScopeEntity : timeScopeEntityList){
                    beginTime = timeScopeEntity.getBeginTime();
                    endTime = timeScopeEntity.getEndTime();
                }
                if(StringUtils.isNotBlank(beginTime) && StringUtils.isNotBlank(endTime)){
                    if(qjkssj.compareTo(endTime) < 0){
                        total_workMins+=workMins ;
                        out.print("workMins:"+workMins+"</br>");
                        out.print("workworkHour_:"+(workMins/60.00)+"</br>");
                    }
                }
            }
        }else{
            LocalDate qjksrq_date = LocalDate.parse(qjksrq);
            LocalDate qjjsrq_date = LocalDate.parse(qjjsrq);
            List<String> qjrqList = new ArrayList<String>();
            long numOfDaysBetween = ChronoUnit.DAYS.between(qjksrq_date, qjjsrq_date);
            for (int i = 0; i <= numOfDaysBetween; i++) {
                LocalDate date = qjksrq_date.plusDays(i);
                qjrqList.add(date.toString());
            }

            for(int i=0;i<qjrqList.size();i++){
                String kq_date = qjrqList.get(i);
                out.print("kq_date:"+kq_date+"</br>");
                if(i==0){
                    if(StringUtils.isNotBlank(userid) && StringUtils.isNotBlank(kq_date)){
                        KQWorkTime kqWorkTime = new KQWorkTime();
                        WorkTimeEntity workTime = kqWorkTime.getWorkTime(userid, kq_date);
                        if(workTime !=null){
                            int workMins = workTime.getWorkMins();
                            String beginTime = "";
                            String endTime = "" ;
                            List<TimeScopeEntity> timeScopeEntityList =  workTime.getWorkTime();
                            for(TimeScopeEntity timeScopeEntity : timeScopeEntityList){
                                beginTime = timeScopeEntity.getBeginTime();
                                endTime = timeScopeEntity.getEndTime();
                            }
                            if(StringUtils.isNotBlank(beginTime) && StringUtils.isNotBlank(endTime)){
                                if(qjkssj.compareTo(endTime) < 0){
                                    total_workMins+=workMins ;
                                    out.print("workMins:"+workMins+"</br>");
                                    out.print("workworkHour_:"+(workMins/60.00)+"</br>");
                                }
                            }
                        }
                    }
                }else if(i==qjrqList.size()){
                    if(StringUtils.isNotBlank(userid) && StringUtils.isNotBlank(kq_date)){
                        KQWorkTime kqWorkTime = new KQWorkTime();
                        WorkTimeEntity workTime = kqWorkTime.getWorkTime(userid, kq_date);
                        if(workTime !=null){
                            int workMins = workTime.getWorkMins();
                            String beginTime = "";
                            String endTime = "" ;
                            List<TimeScopeEntity> timeScopeEntityList =  workTime.getWorkTime();
                            for(TimeScopeEntity timeScopeEntity : timeScopeEntityList){
                                beginTime = timeScopeEntity.getBeginTime();
                                endTime = timeScopeEntity.getEndTime();
                            }
                            if(StringUtils.isNotBlank(beginTime) && StringUtils.isNotBlank(endTime)){
                                if(qjjssj.compareTo(beginTime) > 0){
                                    total_workMins+=workMins ;
                                    out.print("workMins:"+workMins+"</br>");
                                    out.print("workworkHour_:"+(workMins/60.00)+"</br>");
                                }
                            }
                        }
                    }
                }else{
                    if(StringUtils.isNotBlank(userid) && StringUtils.isNotBlank(kq_date)){
                        KQWorkTime kqWorkTime = new KQWorkTime();
                        WorkTimeEntity workTime = kqWorkTime.getWorkTime(userid, kq_date);
                        if(workTime !=null){
                            int workMins = workTime.getWorkMins();
                            total_workMins+=workMins ;
                            out.print("workMins:"+workMins+"</br>");
                            out.print("workworkHour_:"+(workMins/60.00)+"</br>");
                        }
                    }
                }
            }
        }
    }
    out.print("total_workMins:"+total_workMins+"</br>");
    out.print("total_workMins_:"+(total_workMins/60.00)+"</br>");
//    KQWorkTime kqWorkTime = new KQWorkTime();
//    WorkTimeEntity workTime = kqWorkTime.getWorkTime(userid, kq_date);
//    if(workTime !=null){
//       out.println("SerialId:"+workTime.getSerialId()+"</br>");
//        out.println("WorkMins:"+workTime.getWorkMins()+"</br>");
//        out.println("GroupName:"+workTime.getGroupName()+"</br>");
//        int workMins = workTime.getWorkMins();
//        DecimalFormat df = new DecimalFormat("#.##");
//        String workHour = df.format(workMins/60.00);
//
//        out.println("workHour1:"+(workMins/60.00)+"</br>");
//        out.println("workHour2:"+workHour+"</br>");
//    }
//    List<TimeScopeEntity> timeScopeEntityList =  workTime.getWorkTime();
//    for(TimeScopeEntity timeScopeEntity : timeScopeEntityList){
//        out.println("BeginTime:"+timeScopeEntity.getBeginTime()+"</br>");
//        out.println("EndTime:"+timeScopeEntity.getEndTime()+"</br>");
//        out.println("WorkMins:"+timeScopeEntity.getWorkMins()+"</br>");
//    }



//    int workmins = 0;
//    String serialid = "";
//    if("3".equals(minimumUnit)){
//        if(leavetime > 0){
//            if(StringUtils.isNotBlank(userid) && StringUtils.isNotBlank(kq_date)){
//                KQWorkTime kqWorkTime = new KQWorkTime();
//                WorkTimeEntity workTime = kqWorkTime.getWorkTime(userid, kq_date);
//                if(workTime !=null){
//                    int workMins = workTime.getWorkMins();
//                    DecimalFormat df = new DecimalFormat("#.##");
//                    workHour = df.format(leavetime/(workMins/60.00));
//
//                    workmins = workTime.getWorkMins();
//
//                    serialid = workTime.getSerialId();
//
//                }
//            }
//        }
//    }

    DecimalFormat df = new DecimalFormat("#.##");
    workHour = df.format(leavetime/(total_workMins/60.00));
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("workhour",workHour);
    jsonObject.put("workmins",total_workMins);
    jsonObject.put("leavetime",leavetime);

%>
<%=jsonObject.toJSONString() %>