• 苍穹外卖项目学习日记(14)


    苍穹外卖项目学习日记(14) day11

    营业额统计

    • 在admin文件夹下创建ReportController类,并且添加营业额统计方法
    • ReportController.java
    package com.sky.controller.admin;
    
    import com.sky.result.Result;
    import com.sky.service.ReportService;
    import com.sky.vo.TurnoverReportVO;
    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<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
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 同理创建ReportService类,添加相应接口,并且在实现类中实现
    • ReportServiceImpl.java
    package com.sky.service.impl;
    
    import com.sky.entity.Orders;
    import com.sky.mapper.OrderMapper;
    import com.sky.service.ReportService;
    import com.sky.vo.TurnoverReportVO;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.lang3.StringUtils;
    import org.apache.poi.util.StringUtil;
    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;
        /**
         * 根据开始结束时间,进行营业额统计
         * @param begin
         * @param end
         * @return
         */
        @Override
        public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate 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) {
                LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
                LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
                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()
                    .turnoverList(StringUtils.join(turnoverList,","))
                    .dateList(StringUtils.join(dateList,","))
                    .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
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • mapper层添加相应方法
    • OrderMapper.xml
        <select id="sumByMap" resultType="java.lang.Double">
            select sum(amount) 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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    用户统计

    • 在ReportController类添加用户统计方法
    • ReportController.java
        @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));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 在ReportService类,添加相应接口,并且在实现类中实现
    • ReportServiceImpl.java
        @Override
        public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
            List<LocalDate> dateList = new ArrayList<>();
            dateList.add(begin);
            while (!begin.equals(end)){
                begin = begin.plusDays(1);
                dateList.add(begin);
            }
            List<Integer> newUserList = new ArrayList<>();
            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 totalcount = userMapper.countByMap(map);
                map.put("begin",beginTime);
                Integer newcount = userMapper.countByMap(map);
                newUserList.add(newcount);
                totalUserList.add(totalcount);
            }
            return UserReportVO
                    .builder()
                    .dateList(StringUtils.join(dateList,","))
                    .totalUserList(StringUtils.join(totalUserList,","))
                    .newUserList(StringUtils.join(newUserList,","))
                    .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
    • mapper层添加相应方法
    • UserrMapper.xml
        <select id="countByMap" resultType="java.lang.Integer">
            select count(id) from user
            <where>
                <if test="begin != null">
                    and create_time &gt; #{begin}
                </if>
                <if test="end != null">
                    and create_time &lt; #{end}
                </if>
            </where>
        </select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    订单统计

    • 在ReportController类添加订单统计方法
    • ReportController.java
        @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));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 在ReportService类,添加相应接口,并且在实现类中实现
    • ReportServiceImpl.java
        @Override
        public OrderReportVO getOrdersStatistics(LocalDate begin, LocalDate 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);
                Integer orderCount = getOrderCount(beginTime, endTime, null);
                Integer validOrderCount = getOrderCount(beginTime, endTime, Orders.COMPLETED);
                orderCountList.add(orderCount);
                validOrderCountList.add(validOrderCount);
            }
            Integer allordercount = orderCountList.stream().reduce(Integer::sum).get();
            Integer allvalidordercount = validOrderCountList.stream().reduce(Integer::sum).get();
            Double complete = 0.0;
            if (allordercount != 0){
                complete = allvalidordercount.doubleValue()/allordercount.doubleValue();
            }
            return OrderReportVO
                    .builder()
                    .dateList(StringUtils.join(dateList,","))
                    .orderCountList(StringUtils.join(orderCountList,","))
                    .validOrderCountList(StringUtils.join(validOrderCountList,","))
                    .totalOrderCount(allordercount)
                    .validOrderCount(allvalidordercount)
                    .orderCompletionRate(complete)
                    .build();
        }
    
        private Integer getOrderCount(LocalDateTime begin, LocalDateTime end, Integer status){
            Map map = new HashMap<>();
            map.put("end",end);
            map.put("begin",begin);
            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
    • mapper层添加相应方法
    • OrderMapper.xml
        <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    销量统计

    • 在ReportController类添加销量统计方法
    • ReportController.java
        @GetMapping("/top10")
        @ApiOperation("销量统计")
        public Result<SalesTop10ReportVO> top10(
                @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
            log.info("销量统计:{},{}",begin,end);
            return Result.success(reportService.getSalesTop10(begin,end));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 在ReportService类,添加相应接口,并且在实现类中实现
    • ReportServiceImpl.java
        @Override
        public SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end) {
            LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
            List<GoodsSalesDTO> goodsSalesDTOList = orderMapper.top10(beginTime, endTime);
            List<String> names = goodsSalesDTOList.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
            String nameList = StringUtils.join(names, ",");
            List<Integer> numbers = goodsSalesDTOList.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
    • mapper层添加相应方法
    • OrderMapper.xml
        <select id="top10" 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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    【数据结构】链表--双链表
    使用Nginx实现采集端和数据分析平台的数据加密传输
    记录一些使用的工具
    NISP证书有什么用?NISP证书就业方向?
    JZ68 二叉搜索树的最近公共祖先
    Mybatis源码编译
    【C++】C++多线程库的使用
    虚拟机CentOS7中无图形界面安装Oracle(保姆级安装)
    软件测试面试技巧:如何提高面试通过率?这3点一定要做到
    航迹推演通过左右轮速度得到机器人前进线速度和角速度
  • 原文地址:https://blog.csdn.net/qq_43713798/article/details/134361016