caoyiwen 1 year ago
parent
commit
bd093a8fcd
  1. 39
      diet-core/src/main/java/com/mathvision/diet/service/MenuDishService.java
  2. 10
      diet-core/src/main/java/com/mathvision/diet/service/MenuReleaseService.java
  3. 28
      diet-core/src/main/java/com/mathvision/diet/service/MenuReportService.java
  4. 11
      diet-core/src/main/java/com/mathvision/diet/service/NutritionService.java
  5. 2
      diet-dao/src/main/java/com/mathvision/diet/entity/Dish.java
  6. 6
      diet-dao/src/main/java/com/mathvision/diet/entity/Menu.java
  7. 4
      diet-dao/src/main/java/com/mathvision/diet/entity/MenuDish.java
  8. 8
      diet-dao/src/main/java/com/mathvision/diet/entity/Nutrition.java
  9. 9
      diet-dao/src/main/java/com/mathvision/diet/repository/MenuDishRepository.java
  10. 5
      diet-dao/src/main/java/com/mathvision/diet/repository/MenuRepository.java
  11. 9
      diet-web/src/main/java/com/mathvision/diet/controller/MenuDisplayController.java
  12. 7
      diet-web/src/main/java/com/mathvision/diet/controller/MenuReportController.java
  13. 30
      diet-web/src/main/java/com/mathvision/diet/controller/NutritionController.java
  14. 10
      diet-web/src/main/resources/static/change.html
  15. 20
      diet-web/src/main/resources/static/menu/display.html
  16. 4
      diet-web/src/main/resources/static/menu/report.html
  17. 8
      doc/change.md
  18. 22
      doc/menu/display.md
  19. 4
      doc/menu/report.md
  20. 8
      sql/update.sql

39
diet-core/src/main/java/com/mathvision/diet/service/MenuDishService.java

