报表统计——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-); //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 =
<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
=
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()> && orderCountList!=null) {
for (int i=;i<orderCountList.size();i++) {
OrderCount oc = orderCountList.get(i);
//获取对应日期补充数据
if(oc!=null){
countVIP[oc.getDay()-] = 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)+ ;
//判断是否是当前年月,不是的话取满月,是的话,取当前截止日期
int argYear = Integer.parseInt(pageData.getString("year"));
int argMonth = Integer.parseInt(pageData.getString("month"));
//定义最大天数,默认取满天
int maxDays= getMaxDay(argYear,argMonth);
//如果有数据,补充数据===================
if(orderCountList!=null && orderCountList.size()>) {
if (argYear == currentYear && argMonth == currentMonth) {
//获取最后一天日期
PageData lastDay = orderCountList.get(orderCountList.size() - );
//获取截止天数
maxDays = Integer.parseInt(lastDay.get("day").toString());
}
}
//定义最大天数数组
int[] count = new int[maxDays];
int[] days = new int[maxDays];
//补全日期到数组中
for(int i=;i<maxDays;i++){
days[i] = i+;
}
if(orderCountList!=null && orderCountList.size()>) {
//将获取到不同年份不同月份不同日期对应不同的订单量放在数组中
for (int i=;i<orderCountList.size();i++) {
PageData oc = orderCountList.get(i);
//获取对应天
if(oc!=null){
count[Integer.parseInt(oc.get("day").toString())-] = 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 = ;
//优化数据,补充数据=======================
if(orderCountList!=null && orderCountList.size()>){
if(argYear==currentYear){
//获取最后一个月份
PageData lastMonth = orderCountList.get(orderCountList.size()-);
//获取截止月份
maxMonths= Integer.parseInt(lastMonth.get("month").toString());
}
}
//定义返回数据格式,默认都是0
int[] count = new int[maxMonths];
int[] months = new int[maxMonths];
//补全月份到数组中
for(int i=;i<maxMonths;i++){
months[i] = i+;
}
if(orderCountList!=null && orderCountList.size()>){
//将获取到不同年份不同月份对应的订单量放在数组中
for (int i=;i<orderCountList.size();i++) {
PageData oc = orderCountList.get(i);
//获取对应天
if(oc!=null){
count[Integer.parseInt(oc.get("month").toString())-] = Integer.parseInt(oc.get("count").toString());
}
}
}
pageData.put("count",count);
pageData.put("month",months);
getYjlResponseModel().setData(pageData);
getYjlResponseModel().setSuccess(true);
return getYjlResponseModel();
}
报表统计——java实现查询某年某月每天数据,没数据补0的更多相关文章
- 报表统计——java实现查询某年12个月数据,没数据补0
一般图表绘制例如echarts等,返回数据格式都大同小异.重点是利用sql或者java实现数据格式的转型,接下来是关键部分: 1.mapper层sql语句,返回统计好的月份与对应月份的数据. < ...
- java 获取 获取某年某月 所有日期(yyyy-mm-dd格式字符串)
总结一些日期常用的代码,方便以后直接拿 <code> /** * java 获取 获取某年某月 所有日期(yyyy-mm-dd格式字符串) * @param year * @param m ...
- Java练习 SDUT-1160_某年某月的天数
C语言实验--某年某月的天数 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 输入年和月,判断该月有几天? Input ...
- JAVA字符串格式化-String.format()的使用 【生成随机数补0操作】
转: JAVA字符串格式化-String.format()的使用 常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的s ...
- mysql 查询近7天数据,缺失补0
相信很多人的项目都有这种需求,就是查询近7天的记录,但是这7天总有那么几天是没数据的,所以缺失的只能补 0 下面的代码不知道能不能看懂,我简单的说一下思路 1)先查询红色字体的近7天,再转换成日期 2 ...
- sql server查询某年某月有多少天
sql语句如下: ),) date from (),,)+'-01' day) t1, ( ) t2 ),) ),,)+'%' 查询结果如下: 2017年2月共有28天,查询出28条记录.
- Java中查询某个日期下所有时间段的数据
除了利用时间段进行查询外,还有一个方法: 利用mybatis中的函数,将datetime转为date <if test="purch_date!= null and purch_dat ...
- Java获取某年某月的第一天
Java获取某年某月的第一天 1.设计源码 FisrtDayOfMonth.java: /** * @Title:FisrtDayOfMonth.java * @Package:com.you.fre ...
- Java获取某年某月的最后一天
Java获取某年某月的最后一天 1.设计源码 LastDayOfMonth.java: /** * @Title:LastDayOfMonth.java * @Package:com.you.free ...
随机推荐
- javascript之操作数组方法
掌握如何操作数组,会让你的开发变得更加高效 1.栈和队列方法(以下四个方法都改变原数组) arr.push() //接受任意类型的参数,逐个添加到数组的末尾,并返回数组的长度 改变原数组 arr.po ...
- 使用SpringSecurity保护程序安全
首先,引入依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...
- 深入理解static关键字
class A{ public int i = 10; public void show(){ System.out.printf("%d",i); } } class M{ pu ...
- 2019杭电多校6 hdu6638 Snowy Smile(二维最大矩阵和 线段树)
http://acm.hdu.edu.cn/showproblem.php?pid=6638 题意:给你一些点的权值,让找一个矩形圈住一部分点,问圈住点的最大权值和 分析:由于是稀疏图,明显要先把x, ...
- HihoCoder - 1617 方格取数
HihoCoder - 1617 (从群主那里借鉴来的, 群主好强啊) 题意:中文题不解释... 题解: dp[X][i][j] 代表的是X是坐标之和 第一个人 走到位置 dp[i][x-i] 第二个 ...
- Day003_Linux基础——系统目录结构
Linux系统的目录结构: 圆型节点代指目录,方型节点代指文件. 图中省去了很多不常用的目录与文件. 稍后单独讲/proc目录与/var目录. /usr 目录. /usr/local 用户个人安装的软 ...
- spring aop 的一个思考
问题: spring aop 默认使用jdk代理织入. 也就是我们常这样配置:<aop:aspectj-autoproxy /> 通过aop命名空间的<aop:aspectj-au ...
- Spring Cloud(三):声明式调用
声明式服务调用 前面在使用spring cloud时,通常都会利用它对RestTemplate的请求拦截来实现对依赖服务的接口调用,RestTemplate实现了对http的请求封装处理,形成了一套模 ...
- .net core Cookie的使用
缘起: 公司领导让我做一个测试的demo,功能大概是这样的:用户通过微信扫一扫登陆网站,如果用户登录过则直接进入主界面,否则就保留在登录界面. 实现方法: 首先先把网站地址生成个二维码,在扫描二维码后 ...
- Airflow: TypeError can't pickle memoryview objects
apache-airflow1.9.0 + python3 + rabbitmq + librabbitmq2.0.0 相关配置如下: broker_url = amqp://cord:123456@ ...