![39d37316b64391ed3e700cef35d36475.png](https://i-blog.csdnimg.cn/blog_migrate/ddfaf00f803e2672c01c4581f31acea7.jpeg)
公司是前后端分离开发,考虑到前端的朋友更方便取值,本来写的查询方法返回的是下面的对象格式,所以要改成 ECharts 实例上的格式。
{
"code": 1000,
"total": null,
"rows": null,
"message": "SUCCESS",
"data": [
{
"remarks": null,
"createDate": null,
"updateDate": null,
"delFlag": 0,
"id": null,
"msgId": null,
"msgType": 1,
"msgName": null,
"msgContent": null,
"result": null,
"csvFile": null,
"receiveUser": null,
"userName": null,
"userEmail": null,
"userPhone": null,
"isView": null,
"count": 107,
"success": null,
"fail": null,
"no": null
},
{
"remarks": null,
"createDate": null,
"updateDate": null,
"delFlag": 0,
"id": null,
"msgId": null,
"msgType": 2,
"msgName": null,
"msgContent": null,
"result": null,
"csvFile": null,
"receiveUser": null,
"userName": null,
"userEmail": null,
"userPhone": null,
"isView": null,
"count": 103,
"success": null,
"fail": null,
"no": null
}
]
}
上面的返回结果就是方法 List<Object> 接收 SQL resultMap=实体类Map 的结果直接返回,但是 ECharts 实例的格式是这样的。↓
![65d7ca6af0532238c42fd5fb6c8827e2.png](https://i-blog.csdnimg.cn/blog_migrate/f586a454cb625929f28f40ca4aa13a9f.jpeg)
每一类的呈一个数组,比如上图中的直接访问下面的数组对应了一周七天分别的访问数,那么我需要同样返回呈数组而不是对象。于是做了如下处理(截取方法一部分):
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
Calendar cld = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.MONTH, 0);
cld.setTime(date);
cld.set(Calendar.DAY_OF_MONTH, 1);
cld.add(Calendar.MONTH, 1);
Date currentDate = calendar.getTime(); // 获得当月一号
Date nextDate = cld.getTime(); // 获得下月一号 默认查询当月记录
startDate = dateFormat.format(currentDate);
endDate = dateFormat.format(nextDate);
// --------↑获取当月一号和下月一号↑--------
List<PushHistory> result = this.pushHistoryService.selectHistoryCountByMonth(startDate, endDate);
String[] msgType = new String[result.size()]; // 消息类别
Integer[] count = new Integer[result.size()]; // 消息总数
Integer[] success = new Integer[result.size()]; // 发送成功
Integer[] fail = new Integer[result.size()]; // 发送失败
Integer[] no = new Integer[result.size()]; // 未发送
int i = 0;
for (PushHistory res : result) { // 遍历集合得到 List 属性值
switch (res.getMsgType()) {
case 1:
msgType[i] = "微信";
break;
case 2:
msgType[i] = "钉钉";
break;
case 3:
msgType[i] = "QQ邮箱";
break;
case 4:
msgType[i] = "163邮箱";
break;
case 5:
msgType[i] = "内部服务";
break;
}
count[i] = res.getCount();
success[i] = res.getSuccess();
fail[i] = res.getFail();
no[i] = res.getNo();
i++;
}
return ResponseEntity.ok(new BaseResultDh(1000, "SUCCESS", msgType, count, success, fail, no));
先定义了五个数组,分别对应了各消息类别、各个状态数量,再用 foreach 循环得到每个对象里对应字段的值,将值循环添加到事先定义好的数组,再直接返回这几个数组,就能得到 ECharts 需要的格式,前端就可以直接拿来用了。↓
{
"code": 1000,
"total": null,
"rows": null,
"message": "SUCCESS",
"data": null,
"msgType": [
"微信",
"钉钉",
"163邮箱"
],
"counts": [
4,
1,
1
],
"successes": [
2,
1,
1
],
"fails": [
1,
0,
0
],
"nos": [
1,
0,
0
]
}
上面代码为什么要获取当月第一天和次月第一天的日期,因为 SQL 用了 BETWEEN...AND... 来方便查询时间段,而默认是查询当月的,所以就需要限制当月下月之间。↓
<select id="selectHistoryCountByMonth" resultMap="pushMap">
SELECT
p.msg_type AS "msgType",
COUNT( p.msg_type ) AS "count",
SUM( IF ( p.csv_file = 0, 1, 0 ) ) AS "success" ,
SUM( IF ( p.csv_file = 1, 1, 0 ) ) AS "fail",
SUM( IF ( p.csv_file = 2, 1, 0 ) ) AS "no"
FROM
message_push_history p
WHERE
p.del_flag = 0
<!--<if test="endDate != null and endDate != ''">-->
AND p.create_date BETWEEN #{startDate} AND #{endDate}
<!--</if>-->
GROUP BY
p.msg_type
</select>
另外,return 的统一返回结果类 BaseResultDh 是我自己新建的类,原本 BaseResult 没有几个数组,需要自己另行添加:
private String[] msgType;
private Integer[] counts;
private Integer[] successes;
private Integer[] fails;
private Integer[] nos;
至此,ECharts 图表返回数组,循环取值添加数组元素就结束了。