Java 流式编程实现业务分组并根据分组后的数据条数做对比

业务场景:

1.需要根据路由表中运单id和数据状态(为 107妥投 ,117部分妥投, 108拒签 )的数据进行分组,并取出分组后数量

2.需要根据运单id 的集合去订单表及订单详情表查询对应的数据,并运单id 进行分组,并取出分组后数量

3.判断条件,如果路由表根据运单id和状态分组后的数量 == 订单详情表中的数量 ,做后续业务处理

数据库:
路由表:billIdstatus
TESTXS20210517117
TESTXS20210517117
TESTXS20210517107
TESTXS20210517108
TESTXS20210519117
TESTXS20210519108
订单详情:billIdmess
TESTXS202105171
TESTXS202105171
TESTXS202105170
TESTXS202105170
TESTXS202105190
TESTXS202105191

思路:

  1. 首先根据条件查询出所有数据
  2. 其次查询出符合状态的数据: 使用stream().filter() 进行过滤拦截
  3. 然后对取的数据做处理: 需要根据账单1分组,在根据状态分组,最后取得不同账单的不同状态数据。 Map<账单1, Map<状态, List<数据vo>>> billMap
  4. 有了账单1,就可以根据账单1关联订单数据,查询出订单集合
  5. 所以需要获取账单1的集合,根据账单1集合查询账单1的所以订单 List<String> billList = whList.stream().map(WaybillStatusOutsideVo::getRelatedBill1).collect(Collectors.toList());
  6. 变更后进行了优化去重:List<String> billList = whList.stream().map(AuxWaybillStatusOutsideVo::getRelatedBill1).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
  7. 获取根据账单1获取订单数据 List<WayCallBackVo> orderList = WaybillStatusOutsideMapper.findWayBillDetailData(billList);
  8. 将订单数据按照账单1号 进行分组 Map<String, List<WayCallBackVo>> orderMap = orderList.stream().collect(Collectors.groupingBy(WayCallBackVo::getRelatedBill1));
  9. 最后在循环账单1 进行判断,订单数量及账单数量
 billMap.forEach((billId, valueMap) -> {
        valueMap.forEach((stutus,vos) ->{
            // 如果条数相等
            if (orderMap.get(billId).size() == vos.size()) {
                 // 业务处理
            } else {
                 failCount.getAndIncrement();
                 map.put(billId, back.getMsgContent());
                 failNos.add(map);
            }
            }
        });
    });
    

代码:

try {
    // 查询所有数据
    List<WaybillStatusOutsideVo> list = WaybillStatusOutsideMapper.findWayBillData(SonyaConstant.COMP_CODE);
    // 查询符合状态数据
    List<WaybillStatusOutsideVo> whList = list.stream().filter(l -> !l.getXiaomiStatus().equals(SonyaConstant.XIAOMISTATUS)
            && (l.getOrderStatus().equals(SonyaConstant.GOP_WAY_SIGN)
            || l.getOrderStatus().equals(SonyaConstant.GOP_WAY_UNSIGN)
            || l.getOrderStatus().equals(SonyaConstant.GOP_WAY_PARTSIGN))
    ).collect(Collectors.toList());
    // 路由分组Map
    Map<String, Map<String, List<WaybillStatusOutsideVo>>> billMap = new HashMap<>();
    whList.stream().collect(Collectors.groupingBy(WaybillStatusOutsideVo::getRelatedBill1)).forEach((bill1, billVos) -> {
        Map<String, List<WaybillStatusOutsideVo>> result = new HashMap<>();
        billVos.stream().collect(Collectors.groupingBy(WaybillStatusOutsideVo::getOrderStatus)).forEach((status, vos) ->
                result.put(status, vos));
        billMap.put(bill1, result);
    });
    // 查询所有订单信息
    List<String> billList = whList.stream().map(WaybillStatusOutsideVo::getRelatedBill1).collect(Collectors.toList());
    // 订单分组Map
    List<WayCallBackVo> orderList = WaybillStatusOutsideMapper.findWayBillDetailData(billList);
    Map<String, List<WayCallBackVo>> orderMap = orderList.stream().collect(Collectors.groupingBy(WayCallBackVo::getRelatedBill1));
    
    billMap.forEach((billId, valueMap) -> {
        valueMap.forEach((stutus,vos) ->{
            // 如果条数相等
            if (orderMap.get(billId).size() == vos.size()) {
                 // 业务处理
            } else {
                 failCount.getAndIncrement();
                 map.put(billId, back.getMsgContent());
                 failNos.add(map);
            }
            }
        });
    });
} catch (Exception e) {
    e.printStackTrace();
    //业务处理
}

开发总是在变更中进行,心态很重要

业务场景

1.需要根据路由表中运单id和数据状态(为 107妥投 ,117部分妥投, 108拒签 )的数据进行分组,并取出分组后数量

2.需要根据运单id 的集合去订单表及订单详情表查询对应的数据,并运单id 进行分组,并取出分组后数量

3.判断条件,如果路由表根据运单id和状态分组后的数量 == 订单详情表中的数量 ,做后续业务处理
4.需求变更 1.妥投状态为(order_status=107+117,waybill_status = 107),拒签状态为(order_status=108,waybill_status = 108)
5.需求变更2:拒签数据可能存在一条拒签,一条未拒签情况,所以只要有一条拒签,我们就默认全部拒签

