<%@ page import="weaver.general.*" %>
<%@ 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);
	bb.writeLog("dataJson",dataJson);

    String zjkqnj = "0.00";
    String lnkqnj = "0.00";
    String jnkqnj = "0.00";

    String ycqts = "0.00";
    String wcts = "0.00" ;
    double xjts = 0.00;
    int cdsc = 0;
    int qkcs = 0;
    String dkcs = "0.00";
    StringBuffer njxx = new StringBuffer() ;
    int count = 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("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;
                                }
                            }
                        }
                    }
                }


                if(dataJson.containsKey("groupitem4")){
                    JSONObject groupitem4 = dataJson.getJSONObject("groupitem4");
                    bb.writeLog("groupitem4:"+groupitem4);
                    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.trim()) || title.trim().contains("年假")){
                                JSONArray itemArray = itemJson.getJSONArray("item");
                                for(int k=0;k<itemArray.size();k++){
                                    JSONObject itemData = itemArray.getJSONObject(k);
                                    bb.writeLog("itemData:"+itemData);

                                    String itemTitle = itemData.getString("title");

                                    String itemValue = itemData.getString("value");
                                    if("总计可请小时数".equals(itemTitle)){
                                        zjkqnj = itemValue;
                                        count++;
                                    }

                                    if("历年可请小时数".equals(itemTitle)){
                                        lnkqnj = itemValue;
                                        count++;
                                    }

                                    if("今年可请小时数".equals(itemTitle)){
                                        jnkqnj = itemValue;
                                        count++;
                                    }

                                    if(k==itemArray.size()-1){
                                        njxx.append(itemTitle+":"+itemValue+"小时");
                                    }else{
                                        njxx.append(itemTitle+":"+itemValue+"小时,");
                                    }
                                }
                            }
                        }
                    }

                    if(count == 0){
                        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){

                        }
                    }
                }
            }
        }
    }

    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.put("njxx",njxx.toString());
    jsonObject.put("count",count);

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


<%!

    /***
     *
     * @param userid
     * @param itemid
     * @return
     */
    private String getQuerySql(String userid,String itemid) {

        BaseBean bb = new BaseBean();
        StringBuilder sqlSB = new StringBuilder();

        if(StringUtils.isNotEmpty(itemid))
        {
            sqlSB.append(" select e.id as itemdataid,e.userid,e.itemvalue,e.otherinfo as itemdata,d.* from (SELECT a.moduleid,a.name ,b.subname,b.subid ,b.iconname,b.iconcolor,b.iconbgcolor,c.id as itemid,c.iszeroshow,c.name as itemname,c.tipsinfo,c.isability,c.isnegative,c.overcolor,c.showorder")
                    .append(" FROM wr_individualitem_module a join wr_individualitem_submodule b on a.moduleid = b.moduleid ")
                    .append(" join wr_individualitem_info c on b.subid = c.subid ")
                    .append(" where a.status = 1 and b.status = 1 and  c.status = 1 ");
            if (!CommonUtil.hasSubordinate(userid)) {
                sqlSB.append(" and a.moduleid != 3 ");
            }
            String shareSql = "";
            try {
                shareSql = PortraitUtil.getShareBizidSql("10",userid);
            } catch (Exception e) {
                bb.writeLog("获取shareSql失败,userid="+userid+",type=10,"+e);
            }
            if (StringUtils.isNotEmpty(shareSql)) {
                sqlSB.append(" and b.subid in (").append(shareSql).append(")");
            }
            sqlSB.append(" ) d join wr_individualitem_detail e on e.itemid = d.itemid ").append(" WHERE e.userid = ? and e.type = ? ");
            sqlSB.append(" and d.itemid = "+itemid);
            sqlSB.append(" order by d.moduleid asc,d.showorder asc");
        }
        return sqlSB.toString();
    }

%>

<%!

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

%>



