<%@ 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="com.alibaba.fastjson.JSONArray" %>
<%@ 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");

    User user = HrmUserVarify.getUser (request , response) ;
    double total_duration =0.0;
    double total_workday = 0.0 ;
    JSONArray dayArray = new JSONArray();

    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"));
        }
        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());
                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")));
                total_duration += duration;
                int workMins = 0 ;
                if(min_duration > 0){
                    if(StringUtils.isNotBlank(resourceId) && StringUtils.isNotBlank(fromDate)){
                        KQWorkTime kqWorkTime = new KQWorkTime();
                        WorkTimeEntity workTime = kqWorkTime.getWorkTime(resourceId, fromDate);
                        if(workTime !=null){
                            workMins = workTime.getWorkMins();
                            total_workday += (min_duration/(workMins*1.00));
                        }
                    }
                }

                JSONObject dayJson = new JSONObject();
                dayJson.put("duration",duration);
                dayJson.put("min_duration",min_duration);
                dayJson.put("workMins",workMins);
                dayJson.put("total_workday",total_workday);
                dayArray.add(dayJson);
            }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());
                }
                for(int i=0;i<qjrqList.size();i++){
                    String kq_date = qjrqList.get(i);
                    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 = "" ;
                                if(workTime.getWorkTime() !=null){
                                    List<TimeScopeEntity> timeScopeEntityList =  workTime.getWorkTime();
                                    for(TimeScopeEntity timeScopeEntity : timeScopeEntityList){
                                        endTime = timeScopeEntity.getEndTime();
                                    }
                                }


                                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());

                                    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")));

                                    total_duration += duration;

                                    if(min_duration > 0){
                                        total_workday += (min_duration/(workMins*1.00));
                                    }

                                    JSONObject dayJson = new JSONObject();
                                    dayJson.put("duration",duration);
                                    dayJson.put("min_duration",min_duration);
                                    dayJson.put("workMins",workMins);
                                    dayJson.put("total_workday",total_workday);
                                    dayArray.add(dayJson);

                                }
                            }
                        }

                    }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 = "";
                            if(workTime.getWorkTime() !=null){
                                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());

                                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")));

                                total_duration += duration;

                                if(min_duration > 0){
                                    total_workday += (min_duration/(workMins*1.00));
                                }

                                JSONObject dayJson = new JSONObject();
                                dayJson.put("duration",duration);
                                dayJson.put("min_duration",min_duration);
                                dayJson.put("workMins",workMins);
                                dayJson.put("total_workday",total_workday);
                                dayArray.add(dayJson);

                            }
                        }
                    }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();
                                }
                            }

                            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());

                                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")));

                                total_duration += duration;

                                if(min_duration > 0){
                                    total_workday += (min_duration/(workMins*1.00));
                                }

                                JSONObject dayJson = new JSONObject();
                                dayJson.put("duration",duration);
                                dayJson.put("min_duration",min_duration);
                                dayJson.put("workMins",workMins);
                                dayJson.put("total_workday",total_workday);
                                dayArray.add(dayJson);

                            }
                        }
                    }
                }
            }
        }
    }

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

%>

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