思路:

  1. 首先根据条件查询出所有数据
  2. 其次查询出符合状态的数据: 使用stream().filter() 进行过滤拦截
  3. 然后对取的数据做处理: 需要根据账单1分组,在根据状态分组,最后取得不同账单的不同状态数据。 Map<账单1, Map<状态, List<数据vo>>> billMap
  4. 需求变更后,状态的类型不为107,117,108,而是变更为sign,unsign,为了区分妥投和拒签。
  5. 所以有了一个判断,如果状态为107或者117,存入sign,否则存入unsign,
  6. 有了账单1,就可以根据账单1关联订单数据,查询出订单集合
  7. 所以需要获取账单1的集合,根据账单1集合查询账单1的所以订单 List<String> billList = whList.stream().map(WaybillStatusOutsideVo::getRelatedBill1).collect(Collectors.toList());
  8. 变更后进行了优化去重:List<String> billList = whList.stream().map(AuxWaybillStatusOutsideVo::getRelatedBill1).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
  9. 获取根据账单1获取订单数据 List<WayCallBackVo> orderList = WaybillStatusOutsideMapper.findWayBillDetailData(billList);
  10. 将订单数据按照账单1号 进行分组 Map<String, List<WayCallBackVo>> orderMap = orderList.stream().collect(Collectors.groupingBy(WayCallBackVo::getRelatedBill1));
  11. 最后在循环账单1 进行判断,订单数量及账单数量

代码:

 public void wayBillJob() {
      
        AtomicInteger failCount = new AtomicInteger();
        List<Map<String, String>> failNos = new ArrayList<>();
        try {
            //获取奥克斯客户账号
            String accountNo = getKey();
            // 查询所有数据
            List<WaybillStatusOutsideVo> list = waybillStatusOutsideMapper.findWayBillData(accountNo);
            if(!list.isEmpty()){
                // 查询符合状态数据
                List<WaybillStatusOutsideVo> whList = list.stream().filter(l -> !l.getXiaomiStatus().equals(SonyaConstant.XIAOMISTATUS)
                        && (l.getOrderStatus().equals(SonyaConstant.GOP_WAY_SIGN)
                        || l.getOrderStatus().equals(SonyaConstant.GOP_WAY_UNSIGN)
                        || l.getOrderStatus().equals(SonyaConstant.GOP_WAY_PARTSIGN))
                ).collect(Collectors.toList());
                if (!whList.isEmpty()){
                    // 路由分组Map
                    // 类型: 账单1-bill1,状态-status,数据-vos
                    Map<String, Map<String, List<WaybillStatusOutsideVo>>> billMap = new HashMap<>();
                    whList.stream().collect(Collectors.groupingBy(WaybillStatusOutsideVo::getRelatedBill1)).forEach((bill1, billVos) -> {
                        // 类型: 状态-status,数据-vos    eg: 117,vos; 107 vos; 108 vos;
                        Map<String, List<WaybillStatusOutsideVo>> result = new HashMap<>();
                        billVos.stream().collect(Collectors.groupingBy(WaybillStatusOutsideVo::getOrderStatus)).forEach((status, vos) ->{
                            // 状态-status,sign=117+107; unsign= 108;
                            // 第11版需求变更,过滤数据 waybill_status = 108 拒签(order_status=108,waybill_status = 108)
                            if(status.equals(SonyaConstant.GOP_WAY_UNSIGN)){
                                if(result.get(unsign) != null ) {
                                    List<WaybillStatusOutsideVo> un = result.get(unsign);
                                    un.addAll(vos.stream().filter(v -> v.getWaybillStatus().equals(SonyaConstant.GOP_WAY_UNSIGN)).collect(Collectors.toList()));
                                    result.put(unsign,un);
                                }else{
                                    result.put(unsign, vos.stream().filter(v -> v.getWaybillStatus().equals(SonyaConstant.GOP_WAY_UNSIGN)).collect(Collectors.toList()));
                                }
                            }else{
                                if(result.get(sign) != null ) {
                                    List<WaybillStatusOutsideVo> sn = result.get(sign);
                                    sn.addAll(vos.stream().filter(v -> v.getWaybillStatus().equals(SonyaConstant.GOP_WAY_SIGN)).collect(Collectors.toList()));
                                    result.put(sign,sn);
                                }else{
                                    result.put(sign, vos.stream().filter(v -> v.getWaybillStatus().equals(SonyaConstant.GOP_WAY_SIGN)).collect(Collectors.toList()));
                                }
                            }});
                        billMap.put(bill1, result);
                    });
                    // 查询所有订单信息
                    List<String> billList = whList.stream().map(WaybillStatusOutsideVo::getRelatedBill1).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
                    // 订单分组Map
                    if(!billList.isEmpty()){
                        List<WayCallBackVo> orderList = WaybillStatusOutsideMapper.findWayBillDetailData(billList);
                        if(!orderList.isEmpty()){
                            Map<String, List<WayCallBackVo>> orderMap = orderList.stream().collect(Collectors.groupingBy(WayCallBackVo::getRelatedBill1));
                            // 从redis获取token
                            String signature = OrderService.getToken();
                            billMap.forEach((billId, valueMap) -> {
                                valueMap.forEach((stutus,vos) ->{
                                    // 第12版需求变更,拒签108, 可能存在多个商品,只有一条拒签记录的请况,所以如果这样,我们默认一条拒签为全部拒签
                                    if ( !orderMap.get(billId).isEmpty() && !vos.isEmpty() ){
                                        if(orderMap.get(billId).size() == vos.size() || stutus.equals(unsign)) {
                                            // 判断条件  对比订单详情数量 和 路由表中数据数量,一致为妥投回传,不一致不回传
                                            Map<String, String> map = new HashMap<>();
                                            // 业务处理
                                            if (SonyaConstant.SUCCESS_TRUE.equals(back.getSuccess())) {
                                                // 业务处理
                                            } else {
                                                failCount.getAndIncrement();
                                                map.put(billId, back.getMsgContent());
                                                failNos.add(map);
                                             
                                            }
                                        }
                                    }
                                });
                            });
                        }
                    }


                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            // 业务处理
        }
    }

加油!

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值