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()); }怎么优化
时间: 2024-03-12 21:50:15 浏览: 64
ecshop 100个小技巧
可以考虑将 switch 语句拆分成三个方法,分别处理不同商品类型的逻辑,从而降低代码的重复度和复杂度。例如:
```
private GoodsExplosive getExplosiveData(GoodsItem goodsItem) {
List<GoodsExplosive> explosives = new GoodsExplosive().selectList(new LambdaUpdateWrapper<GoodsExplosive>()
.eq(GoodsExplosive::getGoodsItemId, goodsItem.getId())
.orderByDesc(GoodsExplosive::getId));
if (explosives.isEmpty()) {
return null;
}
if (explosives.size() == 1) {
return explosives.get(0);
}
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()
.orElse(null);
}
private GoodsExcellent getExcellentData(GoodsItem goodsItem) {
List<GoodsExcellent> excellents = new GoodsExcellent().selectList(new LambdaUpdateWrapper<GoodsExcellent>()
.eq(GoodsExcellent::getGoodsItemId, goodsItem.getId())
.orderByDesc(GoodsExcellent::getId));
if (excellents.isEmpty()) {
return null;
}
if (excellents.size() == 1) {
return excellents.get(0);
}
return 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);
}
private GoodsEnjoy getEnjoyData(GoodsItem goodsItem) {
List<GoodsEnjoy> enjoys = new GoodsEnjoy().selectList(new LambdaUpdateWrapper<GoodsEnjoy>()
.eq(GoodsEnjoy::getGoodsItemId, goodsItem.getId())
.orderByDesc(GoodsEnjoy::getId));
if (enjoys.isEmpty()) {
return null;
}
if (enjoys.size() == 1) {
return enjoys.get(0);
}
return 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);
}
```
然后在原来的 switch 语句中,通过调用这三个方法来获取不同商品类型的数据:
```
switch (goodsItem.getProductType()) {
case 1:
map.put("productData", getExplosiveData(goodsItem));
break;
case 2:
map.put("productData", getExcellentData(goodsItem));
break;
case 3:
map.put("productData", getEnjoyData(goodsItem));
break;
default:
throw new IllegalStateException("Unexpected value: " + goodsItem.getProductType());
}
```
这样可以使代码更加清晰、简洁,并且易于维护。
阅读全文