<%@ page import="com.engine.kq.biz.KQWorkTime" %>
<%@ page import="com.engine.kq.entity.WorkTimeEntity" %>
<%@ 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 import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="com.engine.kq.service.KQAttendanceEventService" %>
<%@ page import="com.engine.kq.service.impl.KQAttendanceEventServiceImpl" %>
<%@ page import="com.engine.common.util.ServiceUtil" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="com.alibaba.fastjson.JSON" %>
<%@ page import="java.text.DecimalFormat" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%

    RecordSet rs = new RecordSet();
    String resourceId = request.getParameter("resourceId");
    String newLeaveType = request.getParameter("newLeaveType");

    String fromDate = request.getParameter("fromDate");
    String toDate = request.getParameter("toDate");
    String fromTime = request.getParameter("fromTime");
    String toTime = request.getParameter("toTime");
    out.print("resourceId:"+resourceId+"</br>");
    out.print("newLeaveType:"+newLeaveType+"</br>");
    out.print("fromDate:"+fromDate+"</br>");
    out.print("toDate:"+toDate+"</br>");
    out.print("fromTime:"+fromTime+"</br>");
    out.print("toTime:"+toTime+"</br>");

    User user = HrmUserVarify.getUser (request , response) ;
    double total_workday =0.0;
    if(StringUtils.isNotBlank(newLeaveType)){
        String minimumUnit = "";
        String sql = " select minimumUnit from kq_LeaveRules where (isDelete is null or isDelete!=1) and id = "+newLeaveType;
        rs.executeQuery(sql);
        if(rs.next()){
            minimumUnit = Util.null2String(rs.getString("minimumUnit"));
        }
        out.print("minimumUnit:"+minimumUnit+"</br>");
        if("3".equals(minimumUnit)){
            if(fromDate.equals(toDate)){
                Map<String, Object> params = new HashMap<String,Object>();
                params.put("newLeaveType",newLeaveType);
                params.put("resourceId",resourceId);
                params.put("fromDate",fromDate);
                params.put("fromTime",fromTime);
                params.put("toDate",toDate);
                params.put("toTime",toTime);
                params.put("timestamp",System.currentTimeMillis());
                out.print("params1:"+ JSON.toJSON(params)+"</br>");
                Map<String, Object>  apidatas = getService(user).getLeaveWorkDuration(params, user);

                double duration = Util.getDoubleValue(Util.null2String(apidatas.get("duration")));
                double min_duration = Util.getDoubleValue(Util.null2String(apidatas.get("min_duration")));

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

                if(min_duration > 0){
                    if(StringUtils.isNotBlank(resourceId) && StringUtils.isNotBlank(fromDate)){
                        KQWorkTime kqWorkTime = new KQWorkTime();
                        WorkTimeEntity workTime = kqWorkTime.getWorkTime(resourceId, fromDate);
                        if(workTime !=null){
                            int workMins = workTime.getWorkMins();
                            out.print("workMins1:"+ workMins +"</br>");
                            total_workday += (min_duration/(workMins*1.00));
                            out.print("total_workday:"+ total_workday +"</br>");
                        }
                    }
                }
            }else{
                LocalDate qjksrq_date = LocalDate.parse(fromDate);
                LocalDate qjjsrq_date = LocalDate.parse(toDate);
                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());
                }
                out.print("qjrqList:"+qjrqList.size()+"</br>");

                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(resourceId) && StringUtils.isNotBlank(kq_date)){
                            KQWorkTime kqWorkTime = new KQWorkTime();
                            WorkTimeEntity workTime = kqWorkTime.getWorkTime(resourceId, kq_date);
                            if(workTime !=null){
                                int workMins = workTime.getWorkMins();
                                String endTime = "" ;
                                List<TimeScopeEntity> timeScopeEntityList =  workTime.getWorkTime();
                                for(TimeScopeEntity timeScopeEntity : timeScopeEntityList){
                                    endTime = timeScopeEntity.getEndTime();
                                }
                                out.print("kq_date:"+kq_date+"</br>");
                                if(StringUtils.isNotBlank(endTime)){
                                    Map<String, Object> params = new HashMap<String,Object>();
                                    params.put("newLeaveType",newLeaveType);
                                    params.put("resourceId",resourceId);
                                    params.put("fromDate",kq_date);
                                    params.put("fromTime",fromTime);
                                    params.put("toDate",kq_date);
                                    params.put("toTime",endTime);
                                    params.put("timestamp",System.currentTimeMillis());
                                    out.print("params2:"+ JSON.toJSON(params)+"</br>");

                                    Map<String, Object>  apidatas = getService(user).getLeaveWorkDuration(params, user);
                                    double duration = Util.getDoubleValue(Util.null2String(apidatas.get("duration")));
                                    double min_duration = Util.getDoubleValue(Util.null2String(apidatas.get("min_duration")));

                                    out.print("duration:"+ duration+"</br>");
                                    out.print("min_duration:"+ min_duration +"</br>");
                                    out.print("workMins1:"+ workMins +"</br>");
                                    if(min_duration > 0 && workMins>0){
                                        total_workday += (min_duration/(workMins*1.00));
                                    }
                                    out.print("total_workday:"+ total_workday +"</br>");
                                }
                            }
                        }

                    }else if(i==(qjrqList.size()-1)){

                        KQWorkTime kqWorkTime = new KQWorkTime();
                        WorkTimeEntity workTime = kqWorkTime.getWorkTime(resourceId, kq_date);
                        if(workTime !=null) {
                            int workMins = workTime.getWorkMins();
                            String beginTime = "";
                            List<TimeScopeEntity> timeScopeEntityList =  workTime.getWorkTime();
                            for(TimeScopeEntity timeScopeEntity : timeScopeEntityList){
                                beginTime = timeScopeEntity.getBeginTime();
                            }

                            if(StringUtils.isNotBlank(beginTime)){
                                Map<String, Object> params = new HashMap<String,Object>();
                                params.put("newLeaveType",newLeaveType);
                                params.put("resourceId",resourceId);
                                params.put("fromDate",kq_date);
                                params.put("fromTime",beginTime);
                                params.put("toDate",kq_date);
                                params.put("toTime",toTime);
                                params.put("timestamp",System.currentTimeMillis());
                                out.print("params3:"+ JSON.toJSON(params)+"</br>");
                                Map<String, Object>  apidatas = getService(user).getLeaveWorkDuration(params, user);
                                double duration = Util.getDoubleValue(Util.null2String(apidatas.get("duration")));
                                double min_duration = Util.getDoubleValue(Util.null2String(apidatas.get("min_duration")));
                                out.print("duration:"+ duration+"</br>");
                                out.print("min_duration:"+ min_duration +"</br>");
                                out.print("workMins1:"+ workMins +"</br>");
                                if(min_duration > 0 && workMins >0){
                                    total_workday += (min_duration/(workMins*1.00));
                                }
                                out.print("total_workday:"+ total_workday +"</br>");
                            }
                        }

                    }else{

                        KQWorkTime kqWorkTime = new KQWorkTime();
                        WorkTimeEntity workTime = kqWorkTime.getWorkTime(resourceId, kq_date);
                        if(workTime !=null){
                            int workMins = workTime.getWorkMins();
                            String beginTime = "";
                            String endTime = "" ;
                            if(workTime.getWorkTime() !=null){
                                List<TimeScopeEntity> timeScopeEntityList =  workTime.getWorkTime();
                                for(TimeScopeEntity timeScopeEntity : timeScopeEntityList){
                                    beginTime = timeScopeEntity.getBeginTime();
                                    endTime = timeScopeEntity.getEndTime();
                                }

                                out.print("beginTime:"+beginTime);
                                out.print("endTime:"+endTime);
                                if(StringUtils.isNotBlank(beginTime) && StringUtils.isNotBlank(endTime)){
                                    Map<String, Object> params = new HashMap<String,Object>();
                                    params.put("newLeaveType",newLeaveType);
                                    params.put("resourceId",resourceId);
                                    params.put("fromDate",kq_date);
                                    params.put("fromTime",beginTime);
                                    params.put("toDate",kq_date);
                                    params.put("toTime",endTime);
                                    params.put("timestamp",System.currentTimeMillis());
                                    out.print("params4:"+ JSON.toJSON(params)+"</br>");
                                    Map<String, Object>  apidatas = getService(user).getLeaveWorkDuration(params, user);
                                    double duration = Util.getDoubleValue(Util.null2String(apidatas.get("duration")));
                                    double min_duration = Util.getDoubleValue(Util.null2String(apidatas.get("min_duration")));
                                    out.print("workMins1:"+ workMins +"</br>");
                                    out.print("duration:"+ duration+"</br>");
                                    out.print("min_duration:"+ min_duration +"</br>");

                                    if(min_duration > 0 && workMins >0){
                                        total_workday += (min_duration/(workMins*1.00));
                                    }
                                    out.print("total_workday:"+ total_workday +"</br>");
                                }
                            }
                        }
                    }
                }
            }
        }
    }

//    if(total_workMins > 0){
//        DecimalFormat df = new DecimalFormat("#.##");
//        workHour = df.format(leavetime/(total_workMins/60.00));
//    }

    DecimalFormat df = new DecimalFormat("#.##");
    String total_days = df.format(total_workday);
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("workhour",total_days);

%>

<%!
    private KQAttendanceEventService getService(User user) {
        return (KQAttendanceEventServiceImpl) ServiceUtil.getService(KQAttendanceEventServiceImpl.class, user);
    }
%>
<%=jsonObject.toJSONString() %>