欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 明星 > 如何接受Date范围的数据

如何接受Date范围的数据

2024/10/25 23:36:24 来源:https://blog.csdn.net/qq_40603125/article/details/143080439  浏览:    关键词:如何接受Date范围的数据

controller

  • 注解 @DateTimeFormat 会自动完成字符串LocalDate的转换
/*** 报表的接口*/
@RestController
@RequestMapping("/admin/report")
@Api(tags = "报表的接口")
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ReportController {// 日期列表 2020-01-01,2020-01-02,2020-01-03private String dateList ;// 交易额列表 100,200,300private 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
@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 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>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com