文章目录
概要
营业额统计是商业活动中一个非常重要的环节,它可以帮助企业了解自身的经营状况,并为决策提供依据。
需求分析以及接口设计
营业额统计是基于折现图来展现,并且按照天来展示的。实际上,就是某一个时间范围之内的每一天的营业额。同时,不管光标放在哪个点上,那么它就会把具体的数值展示出来。并且还需要注意日期并不是固定写死的,是由上边时间选择器来决定。比如选择是近7天、或者是近30日,或者是本周,就会把相应这个时间段之内的每一天日期通过横坐标展示。
业务规则:
-
营业额指订单状态为已完成的订单金额合计
-
基于可视化报表的折线图展示营业额数据,X轴为日期,Y轴为营业额
-
根据时间选择区间,展示每天的营业额数据
技术细节
1.Controller层:
* 营业额统计
* @param begin
* @param end
* @return
*/
@ApiOperation("营业额统计")
@GetMapping("turnoverStatistics")
public Result<TurnoverReportVO> statistics(
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
@DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end){
log.info("营业额统计:{},{}",begin,end);
TurnoverReportVO turnoverReportVO = reportService.statistics(begin,end);
return Result.success(turnoverReportVO);
}
2.Service层:
/**
* 营业额统计
* @param begin
* @param end
* @return
*/
public TurnoverReportVO statistics(LocalDate begin, LocalDate end) {
//时间
ArrayList<LocalDate> dateList = new ArrayList<>();
dateList.add(begin);
while (!begin.equals(end)){
begin = begin.plusDays(1);
dateList.add(begin);
}
String dateListStr = StringUtils.join(dateList, ",");
//营业额:通过查已完成的订单表得到这些订单
ArrayList<Double> turnoverList = new ArrayList<>();
for (LocalDate date : dateList) {
//统计每天的营业额
LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
HashMap<String, Object> map = new HashMap<>();
map.put("begin", beginTime);
map.put("end", endTime);
map.put("status", Orders.COMPLETED);
//统计一天的营业额
Double turnover = orderMapper.getSumByMap(map);
turnover = turnover == null ? 0.0 : turnover;
//将每天的营业额添加到集合中
turnoverList.add(turnover);
}
String turnoverListStr = StringUtils.join(turnoverList, ",");
return TurnoverReportVO.builder()
.dateList(dateListStr)
.turnoverList(turnoverListStr)
.build();
}
3.Mapper层
<select id="getSumByMap" resultType="java.lang.Double">
select sum(amount) from `sky-take-out`.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>