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

Day73(10)-F:\硕士阶段\Java\课程资料\1、黑马程序员Java项目《苍穹外卖》企业级开发实战\sky-take-out

苍穹外卖

数据统计

ApacheECharts

前端的技术

image-20251231130438004

image-20251231130657972

image-20251231130729562

https://echarts.apache.org/zh/index.html

营业额统计

image-20251231142547512

image-20251231142927949

image-20251231143120502

技术栈

  1. 传参与时间相关要设定对应格式
  2. 时间的加减plusDays
  3. 将数列中的数据拼接为String
  4. 通过日期算日期的具体时间
  5. MyBatis 的处理方式,传入map和其他普通参数不同
1.传参与时间相关要设定对应格式(38-39行)
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*/@ApiOperation("营业额统计")@GetMapping("/turnoverStatistics")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.时间的加减plusDays
3.将数列中的数据拼接为String
4.通过日期算日期的具体时间
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.apache.poi.util.StringUtil;
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;@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<>();List<Double> turnOverList = new ArrayList<>();dateList.add(begin);while (!begin.equals(end)){//计算指定日期的后一天对应的日期begin = begin.plusDays(1);dateList.add(begin);}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 > ? and order_time < ? and status = 5HashMap<Object, Object> map = new HashMap<>();map.put("begin",beginTime);map.put("end",endTime);map.put("status", Orders.COMPLETED);Double turnover = orderMapper.sumByMap(map);//判断营业额是否为nullturnover = turnover == null?0.0:turnover;turnOverList.add(turnover);}String date = StringUtils.join(dateList, ",");String turnOver = StringUtils.join(turnOverList, ",");return TurnoverReportVO.builder().dateList(date).turnoverList(turnOver).build();}
}
5.MyBatis 的处理方式,传入map和其他普通参数不同

根据map中的key校验value

/*** 根据动态条件统计营业额* @return*/
Double sumByMap(Map map);
<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>

用户统计

image-20251231153210555

image-20251231153353537

image-20251231153532879

/*** 用户统计* @param begin* @param end* @return*/
@ApiOperation("用户统计")
@GetMapping("/userStatistics")
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));
}
/*** 统计指定时间区间内的用户数据* @param begin* @param end* @return*/
@Override
public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {//当前集合用于存放从begin到end内的每天的日期List<LocalDate> dateList = new ArrayList<>();List<Integer> totalUserList = new ArrayList<>();List<Integer> newUserList = new ArrayList<>();dateList.add(begin);while (!begin.equals(end)){//计算指定日期的后一天对应的日期begin = begin.plusDays(1);dateList.add(begin);}for (LocalDate date : dateList) {//select count(id) from user where create_time < ? and create_time >  ?//select count(id) from user where create_time < ?LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);HashMap<Object, Object> 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);}String date = StringUtils.join(dateList, ",");String totalUser = StringUtils.join(totalUserList, ",");String newUser = StringUtils.join(newUserList, ",");//封装结果数据return new UserReportVO(date,totalUser,newUser);
}
/*** 用户数量统计* @param map* @return*/
Integer countByMap(Map map);
<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>

订单统计

image-20251231162358754

image-20251231162556816

image-20251231162732285

技术栈

  1. 对集合内的元素通过stream流进行累加求和stream().reduce是返回一个容器optional,需要通过get获取值
  2. 将int类型在计算中转换为double
/*** 用户订单统计* @param begin* @param end* @return*/
@ApiOperation("用户订单统计")
@GetMapping("/ordersStatistics")
public Result<OrderReportVO> ordersStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){log.info("用户订单统计:{},{}",begin,end);return Result.success(reportService.getOrdersStatistics(begin,end));
}
1.对集合内的元素通过stream流进行累加求和stream().reduce是返回一个容器optional,需要通过get获取值(32、34行)
2.将int类型在计算中转换为double(37行)
/*** 统计指定时间区间内的营业额数据* @param begin* @param end* @return*/
@Override
public OrderReportVO getOrdersStatistics(LocalDate begin, LocalDate end) {//当前集合用于存放从begin到end内的每天的日期List<LocalDate> dateList = new ArrayList<>();List<Integer> orderCountList = new ArrayList<>();List<Integer> validOrderCountList = new ArrayList<>();dateList.add(begin);while (!begin.equals(end)){//计算指定日期的后一天对应的日期begin = begin.plusDays(1);dateList.add(begin);}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 validOrderCount = getOrderCount(beginTime, endTime, Orders.COMPLETED);//查询每天的订单总数select count(id) from orders where order_time > ? and order_time < ? and status = 5Integer orderCount = getOrderCount(beginTime, endTime, null);orderCountList.add(orderCount);validOrderCountList.add(validOrderCount);}//计算时间区间内的订单总数Integer totalOrderCount = orderCountList.stream().reduce(Integer::sum).get();//计算时间时间区间内的有效订单数量Integer validOrderCount = validOrderCountList.stream().reduce(Integer::sum).get();//计算订单完成率Double orderCompletionRate =  totalOrderCount==0?0: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){HashMap<Object, Object> map = new HashMap<>();map.put("begin",begin);map.put("end",end);map.put("status",status);return orderMapper.countByMap(map);
}
<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>

