苍穹外卖--订单统计功能实现

This commit is contained in:
2025-12-02 11:10:35 +08:00
parent 1ee460f665
commit 4f055f9b15
6 changed files with 107 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ package com.sky.controller.admin;
import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.annotations.Api;
@@ -54,4 +55,19 @@ public class ReportController {
return Result.success(vo);
}
@GetMapping("/ordersStatistics")
@ApiOperation("订单统计")
public Result<OrderReportVO> ordersStatistics(
@DateTimeFormat(pattern = "yyyy-MM-dd")
LocalDate begin,
@DateTimeFormat(pattern = "yyyy-MM-dd")
LocalDate end){
log.info("订单统计:{}{}",begin,end);
//调用业务层,订单统计功能
OrderReportVO vo = reportService.getordersStatistics(begin,end);
return Result.success(vo);
}
}

View File

@@ -60,4 +60,6 @@ public interface OrderMapper {
List<Orders> getByStatusAndOrderTime(Integer status, LocalDateTime time);
Double sumByMap(Map map);
Integer countByMap(Map map);
}

View File

@@ -1,5 +1,6 @@
package com.sky.service;
import com.sky.vo.OrderReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
@@ -21,4 +22,12 @@ public interface ReportService {
* @return
*/
UserReportVO getUserStatistics(LocalDate begin, LocalDate end);
/**
* 订单统计
* @param begin
* @param end
* @return
*/
OrderReportVO getordersStatistics(LocalDate begin, LocalDate end);
}

View File

@@ -4,6 +4,7 @@ import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang3.StringUtils;
@@ -119,4 +120,69 @@ public class ReportServiceImpl implements ReportService {
return userReportVO;
}
/**
* 订单统计
* @param begin
* @param end
* @return
*/
@Override
public OrderReportVO getordersStatistics(LocalDate begin, LocalDate end) {
List<LocalDate> dateList = new ArrayList<>();
dateList.add(begin);
//不停地创建日期对象直到end
while (!begin.equals(end)){
begin = begin.plusDays(1);
dateList.add(begin);
}
//定义VO对象所需的4个局部变量
List<Integer> orderCountList = new ArrayList<>();
List<Integer> validOrderCountList = new ArrayList<>();
//订单总数
Integer totalOrderCount = 0;
//有效订单数
Integer validOrderCount = 0;
//订单完成率
Double orderCompletionRate = 0.0;
//根据日期计算出以上5个数据
for (LocalDate localDate : dateList) {
//准备好开始和结束时间
LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);
//select count(1) from orders where order_time > ? and order_time < ?
//select count(1) from orders where order_time > ? and order_time < ? and status = 5;
Map map = new HashMap();
map.put("end", endTime);
map.put("begin", beginTime);
Integer totalCount = orderMapper.countByMap(map);
orderCountList.add(totalCount);
//累加对应日期的订单数量
totalOrderCount += totalCount;
map.put("status", Orders.COMPLETED);
Integer validCount = orderMapper.countByMap(map);
validOrderCountList.add(validCount);
//累加对应日期的订单数量
validOrderCount += validCount;
}
//循环结束,对时间范围内的总订单数和有效订单数,已经准备好了,计算订单完成率
if (totalOrderCount != 0) {
orderCompletionRate = validOrderCount.doubleValue()/totalOrderCount;//注意java中的运算符前后数据类型计算的结果随大范围的数据类型
}
//封装数据
OrderReportVO orderReportVO = OrderReportVO.builder()
.validOrderCountList(StringUtils.join(validOrderCountList, ","))
.dateList(StringUtils.join(dateList, ","))
.orderCountList(StringUtils.join(orderCountList, ","))
.totalOrderCount(totalOrderCount)
.validOrderCount(validOrderCount)
.orderCompletionRate(orderCompletionRate).build();
return orderReportVO;
}
}

View File

@@ -71,5 +71,19 @@
</if>
</where>
</select>
<select id="countByMap" resultType="java.lang.Integer" parameterType="java.util.Map">
select count(1) from orders
<where>
<if test="begin != null">
and order_time &gt; #{begin}
</if>
<if test="end != null">
and order_time &lt; #{end}
</if>
<if test="status != null">
and status = #{status}
</if>
</where>
</select>
</mapper>