• Java项目-苍穹外卖-Day11-Apache ECharts数据统计


    前言

    主要是以下四项的统计,以不同形式的图形进行展示
    在这里插入图片描述

    Apache ECharts

    介绍

    Apache ECharts是一项前端技术

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

    入门案例

    在这里插入图片描述
    自己去网站上看一哈,我不太懂前端

    营业额统计

    需求分析

    在这里插入图片描述
    在这里插入图片描述

    代码开发

    com.sky.controller.admin.ReportController

    @RestController
    @RequestMapping("/admin/report")
    @Api(tags = "数据统计相关接口")
    @Slf4j
    public class ReportController {
        @Autowired
        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);
            TurnoverReportVO turnoverStatistics = reportService.getTurnoverStatistics(begin,end);
            return Result.success(turnoverStatistics);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    com.sky.service.impl.ReportServiceImpl.java

    
    @Service
    public class ReportServiceImpl implements ReportService {
        @Autowired
        private OrderMapper orderMapper;
        /**
         * 统计指定时间内的营业额
         * @param begin
         * @param end
         * @return
         */
        public TurnoverReportVO getTurnoverStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                                      @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
            //存放从begin到end的日期
            List<LocalDate> dateList = new ArrayList<>();
            //范围是[begin,end)
            dateList.add(begin);
            while(!begin.equals(end)){//相同是最后一天,也有isbefore这种方法,注意plusDays是返回值
                begin = begin.plusDays(1);
                dateList.add(begin);
            }
    
            //存放明天的营业额
            List<Double> turnoverList = new ArrayList<>();
    
            for (LocalDate date : dateList) {
                //查询date日期对应的营业额,营业额值 status=已完成 的金额合计
                LocalDateTime beginOfDay = LocalDateTime.of(date, LocalTime.MIN);
                LocalDateTime endOfDay = LocalDateTime.of(date, LocalTime.MAX);
    
                //select sum(Amount) from where order_time > ? and order_time < ? and status = 5
                Map map = new HashMap();
                map.put("begin",beginOfDay);
                map.put("end",endOfDay);
                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();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    orderMapper

        /**
         *  根据动态条件统计营业额数据
         * @param map
         * @return
         */
        Double sumByMap(Map map);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    orderMapper.xml

    <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    功能测试

    在这里插入图片描述

    订单统计

    需求分析

    在这里插入图片描述
    在这里插入图片描述

    代码开发

    在这里插入图片描述
    Reportcontroller

        /**
         * 统计订单
         * @param begin
         * @param end
         * @return
         */
        @GetMapping("/ordersStatistics")
        public Result<OrderReportVO> orderStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                                     @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
            log.info("统计订单");
            OrderReportVO orderReportVO = reportService.getOrderStatistics(begin,end);
            return Result.success(orderReportVO);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    ReportServiceImpl

       /**
         * 统计订单数量
         * @param begin
         * @param end
         * @return
         */
        public OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {
            //这个获取dateList是相同的,复制上面的就可以
    
            //存放从begin到end的日期
            List<LocalDate> dateList = new ArrayList<>();
            //范围是[begin,end)
            dateList.add(begin);
            while(!begin.equals(end)){//相同是最后一天,也有isbefore这种方法,注意plusDays是返回值
                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 orders where order_time < end and order_time >begin;
                LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
                LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
                Integer orderCount = getOrderCount(beginTime, endTime, null);
                //查询每天的有效订单数 select count() from orders where status = 5 and order_time < end and order_time >begin
                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,","))
                    .validOrderCount(validOrderCount)
                    .totalOrderCount(totalOrderCount)
                    .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);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    orderMapper.xml

        <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    功能测试

    在这里插入图片描述

    销量排名统计

    需求分析

    在这里插入图片描述
    在这里插入图片描述

    代码开发

    在这里插入图片描述
    reportController

        /**
         * 销量排名
         * @param begin
         * @param end
         * @return
         */
        @ApiOperation("销量排名top10")
        @GetMapping("/top10")
        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));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    ReportServiceImpl

        /**
         * 统计销量排名前十
         * @param begin
         * @param end
         * @return
         */
        public SalesTop10ReportVO getSalesTop10(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                                @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
            //select od.name,sum(od.number) number from order_detail od,orders o where od.order_id = o.id
            // and o.status = 5 and o.order_time > ? and o.order_time < ?
            //group by od.name order by number desc
            //limit 0,10
            LocalDateTime beginOfDay = LocalDateTime.of(begin, LocalTime.MIN);
            LocalDateTime endOfDay = LocalDateTime.of(end, LocalTime.MAX);
    
            List<GoodsSalesDTO> salesTop10 = orderMapper.getSalesTop10(beginOfDay, endOfDay);
    
            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();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    orderMapper.xml

        <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 > #{begin}
            if>
            <if test="end != null">
            and o.order_time < #{end}
            if>
            group by od.name
            order by number desc
            limit 0,10
        select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    功能测试

    在这里插入图片描述

  • 相关阅读:
    正交序列扩频_解扩
    使用Python绘制二元函数图像
    STM32物联网项目-SHT30温湿度采集(IIC通信)
    产品Web3D交互展示有什么优势?如何快速制作?
    计算机网络的概念
    05.大模型&大数据量
    一文读懂强化学习:RL全面解析与Pytorch实战
    LED球泡灯IC:PWM调光的LED恒流驱动芯片,适用于宽输入电压范围的应用
    模块引擎 Thymeleaf
    安全保障基于软件全生命周期-Istio的授权机制
  • 原文地址:https://blog.csdn.net/y_k_j_c/article/details/132788399