《苍穹外卖》Day11部分知识点记录(数据统计——图像报表)

本文主要是介绍《苍穹外卖》Day11部分知识点记录(数据统计——图像报表),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、Apache ECharts

介绍

Apache ECharts是一款基于javascript的数据可视化图标库,提供直观、生动、可交互、可个性化定制的数据可视化图表。

官网地址:https://echarts.apache.org/zh/index.html

效果展示

  • 柱形图
  • 饼图
  • 折线图

入门案例

1. 在 echarts CDN by jsDelivr - A CDN for npm and GitHub 选择 dist/echarts.js,点击并保存为 echarts.js 文件

2. 编写入门案列

<!DOCTYPE html>
<html><head><meta charset="utf-8" /><title>ECharts</title><!-- 引入刚刚下载的 ECharts 文件 --><script src="echarts.js"></script></head><body><!-- 为 ECharts 准备一个定义了宽高的 DOM --><div id="main" style="width: 600px;height:400px;"></div><script type="text/javascript">// 基于准备好的dom,初始化echarts实例var myChart = echarts.init(document.getElementById('main'));// 指定图表的配置项和数据var option = {title: {text: 'ECharts 入门示例'},tooltip: {},legend: {data: ['销量']},xAxis: {data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']},yAxis: {},series: [{name: '销量',type: 'bar',data: [5, 20, 36, 10, 10, 20]}]};// 使用刚指定的配置项和数据显示图表。myChart.setOption(option);</script></body>
</html>

效果:

总结:使用ECharts,重点在于研究当前图标所需的数据格式。通常是需要后端提供符合格式要求的动态数据,然后响应给前端来展示图表。

二、营业额统计

需求分析和设计

业务规则:

  • 营业额指订单状态为已完成的订单金额统计
  • 基于可视化报表的折线图展示营业额数据,x轴为日期,y轴为营业额
  • 根据时间选择区间,展示每天都营业额数据

接口设计

根据接口定义设计对应的VO:

代码开发

1. 创建一个新的Controller,即admin/ReportController

package com.sky.controller.admin;import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.TurnoverReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.time.LocalDate;/*** 数据统计相关接口*/
@RestController
@RequestMapping("/admin/report")
@Api(tags = "数据统计相关接口")
@Slf4j
public class ReportController {@Autowiredprivate ReportService reportService;/*** 营业额统计* @param begin* @param end* @return*/@GetMapping("/turnoverStatistics")@ApiOperation("营业额统计")public Result<TurnoverReportVO> turnoverStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {log.info("营业额数据统计:{}, {}", begin, end);return Result.success(reportService.getTurnoverStatistics(begin, end));}
}

2. ReportService

package com.sky.service;import com.sky.vo.TurnoverReportVO;import java.time.LocalDate;public interface ReportService {/*** 统计指定时间区间内的营业额* @param begin* @param end* @return*/TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end);
}

3. ReportServiceImpl

注意StringUtils导的是import org.apache.commons.lang3.StringUtils;

package com.sky.service.impl;import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.service.ReportService;
import com.sky.vo.TurnoverReportVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;@Service
@Slf4j
public class ReportServiceImpl implements ReportService {@Autowiredprivate OrderMapper orderMapper;/*** 统计指定时间区间内的营业额* @param begin* @param end* @return*/@Overridepublic TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {// 当前集合用于存放从begin到end范围内的每天的日期List<LocalDate> dateList = new ArrayList<>();dateList.add(begin);while(!begin.equals(end)) {// 日期计算,计算指定日期的后一天对应的日期begin = begin.plusDays(1);dateList.add(begin);}// 存放每天的营业额List<Double> turnoverList = new ArrayList();for (LocalDate date : dateList) {// 查询date日期对应的营业额数据,指状态为“已完成”的订单金额合计// 一天的开始时间LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);// 一天的结束时间LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);// select sum(amount) from orders where order_time > beginTime and order_time < endTime and status = 5Map map = new HashMap();map.put("begin", beginTime);map.put("end", endTime);map.put("status", Orders.COMPLETED);Double turnover = orderMapper.sumByMap(map);// 判空turnover = turnover == null ? 0.0 : turnover;turnoverList.add(turnover);}// 封装返回结果return TurnoverReportVO.builder().dateList(StringUtils.join(dateList, ",")).turnoverList(StringUtils.join(turnoverList, ",")).build();}
}

4. OrderMapper

