//商品为组合商品,获取其子商品信息 if (GoodsShelveType.COMBINATION.getShelveType() == goods.getShelveType()) { for (GoodsChilds goodsChild : goodsChildsList) { //订单子商品信息 OrderGoods orderGoodsChilds = orderGoodsChildMap.get(goodsChild.getModel()); if (orderGoodsChilds == null) { throw new CustomException("组合商品子商品信息为空"); } //减少现货渠道可用库存 this.reduceBaseGoodsOccupyStock(order, orderGoodsChilds, orderGoodsChilds.getGoodsModel(), goods, BaseGoodsOccupyType.SPOT_CHANNEL_OCCUPY, orderUpdateStatus); } } else { //减少现货渠道可用库存 this.reduceBaseGoodsOccupyStock(order, orderGoods, goods.getModel(), goods, BaseGoodsOccupyType.SPOT_CHANNEL_OCCUPY, orderUpdateStatus); }优化该段代码
时间: 2024-02-10 21:12:23 浏览: 22
这段代码存在重复代码,可以将减少现货渠道可用库存的逻辑提取出来,在 if-else 分支外部执行。另外,对于组合商品的处理可以使用 Java 8 的 Stream API 简化代码。
优化后的代码如下:
```
// 减少现货渠道可用库存
Consumer<OrderGoods> reduceOccupyStock = orderGoods -> {
BaseGoodsOccupyType occupyType = BaseGoodsOccupyType.SPOT_CHANNEL_OCCUPY;
this.reduceBaseGoodsOccupyStock(order, orderGoods, orderGoods.getGoodsModel(), goods, occupyType, orderUpdateStatus);
};
if (GoodsShelveType.COMBINATION.getShelveType() == goods.getShelveType()) {
// 组合商品子商品信息
goodsChildsList.stream()
.map(GoodsChilds::getModel)
.map(orderGoodsChildMap::get)
.filter(Objects::isNull)
.findAny()
.ifPresent(goodsChild -> {
throw new CustomException("组合商品子商品信息为空");
});
// 减少现货渠道可用库存
goodsChildsList.stream()
.map(GoodsChilds::getModel)
.map(orderGoodsChildMap::get)
.forEach(reduceOccupyStock);
} else {
// 减少现货渠道可用库存
reduceOccupyStock.accept(orderGoods);
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)