List couponIssueUsersLists = new ArrayList<>(); List commonCouponList = new ArrayList<>(); List productCouponList = new ArrayList<>();

// Build a set to store the IDs of the products to avoid duplicates Set productIdSet = new HashSet<>(productIds);

// Iterate over the product IDs set for (Long productId : productIdSet) { // Get the list of coupon issues for the current product from the cache or the database List couponIssueUsersList = localCache.get(productId); if (CollectionUtil.isEmpty(couponIssueUsersList)) { couponIssueUsersList = this.baseMapper.productCoupon(productId); } if (CollectionUtil.isNotEmpty(couponIssueUsersList)) { couponIssueUsersLists.addAll(couponIssueUsersList); } }

// Use a stream to remove duplicates from the list of coupon issues couponIssueUsersLists = couponIssueUsersLists.stream() .distinct() .collect(Collectors.toList());

// Build a wrapper to query the list of failed coupon users LambdaQueryWrapper wrapper = new LambdaQueryWrapper() .eq(YxStoreCouponUser::getUid, userId) .eq(YxStoreCouponUser::getIsDel, CommonEnum.DEL_STATUS_0.getValue()) .eq(YxStoreCouponUser::getIsFail, CouponEnum.FALI_0.getValue()) .ge(YxStoreCouponUser::getEndTime, new Date());

// Use a stream to filter out the failed coupon issues from the list of coupon issues couponIssueUsersLists = couponIssueUsersLists.stream() .filter(couponIssue -> couponUserService.count(new LambdaQueryWrapper() .eq(YxStoreCouponUser::getCid, couponIssue.getId()) .apply(wrapper.getSqlSegment())) == 0) .collect(Collectors.toList());

// Use a stream to sort the list of coupon issues by price couponIssueUsersLists = couponIssueUsersLists.stream() .sorted(Comparator.comparing(YxStoreCouponIssue::getCouponPrice).reversed()) .collect(Collectors.toList());

// Use a stream to divide the list of coupon issues into common and product coupon lists couponIssueUsersLists.forEach(couponIssue -> { if (couponIssue.getCtype().equals(CouponEnum.TYPE_0.getValue())) { commonCouponList.add(couponIssue); } else if (couponIssue.getCtype().equals(CouponEnum.TYPE_1.getValue())) { productCouponList.add(couponIssue); } });

// Build the result object CouponConfirmOrderVo couponConfirmOrderVo = new CouponConfirmOrderVo(); couponConfirmOrderVo.setCommonCouponList(commonCouponList); couponConfirmOrderVo.setProductCouponList(productCouponList); return couponConfirmOrderVo

优化以下代码 ListYxStoreCouponIssue couponIssueUsersLists = new ArrayList; ListYxStoreCouponIssue commonCouponList = new ArrayList; ListYxStoreCouponIssue productCouponList = new ArrayList;

原文地址: https://www.cveoy.top/t/topic/gpvX 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录