From 8ffeb751b3a694e8d1cb6a21bec855f6c49b31b6 Mon Sep 17 00:00:00 2001
From: luodangjia <luodangjia>
Date: 星期五, 07 二月 2025 15:38:46 +0800
Subject: [PATCH] 修改物流信息导入模板

---
 ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/OrderServiceImpl.java |  164 ++++++++++++++++++++++++++++++++----------------------
 1 files changed, 98 insertions(+), 66 deletions(-)

diff --git a/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/OrderServiceImpl.java b/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/OrderServiceImpl.java
index 41cf60b..81f0d2c 100644
--- a/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/OrderServiceImpl.java
+++ b/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/OrderServiceImpl.java
@@ -60,6 +60,7 @@
 import java.time.ZoneOffset;
 import java.time.format.DateTimeFormatter;
 import java.util.*;
+import java.util.function.Function;
 import java.util.stream.Collectors;
 
 /**
@@ -310,78 +311,112 @@
      */
     @Override
     public PageInfo<OrderPageListVo> getOrderPageList(OrderPageList orderPageList) {
-        Long userid = tokenService.getLoginUser().getUserid();
-        SysUser sysUser = sysUserClient.getSysUser(userid).getData();
-        if(2 == sysUser.getRoleType()){
+        Long userId = tokenService.getLoginUser().getUserid();
+        SysUser sysUser = sysUserClient.getSysUser(userId).getData();
+
+        // 设置店铺ID
+        if (sysUser.getRoleType() == 2) {
             orderPageList.setShopId(sysUser.getObjectId());
         }
-        //搜索条件,用户姓名
-        if(StringUtils.isNotEmpty(orderPageList.getUserName())){
-            List<AppUser> data = appUserClient.getAppUserByName(orderPageList.getUserName()).getData();
-            List<Long> collect = data.stream().map(AppUser::getId).collect(Collectors.toList());
-            if (CollectionUtils.isEmpty(collect)){
-                return new PageInfo<>();
-            }
-            if(null != orderPageList.getAppUserIds()){
-                List<Long> appUserIds = orderPageList.getAppUserIds();
-                appUserIds.addAll(collect);
-                orderPageList.setAppUserIds(appUserIds);
-            }else{
-                orderPageList.setAppUserIds(collect);
-            }
-        }
-        //搜索条件,用户电话
-        if(StringUtils.isNotEmpty(orderPageList.getPhone())){
-            List<AppUser> data = appUserClient.getAppUserByPhone(orderPageList.getPhone()).getData();
-            List<Long> collect = data.stream().map(AppUser::getId).collect(Collectors.toList());
-            if (CollectionUtils.isEmpty(collect)){
-               return new PageInfo<>();
-            }
-            if(null != orderPageList.getAppUserIds()){
-                List<Long> appUserIds = orderPageList.getAppUserIds();
-                appUserIds.addAll(collect);
-                orderPageList.setAppUserIds(appUserIds);
-            }else{
-                orderPageList.setAppUserIds(collect);
-            }
-        }
 
+        // 处理用户姓名搜索条件
+        processAppUserIds(orderPageList, orderPageList.getUserName(), appUserClient::getAppUserByNameNoFilter);
 
-        
+        // 处理用户电话搜索条件
+        processAppUserIds(orderPageList, orderPageList.getPhone(), appUserClient::getAppUserByPhoneNoFilter);
+
+        // 去重 appUserIds
+        Optional.ofNullable(orderPageList.getAppUserIds())
+                .ifPresent(ids -> orderPageList.setAppUserIds(ids.stream().distinct().collect(Collectors.toList())));
+
+        log.error("orderPageList:"+orderPageList.getAppUserIds());
+        // 分页查询订单列表
         PageInfo<OrderPageListVo> pageInfo = new PageInfo<>(orderPageList.getPageCurr(), orderPageList.getPageSize());
-
         List<OrderPageListVo> list = this.baseMapper.getOrderPageList(pageInfo, orderPageList);
-        for (OrderPageListVo orderPageListVo : list) {
-            Long appUserId = orderPageListVo.getAppUserId();
-            AppUser appUser = appUserClient.getAppUserById(appUserId);
-            if(null != appUser){
-                orderPageListVo.setUserName(appUser.getName());
-                orderPageListVo.setPhone(appUser.getPhone());
-            }
-            RefundPass one = refundPassService.getOne(new LambdaQueryWrapper<RefundPass>().eq(RefundPass::getOrderId, orderPageListVo.getId()).eq(RefundPass::getDelFlag, 0).last(" order by create_time desc limit 0,1"));
-            orderPageListVo.setRefundPassId(null != one ? one.getId().toString() : null);
 
-            // 平台分佣
-            List<ShopBalanceStatement> data = shopBalanceStatementClient.getShopBalanceStatementList(Arrays.asList(1, 2, 3), Long.valueOf(orderPageListVo.getId())).getData();
-            BigDecimal reduce = data.stream().map(ShopBalanceStatement::getVariableAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
-            orderPageListVo.setGetCommission(reduce);
+        // 处理订单列表中的每个订单
+        list.forEach(this::processOrderPageListVo);
 
-            String expressJson = orderPageListVo.getExpressJson();
-            if (StringUtils.isNotEmpty(expressJson) && !expressJson.equals("NULL")){
-                JSONObject jsonObject = null;
-                try {
-                    jsonObject = JSONObject.parseObject(expressJson);
-                    String companyName = ExpressCompanyMap.getCompanyNameByCode(jsonObject.getString("com"));
-                    orderPageListVo.setExpressCompany(companyName);
-                    orderPageListVo.setExpressNum(jsonObject.getString("num"));
-                } catch (Exception e) {
-                }
-
-            }
-
-        }
         return pageInfo.setRecords(list);
     }
+
+
+
+    private void processAppUserIds(OrderPageList orderPageList, String searchKey, Function<String, R<List<AppUser>>> userSearchFunction) {
+        if (StringUtils.isNotEmpty(searchKey)) {
+            List<Long> userIds = Optional.ofNullable(userSearchFunction.apply(searchKey).getData())
+                    .orElse(Collections.emptyList())
+                    .stream()
+                    .map(AppUser::getId)
+                    .collect(Collectors.toList());
+
+            if (CollectionUtils.isEmpty(userIds)) {
+                return;
+            }
+
+            List<Long> existingUserIds = orderPageList.getAppUserIds();
+            if (existingUserIds != null) {
+                if (!containsAny(existingUserIds, userIds)) {
+                    return;
+                }
+                existingUserIds.addAll(userIds);
+            } else {
+                orderPageList.setAppUserIds(userIds);
+            }
+        }
+    }
+
+    private void processOrderPageListVo(OrderPageListVo orderPageListVo) {
+        Long appUserId = orderPageListVo.getAppUserId();
+        Optional.ofNullable(appUserClient.getAppUserById(appUserId))
+                .ifPresent(appUser -> {
+                    orderPageListVo.setUserName(appUser.getName());
+                    orderPageListVo.setPhone(appUser.getPhone());
+                });
+
+        Optional.ofNullable(refundPassService.getOne(new LambdaQueryWrapper<RefundPass>()
+                        .eq(RefundPass::getOrderId, orderPageListVo.getId())
+                        .eq(RefundPass::getDelFlag, 0)
+                        .orderByDesc(RefundPass::getCreateTime)
+                        .last("limit 1")))
+                .ifPresent(refundPass -> orderPageListVo.setRefundPassId(refundPass.getId().toString()));
+
+        // 平台分佣
+        BigDecimal commission = Optional.ofNullable(shopBalanceStatementClient.getShopBalanceStatementList(Arrays.asList(1, 2, 3), Long.valueOf(orderPageListVo.getId())).getData())
+                .orElse(Collections.emptyList())
+                .stream()
+                .map(ShopBalanceStatement::getVariableAmount)
+                .reduce(BigDecimal.ZERO, BigDecimal::add);
+        orderPageListVo.setGetCommission(commission);
+
+        // 处理快递信息
+        Optional.ofNullable(orderPageListVo.getExpressJson())
+                .filter(expressJson -> !expressJson.equals("NULL"))
+                .ifPresent(expressJson -> {
+                    try {
+                        JSONObject jsonObject = JSONObject.parseObject(expressJson);
+                        orderPageListVo.setExpressCompany(ExpressCompanyMap.getCompanyNameByCode(jsonObject.getString("com")));
+                        orderPageListVo.setExpressNum(jsonObject.getString("num"));
+                    } catch (Exception e) {
+                    }
+                });
+    }
+
+    /**
+     * 判断两个列表中是否至少有一个相同的元素
+     * 此方法用于检查两个长整型列表之间的元素交集
+     * 主要解决的问题是确定两个列表是否有共同的元素,用于避免潜在的数据不一致或错误
+     *
+     * @param list1 第一个列表,包含一系列长整型数值
+     * @param list2 第二个列表,同样包含一系列长整型数值
+     * @return 返回一个布尔值,如果两个列表中至少有一个相同的元素,则返回true,否则返回false
+     */
+    private boolean containsAny(List<Long> list1, List<Long> list2) {
+        return list1.stream().anyMatch(list2::contains) || list2.stream().anyMatch(list1::contains);
+    }
+
+
+
 
 
 
@@ -457,10 +492,7 @@
         return R.ok();
     }
 
-    public static void main(String[] args) {
-        System.out.println(LocalDateTime.now().toEpochSecond(ZoneOffset.UTC));
-    }
-    
+
     
     /**
      * 取消订单操作

--
Gitblit v1.7.1