项目实站day7--功能之营业额统计,用户数量统计

1.Controller层 (ReportController.java)

复制代码
@GetMapping("/turnoverStatistics")
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));
}

2.Service层 (ReportServiceImpl.java)

复制代码
public TurnoverReportVO getTurnover(LocalDate begin, LocalDate end) {
    // 1. 生成日期列表
    List<LocalDate> dateList = new ArrayList<>();
    // [2023-01-01, 2023-01-02, ..., 2023-01-31]
    
    // 2. 遍历每一天,查询营业额
    for (LocalDate date : dateList) {
        LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);  // 00:00:00
        LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);    // 23:59:59
        
        // 3. 构建查询条件
        Map map = new HashMap();
        map.put("status", Orders.COMPLETED);      // 只统计已完成的订单
        map.put("begin", beginTime);
        map.put("end", endTime);
        
        // 4. 调用Mapper查询当日营业额
        Double turnover = orderMapper.sumByMap(map);
    }
    
    // 5. 封装结果
    return TurnoverReportVO.builder()
            .dateList(StringUtils.join(dateList, ","))
            .turnoverList(StringUtils.join(turnoverList, ","))
            .build();
}

3.Mapper层 (OrderMapper.java)

复制代码
// 接口声明
Double sumByMap(Map map);

4.XML映射 (OrderMapper.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 >= #{begin}  <!-- 订单开始时间 -->
        </if>
        <if test="end != null">
            AND order_time <= #{end}    <!-- 订单结束时间 -->
        </if>
    </where>
</select>

用户统计同理,不在赘述。

相关推荐
向上的车轮6 小时前
为什么.NET(C#)转 Java 开发时常常在“吐槽”Java:checked exception
java·c#·.net
Dragon Wu6 小时前
Spring Security Oauth2.1 授权码模式实现前后端分离的方案
java·spring boot·后端·spring cloud·springboot·springcloud
island13146 小时前
CANN GE(图引擎)深度解析:计算图优化管线、内存静态规划与异构任务的 Stream 调度机制
开发语言·人工智能·深度学习·神经网络
跳动的梦想家h6 小时前
环境配置 + AI 提效双管齐下
java·vue.js·spring
坚持就完事了6 小时前
Java中的集合
java·开发语言
魔芋红茶6 小时前
Python 项目版本控制
开发语言·python
wjhx6 小时前
QT中对蓝牙权限的申请,整理一下
java·数据库·qt
YCY^v^6 小时前
JeecgBoot 项目运行指南
java·学习
云小逸6 小时前
【nmap源码解析】Nmap OS识别核心模块深度解析:osscan2.cc源码剖析(1)
开发语言·网络·学习·nmap
冰暮流星6 小时前
javascript之二重循环练习
开发语言·javascript·数据库