苍穹外卖--营业额统计功能实现

This commit is contained in:
2025-12-01 16:55:05 +08:00
parent 8972018b65
commit c0dc470d70
5 changed files with 146 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
package com.sky.service;
import com.sky.vo.TurnoverReportVO;
import java.time.LocalDate;
public interface ReportService {
/**
* 统计指定时间内每天的营业额数据
* @param begin
* @param end
* @return
*/
TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end);
}

View File

@@ -0,0 +1,72 @@
package com.sky.service.impl;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.service.ReportService;
import com.sky.vo.TurnoverReportVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ReportServiceImpl implements ReportService {
@Autowired
private OrderMapper orderMapper;
/**
* 统计指定时间内每天的营业额数据
* @param begin
* @param end
* @return
*/
@Override
public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {//1.1 1.2 1.3----- 1.7
//根据对应的日期将当天的所有已完成的订单查询出来并统计总金额sum
List<LocalDate> dateList = new ArrayList<>();
dateList.add(begin);
//不停地创建日期对象直到end
while (!begin.equals(end)){
begin = begin.plusDays(1);
dateList.add(begin);
}
//创建一个集合用来保存对应日期的当天的营业金额
List<Double> turnoverList = new ArrayList<>();
//遍历日期,将每天的营业额查询并添加到集合中
for (LocalDate localDate : dateList) {
//查询localDate日期对应的营业额营业额指的是已完成状态的订单的总和
//一天的订单如何获取通过localDate对象获取一天的最小时间和最大时间订单的下单时间在此期间那就就是当天的 订单!!!
LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);
//sql:select sum(amount) from orders where order_time > beginTime and order_time < endTime and status = 5;
//准备下查询条件数据
Map map = new HashMap();
map.put("begin", beginTime);
map.put("end", endTime);
map.put("status", Orders.COMPLETED);
Double turnover = orderMapper.sumByMap(map);
//注意如果当日没有订单那么金额为0
turnoverList.add(turnover == null?0.0:turnover);
}
TurnoverReportVO turnoverReportVO = TurnoverReportVO.builder()
.dateList(StringUtils.join(dateList, ","))
.turnoverList(StringUtils.join(turnoverList, ","))
.build();
return turnoverReportVO;
}
}