<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="java.util.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%

    Map<String,String> signMap = new HashMap<String,String>();
    signMap.put("总体集成","ztjc");
    signMap.put("系统咨询","xtzx");
    signMap.put("软件开发","rjkf");
    signMap.put("安防监控","afjk");
    signMap.put("运行维护","yxwh");
    signMap.put("数据恢复","sjhf");
    signMap.put("工程监理","gcjl");


    Map<String,Integer> countMap = new HashMap<String,Integer>();
    countMap.put("ztjc",0);
    countMap.put("xtzx",0);
    countMap.put("rjkf",0);
    countMap.put("afjk",0);
    countMap.put("yxwh",0);
    countMap.put("sjhf",0);
    countMap.put("gcjl",0);


    User user = HrmUserVarify.getUser(request, response);
    if(user==null) {
        response.sendRedirect("/login/Login.jsp");
        return;
    }


    String startDate = request.getParameter("startDate");
    String endDate = request.getParameter("endDate");

    String zzlx = "0";
    RecordSet rs = new RecordSet();
    List<Map<String,Object>> cityList = new ArrayList<Map<String,Object>>();
    String sql =" select selectvalue,selectname \n" +
                "   from workflow_selectitem \n" +
                "   where fieldid in(\n" +
                "       select id from workflow_billfield \n" +
                "       where billid in( select id from workflow_bill where tablename='uf_dwjbxx' ) and fieldname='szcs' \n" +
                "       and detailtable  is null\n" +
                " ) ";
    rs.executeQuery(sql);
    while (rs.next()){
        String selectvalue = rs.getString("selectvalue");
        String selectname = rs.getString("selectname");
        Map<String,Object> cityMap = new HashMap<String,Object>();
        cityMap.put("key","City"+selectvalue);
        cityMap.put("title",selectname);
        cityList.add(cityMap);
    }

    String selectnames = "";
    Iterator<Map.Entry<String,String>> iterator = signMap.entrySet().iterator();
    while (iterator.hasNext()){
        Map.Entry<String,String> entry = iterator.next();
        String key = entry.getKey();
        selectnames += StringUtils.isBlank(selectnames) ? "'" + key + "'" : ",'" + key + "'";
    }


    List<Map<String,String>> typeList = new ArrayList<Map<String,String>>();
    sql =   " select selectvalue,selectname \n" +
            " from workflow_selectitem\n" +
            " where fieldid in(\n" +
            "       select id from workflow_billfield \n" +
            "       where billid in( select id from workflow_bill where tablename='uf_bmzzzsxx' ) and fieldname='ywzl' \n" +
            "       and detailtable  is null\n" +
            " ) \n" +
            " AND selectname in("+selectnames+")" ;

    out.print("sql:"+sql+"</br>");
    rs.executeQuery(sql);
    while (rs.next()){
        String selectvalue = Util.null2String(rs.getString("selectvalue"));
        String selectname = Util.null2String(rs.getString("selectname"));

        Map<String,String> typeMap = new HashMap<String,String>();
        typeMap.put("selectname",selectname);
        typeMap.put("selectvalue",selectvalue);
        String selectType = signMap.get(selectname);
        typeMap.put("selecttype",selectType);
        typeList.add(typeMap);
    }

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

    int yjjg_total = 0;
    int ejjg_total = 0 ;
//    int ztjc_tootal = 0;
//    int xtzx_tootal = 0;
//    int rjkf_tootal = 0;
//    int afjk_tootal = 0;
//    int yxwh_tootal = 0;
//    int sjhf_tootal = 0;
//    int gcjl_tootal = 0;


    for(int i=0;i<cityList.size();i++){
        Map<String,Object> cityMap = cityList.get(i);
        String yjjg = "0";
        String ejjg = "0";
        String cityKey = Util.null2String(cityMap.get("key")).replace("City","");

        String where = "";
        if(StringUtils.isNotBlank(startDate)){
            where += " and k.fzrq >= '"+startDate+"' ";
        }

        if(StringUtils.isNotBlank(endDate)){
            where += " and k.fzrq <= '"+endDate+"' ";
        }

        sql =   " select zzdj,count(1) as cou\n" +
                " from uf_dwjbxx t\n" +
                " inner join uf_bmzzzsxx k on k.dwmc = t.id\n" +
                " where k.zszt=0\n" +
                " and k.zzlx = " + zzlx +
                " and t.szcs=" + cityKey + where +
                " group by zzdj ";

        rs.executeQuery(sql);
        while (rs.next()){
            String zzdj = Util.null2String(rs.getString("zzdj"));
            String cou = Util.null2String(rs.getString("cou"));
            if("1".equals(zzdj)){
                yjjg = cou;
            }else if("2".equals(zzdj)){
                ejjg = cou;
            }
        }

        if(StringUtils.isNotBlank(yjjg)){
            yjjg_total += Util.getIntValue(yjjg,0);
        }

        if(StringUtils.isNotBlank(ejjg)){
            ejjg_total += Util.getIntValue(ejjg,0);
        }

        cityMap.put("yjjg",yjjg);
        cityMap.put("ejjg",ejjg);

        for(int k=0;k<typeList.size();k++){
            Map<String,String> typeMap = typeList.get(k);
            String selectvalue = typeMap.get("selectvalue");
            String selecttype = typeMap.get("selecttype");
            int count = 0;
            if(StringUtils.isNotBlank(selectvalue)){
                sql = " select k.id \n" +
                        "    from uf_dwjbxx t\n" +
                        "   inner join uf_bmzzzsxx k on k.dwmc = t.id\n" +
                        "   where k.zszt=0 \n" +
                        "   and instr(','||ywzl||',',',"+selectvalue+",')>0 \n" +
                        "   and k.zzlx = " + zzlx +
                        "   and t.szcs="+cityKey + where  ;
                rs.executeQuery(sql);
                while (rs.next()){
                    count++;
                }
                int total = countMap.get(selecttype);
                total += count;
                countMap.put(selecttype,total);
            }

            cityMap.put(selecttype,count);
        }
    }

    for(int i=0;i<cityList.size();i++){
        Map<String,Object> cityMap = cityList.get(i);
        String cityKey = Util.null2String(cityMap.get("key")).replace("City","");

        List<Map<String,Object>> districtList = new ArrayList<Map<String,Object>>();

        if(StringUtils.isNotBlank(cityKey)){
            int cityKey_ = Util.getIntValue(cityKey)+1;
            sql = "select id,qm,yb from uf_qjsj where sss="+cityKey_;
            rs.executeQuery(sql);
            while (rs.next()){
                String id = Util.null2String(rs.getString("id"));
                String qm = Util.null2String(rs.getString("qm"));

                Map<String,Object> districtMap = new HashMap<String,Object>();
                districtMap.put("key","District"+id);
                districtMap.put("title",qm);
                districtList.add(districtMap);
            }
        }

        for(int k=0;k<districtList.size();k++){
            Map<String,Object> districtMap = districtList.get(k);
            String districtKey = Util.null2String(districtMap.get("key")).replace("District","");


            String where  = "";
            if(StringUtils.isNotBlank(startDate)){
                where += " and k.fzrq >= '"+startDate+"' ";
            }

            if(StringUtils.isNotBlank(endDate)){
                where += " and k.fzrq <= '"+endDate+"' ";
            }

            sql = "select zzdj,count(1) as cou\n" +
                    "from uf_dwjbxx t\n" +
                    "inner join uf_bmzzzsxx k on k.dwmc = t.id\n" +
                    "where k.zszt=0\n" +
                    "and k.zzlx = " + zzlx +
                    "and t.szqx= " + districtKey + where +
                    "group by zzdj ";

            rs.executeQuery(sql);
            while (rs.next()){
                String zzdj = Util.null2String(rs.getString("zzdj"));
                String cou = Util.null2String(rs.getString("cou"));
                if("1".equals(zzdj)){
                    districtMap.put("yjjg",cou);
                }else if("2".equals(zzdj)){
                    districtMap.put("ejjg",cou);
                }
            }


            for(int n=0;n<typeList.size();n++){
                Map<String,String> typeMap = typeList.get(n);
                String selectvalue = typeMap.get("selectvalue");
                String selecttype = typeMap.get("selecttype");
                out.print("selectvalue:"+selectvalue+" selecttype:"+selecttype+"</br>");

                int count = 0;

                if(StringUtils.isNotBlank(selectvalue)){
                    sql = " select k.id \n" +
                            "    from uf_dwjbxx t\n" +
                            "   inner join uf_bmzzzsxx k on k.dwmc = t.id\n" +
                            "   where k.zszt=0 \n" +
                            "   and instr(','||ywzl||',',',"+selectvalue+",')>0 \n" +
                            "   and k.zzlx = " + zzlx +
                            "   and t.szqx= " + districtKey + where  ;
                    out.print("sql:"+sql+"</br>");
                    rs.executeQuery(sql);
                    while (rs.next()){
                        count++;
                    }
                }
                out.print("count:"+count+"</br>");
                districtMap.put(selecttype,count);
            }
        }
        cityMap.put("subList",districtList);
    }

    Map<String,Object> totalMap = new HashMap<String,Object>();
    totalMap.put("key","total");
    totalMap.put("title","合计");
    totalMap.put("yjjg",yjjg_total);
    totalMap.put("ejjg",ejjg_total);
    totalMap.putAll(countMap);

    cityList.add(totalMap);

    JSONObject dataJson = new JSONObject();
    dataJson.put("data",cityList);

%>

<%=dataJson.toJSONString() %>

