当前位置: 首页 > ds >正文

苍穹外卖11

数据统计

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

入门案例

引入js文件(已提供)

为 ECharts 准备一个设置宽高的 DOM

初始化echarts实例

指定图表的配置项和数据

使用指定的配置项和数据显示图表

代码

<!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>

营业额统计:

业务规则:

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

接口设计:

前端需要什么格式的数据,后端去适应前端返回该格式的数据

vo已定义

package com.sky.vo;import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;import java.io.Serializable;@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TurnoverReportVO implements Serializable {//日期,以逗号分隔,例如:2022-10-01,2022-10-02,2022-10-03private String dateList;//营业额,以逗号分隔,例如:406.0,1520.0,75.0private String turnoverList;}
Controller层
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")
@Slf4j
@Api(tags = "统计报表相关接口")
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) {return Result.success(reportService.getTurnover(begin, end));}}
 Service层接口
package com.sky.service;import com.sky.vo.TurnoverReportVO;
import java.time.LocalDate;public interface ReportService {/*** 根据时间区间统计营业额* @param beginTime* @param endTime* @return*/TurnoverReportVO getTurnover(LocalDate beginTime, LocalDate endTime);
}
service层实现类
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.lang.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*/public TurnoverReportVO getTurnover(LocalDate begin, LocalDate end) {List<LocalDate> dateList = new ArrayList<>();dateList.add(begin);while (!begin.equals(end)){begin = begin.plusDays(1);//日期计算,获得指定日期后1天的日期dateList.add(begin);}List<Double> turnoverList = 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("status", Orders.COMPLETED);map.put("begin",beginTime);map.put("end", endTime);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();}
}
 Mapper层
	/*** 根据动态条件统计营业额* @param map*/Double sumByMap(Map map);

Mapper.xml

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

用户统计

Controller层
/*** 用户数据统计* @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){return Result.success(reportService.getUserStatistics(begin,end));            
}
Service层接口
	/*** 根据时间区间统计用户数量* @param begin* @param end* @return*/UserReportVO getUserStatistics(LocalDate begin, LocalDate end);
 Service层实现类

在ReportServiceImpl实现类中实现getUserStatistics方法:

	@Overridepublic UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {List<LocalDate> dateList = new ArrayList<>();dateList.add(begin);while (!begin.equals(end)){begin = begin.plusDays(1);dateList.add(begin);}List<Integer> newUserList = new ArrayList<>(); //新增用户数List<Integer> totalUserList = new ArrayList<>(); //总用户数for (LocalDate date : dateList) {LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);//新增用户数量 select count(id) from user where create_time > ? and create_time < ?Integer newUser = getUserCount(beginTime, endTime);//总用户数量 select count(id) from user where  create_time < ?Integer totalUser = getUserCount(null, endTime);newUserList.add(newUser);totalUserList.add(totalUser);}return UserReportVO.builder().dateList(StringUtils.join(dateList,",")).newUserList(StringUtils.join(newUserList,",")).totalUserList(StringUtils.join(totalUserList,",")).build();}

在ReportServiceImpl实现类中创建私有方法getUserCount:

	/*** 根据时间区间统计用户数量* @param beginTime* @param endTime* @return*/private Integer getUserCount(LocalDateTime beginTime, LocalDateTime endTime) {Map map = new HashMap();map.put("begin",beginTime);map.put("end", endTime);return userMapper.countByMap(map);}
 Mapper层
	/*** 根据动态条件统计用户数量* @param map* @return*/Integer countByMap(Map map);

Mapper.xml

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

订单统计

业务规则:

  • 有效订单指状态为 “已完成” 的订单
  • 基于可视化报表的折线图展示订单数据,X轴为日期,Y轴为订单数量
  • 根据时间选择区间,展示每天的订单总数和有效订单数
  • 展示所选时间区间内的有效订单数、总订单数、订单完成率,订单完成率 = 有效订单数 / 总订单数 * 100%
Controller层
/*** 订单数据统计* @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){return Result.success(reportService.getOrderStatistics(begin,end));}
 Service层接口
/**
* 根据时间区间统计订单数量
* @param begin 
* @param end
* @return 
*/
OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end);
 Service层实现类

在ReportServiceImpl实现类中实现getOrderStatistics方法:

/**
* 根据时间区间统计订单数量
* @param begin 
* @param end
* @return 
*/
public OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end){List<LocalDate> dateList = new ArrayList<>();dateList.add(begin);while (!begin.equals(end)){begin = begin.plusDays(1);dateList.add(begin);}//每天订单总数集合List<Integer> orderCountList = new ArrayList<>();//每天有效订单数集合List<Integer> validOrderCountList = new ArrayList<>();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 = ?Integer 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();}

在ReportServiceImpl实现类中提供私有方法getOrderCount:

/**
* 根据时间区间统计指定状态的订单数量
* @param beginTime
* @param endTime
* @param status
* @return
*/
private Integer getOrderCount(LocalDateTime beginTime, LocalDateTime endTime, Integer status) {Map map = new HashMap();map.put("status", status);map.put("begin",beginTime);map.put("end", endTime);return orderMapper.countByMap(map);
}
Mapper层
/**
*根据动态条件统计订单数量
* @param map
*/
Integer countByMap(Map map);

Mapper.xml

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

销量排名Top10

业务规则:

  • 根据时间选择区间,展示销量前10的商品(包括菜品和套餐)
  • 基于可视化报表的柱状图降序展示商品销量
  • 此处的销量为商品销售的份数
Controller层
/**
* 销量排名统计
* @param begin
* @param end
* @return
*/
@GetMapping("/top10")
@ApiOperation("销量排名统计")
public Result<SalesTop10ReportVO> top10(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){return Result.success(reportService.getSalesTop10(begin,end));
}
Service层接口
/**
* 查询指定时间区间内的销量排名top10 
* @param begin
* @param end
* @return
*/
SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end);
Service层实现类
/*** 查询指定时间区间内的销量排名top10* @param begin* @param end* @return* */public SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end){LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);List<GoodsSalesDTO> goodsSalesDTOList = orderMapper.getSalesTop10(beginTime, endTime);String nameList = StringUtils.join(goodsSalesDTOList.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList()),",");String numberList = StringUtils.join(goodsSalesDTOList.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList()),",");return SalesTop10ReportVO.builder().nameList(nameList).numberList(numberList).build();}
Mapper层
/**
* 查询商品销量排名
* @param begin
* @param end	
*/
List<GoodsSalesDTO> getSalesTop10(LocalDateTime begin, LocalDateTime end);

