如何接受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>
相关推荐
g***B73824 分钟前
Java 工程复杂性的真正来源:从语言设计到现代架构的全链路解析
java·人工智能·架构
期待のcode2 小时前
MyBatisX插件
java·数据库·后端·mybatis·springboot
AI浩5 小时前
【Labelme数据操作】LabelMe标注批量复制工具 - 完整教程
运维·服务器·前端
涔溪5 小时前
CSS 网格布局(Grid Layout)核心概念、基础语法、常用属性、实战示例和进阶技巧全面讲解
前端·css
安审若无5 小时前
oracel迁移数据文件至其他目录操作步骤
数据库
醇氧5 小时前
【Windows】优雅启动:解析一个 Java 服务的后台启动脚本
java·开发语言·windows
2401_878454535 小时前
浏览器工作原理
前端·javascript
sunxunyong5 小时前
doris运维命令
java·运维·数据库
菜鸟起航ing5 小时前
Spring AI 全方位指南:从基础入门到高级实战
java·人工智能·spring
西陵5 小时前
为什么说 AI 赋能前端开发,已经不是选择题,而是必然趋势?
前端·架构·ai编程