销量排名

image-20251231170907229

image-20251231171024334

image-20251231171107810

select name,sum(od.number) number from order_detail od, orders o where od.order_id = o.id and status = 5 and o.order_time > '2022-10-1' and o.order_time < '2026-10-1' group by name order by number desc limit 0,10

技术栈

  1. stream流对DTO数列提取DTO的某种属性成为一个集合
/*** 销量排名top10* @param begin* @param end* @return*/
@ApiOperation("销量排名top10")
@GetMapping("/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));
}
1.stream流对DTO数列提取DTO的某种属性成为一个集合(13、15行)
/*** 统计指定时间区间内的销量排名top10* @param begin* @param end* @return*/
@Override
public SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end) {LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);List<GoodsSalesDTO> salesTop = orderMapper.getSalesTop(beginTime, endTime);List<String> names = salesTop.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());String nameList = StringUtils.join(names, ",");List<Integer> numbers = salesTop.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());String numberList = StringUtils.join(numbers, ",");//封装运行结果return SalesTop10ReportVO.builder().nameList(nameList).numberList(numberList).build();
}
/*** 统计指定时间区间内的销量排名前十* @param begin* @param end* @return*/
List<GoodsSalesDTO> getSalesTop(LocalDateTime begin,LocalDateTime end);
<select id="getSalesTop" resultType="com.sky.dto.GoodsSalesDTO">select name,sum(od.number) number from order_detail od, orders o<where>od.order_id = o.idand 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 nameorder by number desc</where>limit 0,10
</select>
http://www.jsqmd.com/news/172346/

相关文章:

  • five hundred miles
  • 编译错误反复踩坑?这款Java自动修复引擎,本地环境精准适配一次搞定
  • 【R语言时间序列预测优化】:掌握5大核心技巧提升模型精度
  • 【稀缺资源】PHP低代码平台插件开发内部文档流出(仅限前1000人下载)
  • 2026 公众号矩阵跨平台适配 TOP1:广州旗引科技奇码云覆盖全场景 - 品牌推荐官优选
  • YOLOv8在野生动物迁徙研究中的应用
  • YOLOv8训练时数据增强策略分析
  • 2025副业新风口:养一只“机器人”,比养猪还稳?
  • 深度学习框架如何训练 智慧工地 无人机航拍反光衣背心头盔穿戴检测数据集 工地安全施工积水检测数据集 无人机工地积水数据集 无人机建筑施工安全智能化监管 (1)
  • 告别编译错误反复折腾!Java本地环境适配神器,一键搞定不踩坑
  • 代码漏洞藏隐患?Java安全防护神器,分钟级闭环修复
  • 家用电器管理系统厂商哪家强?权威排行来了! - 百誉集团
  • 【R语言随机森林分类实战】:从零构建高精度模型的完整指南
  • YOLOv8在纺织品瑕疵检测中的表现评估
  • AI狂欢,谁在“埋单”?——2025年广告业的底层逻辑
  • 陪诊陪护小程序定制系统,我们这样开发!
  • GLM-4.7编程环境10分钟搭建指南:3种官方配置方法,实测有效,一键即用!
  • YOLOv8镜像内置tmux/screen终端复用工具
  • YOLOv8训练完成后模型体积有多大?
  • Git使用教程
  • Java程序员转战大模型开发:完整学习路径与高薪岗位指南,大模型入门到精通
  • YOLOv8镜像提供FAQ文档解决常见问题
  • YOLOv8如何实现旋转框检测功能?
  • 2025实测:5款主流AI编程工具终极横评,Java开发者选型不踩坑
  • YOLOv8镜像内置wget/curl工具方便数据下载
  • YOLOv8推理时如何适应不同分辨率输入?
  • YOLOv8在工业流水线产品计数中的应用
  • YOLOv8项目目录结构解析:/root/ultralytics详解
  • YOLOv8训练时如何使用EMA指数移动平均?
  • YOLOv8在智能安防领域的落地实践