    /*** 根据动态条件统计营业额数据* @param map* @return*/Double sumByMap(Map map);

5. OrderMapper.xml

    <select id="sumByMap" resultType="java.lang.Double">select sum(amount) from orders<where><if test="begin != null">and order_time &gt; #{begin}</if><if test="end != null">and order_time &lt; #{end}</if><if test="status != null">and status = #{status}</if></where></select>

在XML文件中,有一些特殊字符需要使用实体编码表示,以避免与XML语法冲突,如:

  • &lt; :小于号(<)
  • &gt; : 大于号(>)
  • &amp; : 与号(&)
  • &quot; : 双引号(")
  • &apos : 单引号(')
  • &nbsp;:空格
  • &copy;:版权符号(©)
  • &reg;:注册商标符号(®)
  • &euro;:欧元符号(€)
  • &yen;:日元符号(¥)
  • &pound;:英镑符号(£)

功能测试

三、用户统计

需求分析和设计

产品原型

业务规则

  • 基于可视化报表的折线图展示用户数据,x轴为日期,y轴为用户数
  • 根据时间选择区间,展示每天的用户总量和新增用户数据

接口设计

代码开发

1. ReportController

    /*** 用户统计* @param begin* @param end* @return*/@GetMapping("/userStatistics")@ApiOperation("用户统计")public Result<UserReportVO> userStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {log.info("用户数据统计:{}, {}", begin, end);return Result.success(reportService.getUserStatistics(begin, end));}

2. ReportService

    /*** 统计指定时间区间内的用户数据* @param begin* @param end* @return*/UserReportVO getUserStatistics(LocalDate begin, LocalDate end);

3. ReportServiceImpl

    @Autowiredprivate UserMapper userMapper;/*** 统计指定时间区间内的用户数据* @param begin* @param end* @return*/@Overridepublic UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {// 当前集合用于存放从begin到end范围内的每天的日期List<LocalDate> dateList = new ArrayList<>();dateList.add(begin);while(!begin.equals(end)) {// 日期计算,计算指定日期的后踢腿对应的日期// LocalDate类的plusDays方法并不会修改原始的LocalDate对象,而是返回一个新的LocalDate对象begin = begin.plusDays(1);dateList.add(begin);}// 存放每天的新增用户数量// select count(id) from user where create_time < ? and create_time > ?List<Integer> newUserList = new ArrayList<>();// 存放每天的总用户数量// select count(id) from user where create_time < ?List<Integer> totalUserList = new ArrayList<>();for (LocalDate date : dateList) {// 统计每天的新增用户数量以及每天的总用户数量// 一天的开始时间LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);// 一天的结束时间LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);Map map = new HashMap();map.put("end", endTime);// 总用户数量Integer totalUser = userMapper.countByMap(map);map.put("begin", beginTime);// 新增用户数量Integer newUser = userMapper.countByMap(map);totalUserList.add(totalUser);newUserList.add(newUser);}// 封装返回结果return UserReportVO.builder().dateList(StringUtils.join(dateList, ",")).totalUserList(StringUtils.join(totalUserList, ",")).newUserList(StringUtils.join(newUserList, ",")).build();}

4. UserMapper

    /*** 根据动态条件统计用户数量* @return*/Integer countByMap(Map map);

5. UserMapper.xml

    <select id="countByMap" resultType="java.lang.Integer">select count(id) from orders<where><if test="begin != null">and order_time &gt; #{begin}</if><if test="end != null">and order_time &lt; #{end}</if><if test="status != null">and status = #{status}</if></where></select>

功能测试

四、订单统计

需求分析和设计

产品原型

业务规则

  • 有效订单指状态为”已完成“的订单
  • 基于可视化报表的折线图展示订单数据,x轴为日期,y轴为订单数量
  • 根据时间选择区间,展示每天的订单总数和有效订单数
  • 展示所选时间区间内的有效订单数、总订单数、订单完成率,订单完成率 = 有效订单数 / 总订单数 * 100%。

接口设计

代码开发

1. ReportController

注意不要写错路径

    /*** 订单统计* @param begin* @param end* @return*/@GetMapping("/ordersStatistics")@ApiOperation("订单统计")public Result<OrderReportVO> orderStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {log.info("订单数据统计:{}, {}", begin, end);return Result.success(reportService.getOrderStatistics(begin, end));}

2. ReportService

    /*** 统计指定时间区间内的订单数据* @param begin* @param end* @return*/OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end);

3. ReportServiceImpl

    /*** 统计指定时间区间内的订单数据* @param begin* @param end* @return*/@Overridepublic OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {// 当前集合用于存放从begin到end范围内的每天的日期List<LocalDate> dateList = new ArrayList<>();dateList.add(begin);while(!begin.equals(end)) {// 日期计算,计算指定日期的后踢腿对应的日期// LocalDate类的plusDays方法并不会修改原始的LocalDate对象,而是返回一个新的LocalDate对象begin = begin.plusDays(1);dateList.add(begin);}// 存放每天的订单总数List<Integer> orderCountList = new ArrayList<>();// 存放每天的有效订单总数List<Integer> validOrderCountList = new ArrayList<>();// 遍历dateList集合,查询每天的有效订单数和订单总数for (LocalDate date : dateList) {// 一天的开始时间LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);// 一天的结束时间LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);// 查询每天的订单总数// select count(id) from orders where order_time > ? and order_time < ?Integer orderCount = getOrderCount(beginTime, endTime, null);// 查询每天的有效订单总数// select count(id) from orders where order_time > ? and order_time < ? and status = 5Integer validOrderCount = getOrderCount(beginTime, endTime, Orders.COMPLETED);orderCountList.add(orderCount);validOrderCountList.add(validOrderCount);}// 计算时间区间内的订单总数量Integer totalOrderCount = orderCountList.stream().reduce(Integer::sum).get();// 计算时间区间内的有效订单总数量Integer validOrderCount = validOrderCountList.stream().reduce(Integer::sum).get();// 计算订单完成率Double orderCompletionRate = 0.0;if(totalOrderCount != 0) {orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;}// 封装返回结果return OrderReportVO.builder().dateList(StringUtils.join(dateList, ",")).orderCountList(StringUtils.join(orderCountList, ",")).validOrderCountList(StringUtils.join(validOrderCountList, ",")).totalOrderCount(totalOrderCount).validOrderCount(validOrderCount).orderCompletionRate(orderCompletionRate).build();}private Integer getOrderCount(LocalDateTime begin, LocalDateTime end, Integer status) {Map map = new HashMap();map.put("begin", begin);map.put("end", end);map.put("status", status);return orderMapper.countByMap(map);}

4. OrderMapper

    /*** 根据动态条件统计订单数量* @param map* @return*/Integer countByMap(Map map);

5. OrderMapper.xml

    <select id="countByMap" resultType="java.lang.Integer">select count(id) from orders<where><if test="begin != null">and order_time &gt; #{begin}</if><if test="end != null">and order_time &lt; #{end}</if><if test="status != null">and status = #{status}</if></where></select>

功能测试

五、销量排名Top10

需求分析和设计

产品原型

业务规则

  • 根据时间选择区间,展示销量前10的商品(包括菜品和套餐)
  • 基于可视化报表的柱状图展示商品销量
  • 此处的销量为商品销售的份数

接口设计

代码开发

1. ReportController

    /*** 销量排名top10* @param begin* @param end* @return*/@GetMapping("/top10")@ApiOperation("销量排名top10")public Result<SalesTop10ReportVO> top10(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {log.info("销量排名top10:{}, {}", begin, end);return Result.success(reportService.getSalesTop10(begin, end));}

2. ReportService

    /*** 统计指定时间区间内的销量排名top10* @param begin* @param end* @return*/SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end);

3. ReportServiceImpl

    /*** 统计指定时间区间内的销量排名top10* @param begin* @param end* @return*/@Overridepublic SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end) {LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);List<GoodsSalesDTO> salesTop10 = orderMapper.getSalesTop10(beginTime, endTime);List<String> names = salesTop10.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());String nameList = StringUtils.join(names, ",");List<Integer> numbers = salesTop10.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());String numberList = StringUtils.join(numbers, ",");// 封装返回结果数据return SalesTop10ReportVO.builder().nameList(nameList).numberList(numberList).build();}

4. OrderMapper

