3.30学习记录

本来今天准备创建github来做自己苍穹外外卖的云端,就是上传代码,后来想着还是完成之后再说。
今天学习了关于Echart的后端开发,就是完成可视化,代码逻辑并不难,但是稍微有点繁琐 这是关于三层的代码(差一个销量统计) 用户、订单、营业额
Controller:
**
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;
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")
    @Api(tags = "数据统计相关接口")
    @Slf4j
    public class ReportController {

    @Autowired
    private ReportService reportService;

    /**

    • 营业额统计
    • @param begin
    • @param end
    • @return
      */
      @GetMapping("/turnoverStatistics")
      @ApiOperation("营业额统计")
      public Result 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));
      }

    /**

    • 用户统计
    • @param begin
    • @param end
    • @return
      */
      @GetMapping("/userStatistics")
      @ApiOperation("用户统计")
      public Result 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));
      }

    /**

    • 订单统计
    • @param begin
    • @param end
    • @return
      */
      @GetMapping("/ordersStatistics")
      @ApiOperation("订单统计")
      public Result 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));
      }

}**

service(实现类,接口不再提供):
package com.sky.service.impl;

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 io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
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
@Slf4j
public class ReportServiceImpl implements ReportService {

@Autowired
private OrderMapper orderMapper;

@Autowired
private UserMapper userMapper;

/**
 * 统计指定时间内的营业额数据
 * @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 sun(amount) from orders where order_time > beginTime and order_time < ? 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);
        turnover = turnover == null ? 0.0 : turnover;
        turnoverList.add(turnover);
    }

    //封装返回结果
    return TurnoverReportVO
            .builder()
            .dateList(StringUtils.join(dateList, ","))
            .turnoverList(StringUtils.join(turnoverList, ","))
            .build();
}

/**
 * 统计指定时间段的用户数据
 * @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);
        map.put("begin", beginTime);
        Integer newUser = userMapper.countByMap(map);
        totalUserList.add(totalUser);
        newUserList.add(newUser);

    }
    return UserReportVO.builder()
            .dateList(StringUtils.join(dateList, ","))
            .totalUserList(StringUtils.join(totalUserList, ","))
            .newUserList(StringUtils.join(newUserList, ","))
            .build();
}

/**
 * 订指定时间内的订单统计
 * @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<>();

    //遍历dateList集合 查询每天有效订单数和订单总数
    for (LocalDate date : dateList) {
        //查询每天的订单总数 select count(id) from order where order_time > ? and order_time < ?
        LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
        Integer orderCount = getOrderCount(beginTime, endTime, null);

        //查询每天的有效订单数 select count(id) from order where order_time > ? and order_time < ? 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);
}

}
mapper:

/**
 * 根据动态条件统计营业额数据
 * @param map
 * @return
 */
Double sumByMap(Map map);

/**
 * 根据动态条件统计订单
 * @param map
 * @return
 */
Integer countByMap(Map map);

mapper.xml:

posted @ 2025-03-30 23:34  好像是Cwk  阅读(30)  评论(0)    收藏  举报