《苍穹外卖》项目学习记录-Day11订单统计

news/2025/2/3 14:30:00 标签: 学习

根据起始时间和结束时间,先把begin放入集合中用while循环当begin不等于end的时候,让begin加一天,这样就把这个区间内的时间放到List集合。

查询每天的订单总数也就是查询的时间段是大于当天的开始时间(0点0分0秒)小于当天的结束时间的(23点59分59秒)的。

//查询每天的订单总数 select count(id) from orders where order_time > ? and order_time < ?

查询有效订单数,不仅是当天的时间段还要状态是已完成的订单,status=5代表已完成。

//查询每天的有效订单数 select count(id) from orders where order_time > ? and order_time < ? and status = 5

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

Stream流的使用步骤

1.获取Stream流?

·获取集合的Stream流

Collection提供的如下方法         说明

default Stream<E> stream()      获取当前集合对象的Stream流

·获取数组的Stream流

Arrays类提供的如下方法            说明

public static <T> Stream<T> stream(T[] array)  获取当前数组的Stream流

public static <T> Stream<T> of(T...values)         获取当前接收数据的Stream流

中间方法指的是调用完成后会返回新的Stream流,可以继续使用(支持链式编程)。

终结方法指的是调用完成后,不会返回新的Stream了,没法继续使用流了。

2.Stream提供的常用中间方法     说明

Stream <T> filter(Predicate<? super T>predicate)    用于对流中的数据进行过滤

Stream <T> sorted()                                                  对元素进行升序排序

Stream <T> sorted(Comparator<? super T> comparator) 按照指定规则排序

Stream <T> limit(long maxSize)                                 获取前几个元素

Stream <T> skip(long n)                                             跳过前几个元素

Stream <T> distinct()                                                  去除流中重复的元素

<R>Stream<R> map(Function<? super T,?extends R> mapper)

对元素进行加工,并返回对应的新流

static <T> Stream<T> concat(Stream a,Stream b)     合并a和b两个流为一个流

3.Stream流常见的终结方法

Stream提供的常用终结方法               说明

void forEach(Consumer action)          对此流运算后的元素执行遍历

long count()                                         统计此流运算后的元素个数

Optional<T> max(Comparator<? super T> comparator)

获取此流运算后最大值元素

Optional<T> min(Comparator<? super T> comparator)

获取此流运算后最小值元素

收集Stream流:就是把Stream流操作后的结果转回到集合或者数组中去返回。

Stream流:方便操作集合/数组手段;集合/数组:才是开发中的目的。

Stream提供的常用终结方法                说明

R collect(Collector collector)                把流处理后的结果收集到一个指定的集合中去

Object toArray()                                   把流处理后的结果收集到一个数组中去

流只能收集一次

Collectors工具类提供了具体的收集方式             说明

public static <T> Collector toList()                       把元素收集到List集合中去

public static <T> Collector toSet()                       把元素收集到Set集合中去

public static Collector toMap(Function KeyMapper,Function valueMapper)

把元素收集到Map集合中

我的笔记中没有这个Stream.reduce()方法,所以我去查了一下。

Stream.reduce()是Java 8引入的一个强大的工具,用于对流中的元素进行归纳操作。

reduce方法可以将流中的多个元素组合成一个单一的结果,通常用于求和、求乘积、查找最大值或最小值等操作。

reduce方法的基本用法

reduce(BinaryOperator<T> accumulator)

这种方法没有初始值,流的第一个元素将作为初始值。返回的是Optional<T>,以防流为空。例如:

Optional<Integer> sum = list.stream().reduce((a,b)->a + b);

