controller
- 注解
@DateTimeFormat
会自动完成字符串
到LocalDate
的转换
@RestController
@RequestMapping("/admin/report")
@Api(tags = "报表的接口")
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ReportController {private String dateList ;private String turnoverList ;private final ReportService reportService;@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>