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

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

    String zzlx = "3";
    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);
    }

    int yjjg_total = 0;
    int ejjg_total = 0 ;
    for(int i=0;i<cityList.size();i++){
        Map<String,Object> cityMap = cityList.get(i);
        String yjjg = "0";
        String ejjg = "0";

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

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

        String cityKey = Util.null2String(cityMap.get("key")).replace("City","");
        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("3".equals(zzdj)){
                yjjg = cou;
            }else if("4".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 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+"' ";
            }

            String dist_yjjg = "0";
            String dist_ejjg = "0";
            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("3".equals(zzdj)){
                    dist_yjjg = cou;
                }else if("4".equals(zzdj)){
                    dist_ejjg = cou;
                }
            }

            districtMap.put("yjjg",dist_yjjg);
            districtMap.put("ejjg",dist_ejjg);
        }
        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);

    cityList.add(totalMap);

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

%>

<%=dataJson.toJSONString() %>

