|
|
@ -99,7 +99,7 @@ public class DishService { |
|
|
|
|
|
|
|
public void copy(Dish dish, List<Long> vendors, String operator) { |
|
|
|
Instant instant = Instant.now(); |
|
|
|
List<Dish> dishes = vendors.stream().filter(vender -> !exists(null, dish.getName(), vender)).map(vender -> Dish.builder().name(dish.getName()).vender(vender).marks(dish.getMarks()).poly(dish.getPoly()).label(dish.getLabel()).month(dish.getMonth()).icon(dish.getIcon()).ingredient(dish.getIngredient()).operate(operator).created(instant).modify(instant).build()).collect(Collectors.toList()); |
|
|
|
List<Dish> dishes = vendors.stream().filter(vender -> notExists(null, dish.getName(), vender)).map(vender -> Dish.builder().name(dish.getName()).vender(vender).marks(dish.getMarks()).poly(dish.getPoly()).label(dish.getLabel()).month(dish.getMonth()).icon(dish.getIcon()).ingredient(dish.getIngredient()).operate(operator).created(instant).modify(instant).build()).collect(Collectors.toList()); |
|
|
|
dishRepository.saveAll(dishes); |
|
|
|
log.info("[DishService] copy dishes count = " + dishes.size() + ", operator = " + operator); |
|
|
|
} |
|
|
@ -132,8 +132,8 @@ public class DishService { |
|
|
|
return dishRepository.existsByVenderAndIdIn(vender, ids); |
|
|
|
} |
|
|
|
|
|
|
|
public boolean exists(Long id, String name, Long vender) { |
|
|
|
return id == null ? dishRepository.existsByVenderAndName(vender, name) : dishRepository.existsByVenderAndNameAndIdNot(vender, name, id); |
|
|
|
public boolean notExists(Long id, String name, Long vender) { |
|
|
|
return id == null ? !dishRepository.existsByVenderAndName(vender, name) : !dishRepository.existsByVenderAndNameAndIdNot(vender, name, id); |
|
|
|
} |
|
|
|
|
|
|
|
public Dish get(Long id) { |
|
|
@ -220,9 +220,17 @@ public class DishService { |
|
|
|
return dishes.parallelStream().filter(dish -> CollectionUtils.isNotEmpty(dish.getIngredient())).map(dish -> { |
|
|
|
List<String> ingredients = dish.getIngredient().stream().filter(item -> BooleanUtils.isTrue(item.getIsMain())).map(DishItemDTO::getKey).filter(ingredientMap::containsKey).map(x -> ingredientMap.get(x).getName()).collect(Collectors.toList()); |
|
|
|
|
|
|
|
Map<String, BigDecimal> items = dish.getIngredient().stream().filter(x -> ingredientMap.containsKey(x.getKey())).flatMap(x -> |
|
|
|
Map<String, BigDecimal> _items = dish.getIngredient().stream().filter(x -> ingredientMap.containsKey(x.getKey())).flatMap(x -> |
|
|
|
ingredientMap.get(x.getKey()).getNutrient().entrySet().stream().map(n -> Pair.of(n.getKey(), n.getValue().multiply(x.getValue()).divide(new BigDecimal(100), RoundingMode.HALF_UP))) |
|
|
|
).filter(x -> x.getValue() != null && x.getValue().compareTo(BigDecimal.ZERO) > 0).collect(Collectors.toMap(Pair::getKey, v -> { |
|
|
|
).filter(x -> x.getValue() != null && x.getValue().compareTo(BigDecimal.ZERO) > 0).collect(Collectors.toMap(Pair::getKey, Pair::getValue, BigDecimal::add)); |
|
|
|
_items.put("sugar", sugar(dish, ingredientMap)); |
|
|
|
MUST_DISPLAY_ITEM.forEach(x -> { |
|
|
|
if (!_items.containsKey(x)) { |
|
|
|
_items.put(x, BigDecimal.ZERO); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
Map<String, BigDecimal> items = _items.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, v -> { |
|
|
|
switch (v.getKey()) { |
|
|
|
case "protein" : |
|
|
|
case "fat" : |
|
|
@ -232,15 +240,7 @@ public class DishService { |
|
|
|
case "sodium" : return v.getValue().compareTo(new BigDecimal("5")) > 0 ? v.getValue() : BigDecimal.ZERO; |
|
|
|
default: return v.getValue(); |
|
|
|
} |
|
|
|
}, BigDecimal::add)); |
|
|
|
|
|
|
|
items.put("sugar", sugar(dish, ingredientMap)); |
|
|
|
|
|
|
|
MUST_DISPLAY_ITEM.forEach(x -> { |
|
|
|
if (!items.containsKey(x)) { |
|
|
|
items.put(x, BigDecimal.ZERO); |
|
|
|
} |
|
|
|
}); |
|
|
|
})); |
|
|
|
|
|
|
|
List<ComponentAnalysisDO> component = items.entrySet().stream().filter(x -> MUST_DISPLAY_ITEM.contains(x.getKey()) || WANT_DISPLAY_ITEM.contains(x.getKey()) && x.getValue() != null && x.getValue().compareTo(BigDecimal.ZERO) > 0).map(x -> { |
|
|
|
FoodNutrient foodNutrient = enumService.getNutrient(x.getKey()); |
|
|
@ -254,13 +254,13 @@ public class DishService { |
|
|
|
|
|
|
|
String name = foodNutrient.getValue(); |
|
|
|
String value = String.format("%s(%s)", formatValue.format(x.getValue().floatValue()), foodNutrient.getMeasurement() == null ? "-" : foodNutrient.getMeasurement()); |
|
|
|
String nvr = foodNutrient.getNrv() == null || foodNutrient.getNrv().floatValue() == 0 ? "-" : String.format("%s%%", formatInteger.format(x.getValue().divide(foodNutrient.getNrv(), RoundingMode.HALF_UP).multiply(new BigDecimal(100)))); |
|
|
|
String nrv = foodNutrient.getNrv() == null || foodNutrient.getNrv().floatValue() == 0 ? "-" : String.format("%s%%", formatInteger.format(x.getValue().divide(foodNutrient.getNrv(), 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)))); |
|
|
|
if (StringUtils.equalsIgnoreCase(x.getKey(), "sodium")) { |
|
|
|
float salt = x.getValue().multiply(new BigDecimal("0.0025")).floatValue(); |
|
|
|
name = "钠/食盐"; |
|
|
|
value = String.format("%s(mg)/%s(g)", formatInteger.format(x.getValue().floatValue()), formatValue.format(salt)); |
|
|
|
} |
|
|
|
return ComponentAnalysisDO.builder().key(x.getKey()).name(name).nutrition(value).nvr(nvr).build(); |
|
|
|
return ComponentAnalysisDO.builder().key(x.getKey()).name(name).nutrition(value).nrv(nrv).build(); |
|
|
|
}).collect(Collectors.toList()); |
|
|
|
|
|
|
|
return DishLabelDO.builder().name(dish.getName()).ingredients(ingredients).component(component).build(); |
|
|
|