caoyiwen 1 year ago
parent
commit
e34cbcd2a9
  1. 2
      diet-core/src/main/java/com/mathvision/diet/domain/ComponentAnalysisDO.java
  2. 32
      diet-core/src/main/java/com/mathvision/diet/service/DishService.java
  3. 5
      diet-web/src/main/java/com/mathvision/diet/controller/DishController.java
  4. 15
      diet-web/src/main/resources/application-dev.yml
  5. 8
      diet-web/src/main/resources/application-prod.yml
  6. 3
      diet-web/src/main/resources/application.yml

2
diet-core/src/main/java/com/mathvision/diet/domain/ComponentAnalysisDO.java

@ -13,5 +13,5 @@ public class ComponentAnalysisDO {
String key;
String name;
String nutrition;
String nvr;
String nrv;
}

32
diet-core/src/main/java/com/mathvision/diet/service/DishService.java

@ -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();

5
diet-web/src/main/java/com/mathvision/diet/controller/DishController.java

@ -7,8 +7,6 @@ import com.google.common.collect.Range;
import com.mathvision.diet.domain.DishLabelDO;
import com.mathvision.diet.domian.DishItemDTO;
import com.mathvision.diet.entity.Dish;
import com.mathvision.diet.entity.Menu;
import com.mathvision.diet.entity.Nutrition;
import com.mathvision.diet.service.DishService;
import com.mathvision.diet.service.EnumService;
import com.mathvision.diet.service.IngredientService;
@ -26,7 +24,6 @@ import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.time.Instant;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
@ -100,7 +97,7 @@ public class DishController extends BaseController {
boolean flag = false;
if(StringUtils.isNotBlank(name) && !StringUtils.equals(name, dish.getName())) {
Assert.isTrue(!dishService.exists(id, name, dish.getVender()), "[参数错误]菜品名称已存在!");
Assert.isTrue(dishService.notExists(id, name, dish.getVender()), "[参数错误]菜品名称已存在!");
dish.setName(name);
flag = true;
}

15
diet-web/src/main/resources/application-dev.yml

@ -1,17 +1,10 @@
spring:
datasource:
url: jdbc:log4jdbc:mysql://localhost:3306/diet?useUnicode=true&characterEncoding=utf-8
username: root
password: 123456
driver-class-name: net.sf.log4jdbc.DriverSpy
url: jdbc:mysql://47.109.27.8:3306/diet?useUnicode=true&characterEncoding=utf-8
username: admin
password: '@Jiluo2019'
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
show-sql: false
hibernate:
ddl-auto: none
logging:
level:
jdbc.resultset: off
jdbc.audit: off
jdbc.sqlonly: off
jdbc.connection: off

8
diet-web/src/main/resources/application-prod.yml

@ -1,14 +1,10 @@
spring:
datasource:
url: jdbc:mysql://47.109.27.8:3306/diet?useUnicode=true&characterEncoding=utf-8
username: admin
url: jdbc:mysql://shishangpei.mysql.cn-chengdu.rds.aliyuncs.com:3306/diet?useUnicode=true&characterEncoding=utf-8
username: jiluo
password: '@Jiluo2019'
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
show-sql: false
hibernate:
ddl-auto: none
icon:
path: /data/icon

3
diet-web/src/main/resources/application.yml

@ -23,3 +23,6 @@ logging:
file:
encoding: utf-8
icon:
path: /data/icon
Loading…
Cancel
Save