Pu Zhibing
2025-02-08 d94776ba25ffcd19cdd7970048ed88d9212bd394
ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/OrderServiceImpl.java
@@ -43,6 +43,7 @@
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jetbrains.annotations.Nullable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -311,116 +312,107 @@
     */
    @Override
    public PageInfo<OrderPageListVo> getOrderPageList(OrderPageList orderPageList) {
        Long userId = tokenService.getLoginUser().getUserid();
        SysUser sysUser = sysUserClient.getSysUser(userId).getData();
        // 设置店铺ID
        if (sysUser.getRoleType() == 2) {
        Long userid = tokenService.getLoginUser().getUserid();
        SysUser sysUser = sysUserClient.getSysUser(userid).getData();
        if(2 == sysUser.getRoleType()){
            orderPageList.setShopId(sysUser.getObjectId());
        }
        //搜索条件,用户姓名
        if(StringUtils.isNotEmpty(orderPageList.getUserName())){
            List<AppUser> data = appUserClient.getAppUserByNameNoFilter(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.getAppUserByPhoneNoFilter(orderPageList.getPhone()).getData();
            List<Long> collect = data.stream().map(AppUser::getId).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(collect)){
                return new PageInfo<>();
            }
        // 处理用户姓名搜索条件
        processAppUserIds(orderPageList, orderPageList.getUserName(), appUserClient::getAppUserByNameNoFilter);
        // 处理用户电话搜索条件
        processAppUserIds(orderPageList, orderPageList.getPhone(), appUserClient::getAppUserByPhoneNoFilter);
        if (orderPageList.getAppUserIds() == null) {
            return new PageInfo<>();
            if(null != orderPageList.getAppUserIds()){
                List<Long> appUserIds = orderPageList.getAppUserIds();
                if (!containsAny(appUserIds,collect)) {
                    return new PageInfo<>();
                }
                appUserIds.addAll(collect);
                orderPageList.setAppUserIds(appUserIds);
            }else{
                orderPageList.setAppUserIds(collect);
            }
        }
        if (null != orderPageList.getAppUserIds()){
            orderPageList.setAppUserIds(orderPageList.getAppUserIds().stream().distinct().collect(Collectors.toList()));
        }
        // 去重 appUserIds
        Optional.of(orderPageList.getAppUserIds())
                .ifPresent(ids -> orderPageList.setAppUserIds(ids.stream().distinct().collect(Collectors.toList())));
        // 分页查询订单列表
        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.forEach(this::processOrderPageListVo);
            // 平台分佣
            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);
            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) {
                        e.printStackTrace();
                        throw new RuntimeException("快递信息解析失败");
                    }
                });
    }
    /**
     * 判断两个列表中是否至少有一个相同的元素
     * 此方法用于检查两个长整型列表之间的元素交集
     * 主要解决的问题是确定两个列表是否有共同的元素,用于避免潜在的数据不一致或错误
     * 判断 list1 是否包含 list2 中的至少一个元素
     *
     * @param list1 第一个列表,包含一系列长整型数值
     * @param list2 第二个列表,同样包含一系列长整型数值
     * @return 返回一个布尔值,如果两个列表中至少有一个相同的元素,则返回true,否则返回false
     * @param list1 第一个列表
     * @param list2 第二个列表
     * @return 如果 list1 包含 list2 中的至少一个元素,返回 true;否则返回 false
     */
    private boolean containsAny(List<Long> list1, List<Long> list2) {
        return list1.stream().anyMatch(list2::contains) || list2.stream().anyMatch(list1::contains);
        // 将 list1 转换为 HashSet 以提高查询效率
        Set<Long> set1 = new HashSet<>(list1);
        // 遍历 list2,检查是否有元素存在于 set1 中
        for (Long element : list2) {
            if (set1.contains(element)) {
                return true;
            }
        }
        // 如果没有找到共同元素,返回 false
        return false;
    }