|
@ -22,14 +22,13 @@ import javax.annotation.Resource; |
|
|
import javax.persistence.criteria.Predicate; |
|
|
import javax.persistence.criteria.Predicate; |
|
|
import java.math.BigDecimal; |
|
|
import java.math.BigDecimal; |
|
|
import java.math.RoundingMode; |
|
|
import java.math.RoundingMode; |
|
|
import java.util.ArrayList; |
|
|
import java.util.*; |
|
|
import java.util.List; |
|
|
|
|
|
import java.util.Map; |
|
|
|
|
|
import java.util.stream.Collectors; |
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
@Slf4j |
|
|
@Slf4j |
|
|
@Service |
|
|
@Service |
|
|
public class DishService { |
|
|
public class DishService { |
|
|
|
|
|
private static final List<String> MUST_DISPLAY_ITEM = Lists.newArrayList("energy", "fat", "sodium"); |
|
|
|
|
|
|
|
|
@Resource |
|
|
@Resource |
|
|
private EnumService enumService; |
|
|
private EnumService enumService; |
|
@ -144,12 +143,41 @@ public class DishService { |
|
|
List<ComponentAnalysisDO> component = dish.getIngredient().stream().filter(x -> ingredientMap.containsKey(x.getKey())).flatMap(x -> { |
|
|
List<ComponentAnalysisDO> component = dish.getIngredient().stream().filter(x -> ingredientMap.containsKey(x.getKey())).flatMap(x -> { |
|
|
Ingredient ingredient = ingredientMap.get(x.getKey()); |
|
|
Ingredient ingredient = ingredientMap.get(x.getKey()); |
|
|
return ingredient.getNutrient().entrySet().stream().map(n -> Pair.of(n.getKey(), n.getValue().multiply(x.getValue()).divide(new BigDecimal(100), RoundingMode.HALF_UP))); |
|
|
return ingredient.getNutrient().entrySet().stream().map(n -> Pair.of(n.getKey(), n.getValue().multiply(x.getValue()).divide(new BigDecimal(100), RoundingMode.HALF_UP))); |
|
|
}).collect(Collectors.toMap(Pair::getKey, Pair::getValue, BigDecimal::add)).entrySet().stream().map(r -> { |
|
|
}).collect(Collectors.toMap(Pair::getKey, Pair::getValue, BigDecimal::add)).entrySet().stream().filter(r -> r.getValue() != null && r.getValue().compareTo(BigDecimal.ZERO) > 0).map(r -> { |
|
|
FoodNutrient foodNutrient = enumService.getNutrient(r.getKey()); |
|
|
FoodNutrient foodNutrient = enumService.getNutrient(r.getKey()); |
|
|
return foodNutrient == null ? |
|
|
if (foodNutrient == null) { |
|
|
ComponentAnalysisDO.builder().name(r.getKey()).nutrition(String.format("%.2f(-)", r.getValue())).nvr("-").build() : |
|
|
return ComponentAnalysisDO.builder().key(r.getKey()).name(r.getKey()).nutrition(String.format("%.2f(-)", r.getValue())).nvr("-").build(); |
|
|
ComponentAnalysisDO.builder().name(foodNutrient.getValue()).nutrition(String.format("%.2f(%s)", r.getValue().floatValue(), foodNutrient.getMeasurement())).nvr(foodNutrient.getNrv() == null || foodNutrient.getNrv().floatValue() ==0 ? "-" : String.format("%.2f%%", r.getValue().divide(foodNutrient.getNrv(), RoundingMode.HALF_UP))).build(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (StringUtils.endsWithIgnoreCase(r.getKey(), "sodium")) { |
|
|
|
|
|
return ComponentAnalysisDO.builder().key(r.getKey()).name(foodNutrient.getValue() + "/食盐").nutrition(String.format("%.2f(%s)/%.2f(g)", r.getValue().floatValue(), foodNutrient.getMeasurement(), r.getValue().multiply(new BigDecimal("0.0025")).floatValue())).nvr(foodNutrient.getNrv() == null || foodNutrient.getNrv().floatValue() == 0 ? "-" : String.format("%.2f%%", r.getValue().divide(foodNutrient.getNrv(), RoundingMode.HALF_UP))).build(); |
|
|
|
|
|
} else { |
|
|
|
|
|
return ComponentAnalysisDO.builder().key(r.getKey()).name(foodNutrient.getValue()).nutrition(String.format("%.2f(%s)", r.getValue().floatValue(), foodNutrient.getMeasurement())).nvr(foodNutrient.getNrv() == null || foodNutrient.getNrv().floatValue() == 0 ? "-" : String.format("%.2f%%", r.getValue().divide(foodNutrient.getNrv(), RoundingMode.HALF_UP))).build(); |
|
|
|
|
|
} |
|
|
}).collect(Collectors.toList()); |
|
|
}).collect(Collectors.toList()); |
|
|
|
|
|
|
|
|
|
|
|
MUST_DISPLAY_ITEM.forEach(item -> { |
|
|
|
|
|
if(component.stream().noneMatch(x -> StringUtils.equals(x.getKey(), item))) { |
|
|
|
|
|
FoodNutrient foodNutrient = enumService.getNutrient(item); |
|
|
|
|
|
if(foodNutrient == null) { |
|
|
|
|
|
component.add(ComponentAnalysisDO.builder().key(item).name(item).nutrition(String.format("%.2f(-)", 0.0)).nvr("-").build()); |
|
|
|
|
|
} else { |
|
|
|
|
|
if (StringUtils.endsWithIgnoreCase(item, "sodium")) { |
|
|
|
|
|
component.add(ComponentAnalysisDO.builder().key(foodNutrient.getKey()).name(foodNutrient.getValue() + "/食盐").nutrition(String.format("0(%s)/0(g)", foodNutrient.getMeasurement())).nvr("-").build() ); |
|
|
|
|
|
} else { |
|
|
|
|
|
component.add(ComponentAnalysisDO.builder().key(foodNutrient.getKey()).name(foodNutrient.getValue()).nutrition(String.format("0(%s)", foodNutrient.getMeasurement())).nvr("-").build() ); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
BigDecimal sugar = dish.getIngredient().stream().filter(x -> ingredientMap.containsKey(x.getKey())) |
|
|
|
|
|
.map(x -> Pair.of(ingredientMap.get(x.getKey()), x.getValue())) |
|
|
|
|
|
.filter(x -> StringUtils.equals("糖类", x.getKey().getType())).map(Pair::getValue).reduce(BigDecimal::add).orElse(null); |
|
|
|
|
|
if (sugar != null && sugar.compareTo(BigDecimal.ZERO) > 0) { |
|
|
|
|
|
component.add(ComponentAnalysisDO.builder().key("sugar").name("糖").nutrition(String.format("%.2f(g)", sugar)).nvr("-").build()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return DishLabelDO.builder().name(dish.getName()).ingredients(ingredients).component(component).build(); |
|
|
return DishLabelDO.builder().name(dish.getName()).ingredients(ingredients).component(component).build(); |
|
|
}).collect(Collectors.toList()); |
|
|
}).collect(Collectors.toList()); |
|
|
} |
|
|
} |
|
|