controller
重点:根据时间格式接受时间类型参数
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime begin,
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime end)
@RestController
@RequestMapping("/admin/report")
@Slf4j
public class ReportContrroller {@GetMapping("/turnoverStatics")public Result<TurnoverReportVO> turnoverStatics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime begin,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime end){return null;}
实现类
根据前端传过来的两个时间点(开始时间点,结束时间点),算出这两个时间点间隔多少天,存放到一个ArrayList中。
并计算每天营业额总数,也存放到ArrayList中,构建成一个TurnoverReportVO返回给前端。
重点:
根据LocalTime算出当天的开始时间MIN和结束时间MAX并专程LocalDateTime
LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
表达式的方式:
判断如果是空就赋值0.0,否则还是保持原来数值。
turnover = turnover == null ? 0.0 : turnover;
@Service
public class ReportServiceImpl implements ReportService {@Autowiredprivate OrderMapper orderMapper;public TurnoverReportVO getTrunoverStatics(LocalDate begin, LocalDate end) {List<LocalDate> dateList = new ArrayList();dateList.add(begin);while (!begin.equals(end)){begin = begin.plusDays(1);dateList.add(begin);}List<Double> Amounts = new ArrayList();for (LocalDate date : dateList){LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);//select sum(Amount) from orders where order_time >beginTime and order_time < endTime and status = 5;//Double sumAmount = orderMapper.getSumamount(beginTime, endTime);Map map = new HashMap();map.put("begin",beginTime);map.put("end",endTime);map.put("status", Orders.COMPLETED);Double turnover = orderMapper.getSumamount(map);turnover = turnover == null ? 0.0 : turnover;Amounts.add(turnover);}TurnoverReportVO turnoverReportVO = TurnoverReportVO.builder().dateList(StringUtils.join(dateList,",")).turnoverList(StringUtils.join(Amounts,",")).build();return turnoverReportVO;}
}
<select id="getSumamount" resultType="java.lang.Double">select sum(Amount) from orders<where><if test="begin != null">and order_time > #{begin}</if><if test="end != null">and order_time < #{end}</if><if test="status != null">and status=#{status}</if></where></select>