如何接受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>
相关推荐
RainbowSea21 小时前
12. LangChain4j + 向量数据库操作详细说明
java·langchain·ai编程
RainbowSea21 小时前
11. LangChain4j + Tools(Function Calling)的使用详细说明
java·langchain·ai编程
excel1 天前
为什么在 Three.js 中平面能产生“起伏效果”?
前端
excel1 天前
Node.js 断言与测试框架示例对比
前端
天蓝色的鱼鱼1 天前
前端开发者的组件设计之痛:为什么我的组件总是难以维护?
前端·react.js
codingandsleeping1 天前
使用orval自动拉取swagger文档并生成ts接口
前端·javascript
考虑考虑1 天前
Jpa使用union all
java·spring boot·后端
石金龙1 天前
[译] Composition in CSS
前端·css
用户3721574261351 天前
Java 实现 Excel 与 TXT 文本高效互转
java
白水清风1 天前
微前端学习记录(qiankun、wujie、micro-app)
前端·javascript·前端工程化