销量排名统计功能代码开发

This commit is contained in:
zvv
2026-04-10 10:10:22 +08:00
parent 77ffe62c8d
commit 0da38cd6da
5 changed files with 207 additions and 0 deletions

View File

@@ -3,6 +3,8 @@ package com.sky.controller.admin;
import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.annotations.Api;
@@ -59,4 +61,38 @@ public class ReportController {
log.info("用户数据统计:{}{}", begin, end);
return Result.success(reportService.getUserStatistics(begin, end));
}
/**
* 订单统计
* @param begin
* @param end
* @return
*/
@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);
return Result.success(reportService.getOrdersStatistics(begin, end));
}
/**
* 销量排名
* @param begin
* @param end
* @return
*/
@GetMapping("/top10")
@ApiOperation("销量排名")
public Result<SalesTop10ReportVO> top10(
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
){
log.info("销量排名top10{}{}", begin, end);
return Result.success(reportService.getSalesTop10(begin, end));
}
}

View File

@@ -2,11 +2,13 @@ package com.sky.mapper;
import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
@@ -76,4 +78,22 @@ public interface OrderMapper {
*/
//select sum(amount) from orders where order_time < ? and order_time > ? and status = 5
Double sumByMap(Map map);
/**
* 根据动态条件统计订单数据
* @param map
* @return
*/
Integer countByMap(Map map);
/**
* 统计指定时间区间内的销量排名前10
* @param begin
* @param end
* @return
*/
List<GoodsSalesDTO> getSalesTop10(LocalDateTime begin, LocalDateTime end);
}

View File

@@ -1,5 +1,7 @@
package com.sky.service;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
@@ -24,4 +26,22 @@ public interface ReportService {
* @return
*/
UserReportVO getUserStatistics(LocalDate begin, LocalDate end);
/**
* 统计指定时间内的订单数据
* @param begin
* @param end
* @return
*/
OrderReportVO getOrdersStatistics(LocalDate begin, LocalDate end);
/**
* 统计指定时间内的销量排名前10
* @param begin
* @param end
* @return
*/
SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end);
}

View File

@@ -1,11 +1,15 @@
package com.sky.service.impl;
import com.sky.dto.GoodsSalesDTO;
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.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -18,6 +22,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
@@ -124,4 +129,100 @@ public class ReportServiceImpl implements ReportService {
return userReportVO;
}
/**
* 统计指定时间内的用户数据
* @param begin
* @param end
* @return
*/
@Override
public OrderReportVO getOrdersStatistics(LocalDate begin, LocalDate end) {
//当前集合用于存放从begin到end范围内每天的日期
List<LocalDate> dateList = new ArrayList();
dateList.add(begin);
while(!begin.equals(end)){
//日期计算 计算指定日期的后一天对应的日期
begin = begin.plusDays(1);
dateList.add(begin);
}
List<Integer> orderCountList = new ArrayList<>();
List<Integer> validOrderCountList = new ArrayList<>();
//遍历日期集合 查询每天的有效订单数和订单总数
for(LocalDate date: dateList){
LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
//查询每天的订单总数 select count(id) from orders where order_time > ? and order_time < ?
Integer orderCount = getOrderCount(beginTime, endTime, null);
orderCountList.add(orderCount);
//查询每天的有效订单数 select count(id) from orders where order_time > ? and order_time < ? and status = 5
Integer validOrderCount = getOrderCount(beginTime, endTime, Orders.COMPLETED);
validOrderCountList.add(validOrderCount);
}
//计算时间区间内的订单总数量
Integer totalOrderCount = orderCountList.stream().reduce(Integer::sum).get();
//计算时间区间内的有效订单总数量
Integer totalValidOrderCount = validOrderCountList.stream().reduce(Integer::sum).get();
//计算订单有效率
Double orderCompletionRate = totalOrderCount ==0? 0.0: totalValidOrderCount.doubleValue() / totalOrderCount;
OrderReportVO orderReportVO = OrderReportVO.builder()
.dateList(StringUtils.join(dateList, ","))
.orderCountList(StringUtils.join(orderCountList, ","))
.validOrderCountList(StringUtils.join(validOrderCountList, ","))
.totalOrderCount(totalOrderCount)
.validOrderCount(totalValidOrderCount)
.orderCompletionRate(orderCompletionRate)
.build();
return orderReportVO;
}
/**
* 根据状态统计订单数量
* @param begin
* @param end
* @param status
* @return
*/
private Integer getOrderCount(LocalDateTime begin, LocalDateTime end, Integer status) {
Map map = new HashMap();
map.put("begin", begin);
map.put("end", end);
map.put("status", status);
Integer orderCount = orderMapper.countByMap(map);
return orderCount;
}
/**
*统计指定时间区间内的销量排名前10
* @param begin
* @param end
* @return
*/
@Override
public SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end) {
LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
List<GoodsSalesDTO> salesTop10 = orderMapper.getSalesTop10(beginTime, endTime);
List<String> names = salesTop10.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
String nameList = StringUtils.join(names, ",");
List<Integer> numbers = salesTop10.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
String numberList = StringUtils.join(numbers, ",");
return SalesTop10ReportVO.builder()
.nameList(nameList)
.numberList(numberList)
.build();
}
}

View File

@@ -83,4 +83,34 @@
</if>
</where>
</select>
<select id="countByMap" resultType="java.lang.Integer">
select count(id) 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>
<select id="getSalesTop10" resultType="com.sky.dto.GoodsSalesDTO">
select od.name, sum(od.number) number from order_detail od, orders o where od.order_id = o.id and o.status = 5
<if test="begin != null">
and o.order_time &gt; #{begin}
</if>
<if test="end != null">
and o.order_time &lt; #{end}
</if>
group by od.name
order by number desc
limit 0, 10
</select>
</mapper>