✅P272_商城业务-订单服务-订单确认页库存查询
大约 2 分钟
远程查询库存
cfmall-order/src/main/java/com/gyz/cfmall/order/feign/WmsFeignService.java
package com.gyz.cfmall.order.feign;
import com.gyz.common.utils.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
/**
* @author gong_yz
* @Description
*/
@FeignClient("cfmall-ware")
public interface WmsFeignService {
/**
* 查询sku是否有库存
*
* @return
*/
@PostMapping(value = "/ware/waresku/hasStock")
R getSkuHasStock(@RequestBody List<Long> skuIds);
}
cfmall-ware/src/main/java/com/gyz/cfmall/ware/controller/WareSkuController.java
/**
* 查询sku是否有库存
* @return
*/
@PostMapping(value = "/hasStock")
public R getSkuHasStock(@RequestBody List<Long> skuIds) {
//skuId stock
List<SkuHasStockVo> vos = wareSkuService.getSkuHasStock(skuIds);
return R.ok().setData(vos);
}
cfmall-common/src/main/java/com/gyz/common/to/SkuHasStockVo.java
@Data
public class SkuHasStockVo {
private Long skuId;
private Boolean hasStock;
}
cfmall-ware/src/main/java/com/gyz/cfmall/ware/service/impl/WareSkuServiceImpl.java
@Override
public List<SkuHasStockVo> getSkuHasStock(List<Long> skuIds) {
List<SkuHasStockVo> skuHasStockVos = skuIds.stream().map(item -> {
Long count = this.baseMapper.getSkuStock(item);
SkuHasStockVo skuHasStockVo = new SkuHasStockVo();
skuHasStockVo.setSkuId(item);
skuHasStockVo.setHasStock(count == null ? false : count > 0);
return skuHasStockVo;
}).collect(Collectors.toList());
return skuHasStockVos;
}
异步查询库存信息
编写Map用于封装库存信息
package com.gyz.cfmall.order.vo;
import lombok.Getter;
import lombok.Setter;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
/**
* @author gong_yz
* @Description 订单确认页VO
*/
public class OrderConfirmVo {
@Getter
@Setter
Map<Long, Boolean> stocks;
//省略其它代码
}
异步任务查询库存
cfmall-order/src/main/java/com/gyz/cfmall/order/service/impl/OrderServiceImpl.java
@Override
public OrderConfirmVo confirmOrder() throws ExecutionException, InterruptedException {
//构建OrderConfirmVo
OrderConfirmVo confirmVo = new OrderConfirmVo();
//获取当前用户登录的信息
MemberResponseVo memberResponseVo = LoginUserInterceptor.loginUser.get();
//获取当前线程请求头信息(解决Feign异步调用丢失请求头问题)
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
//开启第一个异步任务
CompletableFuture<Void> addressFuture = CompletableFuture.runAsync(() -> {
//每一个线程都来共享之前的请求数据
RequestContextHolder.setRequestAttributes(requestAttributes);
//1、远程查询所有的收获地址列表
List<MemberAddressVo> address = memberFeignService.getAddress(memberResponseVo.getId());
confirmVo.setMemberAddressVos(address);
}, threadPoolExecutor);
//开启第二个异步任务
CompletableFuture<Void> cartInfoFuture = CompletableFuture.runAsync(() -> {
//每一个线程都来共享之前的请求数据
RequestContextHolder.setRequestAttributes(requestAttributes);
//2、远程查询购物车所有选中的购物项
List<OrderItemVo> currentCartItems = cartFeignService.getCurrentCartItems();
confirmVo.setItems(currentCartItems);
//feign在远程调用之前要构造请求,调用很多的拦截器
}, threadPoolExecutor).thenRunAsync(() -> {
//异步查询库存
List<OrderItemVo> items = confirmVo.getItems();
List<Long> skuIds = items.stream().map(item -> {
return item.getSkuId();
}).collect(Collectors.toList());
R r = wmsFeignService.getSkuHasStock(skuIds);
List<SkuStockVo> data = r.getData(new TypeReference<List<SkuStockVo>>() {
});
Map<Long, Boolean> collect = data.stream().collect(Collectors.toMap(SkuStockVo::getSkuId, SkuStockVo::getHasStock));
confirmVo.setStocks(collect);
}, threadPoolExecutor);
//3、查询用户积分
Integer integration = memberResponseVo.getIntegration();
confirmVo.setIntegration(integration);
CompletableFuture.allOf(addressFuture, cartInfoFuture).get();
return confirmVo;
}
库存信息回显
cfmall-order/src/main/resources/templates/confirm.html