项目实站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>

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

相关推荐
哭哭啼1 小时前
pgSql 事务篇
java·数据库·postgresql
架构源启1 小时前
Spring AI进阶系列(17)- 未来展望与职业发展:Java 工程师迈向 AI 工程化与智能体架构的路线图
java·人工智能·spring
我登哥MVP1 小时前
Spring Boot 从“会用”到“精通”:SpringBoot MVC 请求处理全流程
java·spring boot·后端·spring·mvc·maven·intellij-idea
Thomas_YXQ1 小时前
Unity3D Addressable 深度优化热更性能消耗
开发语言·3d·unity·微信
aini_lovee2 小时前
C# 快递单打印系统(万能套打系统)
开发语言·c#
我登哥MVP2 小时前
Spring Boot 从“会用”到“精通”:ReturnValueHandler原理
java·spring boot·后端·spring·java-ee·maven·intellij-idea
snow@li2 小时前
数据库:MySQL vs PostgreSQL 详尽对比(2026版)
java·mysql·postgresql
天启HTTP2 小时前
开启全局代理后网络变慢,问题出在哪
开发语言·前端·网络·tcp/ip·php
丑过三八线2 小时前
Runc 深度解析:从原理到实操
java·linux·开发语言·docker·容器·rpc