栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java后端如何处理柱状图关于筛选日期范围内没数据的情况下,返回所有日期,没数据返回0,有数据则填充对应的值

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java后端如何处理柱状图关于筛选日期范围内没数据的情况下,返回所有日期,没数据返回0,有数据则填充对应的值

效果图如下:

代码如下:

		//统计时间范围的数组
        List  datList = RangeDateUtil.allRangeDate(startDate, endDate);
        if(CollectionUtil.isEmpty(datList)){
            return new ArrayList<>();
        }
        //查询时间范围内复合条件的所有数据
        List trusteeshipInfoVOS =  wxInsureTrusteeshipInfoMapper.getTreatmentNumber(startDate,endDate);
        //new一个返回的结果数组
        List result = new ArrayList<>();
        //当日期数组不为空时,则开始遍历所有日期的每一天
        for(String date : datList){
            //判断一下查询的结果数组是否为空
            if(CollectionUtil.isEmpty(trusteeshipInfoVOS)){
                //当开始的第一天数据为空时,则将数据对象里面的值除了日期,其他全部初始化为0,然后添加到结果返回数组中
                result.add(initTrusteeshipInfoVO(date));
                continue;
            }
            //遍历查出来的数据,填充进日期
            boolean hasValue = false;
            for(TrusteeshipInfoVO trusteeshipInfoVO : trusteeshipInfoVOS){
                if(date.equals(trusteeshipInfoVO.getDateTime().substring(0,10))){
                    trusteeshipInfoVO.setDateTime(trusteeshipInfoVO.getDateTime().substring(0,10));
                    result.add(trusteeshipInfoVO);
                    hasValue = true;
                    break;
                }
            }
            if(!hasValue) {
                result.add(initTrusteeshipInfoVO(date));
            }
        }
        return result;
    }

    
     private TrusteeshipInfoVO initTrusteeshipInfoVO(String date){
         TrusteeshipInfoVO trusteeshipInfoVO = new TrusteeshipInfoVO();
         trusteeshipInfoVO.setCustomerCount(0);
         trusteeshipInfoVO.setEmployeeCount(0);
         trusteeshipInfoVO.setDateTime(date);
         return trusteeshipInfoVO;
    }


用到的工具类如下:

    
    public static List allRangeDate(String startDateStr, String endDateStr) {
        List listDate = new ArrayList<>();
        DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter df2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        try {
            LocalDateTime startDate = LocalDateTime.parse(startDateStr, df1);
            LocalDateTime endDate = LocalDateTime.parse(endDateStr, df1);
            LocalDateTime tempDate = null;
            while (!(LocalDateTime.of(startDate.plusDays(-1).toLocalDate(), LocalTime.MIN)
                    .equals(LocalDateTime.of(endDate.toLocalDate(), LocalTime.MIN)))) {
                tempDate = startDate;
                String format = tempDate.format(df2);
                listDate.add(format);
                startDate = startDate.plusDays(1);
            }
            System.out.println(listDate.toString());
            return listDate;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/356447.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号