Commit 7be53995 by fanjiaxin

联调问题处理

parent 209b97f8
Pipeline #71594 passed with stages
in 1 minute 6 seconds
...@@ -3,12 +3,13 @@ package com.netease.yanxuan.wx.store.sharer.biz.service.impl; ...@@ -3,12 +3,13 @@ package com.netease.yanxuan.wx.store.sharer.biz.service.impl;
import com.netease.yanxuan.wx.store.sharer.biz.config.DrmSharerConfig; import com.netease.yanxuan.wx.store.sharer.biz.config.DrmSharerConfig;
import com.netease.yanxuan.wx.store.sharer.biz.core.LoginUserContextHolder; import com.netease.yanxuan.wx.store.sharer.biz.core.LoginUserContextHolder;
import com.netease.yanxuan.wx.store.sharer.biz.core.LoginUserInfo; import com.netease.yanxuan.wx.store.sharer.biz.core.LoginUserInfo;
import com.netease.yanxuan.wx.store.sharer.common.exception.NoBindSharerException;
import com.netease.yanxuan.wx.store.sharer.biz.meta.model.vo.ProductListVO; import com.netease.yanxuan.wx.store.sharer.biz.meta.model.vo.ProductListVO;
import com.netease.yanxuan.wx.store.sharer.biz.meta.model.vo.ProductPromotionLinkVO; import com.netease.yanxuan.wx.store.sharer.biz.meta.model.vo.ProductPromotionLinkVO;
import com.netease.yanxuan.wx.store.sharer.biz.meta.page.PageQuery; import com.netease.yanxuan.wx.store.sharer.biz.meta.page.PageQuery;
import com.netease.yanxuan.wx.store.sharer.biz.meta.page.PageVO; import com.netease.yanxuan.wx.store.sharer.biz.meta.page.PageVO;
import com.netease.yanxuan.wx.store.sharer.biz.service.IProductService; import com.netease.yanxuan.wx.store.sharer.biz.service.IProductService;
import com.netease.yanxuan.wx.store.sharer.common.exception.NoBindSharerException;
import com.netease.yanxuan.wx.store.sharer.common.util.AsyncUtils;
import com.netease.yanxuan.wx.store.sharer.integration.handler.impl.WeChatShopPromoteProductDetailRequest; import com.netease.yanxuan.wx.store.sharer.integration.handler.impl.WeChatShopPromoteProductDetailRequest;
import com.netease.yanxuan.wx.store.sharer.integration.handler.impl.WeChatShopPromoteProductLinkRequest; import com.netease.yanxuan.wx.store.sharer.integration.handler.impl.WeChatShopPromoteProductLinkRequest;
import com.netease.yanxuan.wx.store.sharer.integration.handler.impl.WeChatShopPromoteProductListRequest; import com.netease.yanxuan.wx.store.sharer.integration.handler.impl.WeChatShopPromoteProductListRequest;
...@@ -43,18 +44,17 @@ public class ProductServiceImpl implements IProductService { ...@@ -43,18 +44,17 @@ public class ProductServiceImpl implements IProductService {
private final WeChatShopSharerListRequest weChatShopSharerListRequest; private final WeChatShopSharerListRequest weChatShopSharerListRequest;
private final DrmSharerConfig drmSharerConfig; private final DrmSharerConfig drmSharerConfig;
@Override @Override
public PageVO<ProductListVO> getProductPageList(PageQuery pageQuery, String keyword) { public PageVO<ProductListVO> getProductPageList(PageQuery pageQuery, String keyword) {
PageVO<ProductListVO> pageVO = new PageVO<>(); PageVO<ProductListVO> pageVO = new PageVO<>();
WeChatPromoteProductListVO productListVO = weChatShopPromoteProductListRequest.handle(keyword, WeChatPromoteProductListVO productListVO = weChatShopPromoteProductListRequest.handle(keyword,
pageQuery.getNextKey(), pageQuery.getPageSize()); pageQuery.getNextKey(), pageQuery.getPageSize());
pageVO.setHasMore(!CollectionUtils.isEmpty(productListVO.getProduct_list()) pageVO.setHasMore(false);
&& productListVO.getProduct_list().size() >= pageQuery.getPageSize());
pageVO.setNextKey(productListVO.getNext_key());
// 商品列表 // 商品列表
List<ProductListVO> resultList = Optional.ofNullable(productListVO.getProduct_list()).orElseGet(ArrayList::new) if (!CollectionUtils.isEmpty(productListVO.getProduct_list())) {
.stream() // 使用异步多线程处理
.map(item -> { List<ProductListVO> productList = AsyncUtils.processListAsync(productListVO.getProduct_list(), item -> {
WeChatPromoteProductDetailVO detailVO = weChatShopPromoteProductDetailRequest.handle(item.getShop_appid(), WeChatPromoteProductDetailVO detailVO = weChatShopPromoteProductDetailRequest.handle(item.getShop_appid(),
Long.valueOf(item.getProduct_id())); Long.valueOf(item.getProduct_id()));
if (null == detailVO) { if (null == detailVO) {
...@@ -104,10 +104,17 @@ public class ProductServiceImpl implements IProductService { ...@@ -104,10 +104,17 @@ public class ProductServiceImpl implements IProductService {
listVO.setShopAppid(item.getShop_appid()); listVO.setShopAppid(item.getShop_appid());
listVO.setProductId(item.getProduct_id()); listVO.setProductId(item.getProduct_id());
return listVO; return listVO;
}) });
// 过滤空
productList = Optional.of(productList).orElseGet(ArrayList::new)
.stream()
.filter(Objects::nonNull) .filter(Objects::nonNull)
.collect(Collectors.toList()); .collect(Collectors.toList());
pageVO.setList(resultList); pageVO.setList(productList);
pageVO.setHasMore(!CollectionUtils.isEmpty(productList)
&& productList.size() >= pageQuery.getPageSize());
pageVO.setNextKey(productListVO.getNext_key());
}
return pageVO; return pageVO;
} }
......
package com.netease.yanxuan.wx.store.sharer.common.util;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Function;
/**
* @Description 异步处理工具类
* @Author fanjiaxin
* @Date 2024/6/13 09:31
*/
public class AsyncUtils {
/**
* 异步处理列表中的每个元素,并收集所有结果。
*
* @param <T> 列表中的元素类型
* @param <R> 异步处理结果类型
* @param itemList 要处理的列表
* @param asyncTask 定义如何异步处理每个元素的函数
* @return 异步处理后所有结果的列表
*/
public static <T, R> List<R> processListAsync(List<T> itemList, Function<T, R> asyncTask) {
CompletableFuture<R>[] futures = new CompletableFuture[itemList.size()];
for (int i = 0; i < itemList.size(); i++) {
T item = itemList.get(i);
CompletableFuture<R> future = CompletableFuture.supplyAsync(() -> asyncTask.apply(item));
futures[i] = future;
}
CompletableFuture.allOf(futures).join();
List<R> resultList = new ArrayList<>(itemList.size());
for (CompletableFuture<R> future : futures) {
resultList.add(future.join());
}
return resultList;
}
/**
* 异步处理列表中的每个元素,并收集所有结果。
*
* @param <T> 列表中的元素类型
* @param <R> 异步处理结果类型
* @param itemList 要处理的列表
* @param asyncTask 定义如何异步处理每个元素的函数
* @param executor 异步执行器
* @return 异步处理后所有结果的列表
*/
public static <T, R> List<R> processListAsync(List<T> itemList, Function<T, R> asyncTask, Executor executor) {
CompletableFuture<R>[] futures = new CompletableFuture[itemList.size()];
for (int i = 0; i < itemList.size(); i++) {
T item = itemList.get(i);
CompletableFuture<R> future = CompletableFuture.supplyAsync(() -> asyncTask.apply(item), executor);
futures[i] = future;
}
CompletableFuture.allOf(futures).join();
List<R> resultList = new ArrayList<>(itemList.size());
for (CompletableFuture<R> future : futures) {
resultList.add(future.join());
}
return resultList;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment