<%@ page import="weaver.general.*" %>
<%@page import="weaver.conn.RecordSet"%>
<%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page import="com.engine.portrait.util.PortraitUtil" %>
<%@ page import="com.engine.portrait.util.CommonUtil" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="java.util.*" %>
<%@ page import="com.engine.kq.service.impl.KQMyAttendanceServiceImpl" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="com.engine.common.util.ParamUtil" %>
<%@ page import="com.engine.kq.service.KQMyAttendanceService" %>
<%@ page import="com.engine.common.util.ServiceUtil" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>

<%
    BaseBean bb = new BaseBean();
    User user = HrmUserVarify.getUser(request , response);

    //resourceId=5796&typevalue=2022&loaddata=1&type=1&
    //resourceId=5796&typevalue=2022-12&loaddata=1&type=2

//    String type = Util.null2String(request.getParameter("type"));
//    String typevalue = Util.null2String(request.getParameter("typevalue"));
//    String resourceId = Util.null2String(request.getParameter("resourceId"));

//    paramMap.put("loaddata","1");
//    paramMap.put("type",type);
//    paramMap.put("typevalue",typevalue);
//    paramMap.put("resourceId",resourceId);

    Map<String,Object> apiDatas = getService(user).getHrmKQReportInfo(ParamUtil.request2Map(request), user);
    JSONObject dataJson = new JSONObject(apiDatas);

    String zjkqnj = "0.0";
    String lnkqnj = "0.0";
    String jnkqnj = "0.0";

    String ycqts = "0.0";
    String wcts = "0" ;
    double xjts = 0.0;
    int cdsc = 0;
    int qkcs = 0;
    String dkcs = "0";


    if(StringUtils.isNotEmpty(dataJson.toJSONString())){
        if(dataJson.containsKey("status")){
            if("1".equals(dataJson.getString("status"))){
                if(dataJson.containsKey("groupitem1")){
                    JSONObject groupitem1 = dataJson.getJSONObject("groupitem1");
                    if(groupitem1.containsKey("items")){
                        JSONArray items = groupitem1.getJSONArray("items");
                        for(int i=0;i<items.size();i++){
                            JSONObject itemJson = items.getJSONObject(i);
                            if(itemJson.containsKey("type") && itemJson.containsKey("value")){
                                String itemtype = itemJson.getString("type");
                                if("realWorkDays".equalsIgnoreCase(itemtype)){
                                    dkcs = itemJson.getString("value");
                                }
                            }
                        }
                    }

                    if(groupitem1.containsKey("id") && groupitem1.containsKey("value")){
                        if("workDays".equalsIgnoreCase(groupitem1.getString("id"))){
                            ycqts = groupitem1.getString("value");
                        }
                    }
                }
                if(dataJson.containsKey("groupitem2")){
                    JSONObject groupitem2 = dataJson.getJSONObject("groupitem2");
                    if(groupitem2.containsKey("items")){
                        JSONArray items = groupitem2.getJSONArray("items");
                        for(int i=0;i<items.size();i++){
                            JSONObject itemJson = items.getJSONObject(i);
                            if(itemJson.containsKey("type") && itemJson.containsKey("value")){
                                String itemtype = itemJson.getString("type");
                                if("EVECTION".equalsIgnoreCase(itemtype)){
                                    wcts = itemJson.getString("value");
                                }
                            }
                        }
                    }
                }
                if(dataJson.containsKey("groupitem3")){
                    JSONObject groupitem3 = dataJson.getJSONObject("groupitem3");
                    if(groupitem3.containsKey("items")) {
                        JSONArray items = groupitem3.getJSONArray("items");
                        for (int i = 0; i < items.size(); i++) {
                            JSONObject itemJson = items.getJSONObject(i);
                            if(itemJson.containsKey("type") && itemJson.containsKey("value")){
                                String itemtype = itemJson.getString("type");
                                if("BELATE".equalsIgnoreCase(itemtype) || "graveBeLate".equalsIgnoreCase(itemtype)){
                                    String value = itemJson.getString("value");
                                    if(StringUtils.isNotEmpty(value)){
                                        cdsc += Util.getIntValue(value,0);
                                    }
                                }

                                if("ABSENT".equalsIgnoreCase(itemtype) || "noSign".equalsIgnoreCase(itemtype)){
                                    String value = itemJson.getString("value");
                                    if(StringUtils.isNotEmpty(value)){
                                        qkcs += Util.getIntValue(value,0);
                                    }
                                }
                            }
                        }
                    }
                }
                if(dataJson.containsKey("groupitem4")){
                    JSONObject groupitem4 = dataJson.getJSONObject("groupitem4");
                    bb.writeLog("groupitem4:"+groupitem4);
                    if(groupitem4.containsKey("items")){
                        JSONArray items = groupitem4.getJSONArray("items");
                        for(int i=0;i<items.size();i++){
                            JSONObject itemJson = items.getJSONObject(i);
                            bb.writeLog("itemJson:"+itemJson);
                            if(itemJson.containsKey("title")){
                                String title = itemJson.getString("title");
                                if("年假".equals(title)){
                                    if(itemJson.containsKey("item"))
                                    {
                                        JSONArray itemArray = itemJson.getJSONArray("item");
                                        for(int k=0;k<itemArray.size();k++){
                                            JSONObject itemData = itemArray.getJSONObject(k);
                                            bb.writeLog("itemData:"+itemData);
                                            if(itemData.containsKey("title") &&  itemData.containsKey("value")){
                                                String itemTitle = itemData.getString("title");
                                                String itemValue = itemData.getString("value");
                                                if("总计可请天数".equals(itemTitle)){
                                                    zjkqnj = itemValue;
                                                }

                                                if("历年可请天数".equals(itemTitle)){
                                                    lnkqnj = itemValue;
                                                }

                                                if("今年可请天数".equals(itemTitle)){
                                                    jnkqnj = itemValue;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        if("0.0".equals(zjkqnj) && "0.0".equals(lnkqnj) && "0.0".equals(jnkqnj)){
                            try{
                                JSONObject itemJson = items.getJSONObject(1);
                                JSONArray itemArray = itemJson.getJSONArray("item");
                                JSONObject itemData = itemArray.getJSONObject(0);
                                lnkqnj = itemData.getString("value");
                                itemData = itemArray.getJSONObject(1);
                                jnkqnj = itemData.getString("value");
                                itemData = itemArray.getJSONObject(2);
                                zjkqnj = itemData.getString("value");
                            }catch (Exception e){

                            }
                        }
                    }
                }
                if(dataJson.containsKey("groupitem5")){
                    JSONObject groupitem5 = dataJson.getJSONObject("groupitem5");
                    if(groupitem5.containsKey("items")){
                        JSONArray items = groupitem5.getJSONArray("items");
                        for(int i=0;i<items.size();i++){
                            JSONObject itemJson = items.getJSONObject(i);
                            if(itemJson.containsKey("value")){
                                String value = itemJson.getString("value");
                                if(StringUtils.isNotEmpty(value)){
                                    double qj_value = Util.getDoubleValue(value);
                                    xjts += qj_value;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    JSONObject jsonObject = new JSONObject();

    jsonObject.put("zjkqnj",zjkqnj);
    jsonObject.put("lnkqnj",lnkqnj);
    jsonObject.put("jnkqnj",jnkqnj);
    jsonObject.put("wcts",wcts);
    jsonObject.put("xjts",xjts);
    jsonObject.put("cdsc",cdsc);
    jsonObject.put("qkcs",qkcs);
    jsonObject.put("dkcs",dkcs);
    jsonObject.put("ycqts",ycqts);

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

<%!

    private KQMyAttendanceService getService(User user){
        return (KQMyAttendanceService) ServiceUtil.getService(KQMyAttendanceServiceImpl.class, user);
    }

%>



