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;
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.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.ProductPromotionLinkVO;
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.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.WeChatShopPromoteProductLinkRequest;
import com.netease.yanxuan.wx.store.sharer.integration.handler.impl.WeChatShopPromoteProductListRequest;
......@@ -43,71 +44,77 @@ public class ProductServiceImpl implements IProductService {
private final WeChatShopSharerListRequest weChatShopSharerListRequest;
private final DrmSharerConfig drmSharerConfig;
@Override
public PageVO<ProductListVO> getProductPageList(PageQuery pageQuery, String keyword) {
PageVO<ProductListVO> pageVO = new PageVO<>();
WeChatPromoteProductListVO productListVO = weChatShopPromoteProductListRequest.handle(keyword,
pageQuery.getNextKey(), pageQuery.getPageSize());
pageVO.setHasMore(!CollectionUtils.isEmpty(productListVO.getProduct_list())
&& productListVO.getProduct_list().size() >= pageQuery.getPageSize());
pageVO.setNextKey(productListVO.getNext_key());
pageVO.setHasMore(false);
// 商品列表
List<ProductListVO> resultList = Optional.ofNullable(productListVO.getProduct_list()).orElseGet(ArrayList::new)
.stream()
.map(item -> {
WeChatPromoteProductDetailVO detailVO = weChatShopPromoteProductDetailRequest.handle(item.getShop_appid(),
Long.valueOf(item.getProduct_id()));
if (null == detailVO) {
return null;
}
WeChatPromoteProductDetailVO.WeChatPromoteProductVO product = detailVO.getProduct();
if (null == product) {
return null;
}
WeChatPromoteProductDetailVO.WeChatPromoteProductVO.WeChatPromoteProductInfoVO productInfo = product.getProduct_info();
if (null == productInfo) {
return null;
}
ProductListVO listVO = new ProductListVO();
listVO.setTitle(productInfo.getTitle());
listVO.setHeadImg(CollectionUtils.isEmpty(productInfo.getHead_imgs()) ? null : productInfo.getHead_imgs().get(0));
// 取参数SkuInfo中sale_price的最小
BigDecimal price = Optional.ofNullable(productInfo.getSkus()).orElseGet(ArrayList::new)
.stream()
.map(WeChatPromoteProductDetailVO.WeChatPromoteProductVO.WeChatPromoteProductInfoVO.WeChatPromoteProductSkuInfoVO::getSale_price)
.min(BigDecimal::compareTo).orElse(null);
listVO.setPrice(price);
if (!CollectionUtils.isEmpty(productListVO.getProduct_list())) {
// 使用异步多线程处理
List<ProductListVO> productList = AsyncUtils.processListAsync(productListVO.getProduct_list(), item -> {
WeChatPromoteProductDetailVO detailVO = weChatShopPromoteProductDetailRequest.handle(item.getShop_appid(),
Long.valueOf(item.getProduct_id()));
if (null == detailVO) {
return null;
}
WeChatPromoteProductDetailVO.WeChatPromoteProductVO product = detailVO.getProduct();
if (null == product) {
return null;
}
WeChatPromoteProductDetailVO.WeChatPromoteProductVO.WeChatPromoteProductInfoVO productInfo = product.getProduct_info();
if (null == productInfo) {
return null;
}
ProductListVO listVO = new ProductListVO();
listVO.setTitle(productInfo.getTitle());
listVO.setHeadImg(CollectionUtils.isEmpty(productInfo.getHead_imgs()) ? null : productInfo.getHead_imgs().get(0));
// 取参数SkuInfo中sale_price的最小
BigDecimal price = Optional.ofNullable(productInfo.getSkus()).orElseGet(ArrayList::new)
.stream()
.map(WeChatPromoteProductDetailVO.WeChatPromoteProductVO.WeChatPromoteProductInfoVO.WeChatPromoteProductSkuInfoVO::getSale_price)
.min(BigDecimal::compareTo).orElse(null);
listVO.setPrice(price);
// 推客分佣比例
BigDecimal commissionRatio = drmSharerConfig.getDefaultCommissionRatio();
// 获取用户信息
LoginUserInfo loginUserInfo = LoginUserContextHolder.get();
if (null != loginUserInfo && StringUtils.isNotBlank(loginUserInfo.getOpenId())) {
WeChatSharerListVO sharerListVO = weChatShopSharerListRequest.handle(loginUserInfo.getOpenId(), "", 1);
if (null != sharerListVO && !CollectionUtils.isEmpty(sharerListVO.getSharer_info_list())) {
WeChatSharerInfoVO sharerInfoVO = sharerListVO.getSharer_info_list().get(0);
if (null != sharerInfoVO && null != sharerInfoVO.getCommission_ratio()) {
commissionRatio = sharerInfoVO.getCommission_ratio();
}
// 推客分佣比例
BigDecimal commissionRatio = drmSharerConfig.getDefaultCommissionRatio();
// 获取用户信息
LoginUserInfo loginUserInfo = LoginUserContextHolder.get();
if (null != loginUserInfo && StringUtils.isNotBlank(loginUserInfo.getOpenId())) {
WeChatSharerListVO sharerListVO = weChatShopSharerListRequest.handle(loginUserInfo.getOpenId(), "", 1);
if (null != sharerListVO && !CollectionUtils.isEmpty(sharerListVO.getSharer_info_list())) {
WeChatSharerInfoVO sharerInfoVO = sharerListVO.getSharer_info_list().get(0);
if (null != sharerInfoVO && null != sharerInfoVO.getCommission_ratio()) {
commissionRatio = sharerInfoVO.getCommission_ratio();
}
}
WeChatPromoteProductDetailVO.WeChatPromoteProductVO.WeChatPromoteProductCommissionInfoVO commissionInfo = product.getCommission_info();
// 服务费率
Integer serviceRatio = null != commissionInfo ? commissionInfo.getService_ratio() : null;
// 佣金=商品价格*服务费率*推客分佣比例,四舍五入保留小数点后两位
BigDecimal commission = null;
if (null != price && null != commissionRatio && null != serviceRatio) {
commission = price.multiply(new BigDecimal(serviceRatio.toString()))
.multiply(commissionRatio).setScale(2, RoundingMode.HALF_UP);
}
listVO.setCommission(commission);
listVO.setShopAppid(item.getShop_appid());
listVO.setProductId(item.getProduct_id());
return listVO;
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
pageVO.setList(resultList);
}
WeChatPromoteProductDetailVO.WeChatPromoteProductVO.WeChatPromoteProductCommissionInfoVO commissionInfo = product.getCommission_info();
// 服务费率
Integer serviceRatio = null != commissionInfo ? commissionInfo.getService_ratio() : null;
// 佣金=商品价格*服务费率*推客分佣比例,四舍五入保留小数点后两位
BigDecimal commission = null;
if (null != price && null != commissionRatio && null != serviceRatio) {
commission = price.multiply(new BigDecimal(serviceRatio.toString()))
.multiply(commissionRatio).setScale(2, RoundingMode.HALF_UP);
}
listVO.setCommission(commission);
listVO.setShopAppid(item.getShop_appid());
listVO.setProductId(item.getProduct_id());
return listVO;
});
// 过滤空
productList = Optional.of(productList).orElseGet(ArrayList::new)
.stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
pageVO.setList(productList);
pageVO.setHasMore(!CollectionUtils.isEmpty(productList)
&& productList.size() >= pageQuery.getPageSize());
pageVO.setNextKey(productListVO.getNext_key());
}
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