如何接受Date范围的数据

controller

  • 注解 @DateTimeFormat 会自动完成字符串到LocalDate的转换
java 复制代码
/**
 * 报表的接口
 */
@RestController
@RequestMapping("/admin/report")
@Api(tags = "报表的接口")
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ReportController {


    // 日期列表 2020-01-01,2020-01-02,2020-01-03
    private String dateList ;

    // 交易额列表 100,200,300
    private String turnoverList ;

    // 服务类
    private final ReportService reportService;

    /**
     * 交易额统计
     * @param begin 开始时间
     * @param end 结束时间
     * @return 交易额统计
     */
    @GetMapping("/turnoverStatistics")
    @ApiOperation("交易额统计")
    public Result<TurnoverReportVO> turnoverStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
    ) {
        log.info("交易额统计, begin={}, end={}", begin, end);
        return Result.success(reportService.getTurnoverStatistics(begin, end));
    }
}

service层

  • Map<String, Object> map = new HashMap<>(); 使用map封装特殊类型的Date
java 复制代码
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ReportServiceImpl implements ReportService {

    private final OrderMapper orderMapper;

    // 交易额统计
    public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {
        TurnoverReportVO turnoverReportVO = new TurnoverReportVO();

        // 计算从开始时间到结束时间的日期列表
        List<LocalDate> dateList = new ArrayList<>();
        LocalDate tempDate = begin;
        while (!tempDate.isAfter(end)) {
            dateList.add(tempDate);
            tempDate = tempDate.plusDays(1);
        }
        log.info("日期列表: {}", dateList);
        turnoverReportVO.setDateList(String.join(",", dateList.toString()));

        // 计算从开始时间到结束时间的交易额列表
        List<BigDecimal> turnoverList = new ArrayList<>();
        for (LocalDate date : dateList) {
            // 获取当日交易状态为已完成的交易额
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            // 计算当日交易额
            Map<String, Object> map = new HashMap<>();
            map.put("begin", beginTime);
            map.put("end", endTime);
            map.put("status", Orders.COMPLETED);
            BigDecimal turnover =  orderMapper.sumByMap(map);
            if (ObjectUtil.isNull(turnover)) {
                turnover = BigDecimal.ZERO;
            }
            turnoverList.add(turnover);
        }
        log.info("交易额列表: {}", turnoverList);
        turnoverReportVO.setTurnoverList(String.join(",", turnoverList.toString()));


        // 计算从开始时间到结束时间的交易额列表
        return turnoverReportVO;
    }
}

mapper层

xml 复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sky.mapper.OrderMapper">

    <select id="sumByMap" resultType="java.math.BigDecimal">
        select sum(amount) from orders
        <where>
            <if test="begin != null"><![CDATA[
                and order_time >= #{begin}
            ]]></if>
            <if test="end != null"><![CDATA[
                and order_time <= #{end}
            ]]></if>
            <if test="status != null">
                and status = #{status}
            </if>
        </where>
    </select>
</mapper>
相关推荐
AIgorithmGEEK18 分钟前
MySQL 索引全解(下):建、查、删实战与知识速查表
数据库·mysql·b+树·索引
m0_5873830032 分钟前
广州 24 小时自助健身房解决方案技术实现与实战指南
java·spring·小程序·架构·需求分析
·云扬·1 小时前
Prompt 工程与安全:从 System 设计到注入防御
java·人工智能·安全·ai·prompt
代码不停2 小时前
自动化测试(java)
java·开发语言·selenium·自动化
Java后端的Ai之路2 小时前
06-React-Props
前端·react.js·前端框架
Flynt2 小时前
开源"间谍卫星"说数据全是真的,我把它的免key数据源挨个试了一遍
前端·开源·数据可视化
唠点键盘之外的3 小时前
26 向量数据库实战:从 0 搭一条能用的检索链路(pgvector + Spring AI)
数据库·人工智能·spring
云和恩墨3 小时前
数据库监控简史:监控不止于告警——从图表大爆发到健康管理闭环
数据库
Csvn3 小时前
渲染管线
前端
ly76893 小时前
MongoDB 事务开销实测:快照隔离下的写冲突、重试与分片事务边界
数据库·mongodb·事务·分片集群·快照隔离·写冲突