Mapper.xml

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

http://www.xdnf.cn/news/3410.html

相关文章:

  • Windows查看和修改IP,IP互相ping通
  • 使用模块中的`XPath`语法提取非结构化数据
  • Learning vtkjs之ImageMarchingCubes
  • 100 个 NumPy 练习
  • centos安装nginx
  • 新手小白如何查找科研论文?
  • 2025深圳杯东三省数学建模竞赛选题建议+初步分析
  • 26个脑影像工具包合集分享:从预处理到SCI成图
  • 为什么定位关闭了还显示IP属地?
  • 软考中级-软件设计师 数据库(手写笔记)
  • TS类型体操练习
  • Rancher 2.6.3企业级容器管理平台部署实践
  • ESP32-C3 Secure Boot 使用多个签名 Key
  • FEKO许可管理
  • YOLO11改进-模块-引入跨模态注意力机制CMA 提高多尺度 遮挡
  • 6轴、智能、低功耗惯性测量单元BMI270及其OIS接口
  • 开源 RAG 框架对比:LangChain、Haystack、DSPy 技术选型指南
  • 常用矩阵求导
  • Java父类、子类实例初始化顺序详解
  • 92.一个简单的输入与显示示例 Maui例子 C#例子
  • 论文速读 - 通过提示工程创建全面的合成数据集以支持医疗领域模型训练
  • 【Scrapy】简单项目实战--爬取dangdang图书信息
  • 柯希霍夫积分法偏移成像中数据分布不均匀的处理方法
  • Windows和 macOS 上安装 `nvm` 和 Node.js 16.16.0 的详细教程。
  • 深度解析| 信创浪潮下,传统AD域如何破局?
  • HCIP-security常见名词
  • 软考中级-软件设计师 操作系统(手写笔记)
  • pinia-plugin-persistedstate的使用
  • win11 终端 安装ffmpeg 使用终端Scoop
  • Copilot 上线深度推理智能体 Researcher