业务规则:
创建admin/ReportController数据统计相关接口,创建营业额统计接口方法
@RestController
@RequestMapping("/admin/report")
@Api(tags = "数据统计相关接口")
@Slf4j
public class ReportController {
private ReportService reportService;
/**
* 营业额统计
* @param begin
* @param end
* @return
*/
@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);
return Result.success(reportService.getTurnoverStatistics(begin, end));
}
}
在ReportServiceImpl中,实现"统计指定区间内的营业额统计"的方法,及其父类接口
@Service
@Slf4j
public class ReportServiceImpl implements ReportService {
@Autowired
private OrderMapper orderMapper;
/**
* 统计指定区间内的营业额统计
* @param begin
* @param end
* @return
*/
@Override
public TurnoverReportVO getTurnoverStatistics(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<Double> turnoverList = new ArrayList<>();
for (LocalDate date : dateList) {
// 查询date日期对应的营业额,营业额是指:状态为“已完成”的订单金额合计
LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
// 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);
turnoverList.add(turnover);
}
// 封装返回结果
return TurnoverReportVO
.builder()
.dateList(StringUtils.join(dateList, ","))
.turnoverList(StringUtils.join(turnoverList, ","))
.build();
}
}
在OrderMapper中,实现营业额统计的方法
/**
* 根据动态条件统计营业额数据
* @param map
* @return
*/
Double sumByMap(Map map);
在OrderMapper.xml中,写入SQL语句,根据动态条件统计营业额数据
<select id="sumByMap" resultType="java.lang.Double">
select sum(amount) from 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>
业务规则:
在ReportController中,创建用户统计接口方法
/**
* 用户统计
* @param begin
* @param end
* @return
*/
@GetMapping("/userStatistics")
@ApiOperation("用户统计")
public Result<UserReportVO> userStatistics(
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
log.info("用户数据统计:{},{}", begin, end);
return Result.success(reportService.getUserStatistics(begin, end));
}
在ReportServiceImpl中,实现"统计指定区间内的用户统计"的方法,及其父类接口
/**
* 统计指定区间内的用户统计
* @param begin
* @param end
* @return
*/
@Override
public UserReportVO getUserStatistics(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);
}
// 存放新增用户数量 select count(id) from user where create_time > beginTime and create_time < endTime
List<Integer> newUserList = new ArrayList<>();
// 存放总用户数量 select count(id) from user where create_time < endTime
List<Integer> totalUserList = new ArrayList<>();
for (LocalDate date : dateList) {
LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
Map map = new HashMap<>();
map.put("end", endTime);
// 查询总用户数量
Integer totalUser = userMapper.countByMap(map);
totalUserList.add(totalUser);
map.put("begin", beginTime);
// 查询新用户数量
Integer newUser = userMapper.countByMap(map);
newUserList.add(newUser);
}
// 封装返回结果
return UserReportVO
.builder()
.dateList(StringUtils.join(dateList,","))
.newUserList(StringUtils.join(newUserList, ","))
.totalUserList(StringUtils.join(totalUserList, ","))
.build();
}
在UserMapper中,实现用户统计的方法
/**
* 根据动态条件来统计用户数量
* @param map
* @return
*/
Integer countByMap(Map map);
在UserMapper.xml中,写入SQL语句,根据动态条件统计用户数据
<select id="countByMap" resultType="java.lang.Integer">
select count(id) from user
<where>
<if test="begin != null">and create_time > #{begin}if>
<if test="end != null">and create_time < #{end}if>
where>
select>
业务规则:
在ReportController中,创建订单统计接口方法
/**
* 订单统计
* @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.getOrderStatistics(begin, end));
}
在ReportServiceImpl中,实现"统计指定区间内的订单统计"的方法,及其父类接口
/**
* 统计指定区间内的订单数据
* @param begin
* @param end
* @return
*/
@Override
public OrderReportVO getOrderStatistics(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(begin, LocalTime.MIN);
LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
// 查询每天的订单总数 select count(id) form orders where order_time > beginTime and order_time < endTime
Integer orderCount = getOrderCount(beginTime, endTime, null);
// 查询每天的有效订单数 select count(id) form orders where order_time > beginTime and order_time < endTime and Status = 5
Integer validOrderCount = getOrderCount(beginTime, endTime, Orders.COMPLETED);
orderCountList.add(orderCount);
validOrderCountList.add(validOrderCount);
}
// 计算时间区间内的订单总数量
Integer totalOrderCount = orderCountList.stream().reduce(Integer::sum).get();
// 计算区时间间内的有效订单数
Integer validOrderCount = validOrderCountList.stream().reduce(Integer::sum).get();
// 计算订单完成率
Double OrderCompletionRate = 0.0;
if (totalOrderCount != 0){
OrderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
}
return OrderReportVO
.builder()
.dateList(StringUtils.join(dateList, ","))
.orderCountList(StringUtils.join(orderCountList, ","))
.validOrderCountList(StringUtils.join(validOrderCountList, ","))
.totalOrderCount(totalOrderCount)
.validOrderCount(validOrderCount)
.orderCompletionRate(OrderCompletionRate)
.build();
}
/**
* 根据条件统计订单数量
* @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);
return orderMapper.countByMap(map);
}
在OrderMapper中,实现订单统计的方法
/**
* 根据动态条件统计订单数量
* @param map
* @return
*/
Integer countByMap(Map map);
在OrderMapper.xml中,写入SQL语句,根据动态条件统计订单数据
<select id="countByMap" resultType="java.lang.Integer">
select count(id) from 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>
业务规则:
在ReportController中根据销量排名接口创建top10方法:
/**
* 销量排名top10
* @param begin
* @param end
* @return
*/
@GetMapping("/top10")
@ApiOperation("销量排名top10")
public Result<SalesTop10ReportVO> salesTop10(
@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));
}
在ReportServiceImpl中,实现getSalesTop10方法,及其父类接口
/**
* 销量排名top10
* @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();
}
在OrderMapper中,声明getSalesTop10方法:
/**
* 统计指定时间区间内的销量排名前10
* @param begin
* @param end
* @return
*/
List<GoodsSalesDTO> getSalesTop10(LocalDateTime begin, LocalDateTime end);
在OrderMapper.xml文件中编写动态SQL:
<select id="getSalesTop10" resultType="com.sky.dto.GoodsSalesDTO">
select od.name, sum(od.number) as 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 > #{begin}</if>
<if test="end != null">and o.order_time < #{end}</if>
group BY od.name
ORDER BY number desc
limit 0,10
</select>