苍穹外卖学习笔记(三十)-销量排名Top

时间:2024-10-22 08:45:01

ReportController

    /**
     * 销售额Top10
     */
    @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:{}", begin, end);
        SalesTop10ReportVO top10 = reportService.getSalesTop10(begin, end);
        return Result.success(top10);
    }

ReportService

    /**
     * 销售额Top10统计
     * @param begin 开始时间
     * @param end 结束时间
     * @return 销售额Top10统计结果
     */
    SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end);

ReportServiceImpl

    /**
     * 销售额Top10统计
     * @param begin 开始时间
     * @param end 结束时间
     * @return 销售额Top10统计结果
     */
    @Override
    public SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end) {
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);

        // 当前集合用于存放商品名称
        List<String> nameList = new ArrayList<>();
        // 当前集合用于存放销量
        List<Integer> numberList = new ArrayList<>();

        List<GoodsSalesDTO> salesTop = orderMapper.getSalesTop(beginTime, endTime);
        for (GoodsSalesDTO goodsSalesDTO : salesTop) {
            log.info("goodsSalesDTO:{}", goodsSalesDTO);
            nameList.add(goodsSalesDTO.getName());
            numberList.add(goodsSalesDTO.getNumber());
        }
        // 将集合转换为字符串
        String nameJoin = StringUtils.join(nameList, ",");
        String numberJoin = StringUtils.join(numberList, ",");
        return SalesTop10ReportVO.builder()
                .nameList(nameJoin)
                .numberList(numberJoin)
                .build();
    }

OrderMapper

/**
     * 获取指定时间内Top10
     */
    List<GoodsSalesDTO> getSalesTop(LocalDateTime begin, LocalDateTime end);

OrderMapper.xml

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