List<SkuTable> skuTables = goodsItem.getSkuTable(); for (Map.Entry<String, Integer> entry : skuMapById.entrySet()) { StringBuilder sb = new StringBuilder(); String key = entry.getKey(); String[] arr = key.split("-");//将属性值分割去skuTable内转换出前端传入的组合 for (int i = 0; i < arr.length; i++) { for (SkuTable skuTable : skuTables) { if (null == skuTable.getValue() || ((List<?>) skuTable.getValue()).stream().count() == 0) continue;//若没有选中的值则结束本次循环 for (SkuTable table : skuTable.getOptions()) { if (arr[i].equals(table.getId())) {//若前端选中值的list内有该建,则将之转为前端传入的id sb.append(table.getWebId()).append("-"); } } } } if (sb.length() > 0) sb.deleteCharAt(sb.length() - 1); GoodsSkuExtend extend = skuExtendMap.get(sb.toString()); if (null == extend) {//若没有渠道数据则将key分割比较数组是否一致 String[] dataArr = sb.toString().split("-"); for (Map.Entry<String, GoodsSkuExtend> webEntry : skuExtendMap.entrySet()) { String[] webArr = webEntry.getKey().split("-"); Arrays.sort(dataArr); Arrays.sort(webArr); if (Arrays.equals(dataArr, webArr)) {// 比较两个数组是否一致 extend = skuExtendMap.get(webEntry.getKey()); break; } } } if (null == extend) throw new NullPointerException("数据异常,请检查后重新提交!"); extend.setSkuId(entry.getValue()); }
时间: 2024-02-14 13:35:27 浏览: 105
这段代码的功能是根据前端传入的SKU属性值,将其转换为对应的SKU ID,并将其设置为对应的渠道数据(GoodsSkuExtend)的SKU ID。具体实现步骤为:首先,遍历之前生成的SKU属性值ID和SKU ID的HashMap,对于每个SKU属性值ID,先将其转换为前端传入的SKU属性值组合,然后遍历商品的SKU属性列表,找到对应的SKU属性值,并将其对应的前端传入的SKU属性值ID转换为前端传入的ID,拼接成一个新的SKU属性值组合。然后,根据这个新的SKU属性值组合,从之前生成的SKU属性值组合和渠道数据的HashMap中查找对应的渠道数据,并将其对应的SKU ID设置为当前SKU的ID。如果找不到对应的渠道数据,则通过比较SKU属性值组合数组是否一致来查找对应的渠道数据。如果仍然找不到,则抛出一个空指针异常。
相关问题
switch (goodsItem.getProductType()) { case 1://爆品 GoodsExplosive explosive = null; List<GoodsExplosive> explosives = new GoodsExplosive().selectList(new LambdaUpdateWrapper<GoodsExplosive>() .eq(GoodsExplosive::getGoodsItemId, goodsItem.getId()) .orderByDesc(GoodsExplosive::getId)); if (explosives.size() > 1) { explosive = explosives.stream().sorted(Comparator.comparing(GoodsExplosive::getGroundingState, (s1, s2) -> { if (s1.equals(s2)) { return 0; } else if (s1 == 1 || s2 == 5) { return -1; } else if (s1 == 5 || s2 == 1) { return 1; } else if (s1 == 2) { return -1; } else if (s2 == 2) { return 1; } else if (s1 == 0) { return -1; } else { return 1; } })).findFirst().orElse(null); } else { explosive = explosives.size() == 1 ? explosives.get(0) : explosive; } map.put("productData", explosive); break; case 2:// GoodsExcellent excellent = null; List<GoodsExcellent> excellents = new GoodsExcellent().selectList(new LambdaUpdateWrapper<GoodsExcellent>() .eq(GoodsExcellent::getGoodsItemId, goodsItem.getId()) .orderByDesc(GoodsExcellent::getId)); if (excellents.size() > 1) { excellent = excellents.stream().sorted(Comparator.comparing(GoodsExcellent::getGroundingState, (s1, s2) -> { if (s1.equals(s2)) { return 0; } else if (s1 == 1 || s2 == 5) { return -1; } else if (s1 == 5 || s2 == 1) { return 1; } else if (s1 == 2) { return -1; } else if (s2 == 2) { return 1; } else if (s1 == 0) { return -1; } else { return 1; } })).findFirst().orElse(null); } else { excellent = excellents.size() == 1 ? excellents.get(0) : excellent; } map.put("productData", excellent); break; case 3://现货 GoodsEnjoy enjoy = null; List<GoodsEnjoy> enjoys = new GoodsEnjoy().selectList(new LambdaUpdateWrapper<GoodsEnjoy>() .eq(GoodsEnjoy::getGoodsItemId, goodsItem.getId() .orderByDesc(GoodsEnjoy::getId)); if (enjoys.size() > 1) { enjoy = enjoys.stream().sorted(Comparator.comparing(GoodsEnjoy::getGroundingState, (s1, s2) -> { if (s1.equals(s2)) { return 0; } else if (s1 == 1 || s2 == 5) { return -1; } else if (s1 == 5 || s2 == 1) { return 1; } else if (s1 == 2) { return -1; } else if (s2 == 2) { return 1; } else if (s1 == 0) { return -1; } else { return 1; } })).findFirst().orElse(null); } else { enjoy = enjoys.size() == 1 ? enjoys.get(0) : enjoy; } map.put("productData", enjoy); break; default: throw new IllegalStateException("Unexpected value: " + goodsItem.getProductType()); }怎么优化
这段代码中有重复的逻辑,可以抽象成一个方法,减少重复代码。同时,可以使用 Java 8 的 Optional 等新特性简化代码。具体来说,可以将以下部分重构:
```
GoodsExplosive explosive = null;
List<GoodsExplosive> explosives = new GoodsExplosive().selectList(new LambdaUpdateWrapper<GoodsExplosive>() .eq(GoodsExplosive::getGoodsItemId, goodsItem.getId()) .orderByDesc(GoodsExplosive::getId));
if (explosives.size() > 1) {
explosive = explosives.stream().sorted(Comparator.comparing(GoodsExplosive::getGroundingState, (s1, s2) -> {
if (s1.equals(s2)) {
return 0;
} else if (s1 == 1 || s2 == 5) {
return -1;
} else if (s1 == 5 || s2 == 1) {
return 1;
} else if (s1 == 2) {
return -1;
} else if (s2 == 2) {
return 1;
} else if (s1 == 0) {
return -1;
} else {
return 1;
}
})).findFirst().orElse(null);
} else {
explosive = explosives.size() == 1 ? explosives.get(0) : explosive;
}
```
可以抽象成一个方法:
```
private Optional<GoodsExplosive> getGoodsExplosive(GoodsItem goodsItem) {
List<GoodsExplosive> explosives = new GoodsExplosive().selectList(new LambdaUpdateWrapper<GoodsExplosive>()
.eq(GoodsExplosive::getGoodsItemId, goodsItem.getId())
.orderByDesc(GoodsExplosive::getId));
return explosives.stream().sorted(Comparator.comparing(GoodsExplosive::getGroundingState, (s1, s2) -> {
if (s1.equals(s2)) {
return 0;
} else if (s1 == 1 || s2 == 5) {
return -1;
} else if (s1 == 5 || s2 == 1) {
return 1;
} else if (s1 == 2) {
return -1;
} else if (s2 == 2) {
return 1;
} else if (s1 == 0) {
return -1;
} else {
return 1;
}
})).findFirst();
}
```
然后可以简化 switch 语句:
```
switch (goodsItem.getProductType()) {
case 1: //爆品
map.put("productData", getGoodsExplosive(goodsItem).orElse(null));
break;
case 2: //精品
map.put("productData", new GoodsExcellent().selectList(new LambdaUpdateWrapper<GoodsExcellent>()
.eq(GoodsExcellent::getGoodsItemId, goodsItem.getId())
.orderByDesc(GoodsExcellent::getId))
.stream().findFirst().orElse(null));
break;
case 3: //现货
map.put("productData", new GoodsEnjoy().selectList(new LambdaUpdateWrapper<GoodsEnjoy>()
.eq(GoodsEnjoy::getGoodsItemId, goodsItem.getId())
.orderByDesc(GoodsEnjoy::getId))
.stream().findFirst().orElse(null));
break;
default:
throw new IllegalStateException("Unexpected value: " + goodsItem.getProductType());
}
```
List<ForecastBillGoodsDTO> goods =new ArrayList<>();List<ForecastBillGoods> goodsList,将goodsList复制到goods
您可以使用循环遍历 `goodsList`,并将每个元素添加到 `goods` 列表中,如下所示:
```
List<ForecastBillGoodsDTO> goods = new ArrayList<>();
for (ForecastBillGoods goodsItem : goodsList) {
goods.add(new ForecastBillGoodsDTO(goodsItem));
}
```
假设 `ForecastBillGoodsDTO` 是一个 DTO 类,它有一个构造函数,可以将 `ForecastBillGoods` 对象作为参数,并将其转换为 `ForecastBillGoodsDTO` 对象。这样,您就可以将 `goodsList` 中的每个元素复制到 `goods` 列表中了。
阅读全文