zoukankan      html  css  js  c++  java
  • 报表统计——java实现查询某年某月每天数据,没数据补0

    一般图表绘制例如echarts等,返回数据格式都大同小异。重点是利用sql或者java实现数据格式的转型,接下来是关键部分:

    1.前提:提供的工具方法——获取某月有多少天

    //通过年份和月份确定该月的最后一天
        public static int getMaxDay(int year,int month ){
    
            Calendar time=Calendar.getInstance();
            time.clear();
            time.set(Calendar.YEAR,year); //year 为 int 
            time.set(Calendar.MONTH,month-1); //month 为int
             return time.getActualMaximum(Calendar.DAY_OF_MONTH);
        }

    2.mapper层sql语句书写

    <select id="getAllOrderCountByYearAndMonth" parameterType="pd" resultType="OrderCount" >
            SELECT 
                sum(t.ordercount) ordercount,
                t.[month] month ,
                t.[day] day
            from 
                order t
            where 1=1 
            <if test="year != null and year != ''">
                    and t.year=convert(int, #{year}) 
            </if>
            <if test="month != null and month != ''">
                    and t.month=convert(int, #{month}) 
            </if>
            GROUP BY 
                t.[month],t.[day]
            HAVING 
                1=1
            ORDER BY t.[day] asc
        </select>

    3.service层实现,调用sql返回结果,及对其返回结果进行格式转换(重要)

    Map<String,String> resultMap = new HashMap<String,String>();
        //获取到数据库搜索的年份对应某月份的31天订单量
        List<OrderCount> orderCountList = orderCountManager.getAllOrderCountByYearAndMonth(pd);
        //确定某个月的天数(如果日期截止只要到有数据的最大日期,那么可以修改sql语句排列方式,例如,日期从大到小排列,那么就是位置在0上的数据是最大天数,很简单实现,此处不列出来了)
        int days = getMaxDay(Integer.parseInt(pd.get("year").toString()),Integer.parseInt(pd.get("month").toString()));
        //定义数组,默认都是0
        int[] countVIP = new int[days];
        //将获取到不同年份不同月份不同日期对应不同的订单量放在数组中
        if (orderCountList.size()>0 && orderCountList!=null) {
            for (int i=0;i<orderCountList.size();i++) {
                OrderCount oc = orderCountList.get(i);
                //获取对应日期补充数据
                if(oc!=null){
                    countVIP[oc.getDay()-1] = oc.getOrderCount();
                }
            }
        }
        resultMap.put("orderStatistics", countVIP);

     优化部分

    接下来是后期我们的需求优化,即,数据只获取到当前日期,当前月之前月份日期数据要补充满,当前日期数据补充到下单截止日期;当前年份之前年份12个月要补全,当前年份数据补充到下单截止月份。具体如下:

    @Override
      public YJLResponseModel getOrderNewDayCountByYearAndMonth(String condtionStr) {
        setYjlResponseModel(new YJLResponseModel());
        PageData pageData = JSON.parseObject(condtionStr, PageData.class);
        //按年+月获取每一天数据
        List<PageData> orderCountList = ordOrderNewMapper.getOrderNewDayCountByYearAndMonth(pageData);
        //初始化====================
        //java获取当前年份
        Calendar cale = Calendar.getInstance();
        int currentYear = cale.get(Calendar.YEAR);
        int currentMonth= cale.get(Calendar.MONTH)+ 1;
        //判断是否是当前年月,不是的话取满月,是的话,取当前截止日期
        int argYear = Integer.parseInt(pageData.getString("year"));
        int argMonth = Integer.parseInt(pageData.getString("month"));
        //定义最大天数,默认取满天
        int maxDays= getMaxDay(argYear,argMonth);
        //如果有数据,补充数据===================
        if(orderCountList!=null && orderCountList.size()>0) {
          if (argYear == currentYear && argMonth == currentMonth) {
            //获取最后一天日期
            PageData lastDay = orderCountList.get(orderCountList.size() - 1);
            //获取截止天数
            maxDays = Integer.parseInt(lastDay.get("day").toString());
          }
        }
        //定义最大天数数组
        int[] count = new int[maxDays];
        int[] days = new int[maxDays];
        //补全日期到数组中
        for(int i=0;i<maxDays;i++){
          days[i] = i+1;
        }
        if(orderCountList!=null && orderCountList.size()>0) {
          //将获取到不同年份不同月份不同日期对应不同的订单量放在数组中
          for (int i=0;i<orderCountList.size();i++) {
            PageData oc = orderCountList.get(i);
            //获取对应天
            if(oc!=null){
              count[Integer.parseInt(oc.get("day").toString())-1] = Integer.parseInt(oc.get("count").toString());
            }
          }
        }
        pageData.put("count",count);
        pageData.put("days",days);
        getYjlResponseModel().setData(pageData);
        getYjlResponseModel().setSuccess(true);
        return getYjlResponseModel();
      }
    
      @Override
      public YJLResponseModel getOrderNewDayCountByYear(String condtionStr) {
        setYjlResponseModel(new YJLResponseModel());
        PageData pageData = JSON.parseObject(condtionStr, PageData.class);
    
        //按年获取每一天数据
        List<PageData> orderCountList = ordOrderNewMapper.getOrderNewDayCountByYear(pageData);
        //初始化数据==============================
        //定义最大月数数组,默认除了当前年之前年是12个月,当期年是截止月
        //获取传参年数
        int argYear = Integer.parseInt(pageData.getString("year"));
        //java获取当前年份
        Calendar cale = Calendar.getInstance();
        int currentYear = cale.get(Calendar.YEAR);
        //定义某年月数,默认12
        int maxMonths = 12;
        //优化数据,补充数据=======================
        if(orderCountList!=null && orderCountList.size()>0){
          if(argYear==currentYear){
              //获取最后一个月份
              PageData lastMonth = orderCountList.get(orderCountList.size()-1);
              //获取截止月份
              maxMonths= Integer.parseInt(lastMonth.get("month").toString());
          }
        }
        //定义返回数据格式,默认都是0
        int[] count = new int[maxMonths];
        int[] months = new int[maxMonths];
        //补全月份到数组中
        for(int i=0;i<maxMonths;i++){
          months[i] = i+1;
        }
        if(orderCountList!=null && orderCountList.size()>0){
          //将获取到不同年份不同月份对应的订单量放在数组中
          for (int i=0;i<orderCountList.size();i++) {
            PageData oc = orderCountList.get(i);
            //获取对应天
            if(oc!=null){
              count[Integer.parseInt(oc.get("month").toString())-1] = Integer.parseInt(oc.get("count").toString());
            }
          }
        }
        pageData.put("count",count);
        pageData.put("month",months);
        getYjlResponseModel().setData(pageData);
        getYjlResponseModel().setSuccess(true);
        return getYjlResponseModel();
      }
  • 相关阅读:
    开发中常用的JS知识点集锦
    浏览器音频兼容和ffmpeg的音频转码使用
    web页面和小程序页面实现瀑布流效果
    微信小程序之支付密码输入demo
    Mac安装nginx配置过程
    前端工具mock的使用
    汇编语言学习
    Swift学习笔记
    如何快速融入团队并成为团队核心(四)
    如何快速融入团队并成为团队核心(三)
  • 原文地址:https://www.cnblogs.com/yangyuke1994/p/10017661.html
Copyright © 2011-2022 走看看