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

springboot数据统计

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

springboot数据统计

业务代码
    
    @Override
    public DataDateStatistics sortDataCount(String year) {
        //按年或者按月统计
        if (StringUtils.isBlank(year)){
            //统计前十年数据
            Calendar cal = Calendar.getInstance();
            int finishYear = cal.get(Calendar.YEAR);
            int beginYear = finishYear - 9;
            List xAxis = new ArrayList<>();
            while(beginYear < finishYear + 1){
                xAxis.add(beginYear);
                beginYear += 1;
            }
            //todo 按名称排序
            List sortDataCounts = dataStatisticsMapper.sortDataCountYear();

            return dataConvert(sortDataCounts,xAxis);
        }else {
            //获取月份
            List xAxis = new ArrayList<>();
            int month = 1;
            while(xAxis.size() < 12){
                xAxis.add(month);
                month += 1;
            }
            List sortDataCounts = dataStatisticsMapper.sortDataCountMonth(year);

            return dataConvert(sortDataCounts, xAxis);
        }
    }

    
    public static DataDateStatistics dataConvert(List sortDataCounts, List xAxis){
        DataDateStatistics dataDateStatistics = new DataDateStatistics();
        List dataInfos = new ArrayList<>();

        for (SortDataCount sortDataCount : sortDataCounts) {
            if (CollectionUtil.isEmpty(dataInfos) ||
                    !sortDataCount.getName().equals(dataInfos.get(dataInfos.size()-1).getTitle())){
                DataInfo dataInfo = new DataInfo();

                //初始化y轴数据
                List yAxis = new ArrayList<>();
                while (yAxis.size() < xAxis.size()) {
                    yAxis.add(0);
                }
                dataInfo.setYAxis(yAxis);
                dataInfo.setTitle(sortDataCount.getName());
                int index = xAxis.indexOf(sortDataCount.getDate());
                dataInfo.getYAxis().set(index, sortDataCount.getValue());

                dataInfos.add(dataInfo);
            }else {
                DataInfo dataInfo = dataInfos.get(dataInfos.size() - 1);
                int index = xAxis.indexOf(sortDataCount.getDate());
                dataInfo.getYAxis().set(index, sortDataCount.getValue());
            }
        }
        dataDateStatistics.setXAxis(xAxis);
        dataDateStatistics.setDataInfos(dataInfos);

        return dataDateStatistics;
    }
sql语句
//按月
select count(*) as '数量',date_format(publish_time,'%Y-%m') as month from document
where publish_time BETWEEN  '2021-01-01' and '2022-01-01'
group by month;
//按年
select count(*) as '数量',date_format(publish_time,'%Y') as month from document
where publish_time BETWEEN  '2011-01-01' and '2022-01-01'
group by month;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/591184.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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