    /*** 统计指定时间区间内的销量排名前10* @param begin* @param end* @return*/List<GoodsSalesDTO> getSalesTop10(LocalDateTime begin, LocalDateTime end);

5. OrderMapper.xml

    <select id="getSalesTop10" resultType="com.sky.dto.GoodsSalesDTO">select od.name, sum(od.number) numberfrom order_detail od, orders owhere od.order_id = o.id and o.status = 5<if test="begin != null">and o.order_time &gt; #{begin}</if><if test="end != null">and o.order_time &lt; #{end}</if>group by od.nameorder by number desclimit 0,10</select>

功能测试

这篇关于《苍穹外卖》Day11部分知识点记录(数据统计——图像报表)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/942591

相关文章

Python使用Pandas对比两列数据取最大值的五种方法

《Python使用Pandas对比两列数据取最大值的五种方法》本文主要介绍使用Pandas对比两列数据取最大值的五种方法,包括使用max方法、apply方法结合lambda函数、函数、clip方法、w... 目录引言一、使用max方法二、使用apply方法结合lambda函数三、使用np.maximum函数

关于Spring @Bean 相同加载顺序不同结果不同的问题记录

《关于Spring@Bean相同加载顺序不同结果不同的问题记录》本文主要探讨了在Spring5.1.3.RELEASE版本下,当有两个全注解类定义相同类型的Bean时,由于加载顺序不同,最终生成的... 目录问题说明测试输出1测试输出2@Bean注解的BeanDefiChina编程nition加入时机总结问题说明

Redis的数据过期策略和数据淘汰策略

《Redis的数据过期策略和数据淘汰策略》本文主要介绍了Redis的数据过期策略和数据淘汰策略,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录一、数据过期策略1、惰性删除2、定期删除二、数据淘汰策略1、数据淘汰策略概念2、8种数据淘汰策略

轻松上手MYSQL之JSON函数实现高效数据查询与操作

《轻松上手MYSQL之JSON函数实现高效数据查询与操作》:本文主要介绍轻松上手MYSQL之JSON函数实现高效数据查询与操作的相关资料,MySQL提供了多个JSON函数,用于处理和查询JSON数... 目录一、jsON_EXTRACT 提取指定数据二、JSON_UNQUOTE 取消双引号三、JSON_KE

Python给Excel写入数据的四种方法小结

《Python给Excel写入数据的四种方法小结》本文主要介绍了Python给Excel写入数据的四种方法小结,包含openpyxl库、xlsxwriter库、pandas库和win32com库,具有... 目录1. 使用 openpyxl 库2. 使用 xlsxwriter 库3. 使用 pandas 库

SpringBoot定制JSON响应数据的实现

《SpringBoot定制JSON响应数据的实现》本文主要介绍了SpringBoot定制JSON响应数据的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录前言一、如何使用@jsonView这个注解?二、应用场景三、实战案例注解方式编程方式总结 前言

使用Python在Excel中创建和取消数据分组

《使用Python在Excel中创建和取消数据分组》Excel中的分组是一种通过添加层级结构将相邻行或列组织在一起的功能,当分组完成后,用户可以通过折叠或展开数据组来简化数据视图,这篇博客将介绍如何使... 目录引言使用工具python在Excel中创建行和列分组Python在Excel中创建嵌套分组Pyt

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

在Mysql环境下对数据进行增删改查的操作方法

《在Mysql环境下对数据进行增删改查的操作方法》本文介绍了在MySQL环境下对数据进行增删改查的基本操作,包括插入数据、修改数据、删除数据、数据查询(基本查询、连接查询、聚合函数查询、子查询)等,并... 目录一、插入数据:二、修改数据:三、删除数据:1、delete from 表名;2、truncate

Java实现Elasticsearch查询当前索引全部数据的完整代码

《Java实现Elasticsearch查询当前索引全部数据的完整代码》:本文主要介绍如何在Java中实现查询Elasticsearch索引中指定条件下的全部数据,通过设置滚动查询参数(scrol... 目录需求背景通常情况Java 实现查询 Elasticsearch 全部数据写在最后需求背景通常情况下