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

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,40 @@
package com.sky.controller.admin;
import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.TurnoverReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDate;
@RestController
@RequestMapping("/admin/report")
@Slf4j
@Api(tags = "数据统计相关接口")
public class ReportController {
@Autowired
private 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);
//调用业务层,营业额统计功能
TurnoverReportVO vo = reportService.getTurnoverStatistics(begin,end);
return Result.success(vo);
}
}

View File

@@ -7,6 +7,7 @@ import org.apache.ibatis.annotations.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
@Mapper
public interface OrderMapper {
@@ -57,4 +58,6 @@ public interface OrderMapper {
*/
@Select("select * from orders where status = #{status} and order_time < #{time}")
List<Orders> getByStatusAndOrderTime(Integer status, LocalDateTime time);
Double sumByMap(Map map);
}

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;
}
}

View File

@@ -56,4 +56,20 @@
</where>
order by order_time desc
</select>
<select id="sumByMap" resultType="java.lang.Double" parameterType="java.util.Map">
select sum(amount) from orders
<where>
<if test="status != null">
and status = #{status}
</if>
<if test="begin != null">
and order_time &gt; #{begin}
</if>
<if test="end != null">
and order_time &lt; #{end}
</if>
</where>
</select>
</mapper>