查询近几天统计数据,没数据的补0

例1:
@Override
public MyResult findCityNetDetect(HttpServletRequest request, HttpServletResponse response, String year,
String type, String locName) {
try {
// TODO 设置状态等
//传个年,查询每个月的省/市的统计数量,按省/市分组,某个省的个数
List retList = new ArrayList();
String ii = year + “-01”;//查询条件中的日期
String ii2 = Integer.parseInt(year) + “-12”;//查询条件中的日期

        List<Map> lists = dataCleanDao.findCityNetDetect(ii,ii2, type, locName);//返回一个map

        List datelist = new ArrayList<>();
        for (int i = 0; i < lists.size(); i++) {
            String at = lists.get(i).get("date").toString();
            if(!StringUtils.isNullOrEmpty(at) && (at.indexOf("-")+1) < at.length()-1){
                datelist.add(Integer.parseInt(at.substring(at.indexOf("-")+1)));
            }
        }
        for (int i = 1; i < 13; i++) {
            if(!datelist.contains(i)){
                Map map = new HashMap();
                map.put("date", year + "-" + (i<10?("0" + i):("" + i)));
                map.put("count", 0);
                lists.add(map);
            }
        }

        return MyResult.ok(lists);
    } catch (Exception e) {
        e.printStackTrace();
        return MyResult.error("查询失败");
    }
}

例2:
@Override
public MyResult servicePubTrend(HttpServletRequest request, HttpServletResponse response) {
//用户ID, 当前时间
String userId = getUserId(request, response);
if (userId==null)
return MyResult.error(“更新失败,请登录!”);
List list = enterpriseServiceDao.servicePubTrend(userId);
List datelist = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
datelist.add(list.get(i).get(“TIME_ADD”));
}
Calendar now = Calendar.getInstance();
String dat = now.get(Calendar.YEAR)+”-“+((now.get(Calendar.MONTH) + 1)>9?(now.get(Calendar.MONTH) + 1):”0”+(now.get(Calendar.MONTH) + 1))+”-“;

    now.add(now.DATE, -6);
    for (int i = 0; i <=6; i++) {
        int da = now.get(Calendar.DAY_OF_MONTH);//xx天
        String daa = dat + (da>9?da:"0"+da);
        if(!datelist.contains(daa)){
            Map map = new HashMap();
            map.put("TIME_ADD", daa);
            map.put("count", 0);
            list.add(map);
        }
        now.add(now.DATE, 1);
    }
    return MyResult.ok(list);
}

