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

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

相关推荐
鹿角片ljp6 小时前
LeetCode 64:最小路径和复盘|二维 DP 与 ACM 模式完整写法
java·数据结构·算法
aramae6 小时前
模拟实现strcmp()(C语言)
c语言·开发语言·后端
+VX:Fegn08956 小时前
计算机毕业设计|基于springboot + vue蛋糕店管理系统(源码+数据库+文档)
前端·数据库·vue.js·spring boot·课程设计
泡海椒7 小时前
PDF 表格样式优化:jquick-pdf 边框、圆角、背景色
java·开发语言·pdf
Beyond_System|系统之外8 小时前
【学编程】Python基础编程题100道(21-60)
开发语言·python·算法
景熙55238 小时前
15.Java 8 Stream 流入门到实战
java·开发语言·数据结构
wno7048 小时前
Spring Security权限控制
java·python·spring
wno7048 小时前
Spring Boot整合Kafka
spring boot·kafka
杨运交9 小时前
[071][验证码模块]基于Spring拦截器的验证码认证设计思想
java·后端·spring