2.有初始值‌:T reduce(T identity, BinaryOperator<T> accumulator)。这种方法有一个初始值identity,可以保证即使流为空也会有一个默认结果。返回的是T。例如:
int sum = list.stream().reduce(0, (a, b) -> a + b);
‌3.并行流操作‌:reduce(U identity, BiFunction<U, T, U> accumulator, BinaryOperator<U> combiner)。这种方法适用于并行流操作,可以通过两个函数实现累加器和合并器的分离。例如:
int sum = list.parallelStream().reduce(0, (partialResult, element) -> partialResult + element, Integer::sum);
reduce方法的应用场景
‌1.求和‌:将流中的所有元素相加。例如,计算一个整数列表的总和。
‌2.求乘积‌:将流中的所有元素相乘。例如,计算一个整数列表的乘积。
‌3.查找最大值或最小值‌:通过比较操作,找到流中的最大值或最小值。
‌4.连接字符串‌:将流中的字符串元素连接成一个新的字符串。例如,将多个单词连接成一个句子。
‌5.自定义聚合操作‌:通过自定义的累加器函数,实现更复杂的聚合操作。例如,计算两个数的平均值。

 

 


http://www.niftyadmin.cn/n/5840869.html

相关文章

从零开始:用Qt开发一个功能强大的文本编辑器——WPS项目全解析

文章目录 引言项目功能介绍1. **文件操作**2. **文本编辑功能**3. **撤销与重做**4. **剪切、复制与粘贴**5. **文本查找与替换**6. **打印功能**7. **打印预览**8. **设置字体颜色**9. **设置字号**10. **设置字体**11. **左对齐**12. **右对齐**13. **居中对齐**14. **两侧对…

IELTS口语练习题库

IELTS口语1-4月题库 Part 1 Gifts Have you ever sent handmade gifts to others? Yes, I have. I once made a scrapbook for my best friend’s birthday. It included photos of our memories together and some handwritten notes. She loved it because it was personal…

Swoole如何实现多进程

Swoole 是一个高性能的 PHP 扩展&#xff0c;它提供了多进程模型来解决传统 PHP 环境中的并发处理问题。以下是 Swoole 实现多进程的具体方式和原理&#xff1a; 一、多进程模型的实现 进程创建与管理&#xff1a; Swoole 提供了 Swoole\Process 类&#xff08;或在旧版本中使…

abc 390 D(暴搜 复杂度用 bell数 证明 n 的集合的划分方法的数目)

D题意&#xff1a; 将 长度为 N 的数组 划分为集合 有多少种不同的 异或和 这道题做出来和bell 数没什么关系&#xff0c;如果要证明复杂度那么就需要bell 数 #include <bits/stdc.h> using namespace std; typedef pair<int, int> PII; #define int long long i…

无需破解版这个永久免费

聊一聊 有人问我有没有可以把视频或音频人的声音和音乐分离出来的软件。 这个当然有。 只要你能想出来的&#xff0c;基本都有工具能实现。 就怕你想不出来。 有时候能想出来&#xff0c;但不知道怎么表达。 所以&#xff0c;关注我&#xff0c;给大家分享实用、有趣的软…

力扣经典题目之3无重复字符的最长子串

今天继续给大家分享一道力扣的做题心得今天这道题目是 无重复字符的最长子串3. 无重复字符的最长子串 - 力扣&#xff08;LeetCode&#xff09; 题目如下&#xff0c;点击上面题目名称即可跳转到力扣对应题目页面也来挑战这道题 1&#xff0c;题目分析 此题目不难&#xff0c…

035 搜索之DFS基础

DFS&#xff1a;深度优先搜索——本质上是暴力枚举&#xff0c;尽可能一条路走到底&#xff0c;走不了回退 1.DFS与n重循环 例&#xff1a;给定一个数字x&#xff0c;将其拆分为3个正整数&#xff0c;后一个要求大于前一个&#xff0c;给出方案。 分析&#xff1a;这种情况下…

如何实现滑动列表功能

文章目录 1 概念介绍2 使用方法3 示例代码 我们在上一章回中介绍了沉浸式状态栏相关的内容&#xff0c;本章回中将介绍SliverList组件.闲话休提&#xff0c;让我们一起Talk Flutter吧。 1 概念介绍 我们在这里介绍的SliverList组件是一种列表类组件&#xff0c;类似我们之前介…