例3:
@Override
public MyResult findPCXZ(HttpServletRequest request, HttpServletResponse response, String year, String circle,
String colum, String type, String locName) {
try {
// TODO 设置状态等
//传个年,查询每个月的省/市的统计数量,按省/市分组,某个省的个数
//circle的代表按年统计(返回12个月的),最近7天(今天往前推6天)
List retList = new ArrayList();
String ii = year + “-01”;//查询条件中的日期
String ii2 = Integer.parseInt(year) + “-12”;//查询条件中的日期

        String str = "信用动态,政策法规,信息公示,联合奖惩,红名单,黑名单,政务诚信,红黑名单";
        List<String> xxList = new ArrayList<String>();
        String[] ss = str.split(",");
        for (int i = 0; i < ss.length; i++) {
            xxList.add(ss[i]);
        }
        byte[] bytes = locName.getBytes("ISO-8859-1");
        String locName_  = new String(bytes,"utf-8");
        /*List<Map> list = dataCleanDao.findPCXZ(ii,ii2,type, locName_,xxList);//返回一个map*/
        List<Map> list = dataCleanDao.findPCXZ(ii,ii2,type, locName,xxList);//返回一个map

        List datelist = new ArrayList<>();
        List collist = new ArrayList<>();//放列
        for (int i = 0; i < list.size(); i++) {
            String at = list.get(i).get("date").toString();
            String colums = list.get(i).get("colum").toString();
            if(!StringUtils.isNullOrEmpty(at) && (at.indexOf("-")+1) < at.length()-1){
                if(!datelist.contains(Integer.parseInt(at.substring(at.indexOf("-")+1)))){
                    datelist.add(Integer.parseInt(at.substring(at.indexOf("-")+1)));
                }
            }
        }
        String temp = (String) list.get(0).get("date");//标记省/市

        Map mp = new HashMap<>();
        Map mp2 = new HashMap<>();
        List lists = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            if(type.equals("0") && !(list.get(i).get("date")).equals("")){
                if(!temp.equals(list.get(i).get("date"))){
                    temp = (String) list.get(i).get("date");
                    mp = new HashMap<>();
                    if(!"".equals(list.get(i-1).get("date"))){
                        mp.put("date", list.get(i-1).get("date"));
                        for (int j = 0; j < xxList.size(); j++) {
                            String key = (String) ztmap.get(xxList.get(j)); 
                            if(mp2.get(key) != null){
                                mp.put(key,mp2.get(key));
                            }
                        }
                        lists.add(mp);
                        mp2 = new HashMap<>();
                    }
                }
                String key = (String) ztmap.get(list.get(i).get("colum"));
                if(key != null){
                    mp2.put(key, list.get(i).get("count"));
                }
            }else if(type.equals("1") && !(list.get(i).get("date")).equals("")){
                if(!temp.equals(list.get(i).get("date"))){
                    temp = (String) list.get(i).get("date");
                    mp = new HashMap<>();
                    if(!"".equals(list.get(i-1).get("date"))){
                        mp.put("date", list.get(i-1).get("date"));
                        for (int j = 0; j < xxList.size(); j++) {
                            String key = (String) ztmap.get(xxList.get(j)); 
                            if(mp2.get(key) != null){
                                mp.put(key,mp2.get(key));
                            }
                        }
                        lists.add(mp);
                        mp2 = new HashMap<>();
                    }
                }
                String key = (String) ztmap.get(list.get(i).get("colum"));
                if(key != null){
                    mp2.put(key, list.get(i).get("count"));
                }
            }else{
                continue;
            }
            if(i == list.size()-1){
                String type_ = type.equals("0")?"province":"city"; 
                if(!temp.equals(list.get(i).get(type_))){
                    mp = new HashMap<>();
                    if(!"".equals(list.get(i).get(type_))){
                        mp.put("date", list.get(i).get("date"));
                        for (int j = 0; j < xxList.size(); j++) {
                            String key = (String) ztmap.get(xxList.get(j)); 
                            if(mp2.get(key) != null){
                                mp.put(key,mp2.get(key));
                            }
                        }
                        lists.add(mp);
                    }
                }
            }
        }


        int mon = 1;//月份
        int col = 0;//列索引
        //月必须存在,其次看列存在

// 信用动态,政策法规,信息公示,联合奖惩,红名单,黑名单,政务诚信,红黑名单
String colkey = “xydt,zcfg,xxgs,lhjc,hongmd,heimd,zwcx,hhmd”;
List cxList = new ArrayList();
String[] sss = colkey.split(“,”);
for (int i = 0; i < sss.length; i++) {
cxList.add(sss[i]);
}
List lists_ = new ArrayList();
for (int i = 1; i < 13; i++) {
if(datelist.contains(i) && i<=lists.size()){
Map map_ = (Map) lists.get(i-1);
for (int j = 0; j < cxList.size(); j++) {
String key = cxList.get(j);
if(map_.get(key) == null){
map_.put(key, 0);
}
}
lists_.add(map_);
}else{
Map map = new HashMap();
map.put(“date”, year + “-” + (i<10?(“0” + i):(“” + i)));
for (int j = 0; j < cxList.size(); j++) {
String key = cxList.get(j);
map.put(key, 0);
}
lists_.add(map);
}
}

        return MyResult.ok(lists_);
    } catch (Exception e) {
        e.printStackTrace();
        return MyResult.error("查询失败");
    }
}

}

猜你喜欢

转载自blog.csdn.net/qq_28710139/article/details/81777427