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

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

相关推荐
2301_822376941 天前
C++中的解释器模式
开发语言·c++·算法
czlczl200209251 天前
Spring Boot :如何高性能地在 Filter 中获取响应体(Response Body)
java·spring boot·后端
爱学习的阿磊1 天前
C++代码冗余消除
开发语言·c++·算法
sg_knight1 天前
抽象工厂模式(Abstract Factory)
java·python·设计模式·抽象工厂模式·开发
春日见1 天前
win11 分屏设置
java·开发语言·驱动开发·docker·单例模式·计算机外设
2301_780029041 天前
支付宝sdk导入错误
java·开发语言·maven
码界奇点1 天前
基于Spring Boot和Vue3的无头内容管理系统设计与实现
java·spring boot·后端·vue·毕业设计·源代码管理
九皇叔叔1 天前
【03】微服务系列 之Nacos 注册中心(服务注册)
java·微服务·nacos·架构·注册中心·服务注册
木辰風1 天前
PLSQL自定义自动替换(AutoReplace)
java·数据库·sql
2501_944525541 天前
Flutter for OpenHarmony 个人理财管理App实战 - 预算详情页面
android·开发语言·前端·javascript·flutter·ecmascript