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 {

    @Autowired
    private 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 {

    @Autowired
    private OrderMapper orderMapper;


    /**
     * 统计指定时间区间内的营业额数据
     * @param begin
     * @param end
     * @return
     */
    @Override
    public 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 = 5
            HashMap<Object, Object> map = new HashMap<>();
            map.put("begin",beginTime);
            map.put("end",endTime);
            map.put("status", Orders.COMPLETED);
            Double turnover = orderMapper.sumByMap(map);
            //判断营业额是否为null
            turnover = 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 = 5
        Integer 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.id
        and 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 name
        order by number desc
    </where>
    limit 0,10
</select>
posted @ 2025-12-31 17:54  David大胃  阅读(1)  评论(0)    收藏  举报