苍穹外卖8-Day11

数据统计

Apache ECharts(前端技术,了解即可)

Apache Echarts 是一款基于Javascript的数据可视化图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表
官网地址:https://echarts.apache.org/zh/index.html

快速上手-下载echarts.js

快速上手 - 使用手册 - Apache ECharts

营业额统计

需求分析和设计

接口设计

具体代码开发

控制层-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);
        return Result.success(reportService.getTrunoverStatistics(begin, end));
    }
}

业务层-ReportServiceImpl

 @Autowired
    private OrderMapper orderMapper;

    /**
     * 统计指定时间区间内的营业额数据
     *
     * @param begin
     * @param end
     * @return
     */
    public TurnoverReportVO getTrunoverStatistics(LocalDate begin, LocalDate end) {
        //用集合存放begin到end范围内的每天日期
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);

        while (!begin.equals(end)){
             begin = begin.plusDays(1);// 日期+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);

            // 封装map 准备调用mapper查询
            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; // 判断当天营业额是否为0,为0要处理赋值,要不然会出现空值
            turnoverList.add(turnover);
        }
        //封装返回结果
        return TurnoverReportVO
                .builder()
                .dateList(StringUtils.join(dateList,","))  //使用join分割dateList,并存进TurnoverReportVO.dateList
                .turnoverList(StringUtils.join(turnoverList,","))
                .build();
    }

数据持久层-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>

用户统计

需求分析和设计

原型图

业务规则

接口设计

具体代码开发

控制层-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
     */
    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);// 日期+1天
            dateList.add(begin);
        }

        //存放每天的新增用户数量 select count(id)from user where create time < ? and create time > ?
        List<Integer> newUserList = new ArrayList<>();
        //存放每天的总用户数量 select count(id)from user where create time < ?
        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.countMap(map);

            map.put("begin",beginTime);
            Integer newUser = userMapper.countMap(map);

            totalUserList.add(totalUser);
            newUserList.add(newUser);
        }
        //封装结果并返回
        return UserReportVO
                .builder()
                .dateList(StringUtils.join(dateList,","))
                .newUserList(StringUtils.join(newUserList,","))
                .totalUserList(StringUtils.join(totalUserList,","))
                .build();
    }

数据持久层-userMapper.xml

    <select id="countMap" 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>

订单统计

需求分析和设计

原型图

业务规则

接口设计

具体代码开发

控制层-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
     */
    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);// 日期+1天
            dateList.add(begin);
        }

        //存放每天的订单总数
        List<Integer> orderCountList = new ArrayList<>();
        List<Integer> validOrderCountList = new ArrayList<>();
        //存放每天的有效订单总数


        for (LocalDate date : dateList) {
            //查询每天的订单总数  select count(id) from oders where order_time > ? and order_time < ?
            // 因为order_time和date类型不一致,所以要比一天的起始时间(00:00)大;比结束时间小(23:59.9999)
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN); //把日期时间转换为时分秒的时间格式
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Integer orderCount = getOrderCount(beginTime, endTime, null);
            //查询每天的有效订单数  select count(id) from oders 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 orderComplteionRate = 0.0;
                if (totalOrderCount !=0){
                    //计算订单完成率
                    orderComplteionRate = validOrderCount.doubleValue() / totalOrderCount;

                }
        return OrderReportVO
                .builder()
                .dateList(StringUtils.join(dateList,","))
                .orderCountList(StringUtils.join(orderCountList,","))
                .validOrderCountList(StringUtils.join(validOrderCountList,","))
                .totalOrderCount(totalOrderCount)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderComplteionRate)
                .build();
    }
    /**
     * 根据条件统计订单数量
     *@param status
     * @param begin
     * @param end
     * @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.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>

销量排名

需求分析和设计

原型图

业务规则

接口设计

具体代码开发

控制层

    /**
     * 销量排名top10
     *
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/top10")
    @ApiOperation("销量排名top10")
    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));
    }

业务层

 /**
     * 统计指定时间范围内的销量排名前10的商品
     *
     * @param begin
     * @param end
     * @return
     */
    public SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end) {
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN); //把日期时间转换为时分秒的时间格式
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);

        //调用mapper查询指定时间范围内的销量排名前10的商品
        List<GoodsSalesDTO> salesTop10 = orderMapper.getSalesTop(beginTime, endTime);
        //处理salesTop10中的数据
        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.xml

    <select id="getSalesTop" 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 order_time &gt; #{begin}
        </if>
        <if test="end != null " >
            and order_time &lt; #{end}
        </if>
        group by od.name
        order by number desc
        limit 0,10
    </select>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值