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

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

相关推荐
无名-CODING1 天前
SpringMVC处理流程完全指南:从请求到响应的完整旅程
java·后端·spring
瑶山1 天前
Spring Cloud微服务搭建三、分布式任务调度XXL-JOB
java·spring cloud·微服务·xxljob
Re.不晚1 天前
深入底层理解HashMap——妙哉妙哉的结构!!
java·哈希算法
wWYy.1 天前
C++—集群聊天室(3)CMake详解
开发语言·c++
Serene_Dream1 天前
Java 内存区域
java·jvm
lsx2024061 天前
SciPy 稀疏矩阵
开发语言
小猪咪piggy1 天前
【Python】(6) 文件操作
开发语言·python
柒.梧.1 天前
从零搭建SpringBoot+Vue+Netty+WebSocket+WebRTC视频聊天系统
vue.js·spring boot·websocket
睡一觉就好了。1 天前
C++ 容器
开发语言·c++
你的代码我的心1 天前
微信开发者工具开发网页,不支持tailwindcss v4怎么办?
开发语言·javascript·ecmascript