黑马苍穹外卖学习Day11

Apache ECharts

介绍

在这里插入图片描述

营业额统计

需求分析

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码开发

在这里插入图片描述
Controller层


@RestController
@Slf4j
@Api(tags = "数据统计相关接口")
@RequestMapping("/admin/report")
public class ReportController {
   

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

        TurnoverReportVO turnoverReportVO = reportService.getTurnoverStatistics(begin,end);
        return Result.success(turnoverReportVO);

    }

}

Service实现类

@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<>();
        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 > ? and order_time < ? and status = 5
            Map map = new HashMap<>();
            map.put("begin", beginTime);
            map.put("end",endTime);
            map.put("status", Orders.COMPLETED);
            Double turnover = orderMapper.sumById(map);
            if (turnover == null)turnover = 0.0;
            turnoverList.add(turnover);

        }
        //取出list中每个元素取出来,并加上分隔符
        return TurnoverReportVO.builder()
                                .dateList(StringUtils.join(dateList,","))
                                .turnoverList(StringUtils.join(turnoverList,","))
                                .build();
    }
}

XML层

    <select id="sumById" 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>

用户统计

需求分析

在这里插入图片描述
在这里插入图片描述

代码开发

Controller层

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

Service实现类

    /**
     * 用户量统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public UserReportVO getUserStatistics(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);
        }

        //总用户数量 select count(id) from user where create < ?
        List<Integer> totalUserList = new ArrayList<>();
        //新增用户数量 select count(id) from user where create_time < ? and create_time > ?
        List<Integer> newUserList = 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();
    }

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>

订单统计

需求分析

在这里插入图片描述在这里插入图片描述

代码开发

Controller层


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

Servcie实现类

    /**
     * 订单数据统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public OrderReportVO getOrderStatistics(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<Integer> orderCountList = new ArrayList<>();
        //存放每天的有效订单数
        List<Integer> validOrderCountList = new ArrayList<>();
        //遍历dateList集合,查询每天有效订单数和订单总数
        for (LocalDate date : dateList) {
   
            //查询每天订单总数 select count(id) from orders where order_time > ? and order_time < ?
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.from(LocalDateTime.MIN));
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Integer orderCount = getOrderCount(beginTime, endTime, null);
            //查询每天有效订单数 select count(id) from orders where order_time > ? and order_time < ? and status = 5
            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, ","))
                            .validOrderCountList(StringUtils.join(validOrderCountList, ","))
                            .orderCountList(StringUtils.join(orderCountList,","))
                            .totalOrderCount(totalOrderCount)
                            .validOrderCount(validOrderCount)
                            .orderCompletionRate(orderCompletionRate)
                            .build();
    }

    /**
     * 根据条件统计订单数量
     * @param begin
     * @param end
     * @param status
     * @return
     */
    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);
    }

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>

销量排名Top 10

需求分析

在这里插入图片描述

代码开发

在这里插入图片描述
Controller层

    /**
     * 销量数据统计
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/top10")
    @ApiOperation("销量数据统计")
    public Result<SalesTop10ReportVO> salesTop10ReportVo(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
   
        SalesTop10ReportVO salesTop10ReportVO = reportService.getSalesTop10(begin,end);
        return Result.success(salesTop10ReportVO);
    }

Service实现类

    /**
     * 销量数据统计
     * @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> goodsSalesDTOList = orderMapper.getStatusTop10(beginTime,endTime);
        List<String> names = goodsSalesDTOList.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        String nameList = StringUtils.join(names,",");
        List<Integer> numbers = goodsSalesDTOList.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
        String numberList = StringUtils.join(numbers, ",");
//        封装返回结果数据
        return SalesTop10ReportVO.builder()
                .nameList(nameList)
                .numberList(numberList)
                .build();
    }

XML

    <select id="getStatusTop10" resultType="com.sky.dto.GoodsSalesDTO">
        select od.name,sum(od.number) number from order_detail od,orders o
        where 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.name
        order by number desc
        limit 0,10
    </select>

相关推荐

  1. 黑马苍穹

    2024-01-22 20:32:05       9 阅读
  2. 苍穹项目(黑马)学习笔记

    2024-01-22 20:32:05       222 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-22 20:32:05       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-22 20:32:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-22 20:32:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-22 20:32:05       18 阅读

热门阅读

  1. python中request api详解

    2024-01-22 20:32:05       30 阅读
  2. 结构体练习

    2024-01-22 20:32:05       39 阅读
  3. 深入C++异常处理:构建健壮程序的利器

    2024-01-22 20:32:05       36 阅读
  4. Ubuntu查看版本

    2024-01-22 20:32:05       38 阅读
  5. ANSI C类型限定符

    2024-01-22 20:32:05       40 阅读
  6. 【AI】深度学习在编码中的应用(5)

    2024-01-22 20:32:05       27 阅读
  7. kubectl与 jq的用法

    2024-01-22 20:32:05       34 阅读
  8. 力扣(leetcode)第66题加一(Python)

    2024-01-22 20:32:05       35 阅读
  9. ICMP控制消息 汇总

    2024-01-22 20:32:05       27 阅读
  10. MySQL中对日期时间的处理

    2024-01-22 20:32:05       40 阅读
  11. Python3 如何定位错误:段错误 (核心已转储)

    2024-01-22 20:32:05       38 阅读