java 利用echart统计折线图,柱状图
后端返回数据结构
前言
统计大数据
二、使用步骤
1.时间统计工具
需要引入hutool 的依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.4</version>
</dependency>
工具类
package com.ruoyi.common.utils;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* 运营中心统计一些时间的返回
*
* @author :ljs
* @date : 2022/8/17
*/
public class OperatCenterTimeUtil {
/**
* 获取时间
*
* @return
*/
public static List<String> getHours() {
List<String> list = new ArrayList<>();
String hours[] = {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14",
"15",
"16",
"17",
"18",
"19",
"20",
"21",
"22",
"23"
};
list = Arrays.asList(hours);
return list;
}
/**
* 获取本月的全部日期
*/
public static List<String> getMonthDay() {
List<String> monthDayList = new ArrayList<>();
//本月最后一天
DateTime dateTime = DateUtil.endOfMonth(new Date());
String endDateTimeStr = DateUtil.format(dateTime, "YYYY-MM-dd");
//获取最后一个字符就是天
String day = endDateTimeStr.substring(endDateTimeStr.lastIndexOf("-") + 1);
//天前面的
for (int i = 1; i <= Integer.valueOf(day); i++) {
String startStr = endDateTimeStr.substring(0, endDateTimeStr.lastIndexOf("-") + 1);
if (i < 10) {
startStr += "0" + i;
} else {
startStr += i;
}
monthDayList.add(startStr);
}
return monthDayList;
}
/**
* 获取本周天数
*/
public static List<String> getWeekDay() {
List<String> weekDayList = new ArrayList<>();
//本月最后一天
String weeks[] = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
List<String> weekList = Arrays.asList(weeks);
DateTime beginWeekDay = DateUtil.beginOfWeek(new Date());
String beginWeekDayStr = DateUtil.format(beginWeekDay, "YYYY-MM-dd");
//获取最后一个字符就是天
String beginDay = beginWeekDayStr.substring(beginWeekDayStr.lastIndexOf("-") + 1);
//查询本周的最后一天
DateTime lastWeekDay = DateUtil.endOfWeek(new Date());
String lastWeekDayStr = DateUtil.format(lastWeekDay, "YYYY-MM-dd");
//获取最后一个字符就是天
String endDay = lastWeekDayStr.substring(lastWeekDayStr.lastIndexOf("-") + 1);
//天前面的
for (int i = Integer.valueOf(beginDay); i <= Integer.valueOf(endDay); i++) {
String startStr = beginWeekDayStr.substring(0, beginWeekDayStr.lastIndexOf("-") + 1);
startStr += i;
weekDayList.add(startStr);
}
//加后缀
/* List<String> finalList = new ArrayList<>();
for (int i = 0; i < weekDayList.size(); i++) {
String day = weekDayList.get(i) + "(" + weekList.get(i) + ")";
finalList.add(day);
}*/
return weekDayList;
}
/**
* 获取本年的所有月份
*
* @param
*/
public static List<String> getYearMonth() {
List<String> yearMonthList = new ArrayList<>();
//查询本周的最后一天
DateTime lastYearMonth = DateUtil.endOfYear(new Date());
String lastYearMonthStr = DateUtil.format(lastYearMonth, "YYYY-MM");
//天前面的
for (int i = 1; i <= 12; i++) {
String startStr = lastYearMonthStr.substring(0, lastYearMonthStr.lastIndexOf("-") + 1);
if (i < 10) {
startStr += "0" + i;
} else {
startStr += i;
}
yearMonthList.add(startStr);
}
return yearMonthList;
}
/**
* 获取去年的所有月份
*
* @param
*/
public static List<String> getLastYearMonth() {
List<String> yearMonthList = new ArrayList<>();
//查询本周的最后一天
DateTime lastYearMonth = DateUtil.endOfYear(new Date());
String lastYearMonthStr = DateUtil.format(lastYearMonth, "YYYY-MM");
String year = lastYearMonthStr.substring(0, lastYearMonthStr.lastIndexOf("-"));
//获取年
//天前面的
for (int i = 1; i <= 12; i++) {
int lastYear = Integer.valueOf(year) - 1;
String startStr = String.valueOf(lastYear);
if (i < 10) {
startStr += "-" + "0" + i;
} else {
startStr += "-" + i;
}
yearMonthList.add(startStr);
}
return yearMonthList;
}
}
## 2.sql 查询年月日的统计,时间是时间戳,根据时间来分组
%H是24小时,%h是12小时
```sql
select sum(
IF( pay_status = 1, product_count, 0 )) as productCount,
count(id) as count,
sum(
IF
( pay_status = 1, pay_money, 0 )) as payMoney,
ROUND (avg(IF
( pay_status = 1, pay_money, 0 )),2) as avgMoney,FROM_UNIXTIME(created_at,'%H') as time from dz_addon_shop_order where to_days(FROM_UNIXTIME(created_at,'%Y-%m-%d')) = TO_DAYS(now()) group by time
-- 统计
-- 昨天
select sum(
IF( pay_status = 1, product_count, 0 )) as productCount,
count(id) as count,
sum(
IF
( pay_status = 1, pay_money, 0 )) as payMoney,
ROUND (avg(IF
( pay_status = 1, pay_money, 0 )),2) as avgMoney,FROM_UNIXTIME(created_at,'%H') as time from dz_addon_shop_order where DATEDIFF(now(),FROM_UNIXTIME(created_at,'%Y-%m-%d'))=1 group by time
-- 本周
select sum(
IF( pay_status = 1, product_count, 0 )) as productCount,
count(id) as count,
sum(
IF
( pay_status = 1, pay_money, 0 )) as payMoney,
ROUND (avg(IF
( pay_status = 1, pay_money, 0 )),2) as avgMoney,FROM_UNIXTIME(created_at,'%Y-%m-%d') as time from dz_addon_shop_order where YEARWEEK(date_format(FROM_UNIXTIME(created_at,'%Y-%m-%d'),'%Y-%m-%d'),1) = YEARWEEK(now(),1) group by time
-- 本月
select sum(
IF( pay_status = 1, product_count, 0 )) as productCount,
count(id) as count,
sum(
IF
( pay_status = 1, pay_money, 0 )) as payMoney,
ROUND (avg(IF
( pay_status = 1, pay_money, 0 )),2) as avgMoney,FROM_UNIXTIME(created_at,'%Y-%m-%d') as time from dz_addon_shop_order where date_format(from_unixtime(created_at),'%Y-%m') = date_format(now(), '%Y-%m') group by time
-- 本年
select sum(
IF( pay_status = 1, product_count, 0 )) as productCount,
count(id) as count,
sum(
IF
( pay_status = 1, pay_money, 0 )) as payMoney,
ROUND (avg(IF
( pay_status = 1, pay_money, 0 )),2) as avgMoney,FROM_UNIXTIME(created_at,'%Y-%m') as time from dz_addon_shop_order where YEAR(FROM_UNIXTIME(created_at,'%Y-%m-%d')) = YEAR(NOW()) group by time
-- 去年
select sum(
IF( pay_status = 1, product_count, 0 )) as productCount,
count(id) as count,
sum(
IF
( pay_status = 1, pay_money, 0 )) as payMoney,
ROUND (avg(IF
( pay_status = 1, pay_money, 0 )),2) as avgMoney,FROM_UNIXTIME(created_at,'%Y-%m') as time from dz_addon_shop_order where YEAR(FROM_UNIXTIME(created_at,'%Y-%m-%d')) = YEAR(DATE_SUB(NOW(),INTERVAL 1 YEAR)) group by time
-- 时间段
select sum(
IF( pay_status = 1, product_count, 0 )) as productCount,
count(id) as count,
sum(
IF
( pay_status = 1, pay_money, 0 )) as payMoney,
ROUND (avg(IF
( pay_status = 1, pay_money, 0 )),2) as avgMoney,FROM_UNIXTIME(created_at,'%Y-%m-%d') as time from dz_addon_shop_order where FROM_UNIXTIME(created_at,'%Y-%m-%d') between '2022-08-15' and '2022-08-17' group by time
后台接口数据整合
/**
* 订单统计返回折现图结构
*
* @author :ljs
* @date : 2022/8/17
*/
@Data
public class OrderFieldResponse implements Serializable {
@ApiModelProperty("图形的种类")
private List<String> fieldsName;
@ApiModelProperty("图形的种类的数值")
private List<SeriesDataResponse> seriesData;
@ApiModelProperty("图形的种类的数值")
private List<String> xAxisData;
}
对应的mapper
`` /**
* 获取今天的订单总数
*/
List<OrderSumResponse> getToday();
/**
* 获取昨天的订单总数
*/
List<OrderSumResponse> getYesterday();
/**
* 获取本周的订单总数
*/
List<OrderSumResponse> getWeek();
/**
* 获取本月的订单总数
*/
List<OrderSumResponse> getMonth();
/**
* 获取本年的订单总数
*/
List<OrderSumResponse> getYear();
/**
* 获取去年的订单总数
*
* @return
*/
List<OrderSumResponse> getLastYear();
返回数据结构
public OrderFieldResponse getOrderSum(TypeRequest typeRequest) {
OrderFieldResponse orderFieldResponse = new OrderFieldResponse();
//类型
List<String> fieldsName = new ArrayList<>();
//添加种类
String fields[] = {"订单笔数", "成交量", "成交金额", "人均消费"};
fieldsName = Arrays.asList(fields);
orderFieldResponse.setFieldsName(fieldsName);
//数值和字段
List<SeriesDataResponse> seriesData = new ArrayList<>();
//今日的统计
if (typeRequest.getType().equals(OrderSumEnum.TODAY.getCode())) {
List<String> hours = OperatCenterTimeUtil.getHours();
orderFieldResponse.setXAxisData(hours);
List<OrderSumResponse> orderSumResponseList = dzAddonShopOrderMapper.getToday();
//时间
if (!CollectionUtils.isEmpty(orderSumResponseList)) {
Map<String,List<BigDecimal>> seriesMap = new HashMap<>();
for (int i = 0; i < fieldsName.size(); i++) {
seriesMap.put(fieldsName.get(i),new ArrayList<>());
}
//封装成map,用时间作为key
Map<String, OrderSumResponse> map = orderSumResponseList.stream().collect(Collectors.toMap(item -> item.getTime(), item -> item));
//时间匹配
for (String hour : hours) {
if(map.get(hour)!=null){
for (int i = 0; i < fieldsName.size(); i++) {
List<BigDecimal> list = seriesMap.get(fieldsName.get(i));
if(fieldsName.get(i).equals("订单笔数")){
list.add(BigDecimal.valueOf(map.get(hour).getCount()));
}else if(fieldsName.get(i).equals("成交量")){
list.add(BigDecimal.valueOf(map.get(hour).getProductCount()));
}else if(fieldsName.get(i).equals("成交金额")){
list.add(map.get(hour).getPayMoney());
}else if(fieldsName.get(i).equals("人均消费")){
list.add(map.get(hour).getAvgMoney());
}
seriesMap.put(fieldsName.get(i),list);
}
}else{
for (int i = 0; i < fieldsName.size(); i++) {
List<BigDecimal> list = seriesMap.get(fieldsName.get(i));
list.add(new BigDecimal(0));
seriesMap.put(fieldsName.get(i),list);
}
}
}
for(String key : seriesMap.keySet()){
SeriesDataResponse seriesDataResponse = new SeriesDataResponse();
seriesDataResponse.setName(key);
seriesDataResponse.setData(seriesMap.get(key));
seriesData.add(seriesDataResponse);
}
orderFieldResponse.setSeriesData(seriesData);
} else {
//没有数据
//成交量
//匹配
for (int i = 0; i < fieldsName.size(); i++) {
SeriesDataResponse seriesDataResponseProductCount = new SeriesDataResponse();
if (fieldsName.get(i).equals("成交量")) {
seriesDataResponseProductCount.setName("成交量");
} else if (fieldsName.get(i).equals("订单笔数")) {
seriesDataResponseProductCount.setName("订单笔数");
} else if (fieldsName.get(i).equals("成交金额")) {
seriesDataResponseProductCount.setName("成交金额");
} else if (fieldsName.get(i).equals("人均消费")) {
seriesDataResponseProductCount.setName("人均消费");
}
List<BigDecimal> dataProductCount = new ArrayList<>();
//没有数据,将所有点位数据初始化为0
for (String hour : hours) {
dataProductCount.add(new BigDecimal(0));
}
seriesDataResponseProductCount.setData(dataProductCount);
seriesData.add(seriesDataResponseProductCount);
}
orderFieldResponse.setSeriesData(seriesData);
}
} else if (typeRequest.getType().equals(OrderSumEnum.YESTERDAY.getCode())) {
//昨天
orderFieldResponse.setXAxisData(OperatCenterTimeUtil.getHours());
List<OrderSumResponse> orderSumResponseList = dzAddonShopOrderMapper.getYesterday();
List<String> hours = OperatCenterTimeUtil.getHours();
if (!CollectionUtils.isEmpty(orderSumResponseList)) {
Map<String,List<BigDecimal>> seriesMap = new HashMap<>();
for (int i = 0; i < fieldsName.size(); i++) {
seriesMap.put(fieldsName.get(i),new ArrayList<>());
}
//封装成map,用时间作为key
Map<String, OrderSumResponse> map = orderSumResponseList.stream().collect(Collectors.toMap(item -> item.getTime(), item -> item));
//时间匹配
for (String hour : hours) {
if(map.get(hour)!=null){
for (int i = 0; i < fieldsName.size(); i++) {
List<BigDecimal> list = seriesMap.get(fieldsName.get(i));
if(fieldsName.get(i).equals("订单笔数")){
list.add(BigDecimal.valueOf(map.get(hour).getCount()));
}else if(fieldsName.get(i).equals("成交量")){
list.add(BigDecimal.valueOf(map.get(hour).getProductCount()));
}else if(fieldsName.get(i).equals("成交金额")){
list.add(map.get(hour).getPayMoney());
}else if(fieldsName.get(i).equals("人均消费")){
list.add(map.get(hour).getAvgMoney());
}
seriesMap.put(fieldsName.get(i),list);
}
}else{
for (int i = 0; i < fieldsName.size(); i++) {
List<BigDecimal> list = seriesMap.get(fieldsName.get(i));
list.add(new BigDecimal(0));
seriesMap.put(fieldsName.get(i),list);
}
}
}
for(String key : seriesMap.keySet()){
SeriesDataResponse seriesDataResponse = new SeriesDataResponse();
seriesDataResponse.setName(key);
seriesDataResponse.setData(seriesMap.get(key));
seriesData.add(seriesDataResponse);
}
orderFieldResponse.setSeriesData(seriesData);
}else{
for (int i = 0; i < fieldsName.size(); i++) {
SeriesDataResponse seriesDataResponseProductCount = new SeriesDataResponse();
if (fieldsName.get(i).equals("成交量")) {
seriesDataResponseProductCount.setName("成交量");
} else if (fieldsName.get(i).equals("订单笔数")) {
seriesDataResponseProductCount.setName("订单笔数");
} else if (fieldsName.get(i).equals("成交金额")) {
seriesDataResponseProductCount.setName("成交金额");
} else if (fieldsName.get(i).equals("人均消费")) {
seriesDataResponseProductCount.setName("人均消费");
}
List<BigDecimal> dataProductCount = new ArrayList<>();
//没有数据,将所有点位数据初始化为0
for (String hour : hours) {
dataProductCount.add(new BigDecimal(0));
}
seriesDataResponseProductCount.setData(dataProductCount);
seriesData.add(seriesDataResponseProductCount);
orderFieldResponse.setSeriesData(seriesData);
}
orderFieldResponse.setSeriesData(seriesData);
}
} else if (typeRequest.getType().equals(OrderSumEnum.WEEK.getCode())) {
//本周
orderFieldResponse.setXAxisData(OperatCenterTimeUtil.getWeekDay());
List<OrderSumResponse> orderSumResponseList = dzAddonShopOrderMapper.getWeek();
if (!CollectionUtils.isEmpty(orderSumResponseList)) {
Map<String, List<BigDecimal>> seriesMap = new HashMap<>();
for (int i = 0; i < fieldsName.size(); i++) {
seriesMap.put(fieldsName.get(i), new ArrayList<>());
}
//封装成map,用时间作为key
Map<String, OrderSumResponse> map = orderSumResponseList.stream().collect(Collectors.toMap(item -> item.getTime(), item -> item));
//时间匹配
for (String day : OperatCenterTimeUtil.getWeekDay()) {
if (map.get(day) != null) {
for (int i = 0; i < fieldsName.size(); i++) {
List<BigDecimal> list = seriesMap.get(fieldsName.get(i));
if (fieldsName.get(i).equals("订单笔数")) {
list.add(BigDecimal.valueOf(map.get(day).getCount()));
} else if (fieldsName.get(i).equals("成交量")) {
list.add(BigDecimal.valueOf(map.get(day).getProductCount()));
} else if (fieldsName.get(i).equals("成交金额")) {
list.add(map.get(day).getPayMoney());
} else if (fieldsName.get(i).equals("人均消费")) {
list.add(map.get(day).getAvgMoney());
}
seriesMap.put(fieldsName.get(i), list);
}
} else {
for (int i = 0; i < fieldsName.size(); i++) {
List<BigDecimal> list = seriesMap.get(fieldsName.get(i));
list.add(new BigDecimal(0));
seriesMap.put(fieldsName.get(i), list);
}
}
}
for (String key : seriesMap.keySet()) {
SeriesDataResponse seriesDataResponse = new SeriesDataResponse();
seriesDataResponse.setName(key);
seriesDataResponse.setData(seriesMap.get(key));
seriesData.add(seriesDataResponse);
}
orderFieldResponse.setSeriesData(seriesData);
} else {
for (int i = 0; i < fieldsName.size(); i++) {
SeriesDataResponse seriesDataResponseProductCount = new SeriesDataResponse();
if (fieldsName.get(i).equals("成交量")) {
seriesDataResponseProductCount.setName("成交量");
} else if (fieldsName.get(i).equals("订单笔数")) {
seriesDataResponseProductCount.setName("订单笔数");
} else if (fieldsName.get(i).equals("成交金额")) {
seriesDataResponseProductCount.setName("成交金额");
} else if (fieldsName.get(i).equals("人均消费")) {
seriesDataResponseProductCount.setName("人均消费");
}
List<BigDecimal> dataProductCount = new ArrayList<>();
//没有数据,将所有点位数据初始化为0
for (String hour : OperatCenterTimeUtil.getWeekDay()) {
dataProductCount.add(new BigDecimal(0));
}
seriesDataResponseProductCount.setData(dataProductCount);
seriesData.add(seriesDataResponseProductCount);
}
orderFieldResponse.setSeriesData(seriesData);
}
} else if (typeRequest.getType().equals(OrderSumEnum.MONTH.getCode())) {
//本月
orderFieldResponse.setXAxisData(OperatCenterTimeUtil.getMonthDay());
List<OrderSumResponse> orderSumResponseList = dzAddonShopOrderMapper.getMonth();
if (!CollectionUtils.isEmpty(orderSumResponseList)) {
Map<String,List<BigDecimal>> seriesMap = new HashMap<>();
for (int i = 0; i < fieldsName.size(); i++) {
seriesMap.put(fieldsName.get(i),new ArrayList<>());
}
//封装成map,用时间作为key
Map<String, OrderSumResponse> map = orderSumResponseList.stream().collect(Collectors.toMap(item -> item.getTime(), item -> item));
//时间匹配
for (String day : OperatCenterTimeUtil.getMonthDay()) {
if(map.get(day)!=null){
for (int i = 0; i < fieldsName.size(); i++) {
List<BigDecimal> list = seriesMap.get(fieldsName.get(i));
if(fieldsName.get(i).equals("订单笔数")){
list.add(BigDecimal.valueOf(map.get(day).getCount()));
}else if(fieldsName.get(i).equals("成交量")){
list.add(BigDecimal.valueOf(map.get(day).getProductCount()));
}else if(fieldsName.get(i).equals("成交金额")){
list.add(map.get(day).getPayMoney());
}else if(fieldsName.get(i).equals("人均消费")){
list.add(map.get(day).getAvgMoney());
}
seriesMap.put(fieldsName.get(i),list);
}
}else{
for (int i = 0; i < fieldsName.size(); i++) {
List<BigDecimal> list = seriesMap.get(fieldsName.get(i));
list.add(new BigDecimal(0));
seriesMap.put(fieldsName.get(i),list);
}
}
}
for(String key : seriesMap.keySet()){
SeriesDataResponse seriesDataResponse = new SeriesDataResponse();
seriesDataResponse.setName(key);
seriesDataResponse.setData(seriesMap.get(key));
seriesData.add(seriesDataResponse);
}
orderFieldResponse.setSeriesData(seriesData);
}else{
for (int i = 0; i < fieldsName.size(); i++) {
SeriesDataResponse seriesDataResponseProductCount = new SeriesDataResponse();
if (fieldsName.get(i).equals("成交量")) {
seriesDataResponseProductCount.setName("成交量");
} else if (fieldsName.get(i).equals("订单笔数")) {
seriesDataResponseProductCount.setName("订单笔数");
} else if (fieldsName.get(i).equals("成交金额")) {
seriesDataResponseProductCount.setName("成交金额");
} else if (fieldsName.get(i).equals("人均消费")) {
seriesDataResponseProductCount.setName("人均消费");
}
List<BigDecimal> dataProductCount = new ArrayList<>();
//没有数据,将所有点位数据初始化为0
for (String hour : OperatCenterTimeUtil.getMonthDay()) {
dataProductCount.add(new BigDecimal(0));
}
seriesDataResponseProductCount.setData(dataProductCount);
seriesData.add(seriesDataResponseProductCount);
}
orderFieldResponse.setSeriesData(seriesData);
}
} else if (typeRequest.getType().equals(OrderSumEnum.YEAR.getCode())) {
//本年
orderFieldResponse.setXAxisData(OperatCenterTimeUtil.getYearMonth());
List<OrderSumResponse> orderSumResponseList = dzAddonShopOrderMapper.getYear();
if (!CollectionUtils.isEmpty(orderSumResponseList)) {
Map<String, List<BigDecimal>> seriesMap = new HashMap<>();
for (int i = 0; i < fieldsName.size(); i++) {
seriesMap.put(fieldsName.get(i), new ArrayList<>());
}
//封装成map,用时间作为key
Map<String, OrderSumResponse> map = orderSumResponseList.stream().collect(Collectors.toMap(item -> item.getTime(), item -> item));
//时间匹配
for (String day : OperatCenterTimeUtil.getYearMonth()) {
if (map.get(day) != null) {
for (int i = 0; i < fieldsName.size(); i++) {
List<BigDecimal> list = seriesMap.get(fieldsName.get(i));
if (fieldsName.get(i).equals("订单笔数")) {
list.add(BigDecimal.valueOf(map.get(day).getCount()));
} else if (fieldsName.get(i).equals("成交量")) {
list.add(BigDecimal.valueOf(map.get(day).getProductCount()));
} else if (fieldsName.get(i).equals("成交金额")) {
list.add(map.get(day).getPayMoney());
} else if (fieldsName.get(i).equals("人均消费")) {
list.add(map.get(day).getAvgMoney());
}
seriesMap.put(fieldsName.get(i), list);
}
} else {
for (int i = 0; i < fieldsName.size(); i++) {
List<BigDecimal> list = seriesMap.get(fieldsName.get(i));
list.add(new BigDecimal(0));
seriesMap.put(fieldsName.get(i), list);
}
}
}
for (String key : seriesMap.keySet()) {
SeriesDataResponse seriesDataResponse = new SeriesDataResponse();
seriesDataResponse.setName(key);
seriesDataResponse.setData(seriesMap.get(key));
seriesData.add(seriesDataResponse);
}
orderFieldResponse.setSeriesData(seriesData);
} else {
for (int i = 0; i < fieldsName.size(); i++) {
SeriesDataResponse seriesDataResponseProductCount = new SeriesDataResponse();
if (fieldsName.get(i).equals("成交量")) {
seriesDataResponseProductCount.setName("成交量");
} else if (fieldsName.get(i).equals("订单笔数")) {
seriesDataResponseProductCount.setName("订单笔数");
} else if (fieldsName.get(i).equals("成交金额")) {
seriesDataResponseProductCount.setName("成交金额");
} else if (fieldsName.get(i).equals("人均消费")) {
seriesDataResponseProductCount.setName("人均消费");
}
List<BigDecimal> dataProductCount = new ArrayList<>();
//没有数据,将所有点位数据初始化为0
for (String hour : OperatCenterTimeUtil.getYearMonth()) {
dataProductCount.add(new BigDecimal(0));
}
seriesDataResponseProductCount.setData(dataProductCount);
seriesData.add(seriesDataResponseProductCount);
}
orderFieldResponse.setSeriesData(seriesData);
}
} else if (typeRequest.getType().equals(OrderSumEnum.LAST_YEAR.getCode())) {
//去年
orderFieldResponse.setXAxisData(OperatCenterTimeUtil.getLastYearMonth());
List<OrderSumResponse> orderSumResponseList = dzAddonShopOrderMapper.getLastYear();
if (!CollectionUtils.isEmpty(orderSumResponseList)) {
Map<String, List<BigDecimal>> seriesMap = new HashMap<>();
for (int i = 0; i < fieldsName.size(); i++) {
seriesMap.put(fieldsName.get(i), new ArrayList<>());
}
//封装成map,用时间作为key
Map<String, OrderSumResponse> map = orderSumResponseList.stream().collect(Collectors.toMap(item -> item.getTime(), item -> item));
//时间匹配
for (String day : OperatCenterTimeUtil.getLastYearMonth()) {
if (map.get(day) != null) {
for (int i = 0; i < fieldsName.size(); i++) {
List<BigDecimal> list = seriesMap.get(fieldsName.get(i));
if (fieldsName.get(i).equals("订单笔数")) {
list.add(BigDecimal.valueOf(map.get(day).getCount()));
} else if (fieldsName.get(i).equals("成交量")) {
list.add(BigDecimal.valueOf(map.get(day).getProductCount()));
} else if (fieldsName.get(i).equals("成交金额")) {
list.add(map.get(day).getPayMoney());
} else if (fieldsName.get(i).equals("人均消费")) {
list.add(map.get(day).getAvgMoney());
}
seriesMap.put(fieldsName.get(i), list);
}
} else {
for (int i = 0; i < fieldsName.size(); i++) {
List<BigDecimal> list = seriesMap.get(fieldsName.get(i));
list.add(new BigDecimal(0));
seriesMap.put(fieldsName.get(i), list);
}
}
}
for (String key : seriesMap.keySet()) {
SeriesDataResponse seriesDataResponse = new SeriesDataResponse();
seriesDataResponse.setName(key);
seriesDataResponse.setData(seriesMap.get(key));
seriesData.add(seriesDataResponse);
}
orderFieldResponse.setSeriesData(seriesData);
} else {
for (int i = 0; i < fieldsName.size(); i++) {
SeriesDataResponse seriesDataResponseProductCount = new SeriesDataResponse();
if (fieldsName.get(i).equals("成交量")) {
seriesDataResponseProductCount.setName("成交量");
} else if (fieldsName.get(i).equals("订单笔数")) {
seriesDataResponseProductCount.setName("订单笔数");
} else if (fieldsName.get(i).equals("成交金额")) {
seriesDataResponseProductCount.setName("成交金额");
} else if (fieldsName.get(i).equals("人均消费")) {
seriesDataResponseProductCount.setName("人均消费");
}
List<BigDecimal> dataProductCount = new ArrayList<>();
//没有数据,将所有点位数据初始化为0
for (String hour : OperatCenterTimeUtil.getLastYearMonth()) {
dataProductCount.add(new BigDecimal(0));
}
seriesDataResponseProductCount.setData(dataProductCount);
seriesData.add(seriesDataResponseProductCount);
}
orderFieldResponse.setSeriesData(seriesData);
}
}
return orderFieldResponse;
}
总结
统计图