@ -11,6 +11,7 @@ import com.google.common.collect.Maps;
import com.mathvision.diet.domian.MealType; import com.mathvision.diet.domian.MealType;
import com.mathvision.diet.domian.MenuDishItemDTO; import com.mathvision.diet.domian.MenuDishItemDTO;
import com.mathvision.diet.entity.*; import com.mathvision.diet.entity.*;
import com.mathvision.diet.repository.DishRepository;
import com.mathvision.diet.repository.MenuDishRepository; import com.mathvision.diet.repository.MenuDishRepository;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
@ -18,18 +19,27 @@ import org.apache.commons.lang3.tuple.Pair;
import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.OutputStream; import java.io.OutputStream;
import java.time.Instant;
import java.util.*; import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
@Slf4j @Slf4j
@Service @Service
public class MenuDishService { public class MenuDishService {
private static final Lock UPDATE_LABEL_LOCK = new ReentrantLock();
private static Instant UPDATE_LABEL_TIME;
@Resource
DishRepository dishRepository;
@Resource @Resource
MenuDishRepository menuDishRepository; MenuDishRepository menuDishRepository;
@ -37,6 +47,31 @@ public class MenuDishService {
@Resource @Resource
IngredientService ingredientService; IngredientService ingredientService;
@Scheduled(cron = "0/5 * * * * *")
public void init() {
if (!UPDATE_LABEL_LOCK.tryLock()) {
return;
}
try {
List<MenuDish> dishes = UPDATE_LABEL_TIME == null ? menuDishRepository.findAll() : menuDishRepository.findByModifyGreaterThan(UPDATE_LABEL_TIME);
if (dishes.isEmpty()) {
return;
}
UPDATE_LABEL_TIME = Instant.now();
dishes.forEach(menuDish -> {
Dish dish = dishRepository.findByIdAndVender(menuDish.getDish(), menuDish.getVender());
if (dish != null && dish.getLabel() != null) {
menuDish.setLabel(dish.getLabel());
}
});
menuDishRepository.saveAll(dishes);
} catch (Exception e) {
log.error("[MenuDishService] update label exception : " + e.getMessage(), e);
} finally {
UPDATE_LABEL_LOCK.unlock();
}
}
public MenuDish add(MenuDish menuDish) { public MenuDish add(MenuDish menuDish) {
return menuDishRepository.save(menuDish); return menuDishRepository.save(menuDish);
} }
@ -75,6 +110,10 @@ public class MenuDishService {
return menuDishRepository.findByMenuAndVenderAndDay(menuId, vender, day); return menuDishRepository.findByMenuAndVenderAndDay(menuId, vender, day);
} }
public List<Long> days(Long menuId, Long vender) {
return menuDishRepository.distinctDaysByMenuAndVender(menuId, vender);
}
public void export(Menu menu, OutputStream outputStream) { public void export(Menu menu, OutputStream outputStream) {
Map<String, Map<Long, List<MenuDish>>> menuDishes = menuDishRepository.findByMenu(menu.getId()).stream().collect(Collectors.groupingBy(MenuDish::getMeal, Collectors.groupingBy(MenuDish::getDay))); Map<String, Map<Long, List<MenuDish>>> menuDishes = menuDishRepository.findByMenu(menu.getId()).stream().collect(Collectors.groupingBy(MenuDish::getMeal, Collectors.groupingBy(MenuDish::getDay)));
List<String> allMeals = new ArrayList<>(menuDishes.keySet()).stream().sorted(Comparator.comparing(MealType::toType)).collect(Collectors.toList()); List<String> allMeals = new ArrayList<>(menuDishes.keySet()).stream().sorted(Comparator.comparing(MealType::toType)).collect(Collectors.toList());

10
diet-core/src/main/java/com/mathvision/diet/service/MenuReleaseService.java

@ -15,7 +15,6 @@ import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Predicate;
import java.time.LocalDate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -48,9 +47,14 @@ public class MenuReleaseService {
log.info("[MenuReleaseService] cancel id = " + id + ", operator = " + operator); log.info("[MenuReleaseService] cancel id = " + id + ", operator = " + operator);
} }
public List<Menu> current(Long vender) { public List<Menu> current(Long vender, Long day) {
List<Menu> result = menuRepository.findCurrentMenu(vender); List<Menu> result = menuRepository.findCurrentMenu(vender);
return result.parallelStream().filter(x -> menuDishRepository.existsByMenuAndDay(x.getId(), (long) LocalDate.now().getDayOfWeek().getValue())).collect(Collectors.toList()); return result.parallelStream().filter(x -> menuDishRepository.existsByMenuAndDay(x.getId(), day)).collect(Collectors.toList());
}
public List<Long> currentWeek(Long vender) {
List<Menu> result = menuRepository.findCurrentWeekMenu(vender);
return result.parallelStream().filter(x -> menuDishRepository.existsByMenu(x.getId())).map(Menu::getDay).flatMap(List::stream).distinct().collect(Collectors.toList());
} }
public Page<Menu> list(Long vender, String name, Date startTime, Date endTime, PageRequest pageRequest) { public Page<Menu> list(Long vender, String name, Date startTime, Date endTime, PageRequest pageRequest) {

28
diet-core/src/main/java/com/mathvision/diet/service/MenuReportService.java

@ -12,6 +12,7 @@ import com.mathvision.diet.domian.RuleItemDTO;
import com.mathvision.diet.entity.*; import com.mathvision.diet.entity.*;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple; import org.apache.commons.lang3.tuple.Triple;
@ -34,15 +35,18 @@ public class MenuReportService {
@Resource @Resource
NutritionService nutritionService; NutritionService nutritionService;
@Resource
MenuDishService menuDishService;
@Resource @Resource
IngredientService ingredientService; IngredientService ingredientService;
@Resource @Resource
VenderService venderService; VenderService venderService;
public JSONObject nutrition(Menu menu, long day, String crow, List<MenuDish> dishes) { public JSONObject nutrition(Menu menu, long day, String crow) {
Nutrition nutrition = getNutrition(menu); Nutrition nutrition = getNutrition(menu);
BigDecimal overflow = nutrition.getOverflow(); List<MenuDish> dishes = day == 0 ? menuDishService.query(menu.getId(), menu.getVender()) : menuDishService.query(menu.getId(), menu.getVender(), day);
Map<String, Map<String, BigDecimal>> itemStandard = nutrition.getIngredient().getOrDefault(crow, new HashMap<>()); Map<String, Map<String, BigDecimal>> itemStandard = nutrition.getIngredient().getOrDefault(crow, new HashMap<>());
Map<String, Ingredient> ingredientMap = ingredientService.getFullByKeys(dishes.stream().filter(x -> CollectionUtils.isNotEmpty(x.getIngredient())).flatMap(x -> x.getIngredient().stream().map(MenuDishItemDTO::getKey)).collect(Collectors.toSet())).stream().collect(Collectors.toMap(Ingredient::getKey, v -> v)); Map<String, Ingredient> ingredientMap = ingredientService.getFullByKeys(dishes.stream().filter(x -> CollectionUtils.isNotEmpty(x.getIngredient())).flatMap(x -> x.getIngredient().stream().map(MenuDishItemDTO::getKey)).collect(Collectors.toSet())).stream().collect(Collectors.toMap(Ingredient::getKey, v -> v));
Map<String, BigDecimal> items = dishes.stream() Map<String, BigDecimal> items = dishes.stream()
@ -62,10 +66,23 @@ public class MenuReportService {
result.put("crow", crow); result.put("crow", crow);
result.put("meals", dishes.stream().map(MenuDish::getMeal).collect(Collectors.toSet())); result.put("meals", dishes.stream().map(MenuDish::getMeal).collect(Collectors.toSet()));
result.put("types", types); result.put("types", types);
if (day == 0) {
List<Long> days = dishes.stream().map(MenuDish::getDay).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(days)) {
result.put("days", days);
items.forEach((k, v) -> items.put(k, v.divide(new BigDecimal(days.size()), RoundingMode.HALF_UP)));
}
} else {
List<Long> days = menuDishService.days(menu.getId(), menu.getVender());
if (CollectionUtils.isNotEmpty(days)) {
result.put("days", days);
}
}
JSONArray contents = new JSONArray(); JSONArray contents = new JSONArray();
items.forEach((key, value) -> { items.forEach((key, value) -> {
FoodNutrient foodNutrient = enumService.getNutrient(key); FoodNutrient foodNutrient = enumService.getNutrient(key);
BigDecimal overflow = MapUtils.isNotEmpty(nutrition.getOverflows()) ? nutrition.getOverflows().getOrDefault(key, nutrition.getOverflow()) : nutrition.getOverflow();
if (foodNutrient == null) { if (foodNutrient == null) {
return; return;
@ -235,6 +252,13 @@ public class MenuReportService {
result.put("day", day); result.put("day", day);
result.put("crow", crow); result.put("crow", crow);
result.put("meals", dishes.stream().map(MenuDish::getMeal).collect(Collectors.toSet())); result.put("meals", dishes.stream().map(MenuDish::getMeal).collect(Collectors.toSet()));
List<Long> days = dishes.stream().map(MenuDish::getDay).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(days)) {
result.put("days", days);
if (day == 0) {
items.forEach((k, v) -> items.put(k, v.divide(new BigDecimal(days.size()), RoundingMode.HALF_UP)));
}
}
BigDecimal protein = items.get("protein").multiply(new BigDecimal("4")); BigDecimal protein = items.get("protein").multiply(new BigDecimal("4"));
BigDecimal fat = items.get("fat").multiply(new BigDecimal("9")); BigDecimal fat = items.get("fat").multiply(new BigDecimal("9"));

11
diet-core/src/main/java/com/mathvision/diet/service/NutritionService.java

@ -1,15 +1,18 @@
package com.mathvision.diet.service; package com.mathvision.diet.service;
import com.google.common.collect.Lists;
import com.mathvision.diet.entity.Nutrition; import com.mathvision.diet.entity.Nutrition;
import com.mathvision.diet.repository.NutritionRepository; import com.mathvision.diet.repository.NutritionRepository;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import javax.annotation.PostConstruct;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.Instant; import java.time.Instant;
import java.util.List; import java.util.List;
@ -31,6 +34,14 @@ public class NutritionService {
@Resource @Resource
private NutritionRepository nutritionRepository; private NutritionRepository nutritionRepository;
@PostConstruct
private void init() {
nutritionRepository.findAll().stream().filter(x -> CollectionUtils.isEmpty(x.getCrows()) && MapUtils.isNotEmpty(x.getIngredient())).forEach(x -> {
x.setCrows(Lists.newArrayList(x.getIngredient().keySet()));
nutritionRepository.save(x);
});
}
public Nutrition add(Nutrition nutrition, String operator) { public Nutrition add(Nutrition nutrition, String operator) {
check(nutrition, operator); check(nutrition, operator);
log.info("[NutritionService] add name = " + nutrition.getName() + ", operator = " + operator); log.info("[NutritionService] add name = " + nutrition.getName() + ", operator = " + operator);

2
diet-dao/src/main/java/com/mathvision/diet/entity/Dish.java

@ -56,7 +56,7 @@ public class Dish {
private String marks; private String marks;
@Type(type = "json") @Type(type = "json")
@Column(name = "label", columnDefinition = "json", nullable = false) @Column(name = "label", columnDefinition = "json")
private List<String> label; private List<String> label;
@Column(name = "poly", nullable = false, length = 16) @Column(name = "poly", nullable = false, length = 16)

6
diet-dao/src/main/java/com/mathvision/diet/entity/Menu.java

@ -29,6 +29,12 @@ public class Menu {
setName(name); setName(name);
} }
public Menu(Long id, String name, List<Long> day) {
setId(id);
setName(name);
setDay(day);
}
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false) @Column(name = "id", nullable = false)

4
diet-dao/src/main/java/com/mathvision/diet/entity/MenuDish.java

@ -50,6 +50,10 @@ public class MenuDish {
@Column(name = "marks", nullable = false, length = 45) @Column(name = "marks", nullable = false, length = 45)
private String marks; private String marks;
@Type(type = "json")
@Column(name = "label", columnDefinition = "json")
private List<String> label;
@Column(name = "poly", nullable = false, length = 16) @Column(name = "poly", nullable = false, length = 16)
private String poly; private String poly;

8
diet-dao/src/main/java/com/mathvision/diet/entity/Nutrition.java

@ -36,6 +36,10 @@ public class Nutrition {
@Column(name = "vendors", columnDefinition = "json", nullable = false) @Column(name = "vendors", columnDefinition = "json", nullable = false)
private List<Long> vendors; private List<Long> vendors;
@Type(type = "json")
@Column(name = "crows", columnDefinition = "json")
private List<String> crows;
@Convert(converter = RuleItemConvert.class) @Convert(converter = RuleItemConvert.class)
@Column(name = "food_category_day", columnDefinition = "json") @Column(name = "food_category_day", columnDefinition = "json")
private List<RuleItemDTO> foodCategoryDay; private List<RuleItemDTO> foodCategoryDay;
@ -51,6 +55,10 @@ public class Nutrition {
@Column(name = "overflow", nullable = false, precision = 5, scale = 2) @Column(name = "overflow", nullable = false, precision = 5, scale = 2)
private BigDecimal overflow; private BigDecimal overflow;
@Type(type = "json")
@Column(name = "overflows", columnDefinition = "json")
private Map<String, BigDecimal> overflows;
@JSONField(serialize = false) @JSONField(serialize = false)
@Column(name = "operate", length = 45) @Column(name = "operate", length = 45)
private String operate; private String operate;

9
diet-dao/src/main/java/com/mathvision/diet/repository/MenuDishRepository.java

@ -3,16 +3,18 @@ package com.mathvision.diet.repository;
import com.mathvision.diet.entity.MenuDish; import com.mathvision.diet.entity.MenuDish;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.time.Instant;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
public interface MenuDishRepository extends JpaRepository<MenuDish, Long>, JpaSpecificationExecutor<MenuDish> { public interface MenuDishRepository extends JpaRepository<MenuDish, Long>, JpaSpecificationExecutor<MenuDish> {
boolean existsByMenuAndDay(Long menu, Long day); boolean existsByMenuAndDay(Long menu, Long day);
boolean existsByMenu(Long menu);
@Transactional @Transactional
long deleteByVender(Long vender); long deleteByVender(Long vender);
MenuDish findByMenuAndVenderAndDayAndDishAndMeal(Long menu, Long vender, Long day, Long dish, String meal);
List<MenuDish> findByMenuAndVenderAndDay(Long menu, Long vender, Long day); List<MenuDish> findByMenuAndVenderAndDay(Long menu, Long vender, Long day);
List<MenuDish> findByMenuAndVender(Long menu, Long vender); List<MenuDish> findByMenuAndVender(Long menu, Long vender);
MenuDish findByIdAndVender(Long id, Long vender); MenuDish findByIdAndVender(Long id, Long vender);
@ -23,4 +25,9 @@ public interface MenuDishRepository extends JpaRepository<MenuDish, Long>, JpaSp
long deleteByMenuAndVender(Long menu, Long vender); long deleteByMenuAndVender(Long menu, Long vender);
@Transactional @Transactional
long deleteByMenu(Long menu); long deleteByMenu(Long menu);
List<MenuDish> findByModifyGreaterThan(Instant updateLabelTime);
@Query("select distinct d.day from MenuDish d where d.menu=?1 and d.vender = ?2")
List<Long> distinctDaysByMenuAndVender(Long id, Long name);
} }

5
diet-dao/src/main/java/com/mathvision/diet/repository/MenuRepository.java

@ -22,6 +22,11 @@ public interface MenuRepository extends JpaRepository<Menu, Long>, JpaSpecificat
@Query(value = "select new Menu(m.id, m.name) from Menu m " + @Query(value = "select new Menu(m.id, m.name) from Menu m " +
"where m.vender = ?1 and m.status = 5 and now() between m.startTime and m.endTime") "where m.vender = ?1 and m.status = 5 and now() between m.startTime and m.endTime")
List<Menu> findCurrentMenu(Long vender); List<Menu> findCurrentMenu(Long vender);
@Query(value = "select m from Menu m " +
"where m.vender = ?1 and m.status = 5 and WEEK(now()) between WEEK(m.startTime) and WEEK(m.endTime) and YEAR(now()) between YEAR(m.startTime) and YEAR(m.endTime)")
List<Menu> findCurrentWeekMenu(Long vender);
@Query("select new com.mathvision.diet.domian.MenuCountDTO(m.status, count(m)) from Menu m where m.status in ?1 group by m.status") @Query("select new com.mathvision.diet.domian.MenuCountDTO(m.status, count(m)) from Menu m where m.status in ?1 group by m.status")
List<MenuCountDTO> count(Collection<MenuStatus> statuses); List<MenuCountDTO> count(Collection<MenuStatus> statuses);
@Query("select count(m) from Menu m where m.vender = ?1") @Query("select count(m) from Menu m where m.vender = ?1")

9
diet-web/src/main/java/com/mathvision/diet/controller/MenuDisplayController.java

@ -21,11 +21,14 @@ public class MenuDisplayController extends BaseController {
MenuReleaseService menuReleaseService; MenuReleaseService menuReleaseService;
@ResponseBody @ResponseBody
@RequestMapping(method = RequestMethod.GET) @RequestMapping(method = RequestMethod.GET)
public Object query(@RequestParam(required = false) Long menuId) { public Object query(@RequestParam(required = false) Long menuId, @RequestParam(required = false) Long day) {
Assert.isTrue(!isAdmin(), "[参数错误] 非管理端操作!"); Assert.isTrue(!isAdmin(), "[参数错误] 非管理端操作!");
if (menuId == null && day == null) {
return menuReleaseService.currentWeek(getVender());
}
if (menuId == null) { if (menuId == null) {
return menuReleaseService.current(getVender()); return menuReleaseService.current(getVender(), day);
} }
return menuDishService.query(menuId, getVender(), (long) LocalDate.now().getDayOfWeek().getValue()); return menuDishService.query(menuId, getVender(), day != null ? day : (long) LocalDate.now().getDayOfWeek().getValue());
} }
} }

7
diet-web/src/main/java/com/mathvision/diet/controller/MenuReportController.java

@ -37,8 +37,7 @@ public class MenuReportController extends BaseController {
Menu menu = checkAndGetMenu(id); Menu menu = checkAndGetMenu(id);
day = checkAndGetDay(day, menu.getDay()); day = checkAndGetDay(day, menu.getDay());
crow = checkAndGetCrow(crow, menu.getCrows()); crow = checkAndGetCrow(crow, menu.getCrows());
List<MenuDish> dishes = menuDishService.query(id, menu.getVender(), day); return menuReportService.nutrition(menu, day, crow);
return menuReportService.nutrition(menu, day, crow, dishes);
} }
@ResponseBody @ResponseBody
@ -65,7 +64,7 @@ public class MenuReportController extends BaseController {
Menu menu = checkAndGetMenu(id); Menu menu = checkAndGetMenu(id);
day = checkAndGetDay(day, menu.getDay()); day = checkAndGetDay(day, menu.getDay());
crow = checkAndGetCrow(crow, menu.getCrows()); crow = checkAndGetCrow(crow, menu.getCrows());
List<MenuDish> dishes = menuDishService.query(id, menu.getVender(), day); List<MenuDish> dishes = day == 0 ? menuDishService.query(id, menu.getVender()) : menuDishService.query(id, menu.getVender(), day);
return menuReportService.energy(day, crow, dishes); return menuReportService.energy(day, crow, dishes);
} }
@ -85,7 +84,7 @@ public class MenuReportController extends BaseController {
} }
private Long checkAndGetDay(Long day, List<Long> days) { private Long checkAndGetDay(Long day, List<Long> days) {
return day == null || !days.contains(day) ? (long) LocalDate.now().getDayOfWeek().getValue() : day; return day == null || day !=0 && !days.contains(day) ? (long) LocalDate.now().getDayOfWeek().getValue() : day;
} }
private String checkAndGetCrow(String crow, List<String> allCrow) { private String checkAndGetCrow(String crow, List<String> allCrow) {

30
diet-web/src/main/java/com/mathvision/diet/controller/NutritionController.java

@ -2,7 +2,10 @@ package com.mathvision.diet.controller;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference; import com.alibaba.fastjson2.TypeReference;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mathvision.diet.domian.RuleItemDTO; import com.mathvision.diet.domian.RuleItemDTO;
import com.mathvision.diet.entity.Nutrition; import com.mathvision.diet.entity.Nutrition;
import com.mathvision.diet.service.EnumService; import com.mathvision.diet.service.EnumService;
@ -21,8 +24,10 @@ import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
@RequestMapping("/api/nutrition") @RequestMapping("/api/nutrition")
@Controller @Controller
@ -35,13 +40,19 @@ public class NutritionController extends BaseController {
@ResponseBody @ResponseBody
@RequestMapping(method = RequestMethod.PUT) @RequestMapping(method = RequestMethod.PUT)
public Nutrition add(@RequestParam String name, @RequestParam List<Long> vendors, @RequestParam BigDecimal overflow) { public Nutrition add(@RequestParam String name, @RequestParam(required=false) List<String> crows, @RequestParam List<Long> vendors, @RequestParam BigDecimal overflow, @RequestParam(required=false) String overflows) {
Assert.isTrue(isAdmin(), "[参数错误]无操作权限!"); Assert.isTrue(isAdmin(), "[参数错误]无操作权限!");
Assert.isTrue(StringUtils.isNotBlank(name), "[参数错误]营养计划名称必填!"); Assert.isTrue(StringUtils.isNotBlank(name), "[参数错误]营养计划名称必填!");
Assert.notNull(overflow, "[参数错误]溢出范围必填!"); Assert.notNull(overflow, "[参数错误]溢出范围必填!");
Assert.isTrue(CollectionUtils.isNotEmpty(vendors), "[参数错误]单位列表必填!"); Assert.isTrue(CollectionUtils.isNotEmpty(vendors), "[参数错误]单位列表必填!");
Assert.isTrue(nutritionService.notExists(name), "[参数错误]营养计划名称已存在!"); Assert.isTrue(nutritionService.notExists(name), "[参数错误]营养计划名称已存在!");
return nutritionService.add(Nutrition.builder().name(name).overflow(overflow).vendors(vendors).build(), getUid()); if (StringUtils.isBlank(overflows) || !JSON.isValidObject(overflows)) {
overflows = JSONObject.toJSONString(Maps.newHashMap());
}
if (crows == null) {
crows = new ArrayList<>();
}
return nutritionService.add(Nutrition.builder().name(name).crows(crows).overflow(overflow).overflows(JSON.parseObject(overflows, new TypeReference<Map<String, BigDecimal>>() {})).vendors(vendors).build(), getUid());
} }
@ResponseBody @ResponseBody
@ -56,7 +67,7 @@ public class NutritionController extends BaseController {
@ResponseBody @ResponseBody
@RequestMapping(method = RequestMethod.POST) @RequestMapping(method = RequestMethod.POST)
public Nutrition update(@RequestParam Long id, @RequestParam(required = false) String name, @RequestParam(required = false) BigDecimal overflow, @RequestParam(required = false) List<Long > vendors, @RequestParam(required = false) String foodCategoryDay, @RequestParam(required = false) String foodCategoryWeek, @RequestParam(required = false) String ingredient) { public Nutrition update(@RequestParam Long id, @RequestParam(required = false) String name, @RequestParam(required=false) List<String> crows, @RequestParam(required = false) BigDecimal overflow, @RequestParam(required=false) String overflows, @RequestParam(required = false) List<Long > vendors, @RequestParam(required = false) String foodCategoryDay, @RequestParam(required = false) String foodCategoryWeek, @RequestParam(required = false) String ingredient) {
Assert.isTrue(isAdmin(), "[参数错误]无操作权限!"); Assert.isTrue(isAdmin(), "[参数错误]无操作权限!");
Assert.isTrue(ingredient == null || JSON.isValid(ingredient), "[参数错误]食材JSON解析错误!"); Assert.isTrue(ingredient == null || JSON.isValid(ingredient), "[参数错误]食材JSON解析错误!");
@ -72,6 +83,9 @@ public class NutritionController extends BaseController {
nutrition.setOverflow(overflow); nutrition.setOverflow(overflow);
flag = true; flag = true;
} }
if (StringUtils.isNotBlank(overflows) && JSON.isValidObject(overflows)) {
nutrition.setOverflows(JSON.parseObject(overflows, new TypeReference<Map<String, BigDecimal>>() {}));
}
if(CollectionUtils.isNotEmpty(vendors)) { if(CollectionUtils.isNotEmpty(vendors)) {
nutrition.setVendors(vendors); nutrition.setVendors(vendors);
flag = true; flag = true;
@ -88,6 +102,16 @@ public class NutritionController extends BaseController {
nutrition.setIngredient(JSON.parseObject(ingredient, new TypeReference<Map<String, Map<String, Map<String, BigDecimal>>>>(){})); nutrition.setIngredient(JSON.parseObject(ingredient, new TypeReference<Map<String, Map<String, Map<String, BigDecimal>>>>(){}));
flag = true; flag = true;
} }
if(CollectionUtils.isNotEmpty(crows)) {
Nutrition finalNutrition = nutrition;
crows = crows.stream().filter(x -> finalNutrition.getIngredient().containsKey(x)).collect(Collectors.toList());
nutrition.setCrows(crows);
flag = true;
} else {
if (nutrition.getCrows() == null || nutrition.getCrows().isEmpty()) {
nutrition.setCrows(Lists.newArrayList(nutrition.getIngredient().keySet()));
}
}
if (flag) { if (flag) {
nutrition = nutritionService.update(nutrition, getUid()); nutrition = nutritionService.update(nutrition, getUid());
} }

10
diet-web/src/main/resources/static/change.html

@ -53,5 +53,15 @@
<h3>12.08</h3> <h3>12.08</h3>
<ul> <ul>
<li>²ËÆ·½Ó¿Ú: ·µ»ØÖµÔö¼ÓÈýµÍ±êÇ©, ×Ö¶ÎÃû-label, ÀàÐÍ-list;</li> <li>²ËÆ·½Ó¿Ú: ·µ»ØÖµÔö¼ÓÈýµÍ±êÇ©, ×Ö¶ÎÃû-label, ÀàÐÍ-list;</li>
<li>食谱菜品: 返回值增加三低标签, 字段名-label, 类型-list;</li>
<li>营养计划: 增加crows字段,类型-list, list顺序就是人群的顺序, 字段为空则默认按照原来的顺序;
增加overflows字段,类型Map&lt;String, double&gt;, 该字段用于添加指定营养素的溢出值;原来的overflow字段标识默认溢出范围
新增和修改协议变更以上两个字段; 查询接口返回增加以上两个字段;</li>
</ul>
<h3>12.10</h3>
<ul>
<li>大屏显示: 新增查询本周那些天有食谱, 其他两个接口支持按天查询;</li>
<li>食谱分析: 营养素分析接口增加查询日平均值(day=0),
营养素分析和能量分析两个按天查询的接口返回值增加days字段,标识该食谱上那些天有菜品有数据</li>
</ul> </ul>

20
diet-web/src/main/resources/static/menu/display.html

@ -1,9 +1,23 @@
<h1>大屏展示</h1> <h1>大屏展示</h1>
<h1>1. 查询今日食谱列表(大屏显示)</h1> <h1>1. 查询本周那些天有食谱(大屏显示)</h1>
<blockquote> <blockquote>
<p>GET /api/menu/display</p> <p>GET /api/menu/display</p>
</blockquote> </blockquote>
<h3>输出:</h3> <h3>输出:</h3>
<pre><code class="json">{
&quot;body&quot;: [
1,2,3,4,5,6,7
],
&quot;code&quot;: 200,
&quot;desc&quot;: &quot;成功&quot;,
&quot;success&quot;: true
}
</code></pre>
<h1>2. 查询指定日期食谱列表(大屏显示)</h1>
<blockquote>
<p>GET /api/menu/display?day=1</p>
</blockquote>
<h3>输出:</h3>
<pre><code class="json">{ <pre><code class="json">{
&quot;body&quot;: [ &quot;body&quot;: [
{&quot;id&quot;: 1, &quot;name&quot;: &quot;食谱名字&quot;} {&quot;id&quot;: 1, &quot;name&quot;: &quot;食谱名字&quot;}
@ -13,9 +27,9 @@
&quot;success&quot;: true &quot;success&quot;: true
} }
</code></pre> </code></pre>
<h1>2. 查询今日带量菜品(大屏显示)</h1> <h1>3. 查询指定食谱指定日期带量菜品(大屏显示)</h1>
<blockquote> <blockquote>
<p>GET /api/menu/display?menuId=1</p> <p>GET /api/menu/display?menuId=1&amp;day=1 (day默认是当天)</p>
</blockquote> </blockquote>
<h3>输出:</h3> <h3>输出:</h3>
<pre><code class="json">{ <pre><code class="json">{

4
diet-web/src/main/resources/static/menu/report.html

@ -4,7 +4,7 @@
</blockquote> </blockquote>
<h3>输入:</h3> <h3>输入:</h3>
<pre><code class="text">id=1 // 食谱ID, 必填 <pre><code class="text">id=1 // 食谱ID, 必填
day=3 // 那一天, 默认当天 day=3 // 那一天, 默认当天 , day=0标识查询日平均分析
crow=xxx //人群,默认第一个人群 crow=xxx //人群,默认第一个人群
</code></pre> </code></pre>
<h3>输出:</h3> <h3>输出:</h3>
@ -146,7 +146,7 @@ crow=xxx //
</blockquote> </blockquote>
<h3>输入:</h3> <h3>输入:</h3>
<pre><code class="text">id=1 // 食谱ID, 必填 <pre><code class="text">id=1 // 食谱ID, 必填
day=3 // 那一天, 默认当天 day=3 // 那一天, 默认当天 , day=0标识查询日平均分析
crow=xxx //人群,默认第一个人群 crow=xxx //人群,默认第一个人群
</code></pre> </code></pre>
<h3>输出:</h3> <h3>输出:</h3>

8
doc/change.md

@ -51,3 +51,11 @@
### 12.08 ### 12.08
* 菜品接口: 返回值增加三低标签, 字段名-label, 类型-list; * 菜品接口: 返回值增加三低标签, 字段名-label, 类型-list;
* 食谱菜品: 返回值增加三低标签, 字段名-label, 类型-list;
* 营养计划: 增加crows字段,类型-list, list顺序就是人群的顺序, 字段为空则默认按照原来的顺序;
增加overflows字段,类型Map<String, double>, 该字段用于添加指定营养素的溢出值;原来的overflow字段标识默认溢出范围
新增和修改协议变更以上两个字段; 查询接口返回增加以上两个字段;
### 12.10
* 大屏显示: 新增查询本周那些天有食谱, 其他两个接口支持按天查询;
* 食谱分析: 营养素分析接口增加查询日平均值(day=0),
营养素分析和能量分析两个按天查询的接口返回值增加days字段,标识该食谱上那些天有菜品有数据

22
doc/menu/display.md

@ -1,9 +1,25 @@
# 大屏展示 # 大屏展示
# 1. 查询今日食谱列表(大屏显示) # 1. 查询本周那些天有食谱(大屏显示)
> GET /api/menu/display > GET /api/menu/display
### 输出:
~~~json
{
"body": [
1,2,3,4,5,6,7
],
"code": 200,
"desc": "成功",
"success": true
}
~~~
# 2. 查询指定日期食谱列表(大屏显示)
> GET /api/menu/display?day=1
### 输出: ### 输出:
~~~json ~~~json
{ {
@ -16,9 +32,9 @@
} }
~~~ ~~~
# 2. 查询今日带量菜品(大屏显示) # 3. 查询指定食谱指定日期带量菜品(大屏显示)
> GET /api/menu/display?menuId=1 > GET /api/menu/display?menuId=1&day=1 (day默认是当天)
### 输出: ### 输出:
~~~json ~~~json

4
doc/menu/report.md

@ -6,7 +6,7 @@
### 输入: ### 输入:
```text ```text
id=1 // 食谱ID, 必填 id=1 // 食谱ID, 必填
day=3 // 那一天, 默认当天 day=3 // 那一天, 默认当天 , day=0标识查询日平均分析
crow=xxx //人群,默认第一个人群 crow=xxx //人群,默认第一个人群
``` ```
@ -153,7 +153,7 @@ crow=xxx //
### 输入: ### 输入:
```text ```text
id=1 // 食谱ID, 必填 id=1 // 食谱ID, 必填
day=3 // 那一天, 默认当天 day=3 // 那一天, 默认当天 , day=0标识查询日平均分析
crow=xxx //人群,默认第一个人群 crow=xxx //人群,默认第一个人群
``` ```

8
sql/update.sql

@ -2,4 +2,10 @@ ALTER TABLE `diet`.`ingredient` DROP INDEX `idx_key`;
ALTER TABLE `diet`.`dish` ADD INDEX `idx_marks`(`marks`); ALTER TABLE `diet`.`dish` ADD INDEX `idx_marks`(`marks`);
ALTER TABLE `diet`.`dish` ADD INDEX `idx_modify`(`modify`); ALTER TABLE `diet`.`dish` ADD INDEX `idx_modify`(`modify`);
ALTER TABLE `diet`.`dish` ADD COLUMN `label` json NULL COMMENT '标签' AFTER `poly`; ALTER TABLE `diet`.`dish` ADD COLUMN `label` json NULL COMMENT '标签' AFTER `poly`;
ALTER TABLE `diet`.`menu_dish` ADD COLUMN `label` json NULL COMMENT '标签' AFTER `poly`;
ALTER TABLE `diet`.`nutrition`
ADD COLUMN `crows` json NULL COMMENT '人群' AFTER `vendors`,
ADD COLUMN `overflows` json NULL COMMENT '溢出范围(分营养素)' AFTER `overflow`;
Loading…
Cancel
Save