commit
bfe1ce8535
140 changed files with 11977 additions and 0 deletions
@ -0,0 +1,29 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
|
<modelVersion>4.0.0</modelVersion> |
||||
|
|
||||
|
<groupId>com.mathvision.diet</groupId> |
||||
|
<artifactId>diet-core</artifactId> |
||||
|
<version>1.0-SNAPSHOT</version> |
||||
|
|
||||
|
<properties> |
||||
|
<maven.compiler.source>8</maven.compiler.source> |
||||
|
<maven.compiler.target>8</maven.compiler.target> |
||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
||||
|
</properties> |
||||
|
|
||||
|
<dependencies> |
||||
|
<dependency> |
||||
|
<groupId>com.mathvision.diet</groupId> |
||||
|
<artifactId>diet-dao</artifactId> |
||||
|
<version>1.0-SNAPSHOT</version> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>com.alibaba</groupId> |
||||
|
<artifactId>easyexcel</artifactId> |
||||
|
<version>3.3.2</version> |
||||
|
</dependency> |
||||
|
</dependencies> |
||||
|
</project> |
||||
@ -0,0 +1,17 @@ |
|||||
|
package com.mathvision.diet.domain; |
||||
|
|
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.Builder; |
||||
|
import lombok.Data; |
||||
|
import lombok.NoArgsConstructor; |
||||
|
|
||||
|
@Builder |
||||
|
@Data |
||||
|
@NoArgsConstructor |
||||
|
@AllArgsConstructor |
||||
|
public class ComponentAnalysisDO { |
||||
|
|
||||
|
String name; |
||||
|
String nutrition; |
||||
|
String nvr; |
||||
|
} |
||||
@ -0,0 +1,18 @@ |
|||||
|
package com.mathvision.diet.domain; |
||||
|
|
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.Builder; |
||||
|
import lombok.Data; |
||||
|
import lombok.NoArgsConstructor; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
@Builder |
||||
|
@Data |
||||
|
@NoArgsConstructor |
||||
|
@AllArgsConstructor |
||||
|
public class DishLabelDO { |
||||
|
String name; |
||||
|
List<ComponentAnalysisDO> component; |
||||
|
List<String> ingredients; |
||||
|
} |
||||
@ -0,0 +1,33 @@ |
|||||
|
package com.mathvision.diet.domain; |
||||
|
|
||||
|
import com.alibaba.fastjson2.annotation.JSONField; |
||||
|
import com.mathvision.diet.entity.RoleItem; |
||||
|
import com.mathvision.diet.entity.Vender; |
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.Builder; |
||||
|
import lombok.Data; |
||||
|
import lombok.NoArgsConstructor; |
||||
|
|
||||
|
import java.time.Instant; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@Builder |
||||
|
@Data |
||||
|
@NoArgsConstructor |
||||
|
@AllArgsConstructor |
||||
|
public class UserDO { |
||||
|
private String uid; |
||||
|
private String name; |
||||
|
private String phone; |
||||
|
private Vender vender; |
||||
|
private Long roleId; |
||||
|
private String roleName; |
||||
|
private String roleType; |
||||
|
private Instant time; |
||||
|
private List<RoleItem> roleItems; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
public boolean isAdmin() { |
||||
|
return vender == null; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,43 @@ |
|||||
|
package com.mathvision.diet.excel; |
||||
|
|
||||
|
import com.alibaba.excel.converters.Converter; |
||||
|
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
|
import com.alibaba.excel.metadata.GlobalConfiguration; |
||||
|
import com.alibaba.excel.metadata.data.ReadCellData; |
||||
|
import com.alibaba.excel.metadata.data.WriteCellData; |
||||
|
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
|
import com.alibaba.excel.util.NumberUtils; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
|
||||
|
import java.math.BigDecimal; |
||||
|
import java.text.ParseException; |
||||
|
|
||||
|
public class BigDecimalStringConverter implements Converter<BigDecimal> { |
||||
|
public BigDecimalStringConverter() { |
||||
|
} |
||||
|
|
||||
|
public Class<BigDecimal> supportJavaTypeKey() { |
||||
|
return BigDecimal.class; |
||||
|
} |
||||
|
|
||||
|
public CellDataTypeEnum supportExcelTypeKey() { |
||||
|
return CellDataTypeEnum.STRING; |
||||
|
} |
||||
|
|
||||
|
public BigDecimal convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws ParseException { |
||||
|
if(cellData == null) { |
||||
|
return null; |
||||
|
} |
||||
|
if(CellDataTypeEnum.NUMBER.equals(cellData.getType())) { |
||||
|
return cellData.getNumberValue(); |
||||
|
} |
||||
|
if (CellDataTypeEnum.STRING.equals(cellData.getType())) { |
||||
|
return NumberUtils.parseBigDecimal(cellData.getStringValue(), contentProperty); |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
public WriteCellData<?> convertToExcelData(BigDecimal value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { |
||||
|
return NumberUtils.formatToCellDataString(value, contentProperty); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,69 @@ |
|||||
|
package com.mathvision.diet.excel; |
||||
|
|
||||
|
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
import com.alibaba.excel.annotation.write.style.ColumnWidth; |
||||
|
import com.alibaba.excel.annotation.write.style.ContentFontStyle; |
||||
|
import com.alibaba.excel.annotation.write.style.ContentRowHeight; |
||||
|
import com.alibaba.excel.annotation.write.style.HeadRowHeight; |
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.Builder; |
||||
|
import lombok.Data; |
||||
|
import lombok.NoArgsConstructor; |
||||
|
|
||||
|
import java.math.BigDecimal; |
||||
|
|
||||
|
@Builder |
||||
|
@Data |
||||
|
@NoArgsConstructor |
||||
|
@AllArgsConstructor |
||||
|
@HeadRowHeight(30) |
||||
|
@ContentRowHeight(15) |
||||
|
@ColumnWidth(25) |
||||
|
@ContentFontStyle(fontHeightInPoints = (short) 12) |
||||
|
public class IngredientModel { |
||||
|
|
||||
|
@ExcelProperty("食物编码") |
||||
|
private String key; |
||||
|
|
||||
|
@ExcelProperty("食物名称") |
||||
|
private String name; |
||||
|
|
||||
|
@ExcelProperty("食物类型") |
||||
|
private String type; |
||||
|
|
||||
|
@ExcelProperty(value = "能量(kcal)", converter = BigDecimalStringConverter.class) |
||||
|
private BigDecimal energy; |
||||
|
|
||||
|
@ExcelProperty(value = "蛋白质(g)", converter = BigDecimalStringConverter.class) |
||||
|
private BigDecimal protein; |
||||
|
|
||||
|
@ExcelProperty(value = "脂肪(g)", converter = BigDecimalStringConverter.class) |
||||
|
private BigDecimal fat; |
||||
|
|
||||
|
@ExcelProperty(value = "碳水化合物(g)", converter = BigDecimalStringConverter.class) |
||||
|
private BigDecimal carbs; |
||||
|
|
||||
|
@ExcelProperty(value = "钙(mg)", converter = BigDecimalStringConverter.class) |
||||
|
private BigDecimal calcium; |
||||
|
|
||||
|
@ExcelProperty(value = "铁(mg)", converter = BigDecimalStringConverter.class) |
||||
|
private BigDecimal iron; |
||||
|
|
||||
|
@ExcelProperty(value = "锌(mg)", converter = BigDecimalStringConverter.class) |
||||
|
private BigDecimal zinc; |
||||
|
|
||||
|
@ExcelProperty(value = "维生素A(μgRAE)", converter = BigDecimalStringConverter.class) |
||||
|
private BigDecimal va; |
||||
|
|
||||
|
@ExcelProperty(value = "维生素B1(mg)硫胺素", converter = BigDecimalStringConverter.class) |
||||
|
private BigDecimal vb1; |
||||
|
|
||||
|
@ExcelProperty(value = "维生素B2(mg)核黄素", converter = BigDecimalStringConverter.class) |
||||
|
private BigDecimal vb2; |
||||
|
|
||||
|
@ExcelProperty(value = "维生素C(mg)", converter = BigDecimalStringConverter.class) |
||||
|
private BigDecimal vc; |
||||
|
|
||||
|
@ExcelProperty(value = "膳食纤维(g)", converter = BigDecimalStringConverter.class) |
||||
|
private BigDecimal fiber; |
||||
|
} |
||||
@ -0,0 +1,31 @@ |
|||||
|
package com.mathvision.diet.excel; |
||||
|
|
||||
|
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
import com.alibaba.excel.annotation.write.style.ColumnWidth; |
||||
|
import com.alibaba.excel.annotation.write.style.ContentFontStyle; |
||||
|
import com.alibaba.excel.annotation.write.style.ContentRowHeight; |
||||
|
import com.alibaba.excel.annotation.write.style.HeadRowHeight; |
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.Builder; |
||||
|
import lombok.Data; |
||||
|
import lombok.NoArgsConstructor; |
||||
|
|
||||
|
@Builder |
||||
|
@Data |
||||
|
@NoArgsConstructor |
||||
|
@AllArgsConstructor |
||||
|
@HeadRowHeight(30) |
||||
|
@ContentRowHeight(15) |
||||
|
@ColumnWidth(18) |
||||
|
@ContentFontStyle(fontHeightInPoints = (short) 12) |
||||
|
public class ResultModel { |
||||
|
|
||||
|
@ColumnWidth(80) |
||||
|
@ExcelProperty("数据标识") |
||||
|
private String key; |
||||
|
|
||||
|
@ColumnWidth(35) |
||||
|
@ExcelProperty("导入结果") |
||||
|
private String result; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,147 @@ |
|||||
|
package com.mathvision.diet.service; |
||||
|
|
||||
|
import com.google.common.collect.Lists; |
||||
|
import com.mathvision.diet.domain.ComponentAnalysisDO; |
||||
|
import com.mathvision.diet.domain.DishLabelDO; |
||||
|
import com.mathvision.diet.domian.DishItemDTO; |
||||
|
import com.mathvision.diet.entity.Dish; |
||||
|
import com.mathvision.diet.entity.FoodNutrient; |
||||
|
import com.mathvision.diet.entity.Ingredient; |
||||
|
import com.mathvision.diet.repository.DishRepository; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.collections4.CollectionUtils; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.apache.commons.lang3.tuple.Pair; |
||||
|
import org.springframework.data.domain.Page; |
||||
|
import org.springframework.data.domain.PageRequest; |
||||
|
import org.springframework.data.jpa.domain.Specification; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import javax.persistence.criteria.Predicate; |
||||
|
import java.math.BigDecimal; |
||||
|
import java.math.RoundingMode; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Service |
||||
|
public class DishService { |
||||
|
|
||||
|
@Resource |
||||
|
private EnumService enumService; |
||||
|
|
||||
|
@Resource |
||||
|
private IngredientService ingredientService; |
||||
|
|
||||
|
@Resource |
||||
|
private DishRepository dishRepository; |
||||
|
|
||||
|
public void add(List<Dish> dishes, String operator) { |
||||
|
dishRepository.saveAll(dishes); |
||||
|
log.info("[DishService] add dishes count = " + dishes.size() + ", operator = " + operator); |
||||
|
} |
||||
|
|
||||
|
public void delete(List<Long> ids, Long venderId,String operator) { |
||||
|
if (venderId > 0) { |
||||
|
dishRepository.deleteByIdInAndVender(ids, venderId); |
||||
|
} else { |
||||
|
dishRepository.deleteAllByIdInBatch(ids); |
||||
|
} |
||||
|
log.info("[DishService] delete ids = " + ids + ", operator = " + operator); |
||||
|
} |
||||
|
|
||||
|
public void delete(Long venderId,String operator) { |
||||
|
dishRepository.deleteByVender(venderId); |
||||
|
log.info("[DishService] delete venderId = " + venderId + ", operator = " + operator); |
||||
|
} |
||||
|
|
||||
|
public void update(Dish dish, String operator) { |
||||
|
dishRepository.save(dish); |
||||
|
log.info("[DishService] update name = " + dish.getName() + ", operator = " + operator); |
||||
|
} |
||||
|
|
||||
|
public boolean exists(Long vender, List<Long> ids) { |
||||
|
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 Dish get(Long id) { |
||||
|
return dishRepository.findById(id).orElse(null); |
||||
|
} |
||||
|
|
||||
|
public Dish get(Long id, Long vender) { |
||||
|
return dishRepository.findByIdAndVender(id, vender); |
||||
|
} |
||||
|
|
||||
|
public List<Dish> query(String keyword) { |
||||
|
return dishRepository.findByNameLikeOrderByIdDesc("%" + keyword + "%"); |
||||
|
} |
||||
|
|
||||
|
public Page<Dish> list(Long vender, String name, String mark, PageRequest pageRequest) { |
||||
|
return dishRepository.findAll(toSpecification(vender, name, mark), pageRequest); |
||||
|
} |
||||
|
|
||||
|
private Specification<Dish> toSpecification(Long vender, String name, String mark) { |
||||
|
return (root, query, builder) -> { |
||||
|
List<Predicate> predicates = new ArrayList<>(); |
||||
|
|
||||
|
if (vender > 0) { |
||||
|
predicates.add(builder.equal(root.get("vender"), vender)); |
||||
|
} |
||||
|
|
||||
|
if (StringUtils.isNotBlank(mark)) { |
||||
|
predicates.add(builder.equal(root.get("marks"), mark)); |
||||
|
} |
||||
|
|
||||
|
if (StringUtils.isNotBlank(name)) { |
||||
|
predicates.add(builder.like(root.get("name"), name + "%")); |
||||
|
} |
||||
|
|
||||
|
if (predicates.size() > 1) { |
||||
|
return builder.and(predicates.toArray(new Predicate[0])); |
||||
|
} else if (predicates.size() == 1) { |
||||
|
return predicates.get(0); |
||||
|
} else { |
||||
|
return null; |
||||
|
} |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
public List<DishLabelDO> label(List<Long> ids, Long vender) { |
||||
|
List<Dish> dishes = Lists.newArrayList(); |
||||
|
if (CollectionUtils.isNotEmpty(ids)) { |
||||
|
if (vender > 0) { |
||||
|
dishes.addAll(dishRepository.findByVenderAndIdIn(vender, ids)); |
||||
|
} else { |
||||
|
dishes.addAll(dishRepository.findByIdIn(ids)); |
||||
|
} |
||||
|
} else { |
||||
|
if (vender > 0) { |
||||
|
dishes.addAll(dishRepository.findByVender(vender)); |
||||
|
} else { |
||||
|
dishes.addAll(dishRepository.findAll()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
Map<String, Ingredient> ingredientMap = ingredientService.getFullByKeys(dishes.stream().filter(x -> CollectionUtils.isNotEmpty(x.getIngredient())).flatMap(x -> x.getIngredient().stream().map(DishItemDTO::getKey)).collect(Collectors.toSet())).stream().collect(Collectors.toMap(Ingredient::getKey, v -> v)); |
||||
|
return dishes.parallelStream().filter(dish -> CollectionUtils.isNotEmpty(dish.getIngredient())).map(dish -> { |
||||
|
List<String> ingredients = dish.getIngredient().stream().filter(DishItemDTO::getIsMain).map(DishItemDTO::getKey).filter(ingredientMap::containsKey).map(x -> ingredientMap.get(x).getName()).collect(Collectors.toList()); |
||||
|
List<ComponentAnalysisDO> component = dish.getIngredient().stream().filter(x -> ingredientMap.containsKey(x.getKey())).flatMap(x -> { |
||||
|
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))); |
||||
|
}).collect(Collectors.toMap(Pair::getKey, Pair::getValue, BigDecimal::add)).entrySet().stream().map(r -> { |
||||
|
FoodNutrient foodNutrient = enumService.getNutrient(r.getKey()); |
||||
|
return foodNutrient == null ? |
||||
|
ComponentAnalysisDO.builder().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(); |
||||
|
}).collect(Collectors.toList()); |
||||
|
return DishLabelDO.builder().name(dish.getName()).ingredients(ingredients).component(component).build(); |
||||
|
}).collect(Collectors.toList()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,135 @@ |
|||||
|
package com.mathvision.diet.service; |
||||
|
|
||||
|
import com.google.common.collect.Sets; |
||||
|
import com.mathvision.diet.domian.*; |
||||
|
import com.mathvision.diet.entity.FoodCategory; |
||||
|
import com.mathvision.diet.entity.FoodMark; |
||||
|
import com.mathvision.diet.entity.FoodNutrient; |
||||
|
import com.mathvision.diet.entity.RoleItem; |
||||
|
import com.mathvision.diet.repository.FoodCategoryRepository; |
||||
|
import com.mathvision.diet.repository.FoodMarkRepository; |
||||
|
import com.mathvision.diet.repository.FoodNutrientRepository; |
||||
|
import com.mathvision.diet.repository.RoleItemRepository; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.collections4.KeyValue; |
||||
|
import org.apache.commons.lang3.tuple.Pair; |
||||
|
import org.springframework.scheduling.annotation.Scheduled; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
import javax.annotation.PostConstruct; |
||||
|
import javax.annotation.Resource; |
||||
|
import java.util.*; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Service |
||||
|
public class EnumService { |
||||
|
private static Map<AuthType, Set<String>> AUTH_ITEMS = new HashMap<>(); |
||||
|
private static Map<Long, RoleItem> ROLE_ITEMS = new HashMap<>(); |
||||
|
private static Map<String, FoodMark> FOOD_MARK = new HashMap<>(); |
||||
|
private static Map<String, FoodCategory> FOOD_CATEGORY = new HashMap<>(); |
||||
|
private static Map<String, FoodNutrient> FOOD_NUTRIENT = new HashMap<>(); |
||||
|
|
||||
|
@Resource |
||||
|
private RoleItemRepository roleItemRepository; |
||||
|
|
||||
|
@Resource |
||||
|
private FoodMarkRepository foodMarkRepository; |
||||
|
|
||||
|
@Resource |
||||
|
private FoodCategoryRepository foodCategoryRepository; |
||||
|
|
||||
|
@Resource |
||||
|
private FoodNutrientRepository foodNutrientRepository; |
||||
|
|
||||
|
@PostConstruct |
||||
|
@Scheduled(cron = "0 0/5 * * * *") |
||||
|
public void init() { |
||||
|
ROLE_ITEMS = roleItemRepository.findAll().stream().collect(Collectors.toConcurrentMap(RoleItem::getId, x -> x)); |
||||
|
AUTH_ITEMS = ROLE_ITEMS.values().stream().map(item -> Pair.of(item.getItemType(), "/api/" + item.getItemValue().split(":", 2)[1])).collect(Collectors.toMap(Pair::getKey, v-> Sets.newHashSet(v.getValue()), Sets::union)); |
||||
|
FOOD_MARK = foodMarkRepository.findAll().stream().collect(Collectors.toConcurrentMap(FoodMark::getKey, x -> x)); |
||||
|
FOOD_CATEGORY = foodCategoryRepository.findAll().stream().collect(Collectors.toConcurrentMap(FoodCategory::getKey, x -> x)); |
||||
|
FOOD_NUTRIENT = foodNutrientRepository.findAll().stream().collect(Collectors.toConcurrentMap(FoodNutrient::getKey, x -> x)); |
||||
|
} |
||||
|
|
||||
|
public Map<String, Collection<?>> getAll() { |
||||
|
Map<String, Collection<?>> result = new HashMap<>(); |
||||
|
result.put("mark", FOOD_MARK.values()); |
||||
|
result.put("category", FOOD_CATEGORY.values()); |
||||
|
result.put("nutrient", FOOD_NUTRIENT.values()); |
||||
|
result.put("venderType", Arrays.stream(VenderType.values()).map(x -> new KeyValue<String, String>() { |
||||
|
@Override |
||||
|
public String getKey() { |
||||
|
return x.getType(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getValue() { |
||||
|
return x.getType(); |
||||
|
} |
||||
|
}).collect(Collectors.toList())); |
||||
|
result.put("markType", Arrays.stream(MarkType.values()).map(x -> new KeyValue<String, String>() { |
||||
|
@Override |
||||
|
public String getKey() { |
||||
|
return x.getType(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getValue() { |
||||
|
return x.getType(); |
||||
|
} |
||||
|
}).collect(Collectors.toList())); |
||||
|
result.put("mealType", Arrays.stream(MealType.values()).map(x -> new KeyValue<String, String>() { |
||||
|
@Override |
||||
|
public String getKey() { |
||||
|
return x.getType(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getValue() { |
||||
|
return x.getType(); |
||||
|
} |
||||
|
}).collect(Collectors.toList())); |
||||
|
result.put("menuStatus", Arrays.stream(MenuStatus.values()).map(x -> new KeyValue<Integer, String>() { |
||||
|
@Override |
||||
|
public Integer getKey() { |
||||
|
return x.getCode(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getValue() { |
||||
|
return x.getType(); |
||||
|
} |
||||
|
}).collect(Collectors.toList())); |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
public Set<String> getAuthItems(AuthType authType) { |
||||
|
return AUTH_ITEMS.getOrDefault(authType, Sets.newHashSet()); |
||||
|
} |
||||
|
|
||||
|
public List<RoleItem> getRoleItems(AuthType itemType) { |
||||
|
return ROLE_ITEMS.values().stream().filter(item -> item.getItemType().equals(itemType)).collect(Collectors.toList()); |
||||
|
} |
||||
|
|
||||
|
public List<RoleItem> getRoleItems(List<Long> items) { |
||||
|
return ROLE_ITEMS.entrySet().stream().filter(x -> items.contains(x.getKey())).map(Map.Entry::getValue).collect(Collectors.toList()); |
||||
|
} |
||||
|
|
||||
|
public boolean checkMark(String mark) { |
||||
|
return FOOD_MARK.containsKey(mark); |
||||
|
} |
||||
|
|
||||
|
public boolean checkCategory(String category) { |
||||
|
return FOOD_CATEGORY.containsKey(category); |
||||
|
} |
||||
|
|
||||
|
public boolean checkNutrient(String nutrient) { |
||||
|
return FOOD_NUTRIENT.containsKey(nutrient); |
||||
|
} |
||||
|
|
||||
|
public FoodNutrient getNutrient(String nutrient) { |
||||
|
return FOOD_NUTRIENT.get(nutrient); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,223 @@ |
|||||
|
package com.mathvision.diet.service; |
||||
|
|
||||
|
import com.alibaba.excel.EasyExcel; |
||||
|
import com.alibaba.excel.context.AnalysisContext; |
||||
|
import com.alibaba.excel.read.listener.ReadListener; |
||||
|
import com.alibaba.excel.support.ExcelTypeEnum; |
||||
|
import com.google.common.collect.Lists; |
||||
|
import com.mathvision.diet.domian.MarkType; |
||||
|
import com.mathvision.diet.entity.Ingredient; |
||||
|
import com.mathvision.diet.entity.IngredientMark; |
||||
|
import com.mathvision.diet.excel.IngredientModel; |
||||
|
import com.mathvision.diet.excel.ResultModel; |
||||
|
import com.mathvision.diet.repository.IngredientDTORepository; |
||||
|
import com.mathvision.diet.repository.IngredientMarkRepository; |
||||
|
import com.mathvision.diet.repository.IngredientRepository; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.collections4.MapUtils; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.apache.commons.lang3.tuple.Pair; |
||||
|
import org.springframework.data.domain.Page; |
||||
|
import org.springframework.data.domain.PageRequest; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
import org.springframework.util.Assert; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import java.io.InputStream; |
||||
|
import java.io.OutputStream; |
||||
|
import java.math.BigDecimal; |
||||
|
import java.time.Instant; |
||||
|
import java.util.Arrays; |
||||
|
import java.util.Collection; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Service |
||||
|
public class IngredientService { |
||||
|
|
||||
|
@Resource |
||||
|
private EnumService enumService; |
||||
|
|
||||
|
@Resource |
||||
|
private IngredientRepository ingredientRepository; |
||||
|
|
||||
|
@Resource |
||||
|
private IngredientDTORepository ingredientDTORepository; |
||||
|
|
||||
|
@Resource |
||||
|
private IngredientMarkRepository ingredientMarkRepository; |
||||
|
|
||||
|
public void template(OutputStream outputStream) { |
||||
|
EasyExcel.write(outputStream).head(IngredientModel.class).excelType(ExcelTypeEnum.XLSX).sheet("模板").doWrite(Lists.newArrayList()); |
||||
|
} |
||||
|
|
||||
|
public void upload(InputStream inputStream, OutputStream outputStream, String operator) { |
||||
|
Instant dateTime = Instant.now(); |
||||
|
EasyExcel.read(inputStream, IngredientModel.class, new ReadListener<IngredientModel>() { |
||||
|
final List<ResultModel> resultModels = Lists.newArrayList(); |
||||
|
final List<Ingredient> ingredients = Lists.newArrayList(); |
||||
|
|
||||
|
@Override |
||||
|
public void onException(Exception exception, AnalysisContext context) { |
||||
|
Assert.isTrue(exception == null, "导入异常:" + exception.getMessage()); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void invoke(IngredientModel o, AnalysisContext analysisContext) { |
||||
|
String resultKey = String.format("%s-%s-%s", o.getKey(), o.getName(), o.getType()); |
||||
|
try { |
||||
|
Ingredient ingredient = Ingredient.builder().key(o.getKey()).name(o.getName()).type(o.getType()).nutrient(Arrays.stream(IngredientModel.class.getDeclaredFields()).filter(x -> BigDecimal.class.equals(x.getType())).peek(x -> x.setAccessible(true)).map(x -> { |
||||
|
try { |
||||
|
return Pair.of(x.getName(), (BigDecimal)x.get(o)); |
||||
|
} catch (IllegalAccessException e) { |
||||
|
throw new RuntimeException(e); |
||||
|
} |
||||
|
}).filter(x -> x.getValue() != null).collect(Collectors.toMap(Pair::getKey, Pair::getValue))).operate(operator).created(dateTime).modify(dateTime).build(); |
||||
|
|
||||
|
if (StringUtils.isBlank(ingredient.getKey())) { |
||||
|
resultModels.add(ResultModel.builder().key(resultKey).result("食物编码为空").build()); |
||||
|
return; |
||||
|
} |
||||
|
if (StringUtils.isBlank(ingredient.getName())) { |
||||
|
resultModels.add(ResultModel.builder().key(resultKey).result("食物名称为空").build()); |
||||
|
return; |
||||
|
} |
||||
|
if (ingredientRepository.existsByKeyOrName(ingredient.getKey(), ingredient.getName())) { |
||||
|
resultModels.add(ResultModel.builder().key(resultKey).result("编号/名称已存在").build()); |
||||
|
return; |
||||
|
} |
||||
|
if (ingredients.stream().anyMatch(x -> x.getKey().equals(ingredient.getKey()))) { |
||||
|
resultModels.add(ResultModel.builder().key(resultKey).result("编号在excel文件中存在重复").build()); |
||||
|
return; |
||||
|
} |
||||
|
if (StringUtils.isBlank(ingredient.getType()) || !enumService.checkCategory(ingredient.getType())) { |
||||
|
resultModels.add(ResultModel.builder().key(resultKey).result("食物类型为空或者不支持").build()); |
||||
|
return; |
||||
|
} |
||||
|
if (MapUtils.isEmpty(ingredient.getNutrient()) || !ingredient.getNutrient().entrySet().stream().allMatch(x -> enumService.checkNutrient(x.getKey()))) { |
||||
|
resultModels.add(ResultModel.builder().key(resultKey).result("营养素内容全空或者不支持").build()); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
ingredients.add(ingredient); |
||||
|
resultModels.add(ResultModel.builder().key(resultKey).result("导入成功").build()); |
||||
|
} catch (Exception e) { |
||||
|
resultModels.add(ResultModel.builder().key(resultKey).result("导入失败:" + e.getMessage()).build()); |
||||
|
log.error(String.format("%s-%s-%s 导入失败: %s", o.getKey(), o.getName(), o.getType(), e.getMessage()), e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void doAfterAllAnalysed(AnalysisContext analysisContext) { |
||||
|
if (!ingredients.isEmpty()) { |
||||
|
ingredientRepository.saveAll(ingredients); |
||||
|
} |
||||
|
EasyExcel.write(outputStream).head(ResultModel.class).excelType(ExcelTypeEnum.XLSX).sheet("导入结果").doWrite(resultModels); |
||||
|
} |
||||
|
}).sheet().doRead(); |
||||
|
} |
||||
|
|
||||
|
public void addIngredient(Ingredient ingredient, String operator) { |
||||
|
Assert.isTrue(StringUtils.isNotBlank(ingredient.getKey()) && |
||||
|
StringUtils.isNotBlank(ingredient.getName()) && |
||||
|
StringUtils.isNotBlank(ingredient.getType()) && |
||||
|
MapUtils.isNotEmpty(ingredient.getNutrient()) && |
||||
|
enumService.checkCategory(ingredient.getType()) && |
||||
|
ingredient.getNutrient().entrySet().stream().allMatch(x -> enumService.checkNutrient(x.getKey())) && |
||||
|
!ingredientRepository.existsByKeyOrName(ingredient.getKey(), ingredient.getName()), |
||||
|
"[参数错误]必填参数未填写,或者编号名称重复、类型和营养素不在取值范围内!"); |
||||
|
|
||||
|
Instant dateTime = Instant.now(); |
||||
|
ingredient.setOperate(operator); |
||||
|
ingredient.setCreated(dateTime); |
||||
|
ingredient.setModify(dateTime); |
||||
|
ingredientRepository.save(ingredient); |
||||
|
log.info("[IngredientService] addIngredient name = " + ingredient.getName() + ", operator = " + operator); |
||||
|
} |
||||
|
|
||||
|
public void delIngredient(List<String> ingredients, String operator) { |
||||
|
ingredients.stream().filter(StringUtils::isNotBlank).forEach(ingredient -> { |
||||
|
ingredientRepository.deleteByKey(ingredient); |
||||
|
ingredientMarkRepository.deleteByIngredient(ingredient); |
||||
|
log.info("[IngredientService] delIngredient ingredient = " + ingredient + ", operator = " + operator); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
public void modIngredient(Ingredient ingredient, String operator) { |
||||
|
Assert.isTrue(StringUtils.isNotBlank(ingredient.getKey()) && |
||||
|
StringUtils.isNotBlank(ingredient.getName()) && |
||||
|
StringUtils.isNotBlank(ingredient.getType()) && |
||||
|
MapUtils.isNotEmpty(ingredient.getNutrient()) && |
||||
|
enumService.checkCategory(ingredient.getType()) && |
||||
|
ingredient.getNutrient().entrySet().stream().allMatch(x -> enumService.checkNutrient(x.getKey())) && |
||||
|
!ingredientRepository.existsByIdNotAndKeyOrName(ingredient.getId(), ingredient.getKey(), ingredient.getName()), |
||||
|
"[参数错误]必填参数未填写,或者编号名称重复、类型和营养素不在取值范围内!"); |
||||
|
Instant dateTime = Instant.now(); |
||||
|
ingredient.setOperate(operator); |
||||
|
ingredient.setCreated(dateTime); |
||||
|
ingredient.setModify(dateTime); |
||||
|
ingredientRepository.save(ingredient); |
||||
|
log.info("[IngredientService] modIngredient name = " + ingredient.getName() + ", operator = " + operator); |
||||
|
} |
||||
|
|
||||
|
public Map<String, String> getByKeys() { |
||||
|
return ingredientRepository.findKeyNameMap().stream().collect(Collectors.toMap(Ingredient::getKey, Ingredient::getName)); |
||||
|
} |
||||
|
|
||||
|
public List<Ingredient> getByKeys(List<String> keys) { |
||||
|
return ingredientRepository.findByKeyInOrderByKeyAsc(keys); |
||||
|
} |
||||
|
|
||||
|
public List<Ingredient> getByKeyword(String keyword) { |
||||
|
return ingredientRepository.findByKeyStartsWithOrNameStartsWithOrderByKeyAsc(keyword); |
||||
|
} |
||||
|
|
||||
|
public List<Ingredient> getFullByKeys(Collection<String> keys) { |
||||
|
return ingredientRepository.findByKeyIn(keys); |
||||
|
} |
||||
|
|
||||
|
public Page<?> queryIngredientByMark(Long vender, String mark, String key, String type, PageRequest pageRequest) { |
||||
|
if (vender <= 0) { |
||||
|
if (StringUtils.isBlank(key)) { |
||||
|
return StringUtils.isBlank(type) ? ingredientRepository.findAll(pageRequest) : ingredientRepository.findByTypeOrderByIdDesc(type, pageRequest); |
||||
|
} |
||||
|
if (StringUtils.isNumeric(key)) { |
||||
|
return StringUtils.isBlank(type) ? ingredientRepository.findByKeyStartsWithOrderByIdDesc(key, pageRequest) : ingredientRepository.findByTypeAndKeyStartsWithOrderByIdDesc(type, key, pageRequest); |
||||
|
} else { |
||||
|
return StringUtils.isBlank(type) ? ingredientRepository.findByNameStartsWithOrderByIdDesc(key, pageRequest) : ingredientRepository.findByTypeAndNameStartsWithOrderByIdDesc(type, key, pageRequest); |
||||
|
} |
||||
|
} else { |
||||
|
return ingredientDTORepository.findByVenderAndTypeAndMarkAndKey(vender, type, mark, key, pageRequest); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public Ingredient queryIngredientByKey(String key) { |
||||
|
return ingredientRepository.findByKey(key); |
||||
|
} |
||||
|
|
||||
|
public boolean existsIngredientByKey(String key) { |
||||
|
return ingredientRepository.existsByKey(key); |
||||
|
} |
||||
|
|
||||
|
@Transactional |
||||
|
public void addMark(MarkType mark, String ingredient, Long vender, String operator) { |
||||
|
if (ingredientMarkRepository.updateMarkAndOperateByVenderAndIngredient(mark, operator, vender, ingredient) <= 0) { |
||||
|
Instant dateTime = Instant.now(); |
||||
|
ingredientMarkRepository.save(IngredientMark.builder().mark(mark).ingredient(ingredient).vender(vender).created(dateTime).modify(dateTime).build()); |
||||
|
} |
||||
|
log.info("[IngredientService] addMark ingredient = " + ingredient + ", mark = " + mark + ", operator = " + operator); |
||||
|
} |
||||
|
|
||||
|
public void delMark(String ingredient, Long vender, String operator) { |
||||
|
ingredientMarkRepository.deleteByVenderAndIngredient(vender, ingredient); |
||||
|
log.info("[IngredientService] delMark ingredient = " + ingredient + ", operator = " + operator); |
||||
|
} |
||||
|
|
||||
|
public void delMark(Long vender, String operator) { |
||||
|
ingredientMarkRepository.deleteByVender(vender); |
||||
|
log.info("[IngredientService] delMark vender = " + vender + ", operator = " + operator); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,329 @@ |
|||||
|
package com.mathvision.diet.service; |
||||
|
|
||||
|
import com.alibaba.excel.EasyExcel; |
||||
|
import com.alibaba.excel.support.ExcelTypeEnum; |
||||
|
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy; |
||||
|
import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy; |
||||
|
import com.alibaba.fastjson2.JSONArray; |
||||
|
import com.alibaba.fastjson2.JSONObject; |
||||
|
import com.google.common.collect.Lists; |
||||
|
import com.google.common.collect.Maps; |
||||
|
import com.google.common.collect.Range; |
||||
|
import com.mathvision.diet.domian.MealType; |
||||
|
import com.mathvision.diet.domian.MenuDishItemDTO; |
||||
|
import com.mathvision.diet.entity.*; |
||||
|
import com.mathvision.diet.repository.MenuDishRepository; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.collections4.CollectionUtils; |
||||
|
import org.apache.commons.lang3.tuple.Pair; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import java.io.OutputStream; |
||||
|
import java.math.BigDecimal; |
||||
|
import java.math.RoundingMode; |
||||
|
import java.util.*; |
||||
|
import java.util.stream.Collectors; |
||||
|
import java.util.stream.IntStream; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Service |
||||
|
public class MenuDishService { |
||||
|
|
||||
|
@Resource |
||||
|
EnumService enumService; |
||||
|
|
||||
|
@Resource |
||||
|
NutritionService nutritionService; |
||||
|
|
||||
|
@Resource |
||||
|
MenuDishRepository menuDishRepository; |
||||
|
|
||||
|
@Resource |
||||
|
IngredientService ingredientService; |
||||
|
|
||||
|
public MenuDish add(MenuDish menuDish) { |
||||
|
return menuDishRepository.save(menuDish); |
||||
|
} |
||||
|
|
||||
|
@Transactional |
||||
|
public List<MenuDish> addAll(List<MenuDish> menuDishes) { |
||||
|
menuDishes.forEach(menuDish -> deleteAll(menuDish.getMenu())); |
||||
|
return menuDishRepository.saveAll(menuDishes); |
||||
|
} |
||||
|
|
||||
|
public void deleteAll(Long menuId) { |
||||
|
menuDishRepository.deleteByMenu(menuId); |
||||
|
} |
||||
|
|
||||
|
public void delete(Long menuDishId) { |
||||
|
menuDishRepository.deleteById(menuDishId); |
||||
|
} |
||||
|
|
||||
|
public MenuDish get(Long id) { |
||||
|
return menuDishRepository.findById(id).orElse(null); |
||||
|
} |
||||
|
|
||||
|
public MenuDish get(Long id, Long venderId) { |
||||
|
return menuDishRepository.findByIdAndVender(id, venderId); |
||||
|
} |
||||
|
|
||||
|
public List<MenuDish> query(Long menuId) { |
||||
|
return menuDishRepository.findByMenu(menuId); |
||||
|
} |
||||
|
|
||||
|
public List<MenuDish> query(Long menuId, Long vender) { |
||||
|
return menuDishRepository.findByMenuAndVender(menuId, vender); |
||||
|
} |
||||
|
|
||||
|
public List<MenuDish> query(Long menuId, Long vender, Long day) { |
||||
|
return menuDishRepository.findByMenuAndVenderAndDay(menuId, vender, day); |
||||
|
} |
||||
|
|
||||
|
public JSONObject assess(Menu menu, long day, String crow, List<MenuDish> dishes) { |
||||
|
Nutrition nutrition = nutritionService.get(menu.getNutrient()); |
||||
|
BigDecimal overflow = nutrition.getOverflow(); |
||||
|
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, BigDecimal> items = dishes.stream().flatMap(dish -> dish.getIngredient().stream().filter(item -> item.getValue().containsKey(crow) && item.getValue().get(crow).doubleValue() > 0 && ingredientMap.containsKey(item.getKey()))).flatMap(x -> ingredientMap.get(x.getKey()).getNutrient().entrySet().stream().map(n -> Pair.of(n.getKey(), n.getValue().multiply(x.getValue().get(crow)).divide(new BigDecimal(100), RoundingMode.HALF_UP)))).collect(Collectors.toMap(Pair::getKey, Pair::getValue, BigDecimal::add)); |
||||
|
Map<String, Integer> types = dishes.stream().flatMap(dish -> dish.getIngredient().stream().filter(item -> ingredientMap.containsKey(item.getKey())).map(item -> ingredientMap.get(item.getKey()).getType())).collect(Collectors.toMap(x -> x, x -> 1, Integer::sum)); |
||||
|
|
||||
|
JSONObject result = new JSONObject(); |
||||
|
result.put("day", day); |
||||
|
result.put("crow", crow); |
||||
|
result.put("meals", dishes.stream().map(MenuDish::getMeal).collect(Collectors.toSet())); |
||||
|
result.put("types", types); |
||||
|
|
||||
|
JSONArray contents = new JSONArray(); |
||||
|
items.forEach((key, value) -> { |
||||
|
FoodNutrient foodNutrient = enumService.getNutrient(key); |
||||
|
|
||||
|
if (foodNutrient == null) { |
||||
|
return; |
||||
|
} |
||||
|
JSONObject content = new JSONObject(); |
||||
|
content.put("nutrition", String.format("%s/%s", foodNutrient.getValue(), foodNutrient.getMeasurement())); |
||||
|
content.put("virtual", value); |
||||
|
|
||||
|
Map<String, BigDecimal> standard = itemStandard.get(key); |
||||
|
BigDecimal max = standard == null ? null : standard.get("max"); |
||||
|
BigDecimal min = standard == null ? null : standard.get("min"); |
||||
|
BigDecimal ul = standard == null ? null : standard.get("ul"); |
||||
|
if (max == null) { |
||||
|
if (min == null) { |
||||
|
content.put("standard", "-"); |
||||
|
content.put("ul", ul == null ? "-" : ul.toString()); |
||||
|
content.put("overload", "-"); |
||||
|
content.put("conclusion", "-"); |
||||
|
} else { |
||||
|
content.put("standard", "≥" + min); |
||||
|
content.put("ul", ul == null ? "-" : ul.toString()); |
||||
|
if (min.compareTo(value) > 0) { |
||||
|
content.put("overload", value.subtract(min).divide(min, RoundingMode.FLOOR)); |
||||
|
if(min.subtract(value).compareTo(overflow.divide(new BigDecimal(100), RoundingMode.FLOOR).multiply(value)) > 0) { |
||||
|
content.put("conclusion", "不足"); |
||||
|
} else { |
||||
|
content.put("conclusion", "适量"); |
||||
|
} |
||||
|
} else { |
||||
|
content.put("overload", BigDecimal.ZERO); |
||||
|
content.put("conclusion", "适量"); |
||||
|
} |
||||
|
} |
||||
|
} else { |
||||
|
if (min == null) { |
||||
|
content.put("standard", "≤" + max); |
||||
|
content.put("ul", ul == null ? "-" : ul.toString()); |
||||
|
if (max.compareTo(value) < 0) { |
||||
|
if(value.subtract(max).compareTo(overflow.divide(new BigDecimal(100), RoundingMode.FLOOR).multiply(value)) > 0) { |
||||
|
if(ul != null && value.compareTo(ul) > 0) { |
||||
|
content.put("conclusion", "严重超标"); |
||||
|
} else { |
||||
|
content.put("conclusion", "超量"); |
||||
|
} |
||||
|
} else { |
||||
|
content.put("conclusion", "适量"); |
||||
|
} |
||||
|
content.put("overload", value.subtract(max).divide(max, RoundingMode.FLOOR)); |
||||
|
} else { |
||||
|
content.put("overload", BigDecimal.ZERO); |
||||
|
} |
||||
|
} else { |
||||
|
content.put("standard", min + "~" + max); |
||||
|
content.put("ul", ul == null ? "-" : ul.toString()); |
||||
|
if (min.compareTo(value) > 0) { |
||||
|
content.put("overload", value.subtract(min).divide(min, RoundingMode.FLOOR)); |
||||
|
if(min.subtract(value).compareTo(overflow.divide(new BigDecimal(100), RoundingMode.FLOOR).multiply(value)) > 0) { |
||||
|
content.put("conclusion", "不足"); |
||||
|
} else { |
||||
|
content.put("conclusion", "适量"); |
||||
|
} |
||||
|
} else { |
||||
|
if (max.compareTo(value) < 0) { |
||||
|
content.put("overload", value.subtract(max).divide(max, RoundingMode.FLOOR)); |
||||
|
if(value.subtract(max).compareTo(overflow.divide(new BigDecimal(100), RoundingMode.FLOOR).multiply(value)) > 0) { |
||||
|
if(ul != null && value.compareTo(ul) > 0) { |
||||
|
content.put("conclusion", "严重超标"); |
||||
|
} else { |
||||
|
content.put("conclusion", "超量"); |
||||
|
} |
||||
|
} else { |
||||
|
content.put("conclusion", "适量"); |
||||
|
} |
||||
|
} else { |
||||
|
content.put("overload", "0%"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
contents.add(content); |
||||
|
}); |
||||
|
result.put("ingredient", contents); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
public JSONObject types(Menu menu, List<MenuDish> dishes) { |
||||
|
Nutrition nutrition = nutritionService.get(menu.getNutrient()); |
||||
|
Map<String, BigDecimal> dayStandard = nutrition.getFoodCategoryDay(); |
||||
|
Map<String, BigDecimal> weekStandard = nutrition.getFoodCategoryWeek(); |
||||
|
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<Pair<Long, String>, Integer> typesDay = dishes.stream().flatMap(dish -> dish.getIngredient().stream().filter(item -> ingredientMap.containsKey(item.getKey())).map(item -> Pair.of(dish.getDay(), ingredientMap.get(item.getKey()).getType()))).collect(Collectors.toMap(x -> x, x -> 1, Integer::sum)); |
||||
|
JSONArray dayRule = new JSONArray(); |
||||
|
Map<Long, JSONArray> dayMaps = Maps.newHashMap(); |
||||
|
typesDay.forEach((key, num) -> { |
||||
|
Long day = key.getKey(); |
||||
|
JSONArray dayCollection = dayMaps.getOrDefault(day, new JSONArray()); |
||||
|
String type = key.getValue(); |
||||
|
int standard = dayStandard.getOrDefault(type, BigDecimal.ZERO).intValue(); |
||||
|
JSONObject content = new JSONObject(); |
||||
|
content.put("day", day); |
||||
|
content.put("name", type); |
||||
|
content.put("standard", standard); |
||||
|
content.put("supplied", num); |
||||
|
content.put("lack", num >= standard ? 0 : standard - num); |
||||
|
dayCollection.add(content); |
||||
|
dayMaps.put(day, dayCollection); |
||||
|
}); |
||||
|
dayRule.addAll(dayMaps.values()); |
||||
|
|
||||
|
Map<String, Integer> typesAll = dishes.stream().flatMap(dish -> dish.getIngredient().stream().filter(item -> ingredientMap.containsKey(item.getKey())).map(item -> ingredientMap.get(item.getKey()).getType())).collect(Collectors.toMap(x -> x, x -> 1, Integer::sum)); |
||||
|
JSONArray weekRule = new JSONArray(); |
||||
|
typesAll.forEach((type, num) -> { |
||||
|
BigDecimal standard = weekStandard.getOrDefault(type, BigDecimal.ZERO); |
||||
|
JSONObject content = new JSONObject(); |
||||
|
content.put("name", type); |
||||
|
content.put("standard", standard); |
||||
|
content.put("supplied", num); |
||||
|
content.put("lack", num.compareTo(standard.intValue()) > 0 ? 0 : standard.intValue() - num); |
||||
|
weekRule.add(content); |
||||
|
}); |
||||
|
|
||||
|
JSONObject result = new JSONObject(); |
||||
|
result.put("dayRule", dayRule); |
||||
|
result.put("weekRule", weekRule); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
public JSONObject energy(long day, String crow, List<MenuDish> dishes) { |
||||
|
List<String> allConcerned= Lists.newArrayList("energy", "protein", "fat", "carbs"); |
||||
|
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() |
||||
|
.flatMap(dish -> dish.getIngredient().stream().filter(item -> item.getValue().containsKey(crow) && item.getValue().get(crow).doubleValue() > 0 && ingredientMap.containsKey(item.getKey()))) |
||||
|
.flatMap(x -> ingredientMap.get(x.getKey()).getNutrient().entrySet().stream().map(n -> Pair.of(n.getKey(), n.getValue().multiply(x.getValue().get(crow)).divide(new BigDecimal(100), RoundingMode.HALF_UP)))) |
||||
|
.filter(x -> allConcerned.contains(x.getKey())) |
||||
|
.collect(Collectors.toMap(Pair::getKey, Pair::getValue, BigDecimal::add)); |
||||
|
|
||||
|
JSONObject result = new JSONObject(); |
||||
|
result.put("day", day); |
||||
|
result.put("crow", crow); |
||||
|
result.put("meals", dishes.stream().map(MenuDish::getMeal).collect(Collectors.toSet())); |
||||
|
|
||||
|
BigDecimal energy = items.get("energy"); |
||||
|
if (energy == null || energy.doubleValue() <= 0) { |
||||
|
return result; |
||||
|
} |
||||
|
JSONArray contents = new JSONArray(); |
||||
|
contents.add(toEnergyContent(energy, "protein", Range.closed(10, 20), items, ingredientMap)); |
||||
|
contents.add(toEnergyContent(energy, "fat", Range.closed(20, 30), items, ingredientMap)); |
||||
|
contents.add(toEnergyContent(energy, "carbs", Range.closed(50, 60), items, ingredientMap)); |
||||
|
result.put("energy", contents); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
private JSONObject toEnergyContent(BigDecimal energy, String key, Range<Integer> standard, Map<String, BigDecimal> items, Map<String, Ingredient> ingredientMap) { |
||||
|
double value = items.getOrDefault(key, BigDecimal.ZERO).divide(energy, RoundingMode.FLOOR).multiply(new BigDecimal(100)).doubleValue(); |
||||
|
JSONObject result = JSONObject.of("name", enumService.getNutrient(key).getValue() + "/总能量"); |
||||
|
result.put("standard", String.format("%s~%s", standard.lowerEndpoint(), standard.upperEndpoint())); |
||||
|
result.put("value", value); |
||||
|
result.put("conclusion", standard.lowerEndpoint() > value ? "略低" : standard.upperEndpoint() < value ? "略高" : "合适"); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
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))); |
||||
|
List<String> allMeals = new ArrayList<>(menuDishes.keySet()).stream().sorted(Comparator.comparing(MealType::toType)).collect(Collectors.toList()); |
||||
|
List<Long> allDays = menuDishes.entrySet().stream().flatMap(kv -> kv.getValue().keySet().stream()).distinct().sorted().collect(Collectors.toList()); |
||||
|
List<String> allCrows = menu.getCrows(); |
||||
|
if (CollectionUtils.isEmpty(allDays)) { |
||||
|
allDays = IntStream.rangeClosed(1, Math.toIntExact(menu.getDay())).mapToObj(Long::valueOf).collect(Collectors.toList()); |
||||
|
} |
||||
|
|
||||
|
List<List<String>> headers = Lists.newArrayList(); |
||||
|
headers.add(Lists.newArrayList("")); |
||||
|
allDays.forEach(day -> { |
||||
|
headers.add(Lists.newArrayList(String.format("第%s天", day), "菜品名称")); |
||||
|
headers.add(Lists.newArrayList(String.format("第%s天", day), "食材名称")); |
||||
|
allCrows.forEach(crow -> headers.add(Lists.newArrayList(String.format("第%s天", day), crow))); |
||||
|
}); |
||||
|
|
||||
|
Map<String, String> keyName = ingredientService.getByKeys(); |
||||
|
List<List<Object>> contents = Lists.newArrayList(); |
||||
|
List<Long> finalAllDays = allDays; |
||||
|
allMeals.forEach(meal -> { |
||||
|
int maxDishes = menuDishes.get(meal).values().stream().map(List::size).max(Comparator.naturalOrder()).orElse(0); |
||||
|
if (maxDishes <= 0) return; |
||||
|
IntStream.rangeClosed(1, maxDishes).forEach(dishIndex -> { |
||||
|
List<MenuDish> dishes = finalAllDays.stream().map(day -> { |
||||
|
List<MenuDish> dish = menuDishes.get(meal).get(day); |
||||
|
if (dish == null || dish.size() < dishIndex) { |
||||
|
return MenuDish.builder().meal(meal).day(day).ingredient(Lists.newArrayList()).build(); |
||||
|
} else { |
||||
|
return dish.get(dishIndex -1); |
||||
|
} |
||||
|
}).collect(Collectors.toList()); |
||||
|
|
||||
|
int maxItems = dishes.stream().map(item -> item.getIngredient().size()).max(Comparator.naturalOrder()).orElse(0); |
||||
|
if (maxItems <= 0) return; |
||||
|
IntStream.rangeClosed(1, maxItems).forEach(itemIndex -> { |
||||
|
List<Pair<String, MenuDishItemDTO>> items = dishes.stream().map(dish -> { |
||||
|
List<MenuDishItemDTO> item = dish.getIngredient(); |
||||
|
if (item == null || item.size() < itemIndex) { |
||||
|
return Pair.of(dish.getName(), MenuDishItemDTO.builder().value(Maps.newHashMap()).build()); |
||||
|
} else { |
||||
|
return Pair.of(dish.getName(), item.get(itemIndex - 1)); |
||||
|
} |
||||
|
}).collect(Collectors.toList()); |
||||
|
|
||||
|
List<Object> content = Lists.newArrayList(); |
||||
|
content.add(meal); |
||||
|
items.forEach(item -> { |
||||
|
content.add(item.getKey()); |
||||
|
content.add(keyName.getOrDefault(item.getValue().getKey(), item.getValue().getKey())); |
||||
|
allCrows.forEach(crow -> { |
||||
|
BigDecimal value = item.getValue().getValue().get(crow); |
||||
|
if (value != null) { |
||||
|
content.add(value); |
||||
|
} |
||||
|
}); |
||||
|
}); |
||||
|
contents.add(content); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
EasyExcel.write(outputStream).head(headers) |
||||
|
.registerWriteHandler(new SimpleColumnWidthStyleStrategy(25)) |
||||
|
.registerWriteHandler(new SimpleRowHeightStyleStrategy((short)30,(short)20)) |
||||
|
.excelType(ExcelTypeEnum.XLSX).sheet(menu.getName()).doWrite(contents); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,78 @@ |
|||||
|
package com.mathvision.diet.service; |
||||
|
|
||||
|
import com.mathvision.diet.domian.MenuStatus; |
||||
|
import com.mathvision.diet.entity.Menu; |
||||
|
import com.mathvision.diet.repository.MenuRepository; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.data.domain.Page; |
||||
|
import org.springframework.data.domain.PageRequest; |
||||
|
import org.springframework.data.jpa.domain.Specification; |
||||
|
import org.springframework.scheduling.annotation.Scheduled; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
import javax.annotation.PostConstruct; |
||||
|
import javax.annotation.Resource; |
||||
|
import javax.persistence.criteria.Predicate; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.Date; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Service |
||||
|
public class MenuReleaseService { |
||||
|
@Resource |
||||
|
private MenuRepository menuRepository; |
||||
|
|
||||
|
|
||||
|
@PostConstruct |
||||
|
@Scheduled(cron = "0 0 0/6 * * *") |
||||
|
public void init() { |
||||
|
menuRepository.scanExpired(); |
||||
|
} |
||||
|
|
||||
|
public void publish(Long id, Map<String, Integer> scale, Date startTime, Date endTime, String operator) { |
||||
|
menuRepository.updateStatusAndScaleAndStartTimeAndEndTimeById(MenuStatus.publish, scale, startTime.toInstant(), endTime.toInstant(), id); |
||||
|
log.info("[MenuReleaseService] publish id = " + id + ", operator = " + operator); |
||||
|
} |
||||
|
|
||||
|
public void cancel(Long id, String operator) { |
||||
|
menuRepository.updateStatusAndStartTimeAndEndTimeAndOperateById(MenuStatus.pass, null, null, operator, id); |
||||
|
log.info("[MenuReleaseService] cancel id = " + id + ", operator = " + operator); |
||||
|
} |
||||
|
|
||||
|
public Page<Menu> list(Long vender, String name, Date startTime, Date endTime, PageRequest pageRequest) { |
||||
|
return menuRepository.findAll(toSpecification(vender, name, startTime, endTime), pageRequest); |
||||
|
} |
||||
|
|
||||
|
private Specification<Menu> toSpecification(Long vender, String name, Date startTime, Date endTime) { |
||||
|
return (root, query, builder) -> { |
||||
|
List<Predicate> predicates = new ArrayList<>(); |
||||
|
|
||||
|
if (vender != null && vender > 0) { |
||||
|
predicates.add(builder.equal(root.get("vender"), vender)); |
||||
|
} |
||||
|
|
||||
|
predicates.add(builder.equal(root.get("status"), MenuStatus.publish)); |
||||
|
|
||||
|
if (startTime != null) { |
||||
|
predicates.add(builder.greaterThanOrEqualTo(root.get("modify"), startTime.toInstant())); |
||||
|
} |
||||
|
|
||||
|
if (endTime != null) { |
||||
|
predicates.add(builder.lessThanOrEqualTo(root.get("modify"), endTime.toInstant())); |
||||
|
} |
||||
|
|
||||
|
if (StringUtils.isNotBlank(name)) { |
||||
|
predicates.add(builder.like(root.get("name"), name + "%")); |
||||
|
} |
||||
|
|
||||
|
if (predicates.size() > 1) { |
||||
|
return builder.and(predicates.toArray(new Predicate[0])); |
||||
|
} else { |
||||
|
return predicates.get(0); |
||||
|
} |
||||
|
}; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,105 @@ |
|||||
|
package com.mathvision.diet.service; |
||||
|
|
||||
|
import com.google.common.collect.Lists; |
||||
|
import com.mathvision.diet.domian.MenuStatus; |
||||
|
import com.mathvision.diet.entity.Menu; |
||||
|
import com.mathvision.diet.entity.MenuApprove; |
||||
|
import com.mathvision.diet.repository.MenuApproveRepository; |
||||
|
import com.mathvision.diet.repository.MenuRepository; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.data.domain.Page; |
||||
|
import org.springframework.data.domain.PageRequest; |
||||
|
import org.springframework.data.jpa.domain.Specification; |
||||
|
import org.springframework.scheduling.annotation.Scheduled; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
import javax.annotation.PostConstruct; |
||||
|
import javax.annotation.Resource; |
||||
|
import javax.persistence.criteria.CriteriaBuilder; |
||||
|
import javax.persistence.criteria.Predicate; |
||||
|
import java.time.Instant; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.Date; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Service |
||||
|
public class MenuReviewService { |
||||
|
private static final List<MenuStatus> SUPPORTED_STATUS = Lists.newArrayList(MenuStatus.submit, MenuStatus.pass, MenuStatus.reject); |
||||
|
@Resource |
||||
|
private MenuRepository menuRepository; |
||||
|
|
||||
|
@Resource |
||||
|
private MenuApproveRepository menuApproveRepository; |
||||
|
|
||||
|
@PostConstruct |
||||
|
@Scheduled(cron = "0 0 0/6 * * *") |
||||
|
public void init() { |
||||
|
menuApproveRepository.scanExpired(); |
||||
|
} |
||||
|
|
||||
|
public void submit(Long id, String operator) { |
||||
|
menuRepository.updateStatusAndOperateById(MenuStatus.submit, operator, id); |
||||
|
} |
||||
|
|
||||
|
public void pass(Long id, String reason, String operator) { |
||||
|
Instant datetime = Instant.now(); |
||||
|
menuRepository.updateStatusAndApproveAndOperateById(MenuStatus.pass, reason, operator, id); |
||||
|
menuApproveRepository.save(MenuApprove.builder().menu(id).pass(true).approve(reason).operate(operator).created(datetime).modify(datetime).build()); |
||||
|
} |
||||
|
|
||||
|
public void reject(Long id, String reason, String operator) { |
||||
|
Instant datetime = Instant.now(); |
||||
|
menuRepository.updateStatusAndApproveAndOperateById(MenuStatus.reject, reason, operator, id); |
||||
|
menuApproveRepository.save(MenuApprove.builder().menu(id).pass(false).approve(reason).operate(operator).created(datetime).modify(datetime).build()); |
||||
|
} |
||||
|
|
||||
|
public void disable(Long id, String operator) { |
||||
|
menuRepository.updateStatusAndOperateById(MenuStatus.disabled, operator, id); |
||||
|
} |
||||
|
|
||||
|
public Object count() { |
||||
|
return menuRepository.count(Lists.newArrayList(MenuStatus.submit, MenuStatus.pass, MenuStatus.reject)); |
||||
|
} |
||||
|
|
||||
|
public Page<Menu> list(Long vender, MenuStatus status, String name, Date startTime, Date endTime, PageRequest pageRequest) { |
||||
|
return menuRepository.findAll(toSpecification(vender, status, name, startTime, endTime), pageRequest); |
||||
|
} |
||||
|
|
||||
|
private Specification<Menu> toSpecification(Long vender, MenuStatus status, String name, Date startTime, Date endTime) { |
||||
|
return (root, query, builder) -> { |
||||
|
List<Predicate> predicates = new ArrayList<>(); |
||||
|
|
||||
|
if (vender != null && vender > 0) { |
||||
|
predicates.add(builder.equal(root.get("vender"), vender)); |
||||
|
} |
||||
|
|
||||
|
if( status != null && SUPPORTED_STATUS.contains(status)) { |
||||
|
predicates.add(builder.equal(root.get("status"), status)); |
||||
|
} else { |
||||
|
CriteriaBuilder.In<Object> in = builder.in(root.get("status")); |
||||
|
Lists.newArrayList(MenuStatus.submit, MenuStatus.pass, MenuStatus.reject).forEach(in::value); |
||||
|
predicates.add(in); |
||||
|
} |
||||
|
|
||||
|
if (startTime != null) { |
||||
|
predicates.add(builder.greaterThanOrEqualTo(root.get("modify"), startTime.toInstant())); |
||||
|
} |
||||
|
|
||||
|
if (endTime != null) { |
||||
|
predicates.add(builder.lessThanOrEqualTo(root.get("modify"), endTime.toInstant())); |
||||
|
} |
||||
|
|
||||
|
if (StringUtils.isNotBlank(name)) { |
||||
|
predicates.add(builder.like(root.get("name"), name + "%")); |
||||
|
} |
||||
|
|
||||
|
if (predicates.size() > 1) { |
||||
|
return builder.and(predicates.toArray(new Predicate[0])); |
||||
|
} else { |
||||
|
return predicates.get(0); |
||||
|
} |
||||
|
}; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,125 @@ |
|||||
|
package com.mathvision.diet.service; |
||||
|
|
||||
|
import com.mathvision.diet.domian.MenuDishItemDTO; |
||||
|
import com.mathvision.diet.domian.MenuStatus; |
||||
|
import com.mathvision.diet.entity.Menu; |
||||
|
import com.mathvision.diet.repository.MenuDishRepository; |
||||
|
import com.mathvision.diet.repository.MenuRepository; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.collections4.CollectionUtils; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.data.domain.Page; |
||||
|
import org.springframework.data.domain.PageRequest; |
||||
|
import org.springframework.data.jpa.domain.Specification; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import javax.persistence.criteria.Predicate; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.Date; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Service |
||||
|
public class MenuService { |
||||
|
@Resource |
||||
|
private MenuRepository menuRepository; |
||||
|
|
||||
|
@Resource |
||||
|
private MenuDishRepository menuDishRepository; |
||||
|
|
||||
|
public List<Menu> add(List<Menu> menuList) { |
||||
|
return menuRepository.saveAll(menuList); |
||||
|
} |
||||
|
|
||||
|
@Transactional |
||||
|
public void delete(Long id, Long vender, String operator) { |
||||
|
if (vender == null || vender <= 0) { |
||||
|
menuRepository.deleteById(id); |
||||
|
menuDishRepository.deleteByMenu(id); |
||||
|
} else { |
||||
|
menuRepository.deleteByIdAndVender(id, vender); |
||||
|
menuDishRepository.deleteByMenuAndVender(id, vender); |
||||
|
} |
||||
|
log.info("[MenuService] delete id = " + id + ", vender = " + vender + ", operator = " + operator); |
||||
|
} |
||||
|
|
||||
|
@Transactional |
||||
|
public void delete(Long vender, String operator) { |
||||
|
menuRepository.deleteByVender(vender); |
||||
|
menuDishRepository.deleteByVender(vender); |
||||
|
log.info("[MenuService] delete vender = " + vender + ", operator = " + operator); |
||||
|
} |
||||
|
|
||||
|
public void update(Menu menu) { |
||||
|
menuDishRepository.deleteByMenuAndDayGreaterThanAndMealNotIn(menu.getId(), menu.getDay(), menu.getMeals()); |
||||
|
menuDishRepository.findByMenu(menu.getId()).forEach(x -> { |
||||
|
List<MenuDishItemDTO> ingredient = x.getIngredient(); |
||||
|
if (CollectionUtils.isNotEmpty(ingredient)) { |
||||
|
ingredient.forEach(i -> i.setValue(i.getValue().entrySet().stream().filter(crow -> menu.getCrows().contains(crow.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)))); |
||||
|
x.setOperate(menu.getOperate()); |
||||
|
x.setModify(menu.getModify()); |
||||
|
menuDishRepository.save(x); |
||||
|
} |
||||
|
}); |
||||
|
menuRepository.save(menu); |
||||
|
log.info("[MenuService] update id = " + menu.getId() + ", vender = " + menu.getVender() + ", operator = " + menu.getOperate()); |
||||
|
} |
||||
|
|
||||
|
public Menu get(Long id) { |
||||
|
return menuRepository.findById(id).orElse(null); |
||||
|
} |
||||
|
|
||||
|
public Menu get(Long id, Long vender) { |
||||
|
return menuRepository.findByIdAndVender(id, vender); |
||||
|
} |
||||
|
|
||||
|
public List<Menu> getByNutrition(Long nutrition) { |
||||
|
return menuRepository.findByNutrient(nutrition); |
||||
|
} |
||||
|
|
||||
|
public Long current(Long vender) { |
||||
|
return menuRepository.findCurrentMenu(vender, MenuStatus.publish.getCode()); |
||||
|
} |
||||
|
|
||||
|
public Page<Menu> list(Long vender, String name, MenuStatus status, Date startTime, Date endTime, PageRequest pageRequest) { |
||||
|
return menuRepository.findAll(toSpecification(vender, name, status, startTime, endTime), pageRequest); |
||||
|
} |
||||
|
|
||||
|
private Specification<Menu> toSpecification(Long vender, String name, MenuStatus status, Date startTime, Date endTime) { |
||||
|
return (root, query, builder) -> { |
||||
|
List<Predicate> predicates = new ArrayList<>(); |
||||
|
|
||||
|
if (vender != null && vender > 0) { |
||||
|
predicates.add(builder.equal(root.get("vender"), vender)); |
||||
|
} |
||||
|
|
||||
|
if (status == null || MenuStatus.publish.equals(status)) { |
||||
|
predicates.add(builder.notEqual(root.get("status"), MenuStatus.publish.getCode())); |
||||
|
} else { |
||||
|
predicates.add(builder.equal(root.get("status"), status)); |
||||
|
} |
||||
|
|
||||
|
if (startTime != null) { |
||||
|
predicates.add(builder.greaterThanOrEqualTo(root.get("modify"), startTime.toInstant())); |
||||
|
} |
||||
|
|
||||
|
if (endTime != null) { |
||||
|
predicates.add(builder.lessThanOrEqualTo(root.get("modify"), endTime.toInstant())); |
||||
|
} |
||||
|
|
||||
|
if (StringUtils.isNotBlank(name)) { |
||||
|
predicates.add(builder.like(root.get("name"), name + "%")); |
||||
|
} |
||||
|
|
||||
|
if (predicates.size() > 1) { |
||||
|
return builder.and(predicates.toArray(new Predicate[0])); |
||||
|
} else { |
||||
|
return predicates.get(0); |
||||
|
} |
||||
|
}; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,92 @@ |
|||||
|
package com.mathvision.diet.service; |
||||
|
|
||||
|
import com.mathvision.diet.entity.Nutrition; |
||||
|
import com.mathvision.diet.repository.NutritionRepository; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.collections4.CollectionUtils; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.data.domain.Page; |
||||
|
import org.springframework.data.domain.PageRequest; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.util.Assert; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import java.time.Instant; |
||||
|
import java.util.List; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Service |
||||
|
public class NutritionService { |
||||
|
|
||||
|
@Resource |
||||
|
private EnumService enumService; |
||||
|
|
||||
|
@Resource |
||||
|
private VenderService venderService; |
||||
|
|
||||
|
@Resource |
||||
|
private MenuService menuService; |
||||
|
|
||||
|
@Resource |
||||
|
private NutritionRepository nutritionRepository; |
||||
|
|
||||
|
public Nutrition add(Nutrition nutrition, String operator) { |
||||
|
check(nutrition, operator); |
||||
|
log.info("[NutritionService] add name = " + nutrition.getName() + ", operator = " + operator); |
||||
|
return nutritionRepository.save(nutrition); |
||||
|
} |
||||
|
|
||||
|
public void delete(long id, String operator) { |
||||
|
Assert.isTrue(CollectionUtils.isEmpty(menuService.getByNutrition(id)), "[参数错误]该营养计划使用中不可删除!"); |
||||
|
nutritionRepository.deleteById(id); |
||||
|
log.info("[NutritionService] delete id = " + id + ", operator = " + operator); |
||||
|
} |
||||
|
|
||||
|
public Nutrition update(Nutrition nutrition, String operator) { |
||||
|
check(nutrition, operator); |
||||
|
Assert.isTrue(CollectionUtils.isEmpty(menuService.getByNutrition(nutrition.getId())), "[参数错误]该营养计划使用中不可删除!"); |
||||
|
log.info("[NutritionService] update name = " + nutrition.getName() + ", operator = " + operator); |
||||
|
return nutritionRepository.save(nutrition); |
||||
|
} |
||||
|
|
||||
|
public boolean exists(Long id) { |
||||
|
return nutritionRepository.existsById(id); |
||||
|
} |
||||
|
|
||||
|
public boolean notExists(String name) { |
||||
|
return !nutritionRepository.existsByName(name); |
||||
|
} |
||||
|
|
||||
|
public Nutrition get(Long id) { |
||||
|
return nutritionRepository.findById(id).orElse(null); |
||||
|
} |
||||
|
|
||||
|
public Nutrition get(String name) { |
||||
|
return nutritionRepository.findByName(name); |
||||
|
} |
||||
|
|
||||
|
public List<Nutrition> query(Long vender, String name) { |
||||
|
return nutritionRepository.findAll().stream().filter(x -> (StringUtils.isBlank(name) || x.getName().contains(name)) && (vender == null || vender ==0 || x.getVendors().contains(vender))).collect(Collectors.toList()); |
||||
|
} |
||||
|
|
||||
|
public Page<Nutrition> list(String name, PageRequest pageRequest) { |
||||
|
return StringUtils.isBlank(name) ? nutritionRepository.findAll(pageRequest) : nutritionRepository.findByNameStartsWithOrderByIdDesc(name, pageRequest); |
||||
|
} |
||||
|
|
||||
|
private void check(Nutrition nutrition, String operator) { |
||||
|
nutrition.setVendors(nutrition.getVendors().stream().filter(x -> venderService.exists(x)).collect(Collectors.toList())); |
||||
|
Assert.isTrue(StringUtils.isNotBlank(nutrition.getName()) && |
||||
|
CollectionUtils.isNotEmpty(nutrition.getVendors()) && |
||||
|
nutrition.getOverflow() != null && |
||||
|
(nutrition.getFoodCategoryDay() == null || nutrition.getFoodCategoryDay().keySet().stream().allMatch(x -> enumService.checkCategory(x))) && |
||||
|
(nutrition.getFoodCategoryWeek() == null || nutrition.getFoodCategoryWeek().keySet().stream().allMatch(x -> enumService.checkCategory(x))) && |
||||
|
(nutrition.getIngredient() == null || nutrition.getIngredient().entrySet().stream().allMatch(x -> x.getValue().keySet().stream().allMatch(n -> enumService.checkNutrient(n)))) |
||||
|
, "[参数错误]必填参数未填写,或者单位类别为空、类型和营养素不在取值范围内!"); |
||||
|
|
||||
|
Instant dateTime = Instant.now(); |
||||
|
nutrition.setOperate(operator); |
||||
|
nutrition.setCreated(dateTime); |
||||
|
nutrition.setModify(dateTime); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,275 @@ |
|||||
|
package com.mathvision.diet.service; |
||||
|
|
||||
|
import com.alibaba.fastjson2.JSON; |
||||
|
import com.google.common.collect.Lists; |
||||
|
import com.mathvision.diet.domain.UserDO; |
||||
|
import com.mathvision.diet.domian.AuthType; |
||||
|
import com.mathvision.diet.domian.ClientType; |
||||
|
import com.mathvision.diet.domian.RoleType; |
||||
|
import com.mathvision.diet.entity.*; |
||||
|
import com.mathvision.diet.repository.*; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.scheduling.annotation.Scheduled; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
import org.springframework.util.Assert; |
||||
|
|
||||
|
import javax.annotation.PostConstruct; |
||||
|
import javax.annotation.Resource; |
||||
|
import java.time.Instant; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
import java.util.Objects; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Service |
||||
|
public class UserService { |
||||
|
|
||||
|
@Resource |
||||
|
private EnumService enumService; |
||||
|
|
||||
|
@Resource |
||||
|
private UserRepository userRepository; |
||||
|
|
||||
|
@Resource |
||||
|
private UserLogRepository userLogRepository; |
||||
|
|
||||
|
@Resource |
||||
|
private UserSessionRepository userSessionRepository; |
||||
|
|
||||
|
@Resource |
||||
|
private UserMessageRepository userMessageRepository; |
||||
|
|
||||
|
@Resource |
||||
|
private UserRoleRepository userRoleRepository; |
||||
|
|
||||
|
@Resource |
||||
|
private RoleRepository roleRepository; |
||||
|
|
||||
|
@Resource |
||||
|
private VenderRepository venderRepository; |
||||
|
|
||||
|
@PostConstruct |
||||
|
@Scheduled(cron = "0 0 0/6 * * *") |
||||
|
public void init() { |
||||
|
userLogRepository.scanExpired(); |
||||
|
userMessageRepository.scanExpired(); |
||||
|
userSessionRepository.scanExpired(); |
||||
|
} |
||||
|
|
||||
|
public UserDO login(String uid, String password, ClientType clientType, String clientVersion) { |
||||
|
Assert.isTrue(checkUser(uid, password), "[参数错误]用户名密码错误!"); |
||||
|
userLogRepository.save(UserLog.builder().uid(uid).clientType(clientType).clientVersion(clientVersion).login(Instant.now()).build()); |
||||
|
log.info("[UserService] login uid = " + uid + ", clientType = " + clientType + ", clientVersion = " + clientVersion); |
||||
|
return queryUser(uid); |
||||
|
} |
||||
|
|
||||
|
@Transactional |
||||
|
public void addUser(String uid, String name, String password, Long roleId, Long venderId, String operator) { |
||||
|
Assert.isTrue(checkUser(uid), "[参数错误]用户名重复!"); |
||||
|
Assert.isTrue(StringUtils.isNotBlank(name), "[参数错误]姓名必填!"); |
||||
|
Assert.isTrue(StringUtils.isNotBlank(password) && password.length() == 16, "[参数错误]密码为空, 或者密码长度错误!"); |
||||
|
Assert.isTrue(checkRole(roleId, venderId), "[参数错误]未赋初始角色,或者该角色错误!"); |
||||
|
|
||||
|
Instant dateTime = Instant.now(); |
||||
|
User user = User.builder().uid(uid).name(name).pwd(password).status(true).created(dateTime).modify(dateTime).build(); |
||||
|
UserRole userRole = UserRole.builder().roleId(roleId).uid(uid).vender(venderId).operate(operator).created(dateTime).modify(dateTime).build(); |
||||
|
|
||||
|
userRepository.save(user); |
||||
|
userRoleRepository.save(userRole); |
||||
|
log.info("[UserService] addUser venderId= " + venderId + ", uid = " + uid + ", roleId = " + roleId + ", operator = " + operator); |
||||
|
} |
||||
|
|
||||
|
@Transactional |
||||
|
public void delUser(Long venderId, String operator) { |
||||
|
List<String> users = listUser(venderId).stream().map(UserDO::getUid).collect(Collectors.toList()); |
||||
|
if (!users.isEmpty()) { |
||||
|
userRepository.deleteByUidIn(users); |
||||
|
} |
||||
|
userRoleRepository.deleteByVender(venderId); |
||||
|
roleRepository.deleteByVender(venderId); |
||||
|
log.info("[UserService] delUser venderId = " + venderId + ", operator = " + operator); |
||||
|
} |
||||
|
|
||||
|
@Transactional |
||||
|
public void delUser(String uid, Long venderId, String operator) { |
||||
|
Assert.isTrue(checkRole(uid, venderId), "[参数错误]该用户不是您单位用户!"); |
||||
|
Assert.isTrue(countRole(venderId, uid) > 0, "[参数错误]至少保留一个管理员!"); |
||||
|
if (userRoleRepository.deleteByUidAndVender(uid, venderId) > 0) { |
||||
|
userRepository.deleteByUid(uid); |
||||
|
} |
||||
|
log.info("[UserService] delUser uid = " + uid + "venderId = " + venderId + ", operator = " + operator); |
||||
|
} |
||||
|
|
||||
|
public void changeUser(String uid, String name, String password, Long roleId, Long venderId, String operator) { |
||||
|
Assert.isTrue(checkRole(uid, venderId), "[参数错误]该用户不是您单位用户!"); |
||||
|
if (roleId != null) { |
||||
|
if (roleId > 0) { |
||||
|
Assert.isTrue(checkRole(roleId, venderId), "[参数错误]该角色不属于您单位!"); |
||||
|
} else { |
||||
|
Assert.isTrue(countRole(venderId, uid) > 0, "[参数错误]至少保留一个管理员!"); |
||||
|
} |
||||
|
userRoleRepository.updateRoleIdAndOperateByUidAndVender(roleId, operator, uid, venderId); |
||||
|
log.info("[UserService] changeUser uid = " + uid + ", roleId = " + roleId + ", operator = " + operator); |
||||
|
} |
||||
|
if (StringUtils.isNotBlank(name)) { |
||||
|
userRepository.updateNameByUid(name, uid); |
||||
|
log.info("[UserService] changeUser uid = " + uid + ", name = " + name + ", operator = " + operator); |
||||
|
} |
||||
|
if (StringUtils.isNotBlank(password)) { |
||||
|
userRepository.updatePwdByUid(password, uid); |
||||
|
log.info("[UserService] changeUser uid = " + uid + ", password = " + password + ", operator = " + operator); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public UserDO queryUser(String uid) { |
||||
|
UserDO result = new UserDO(); |
||||
|
User user = userRepository.findByUidAndStatus(uid, true); |
||||
|
if (user == null) { |
||||
|
log.info("[UserService] queryUser user=null uid = " + uid); |
||||
|
return null; |
||||
|
} |
||||
|
result.setUid(user.getUid()); |
||||
|
result.setName(user.getName()); |
||||
|
result.setPhone(user.getPhone()); |
||||
|
result.setTime(user.getCreated()); |
||||
|
|
||||
|
UserRole userRole = userRoleRepository.findByUid(uid); |
||||
|
if (userRole == null) { |
||||
|
log.info("[UserService] queryUser userRole=null uid = " + uid); |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
if (userRole.getVender() != 0) { |
||||
|
Vender vender = venderRepository.findByIdAndStatus(userRole.getVender(), true); |
||||
|
if (userRole.getVender() != 0 && vender == null) { |
||||
|
log.info("[UserService] queryUser vender=null or closed vender uid=" + uid + ", venderId=" + userRole.getVender()); |
||||
|
return null; |
||||
|
} |
||||
|
result.setVender(vender); |
||||
|
} |
||||
|
|
||||
|
Role role = query(userRole.getRoleId()); |
||||
|
if (role != null) { |
||||
|
result.setRoleId(role.getId()); |
||||
|
result.setRoleName(role.getRoleName()); |
||||
|
result.setRoleType(role.getRoleType().getType()); |
||||
|
result.setRoleItems(enumService.getRoleItems(role.getRoleItems())); |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
public List<UserDO> listUser(Long venderId) { |
||||
|
if (venderId == null || venderId < 0) { |
||||
|
return Lists.newArrayList(); |
||||
|
} |
||||
|
Map<Long, Role> roles = roleRepository.findByVender(venderId).stream().collect(Collectors.toMap(Role::getId, v -> v)); |
||||
|
Map<String,Role> userRole = userRoleRepository.findByVender(venderId).stream().collect(Collectors.toMap(UserRole::getUid, v -> roles.getOrDefault(v.getRoleId(), new Role()))); |
||||
|
return userRepository.findByUidIn(userRole.keySet()).stream().map(user -> { |
||||
|
Role role = userRole.getOrDefault(user.getUid(), null); |
||||
|
return UserDO.builder() |
||||
|
.uid(user.getUid()) |
||||
|
.name(user.getName()) |
||||
|
.phone(user.getPhone()) |
||||
|
.time(user.getCreated()) |
||||
|
.roleId(role != null ? role.getId() : null) |
||||
|
.roleName(role != null ? role.getRoleName() : null) |
||||
|
.build(); |
||||
|
}).collect(Collectors.toList()); |
||||
|
} |
||||
|
|
||||
|
public List<RoleItem> listRoleItems(AuthType itemType) { |
||||
|
return enumService.getRoleItems(itemType); |
||||
|
} |
||||
|
|
||||
|
public Role addRole(Long venderId) { |
||||
|
List<Long> items = listRoleItems(AuthType.CLIENT).stream().map(RoleItem::getId).collect(Collectors.toList()); |
||||
|
return addRole("超级管理员", items, RoleType.SYSTEM, AuthType.CLIENT, venderId, "system"); |
||||
|
} |
||||
|
|
||||
|
public Role addRole(String roleName, List<Long> items, RoleType roleType, AuthType itemType, Long venderId, String operator) { |
||||
|
List<Long> allItem = listRoleItems(itemType).stream().map(RoleItem::getId).collect(Collectors.toList()); |
||||
|
items = items == null ? Lists.newArrayList() : items.stream().filter(Objects::nonNull).filter(allItem::contains).collect(Collectors.toList()); |
||||
|
Assert.isTrue(StringUtils.isNotBlank(roleName) && venderId != null, "[参数错误]角色名称不能为空!"); |
||||
|
Instant dateTime = Instant.now(); |
||||
|
Role role = Role.builder().vender(venderId).roleType(roleType).roleName(roleName).roleItems(items).operate(operator).created(dateTime).modify(dateTime).build(); |
||||
|
log.info("[UserService] addRole venderId=" + venderId + ", operator=" + operator); |
||||
|
return roleRepository.save(role); |
||||
|
} |
||||
|
|
||||
|
public void delRole(Long roleId, Long venderId, String operator) { |
||||
|
Assert.isTrue(roleId != null && roleId > 0 && roleRepository.existsByIdAndVender(roleId, venderId) && !userRoleRepository.existsByRoleIdAndVender(roleId, venderId), "[参数错误]角色使用中,不能删除!"); |
||||
|
roleRepository.deleteByIdAndVenderAndRoleTypeNot(roleId, venderId, RoleType.SYSTEM); |
||||
|
log.info("[UserService] delRole venderId=" + venderId + ",roleId=" + roleId + ", operator=" + operator); |
||||
|
} |
||||
|
|
||||
|
public void changeRole(Long roleId, String roleName, List<Long> items, AuthType itemType, Long venderId, String operator) { |
||||
|
Assert.isTrue(roleId != null && roleId > 0 && roleRepository.existsByIdAndVender(roleId, venderId), "[参数错误]该角色不属于您!"); |
||||
|
if(StringUtils.isNotBlank(roleName)) { |
||||
|
roleRepository.updateRoleNameAndOperateByIdAndVenderAndRoleTypeNot(roleName, operator, roleId, venderId); |
||||
|
log.info("[UserService] changeRole venderId=" + venderId + ", roleName=" + roleName + ", roleId=" + roleId + ", operator=" + operator); |
||||
|
} |
||||
|
|
||||
|
List<Long> allItem = listRoleItems(itemType).stream().map(RoleItem::getId).collect(Collectors.toList()); |
||||
|
items = items == null ? Lists.newArrayList() : items.stream().filter(Objects::nonNull).filter(allItem::contains).collect(Collectors.toList()); |
||||
|
if(!items.isEmpty()) { |
||||
|
roleRepository.updateRoleItemsAndOperateByIdAndVenderAndRoleTypeNot(JSON.toJSONString(items), operator, roleId, venderId); |
||||
|
log.info("[UserService] changeRole venderId=" + venderId + ", allItem=" + JSON.toJSONString(items) + ", roleId=" + roleId + ", operator=" + operator); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public List<Role> listRole(Long venderId) { |
||||
|
return roleRepository.findByVender(venderId); |
||||
|
} |
||||
|
|
||||
|
public Role query(Long roleId) { |
||||
|
return roleId != null && roleId > 0 ? roleRepository.findById(roleId).orElse(null) : null; |
||||
|
} |
||||
|
|
||||
|
public boolean checkUser(String uid, String password) { |
||||
|
return StringUtils.isNotBlank(uid) && StringUtils.isNotBlank(password) && userRepository.existsByUidAndPwdAndStatus(uid, password, true); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 查看用户是否存在 |
||||
|
*/ |
||||
|
public boolean checkUser(String uid) { |
||||
|
return StringUtils.isBlank(uid) || !userRepository.existsByUid(uid); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 查看用户属于该单位 |
||||
|
*/ |
||||
|
public boolean checkRole(String uid, Long venderId) { |
||||
|
return StringUtils.isNotBlank(uid) && venderId != null && userRoleRepository.existsByUidAndVender(uid, venderId); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 检查权限属于该单位 |
||||
|
*/ |
||||
|
public boolean checkRole(Long roleId, Long venderId) { |
||||
|
return roleId != null && roleId > 0 && venderId != null && roleRepository.existsByIdAndVender(roleId, venderId); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 统计管理员数量 |
||||
|
*/ |
||||
|
public long countRole(Long venderId, String uid) { |
||||
|
if (venderId == null || venderId < 0) { |
||||
|
return 0; |
||||
|
} |
||||
|
Role role = roleRepository.findByVenderForDefaultRole(venderId, RoleType.SYSTEM); |
||||
|
return role == null ? 0 : userRoleRepository.countByRoleIdAndVenderAndUidNot(role.getId(), venderId, uid); |
||||
|
} |
||||
|
|
||||
|
public void logout(String uid) { |
||||
|
if (StringUtils.isBlank(uid)) { |
||||
|
return; |
||||
|
} |
||||
|
userLogRepository.updateLogoutByUid(Instant.now(), uid); |
||||
|
log.info("[UserService] logout uid=" + uid); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,140 @@ |
|||||
|
package com.mathvision.diet.service; |
||||
|
|
||||
|
import com.mathvision.diet.domian.VenderType; |
||||
|
import com.mathvision.diet.entity.Vender; |
||||
|
import com.mathvision.diet.entity.VenderConfig; |
||||
|
import com.mathvision.diet.repository.VenderConfigRepository; |
||||
|
import com.mathvision.diet.repository.VenderRepository; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.collections4.CollectionUtils; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.data.domain.Page; |
||||
|
import org.springframework.data.domain.PageRequest; |
||||
|
import org.springframework.scheduling.annotation.Scheduled; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
|
||||
|
import javax.annotation.PostConstruct; |
||||
|
import javax.annotation.Resource; |
||||
|
import java.math.BigDecimal; |
||||
|
import java.time.Instant; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Service |
||||
|
public class VenderService { |
||||
|
@Resource |
||||
|
private VenderRepository venderRepository; |
||||
|
|
||||
|
@Resource |
||||
|
private VenderConfigRepository venderConfigRepository; |
||||
|
|
||||
|
@Resource |
||||
|
private UserService userService; |
||||
|
|
||||
|
@Resource |
||||
|
private MenuService menuService; |
||||
|
|
||||
|
@Resource |
||||
|
private DishService dishService; |
||||
|
|
||||
|
@Resource |
||||
|
private IngredientService ingredientService; |
||||
|
|
||||
|
@PostConstruct |
||||
|
@Scheduled(cron = "0 0 0/1 * * *") |
||||
|
public void init() { |
||||
|
venderRepository.scanExpired(); |
||||
|
} |
||||
|
|
||||
|
public VenderConfig queryConfig(Long venderId) { |
||||
|
if (venderId == null || venderId <= 0) { |
||||
|
log.info("[VenderService] queryConfig venderId = " + venderId); |
||||
|
return null; |
||||
|
} |
||||
|
return venderConfigRepository.findByVender(venderId); |
||||
|
} |
||||
|
|
||||
|
public void modConfig(Long venderId, BigDecimal breakfast, BigDecimal lunch, BigDecimal dinner, String operate) { |
||||
|
if (venderId == null || venderId <= 0) { |
||||
|
log.info("[VenderService] modConfig venderId = " + venderId + ", operate = " + operate); |
||||
|
return; |
||||
|
} |
||||
|
venderConfigRepository.updateBreakfastAndLunchAndDinnerAndOperateByVender(breakfast, lunch, dinner, operate, venderId); |
||||
|
log.info("[VenderService] modConfig success: venderId = " + venderId + ", operate = " + operate); |
||||
|
} |
||||
|
|
||||
|
public Vender queryVender(Long venderId) { |
||||
|
if (venderId == null || venderId <= 0) { |
||||
|
log.info("[VenderService] queryVender venderId = " + venderId); |
||||
|
return null; |
||||
|
} |
||||
|
return venderRepository.findById(venderId).orElse(null); |
||||
|
} |
||||
|
|
||||
|
public boolean exists(Long venderId) { |
||||
|
if (venderId == null || venderId <= 0) { |
||||
|
return false; |
||||
|
} |
||||
|
return venderRepository.existsById(venderId); |
||||
|
} |
||||
|
|
||||
|
public List<Vender> listVender(String keyword, List<Long> vendors) { |
||||
|
if (CollectionUtils.isNotEmpty(vendors)) { |
||||
|
return venderRepository.findByIdIn(vendors); |
||||
|
} |
||||
|
return StringUtils.isNotBlank(keyword) ? venderRepository.findByStatusAndNameLikeOrderByNameDesc(true, keyword) : venderRepository.findByStatusOrderByNameAsc(true); |
||||
|
} |
||||
|
|
||||
|
public Page<Vender> pageVender(String keyword, VenderType type, PageRequest pageRequest) { |
||||
|
if (type == null) { |
||||
|
return StringUtils.isBlank(keyword) ? venderRepository.findAll(pageRequest) : venderRepository.findByNameLikeOrderByIdDesc(keyword, pageRequest); |
||||
|
} else { |
||||
|
return StringUtils.isBlank(keyword) ? venderRepository.findByCategoryOrderByIdDesc(type, pageRequest) : venderRepository.findByCategoryAndNameLikeOrderByIdDesc(type, keyword, pageRequest); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Transactional |
||||
|
public void addVender(Vender vender, String password, String operator) { |
||||
|
Instant dateTime = Instant.now(); |
||||
|
vender.setStatus(true); |
||||
|
vender.setOperate(operator); |
||||
|
vender.setCreated(dateTime); |
||||
|
vender.setModify(dateTime); |
||||
|
Vender _vender = venderRepository.save(vender); |
||||
|
venderConfigRepository.save(VenderConfig.builder().vender(vender.getId()).breakfast(new BigDecimal(0)).lunch(new BigDecimal(0)).dinner(new BigDecimal(0)).operate(operator).created(dateTime).modify(dateTime).build()); |
||||
|
userService.addUser(_vender.getAccount(), _vender.getName(), password, userService.addRole(_vender.getId()).getId(), _vender.getId(), operator); |
||||
|
log.info("[VenderService] addVender venderId = " + vender.getName() + ", operator = " + operator); |
||||
|
} |
||||
|
|
||||
|
@Transactional |
||||
|
public void modVender(Vender vender, String operator) { |
||||
|
if (vender == null) return; |
||||
|
venderRepository.save(vender); |
||||
|
log.info("[VenderService] delVender venderId = " + vender.getId() + ", operator = " + operator); |
||||
|
} |
||||
|
|
||||
|
@Transactional |
||||
|
public void delVender(Long venderId, String operator) { |
||||
|
if (venderId == null || venderId <= 0) { |
||||
|
log.info("[VenderService] delVender venderId = " + venderId + ", operator = " + operator); |
||||
|
return; |
||||
|
} |
||||
|
venderRepository.deleteById(venderId); |
||||
|
venderConfigRepository.deleteByVender(venderId); |
||||
|
userService.delUser(venderId, operator); |
||||
|
ingredientService.delMark(venderId, operator); |
||||
|
dishService.delete(venderId, operator); |
||||
|
menuService.delete(venderId, operator); |
||||
|
log.info("[VenderService] delVender venderId = " + venderId + ", operator = " + operator); |
||||
|
} |
||||
|
|
||||
|
public boolean checkName(String venderName) { |
||||
|
return !venderRepository.existsByName(venderName); |
||||
|
} |
||||
|
|
||||
|
public boolean checkAccount(String account) { |
||||
|
return userService.checkUser(account); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,48 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
|
<modelVersion>4.0.0</modelVersion> |
||||
|
|
||||
|
<parent> |
||||
|
<groupId>com.mathvision.diet</groupId> |
||||
|
<artifactId>diet</artifactId> |
||||
|
<version>1.0-SNAPSHOT</version> |
||||
|
</parent> |
||||
|
<artifactId>diet-dao</artifactId> |
||||
|
<packaging>jar</packaging> |
||||
|
|
||||
|
<properties> |
||||
|
<maven.compiler.source>8</maven.compiler.source> |
||||
|
<maven.compiler.target>8</maven.compiler.target> |
||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
||||
|
</properties> |
||||
|
|
||||
|
<dependencies> |
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>com.mysql</groupId> |
||||
|
<artifactId>mysql-connector-j</artifactId> |
||||
|
<scope>runtime</scope> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>com.vladmihalcea</groupId> |
||||
|
<artifactId>hibernate-types-52</artifactId> |
||||
|
<version>2.21.1</version> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>com.googlecode.log4jdbc</groupId> |
||||
|
<artifactId>log4jdbc</artifactId> |
||||
|
<version>1.2</version> |
||||
|
<scope>runtime</scope> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>org.apache.commons</groupId> |
||||
|
<artifactId>commons-compress</artifactId> |
||||
|
<version>1.21</version> |
||||
|
</dependency> |
||||
|
</dependencies> |
||||
|
</project> |
||||
@ -0,0 +1,19 @@ |
|||||
|
package com.mathvision.diet.convert; |
||||
|
|
||||
|
import com.mathvision.diet.domian.AuthType; |
||||
|
|
||||
|
import javax.persistence.AttributeConverter; |
||||
|
import javax.persistence.Converter; |
||||
|
|
||||
|
@Converter(autoApply = true) |
||||
|
public class AuthTypeConvert implements AttributeConverter<AuthType,String> { |
||||
|
@Override |
||||
|
public String convertToDatabaseColumn(AuthType type) { |
||||
|
return type == null ? null : type.getType(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public AuthType convertToEntityAttribute(String s) { |
||||
|
return AuthType.toType(s); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
package com.mathvision.diet.convert; |
||||
|
|
||||
|
import com.alibaba.fastjson2.JSON; |
||||
|
import com.alibaba.fastjson2.TypeReference; |
||||
|
import com.mathvision.diet.domian.DishItemDTO; |
||||
|
|
||||
|
import javax.persistence.AttributeConverter; |
||||
|
import javax.persistence.Converter; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@Converter |
||||
|
public class DishItemConvert implements AttributeConverter<List<DishItemDTO>, String> { |
||||
|
@Override |
||||
|
public String convertToDatabaseColumn(List<DishItemDTO> stringList) { |
||||
|
return JSON.toJSONString(stringList); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public List<DishItemDTO> convertToEntityAttribute(String s) { |
||||
|
return JSON.parseObject(s, new TypeReference<List<DishItemDTO>>(){}); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
package com.mathvision.diet.convert; |
||||
|
|
||||
|
import com.mathvision.diet.domian.GenderType; |
||||
|
|
||||
|
import javax.persistence.AttributeConverter; |
||||
|
import javax.persistence.Converter; |
||||
|
|
||||
|
@Converter(autoApply = true) |
||||
|
public class GenderTypeConvert implements AttributeConverter<GenderType,String> { |
||||
|
@Override |
||||
|
public String convertToDatabaseColumn(GenderType type) { |
||||
|
return type == null ? null : type.getType(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public GenderType convertToEntityAttribute(String s) { |
||||
|
return GenderType.toType(s); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
package com.mathvision.diet.convert; |
||||
|
|
||||
|
import com.alibaba.fastjson2.JSON; |
||||
|
import com.alibaba.fastjson2.TypeReference; |
||||
|
|
||||
|
import javax.persistence.AttributeConverter; |
||||
|
import javax.persistence.Converter; |
||||
|
import java.math.BigDecimal; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
@Converter(autoApply = true) |
||||
|
public class IngredientConvert implements AttributeConverter<Map<String, Map<String, Map<String, BigDecimal>>>,String> { |
||||
|
@Override |
||||
|
public String convertToDatabaseColumn(Map<String, Map<String, Map<String, BigDecimal>>> stringMapMap) { |
||||
|
return JSON.toJSONString(stringMapMap); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Map<String, Map<String, Map<String, BigDecimal>>> convertToEntityAttribute(String s) { |
||||
|
return JSON.parseObject(s, new TypeReference<Map<String, Map<String, Map<String, BigDecimal>>>>(){}); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
package com.mathvision.diet.convert; |
||||
|
|
||||
|
import com.mathvision.diet.domian.MarkType; |
||||
|
|
||||
|
import javax.persistence.AttributeConverter; |
||||
|
import javax.persistence.Converter; |
||||
|
|
||||
|
@Converter(autoApply = true) |
||||
|
public class MarkTypeConvert implements AttributeConverter<MarkType,String> { |
||||
|
@Override |
||||
|
public String convertToDatabaseColumn(MarkType type) { |
||||
|
return type == null ? null : type.getType(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public MarkType convertToEntityAttribute(String s) { |
||||
|
return MarkType.toType(s); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,23 @@ |
|||||
|
package com.mathvision.diet.convert; |
||||
|
|
||||
|
import com.alibaba.fastjson2.JSON; |
||||
|
import com.alibaba.fastjson2.TypeReference; |
||||
|
import com.mathvision.diet.domian.DishItemDTO; |
||||
|
|
||||
|
import javax.persistence.AttributeConverter; |
||||
|
import javax.persistence.Converter; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
@Converter |
||||
|
public class MenuDishConvert implements AttributeConverter<Map<String, List<DishItemDTO>>, String> { |
||||
|
@Override |
||||
|
public String convertToDatabaseColumn(Map<String, List<DishItemDTO>> stringList) { |
||||
|
return JSON.toJSONString(stringList); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Map<String, List<DishItemDTO>> convertToEntityAttribute(String s) { |
||||
|
return JSON.parseObject(s, new TypeReference<Map<String, List<DishItemDTO>>>(){}); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
package com.mathvision.diet.convert; |
||||
|
|
||||
|
import com.mathvision.diet.domian.MenuStatus; |
||||
|
|
||||
|
import javax.persistence.AttributeConverter; |
||||
|
import javax.persistence.Converter; |
||||
|
|
||||
|
@Converter(autoApply = true) |
||||
|
public class MenuStatusConvert implements AttributeConverter<MenuStatus, Integer> { |
||||
|
@Override |
||||
|
public Integer convertToDatabaseColumn(MenuStatus type) { |
||||
|
return type == null ? null : type.getCode(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public MenuStatus convertToEntityAttribute(Integer s) { |
||||
|
return MenuStatus.toType(s); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
package com.mathvision.diet.convert; |
||||
|
|
||||
|
import com.mathvision.diet.domian.RoleType; |
||||
|
|
||||
|
import javax.persistence.AttributeConverter; |
||||
|
import javax.persistence.Converter; |
||||
|
|
||||
|
@Converter(autoApply = true) |
||||
|
public class RoleTypeConvert implements AttributeConverter<RoleType,String> { |
||||
|
@Override |
||||
|
public String convertToDatabaseColumn(RoleType type) { |
||||
|
return type == null ? null : type.getType(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public RoleType convertToEntityAttribute(String s) { |
||||
|
return RoleType.toType(s); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
package com.mathvision.diet.convert; |
||||
|
|
||||
|
import com.mathvision.diet.domian.VenderType; |
||||
|
|
||||
|
import javax.persistence.AttributeConverter; |
||||
|
import javax.persistence.Converter; |
||||
|
|
||||
|
@Converter(autoApply = true) |
||||
|
public class VenderTypeConvert implements AttributeConverter<VenderType,String> { |
||||
|
@Override |
||||
|
public String convertToDatabaseColumn(VenderType type) { |
||||
|
return type == null ? null : type.getType(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public VenderType convertToEntityAttribute(String s) { |
||||
|
return VenderType.toType(s); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,26 @@ |
|||||
|
package com.mathvision.diet.domian; |
||||
|
|
||||
|
import lombok.Getter; |
||||
|
|
||||
|
import java.util.Arrays; |
||||
|
|
||||
|
public enum AuthType { |
||||
|
SERVER("管理端"), CLIENT("业务端"); |
||||
|
|
||||
|
@Getter |
||||
|
private final String type; |
||||
|
|
||||
|
AuthType(String type) { |
||||
|
this.type = type; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String toString() { |
||||
|
return type; |
||||
|
} |
||||
|
|
||||
|
public static AuthType toType(String s) { |
||||
|
return Arrays.stream(AuthType.values()).filter(x -> x.getType().equals(s)).findFirst().orElse(null); |
||||
|
} |
||||
|
} |
||||
|
|
||||
@ -0,0 +1,5 @@ |
|||||
|
package com.mathvision.diet.domian; |
||||
|
|
||||
|
public enum ClientType { |
||||
|
IOS, Android, WEB; |
||||
|
} |
||||
@ -0,0 +1,17 @@ |
|||||
|
package com.mathvision.diet.domian; |
||||
|
|
||||
|
import lombok.*; |
||||
|
|
||||
|
import java.math.BigDecimal; |
||||
|
|
||||
|
@Builder |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@EqualsAndHashCode |
||||
|
@Getter |
||||
|
@Setter |
||||
|
public class DishItemDTO { |
||||
|
private String key; |
||||
|
private BigDecimal value; |
||||
|
private Boolean isMain; |
||||
|
} |
||||
@ -0,0 +1,25 @@ |
|||||
|
package com.mathvision.diet.domian; |
||||
|
|
||||
|
import lombok.Getter; |
||||
|
|
||||
|
import java.util.Arrays; |
||||
|
|
||||
|
public enum GenderType { |
||||
|
F("女"), M("男"); |
||||
|
|
||||
|
@Getter |
||||
|
private final String type; |
||||
|
|
||||
|
GenderType(String type) { |
||||
|
this.type = type; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String toString() { |
||||
|
return type; |
||||
|
} |
||||
|
|
||||
|
public static GenderType toType(String s) { |
||||
|
return Arrays.stream(GenderType.values()).filter(x -> x.getType().equals(s)).findFirst().orElse(null); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,32 @@ |
|||||
|
package com.mathvision.diet.domian; |
||||
|
|
||||
|
import com.vladmihalcea.hibernate.type.json.JsonStringType; |
||||
|
import lombok.*; |
||||
|
import org.hibernate.annotations.Type; |
||||
|
import org.hibernate.annotations.TypeDef; |
||||
|
|
||||
|
import javax.persistence.Column; |
||||
|
import javax.persistence.Entity; |
||||
|
import javax.persistence.Id; |
||||
|
import java.math.BigDecimal; |
||||
|
import java.time.Instant; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
@Builder |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Entity |
||||
|
@TypeDef(name = "json", typeClass = JsonStringType.class) |
||||
|
public class IngredientDTO { |
||||
|
@Id |
||||
|
private String key; |
||||
|
private String name; |
||||
|
private String type; |
||||
|
private String mark; |
||||
|
private Instant modify; |
||||
|
@Type(type = "json") |
||||
|
@Column(columnDefinition = "json", nullable = false) |
||||
|
private Map<String, BigDecimal> nutrient; |
||||
|
} |
||||
@ -0,0 +1,25 @@ |
|||||
|
package com.mathvision.diet.domian; |
||||
|
|
||||
|
import lombok.Getter; |
||||
|
|
||||
|
import java.util.Arrays; |
||||
|
|
||||
|
public enum MarkType { |
||||
|
frequent("常用"), forbidden("忌用"); |
||||
|
|
||||
|
@Getter |
||||
|
private final String type; |
||||
|
|
||||
|
MarkType(String type) { |
||||
|
this.type = type; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String toString() { |
||||
|
return type; |
||||
|
} |
||||
|
|
||||
|
public static MarkType toType(String s) { |
||||
|
return Arrays.stream(MarkType.values()).filter(x -> x.getType().equals(s)).findFirst().orElse(null); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,25 @@ |
|||||
|
package com.mathvision.diet.domian; |
||||
|
|
||||
|
import lombok.Getter; |
||||
|
|
||||
|
import java.util.Arrays; |
||||
|
|
||||
|
public enum MealType { |
||||
|
breakfast("早餐"), lunch("午餐"), dinner("晚餐"); |
||||
|
|
||||
|
@Getter |
||||
|
private final String type; |
||||
|
|
||||
|
MealType(String type) { |
||||
|
this.type = type; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String toString() { |
||||
|
return type; |
||||
|
} |
||||
|
|
||||
|
public static MealType toType(String s) { |
||||
|
return Arrays.stream(MealType.values()).filter(x -> x.getType().equals(s)).findFirst().orElse(null); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,30 @@ |
|||||
|
package com.mathvision.diet.domian; |
||||
|
|
||||
|
import com.alibaba.fastjson2.JSONWriter; |
||||
|
import com.alibaba.fastjson2.annotation.JSONField; |
||||
|
import com.mathvision.diet.convert.MenuStatusConvert; |
||||
|
import lombok.*; |
||||
|
|
||||
|
import javax.persistence.Column; |
||||
|
import javax.persistence.Convert; |
||||
|
|
||||
|
@Builder |
||||
|
@NoArgsConstructor |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@EqualsAndHashCode |
||||
|
public class MenuCountDTO { |
||||
|
|
||||
|
public MenuCountDTO(MenuStatus status, Long count) { |
||||
|
setCount(count); |
||||
|
setStatus(status); |
||||
|
} |
||||
|
|
||||
|
@JSONField(serializeFeatures = { JSONWriter.Feature.WriteEnumUsingOrdinal }) |
||||
|
@Convert(converter = MenuStatusConvert.class) |
||||
|
@Column(name = "status") |
||||
|
private MenuStatus status; |
||||
|
|
||||
|
@Column(name = "count") |
||||
|
private Long count; |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
package com.mathvision.diet.domian; |
||||
|
|
||||
|
import lombok.*; |
||||
|
|
||||
|
import java.math.BigDecimal; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
@Builder |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@EqualsAndHashCode |
||||
|
public class MenuDishItemDTO { |
||||
|
/** |
||||
|
* 食材 |
||||
|
*/ |
||||
|
String key; |
||||
|
|
||||
|
/** |
||||
|
* 是否主料 |
||||
|
*/ |
||||
|
Boolean isMain; |
||||
|
/** |
||||
|
* 人群列表: |
||||
|
*/ |
||||
|
Map<String, BigDecimal> value; |
||||
|
} |
||||
@ -0,0 +1,37 @@ |
|||||
|
package com.mathvision.diet.domian; |
||||
|
|
||||
|
import lombok.Getter; |
||||
|
|
||||
|
import java.util.Arrays; |
||||
|
|
||||
|
public enum MenuStatus { |
||||
|
//0-草稿,1-提交审核,2-审核通过,3-审核失败,4-禁用,5-发布
|
||||
|
draft(0, "草稿"), submit(1,"审核中"), pass(2,"审核通过"), reject(3, "审核失败"), disabled(4, "禁用"), publish(5, "发布"); |
||||
|
|
||||
|
@Getter |
||||
|
private final int code; |
||||
|
@Getter |
||||
|
private final String type; |
||||
|
|
||||
|
MenuStatus(int code, String type) { |
||||
|
this.code = code; |
||||
|
this.type = type; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String toString() { |
||||
|
return type; |
||||
|
} |
||||
|
|
||||
|
public static MenuStatus toType(Integer s) { |
||||
|
return Arrays.stream(MenuStatus.values()).filter(x -> s != null && x.getCode() == s).findFirst().orElse(null); |
||||
|
} |
||||
|
|
||||
|
public static MenuStatus toType(Long s) { |
||||
|
return Arrays.stream(MenuStatus.values()).filter(x -> s != null && x.getCode() == s.intValue()).findFirst().orElse(null); |
||||
|
} |
||||
|
|
||||
|
public static MenuStatus toType(String s) { |
||||
|
return Arrays.stream(MenuStatus.values()).filter(x -> x.getType().equals(s)).findFirst().orElse(null); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,5 @@ |
|||||
|
package com.mathvision.diet.domian; |
||||
|
|
||||
|
public enum MessageType { |
||||
|
CODE, NOTIFY, MESSAGE; |
||||
|
} |
||||
@ -0,0 +1,25 @@ |
|||||
|
package com.mathvision.diet.domian; |
||||
|
|
||||
|
import lombok.Getter; |
||||
|
|
||||
|
import java.util.Arrays; |
||||
|
|
||||
|
public enum RoleType { |
||||
|
SYSTEM("系统"), CUSTOM("自定义"); |
||||
|
|
||||
|
@Getter |
||||
|
private final String type; |
||||
|
|
||||
|
RoleType(String type) { |
||||
|
this.type = type; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String toString() { |
||||
|
return type; |
||||
|
} |
||||
|
|
||||
|
public static RoleType toType(String s) { |
||||
|
return Arrays.stream(RoleType.values()).filter(x -> x.getType().equals(s)).findFirst().orElse(null); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,25 @@ |
|||||
|
package com.mathvision.diet.domian; |
||||
|
|
||||
|
import lombok.Getter; |
||||
|
|
||||
|
import java.util.Arrays; |
||||
|
|
||||
|
public enum VenderType { |
||||
|
school("学校"), hospital("医院"), institution("事业单位"), other("其他"); |
||||
|
|
||||
|
@Getter |
||||
|
private final String type; |
||||
|
|
||||
|
VenderType(String type) { |
||||
|
this.type = type; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String toString() { |
||||
|
return type; |
||||
|
} |
||||
|
|
||||
|
public static VenderType toType(String s) { |
||||
|
return Arrays.stream(VenderType.values()).filter(x -> x.getType().equals(s)).findFirst().orElse(null); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,69 @@ |
|||||
|
package com.mathvision.diet.entity; |
||||
|
|
||||
|
import com.alibaba.fastjson2.annotation.JSONField; |
||||
|
import com.mathvision.diet.convert.DishItemConvert; |
||||
|
import com.mathvision.diet.domian.DishItemDTO; |
||||
|
import com.vladmihalcea.hibernate.type.json.JsonStringType; |
||||
|
import lombok.*; |
||||
|
import org.hibernate.annotations.Type; |
||||
|
import org.hibernate.annotations.TypeDef; |
||||
|
|
||||
|
import javax.persistence.*; |
||||
|
import java.time.Instant; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@Builder |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Entity |
||||
|
@TypeDef(name = "json", typeClass = JsonStringType.class) |
||||
|
@Table(name = "dish") |
||||
|
public class Dish { |
||||
|
|
||||
|
public Dish(Long id, String name, String marks, List<DishItemDTO> ingredient) { |
||||
|
setId(id); |
||||
|
setName(name); |
||||
|
setMarks(marks); |
||||
|
setIngredient(ingredient); |
||||
|
} |
||||
|
|
||||
|
@Id |
||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
|
@Column(name = "id", nullable = false) |
||||
|
private Long id; |
||||
|
|
||||
|
@Column(name = "name", nullable = false, length = 20) |
||||
|
private String name; |
||||
|
|
||||
|
@Column(name = "`vender`", nullable = false, length = 20) |
||||
|
private Long vender; |
||||
|
|
||||
|
@Lob |
||||
|
@Column(name = "icon") |
||||
|
private String icon; |
||||
|
|
||||
|
@Type(type = "json") |
||||
|
@Column(name = "month", columnDefinition = "json", nullable = false) |
||||
|
private List<Integer> month; |
||||
|
|
||||
|
@Convert(converter = DishItemConvert.class) |
||||
|
@Column(name = "ingredient", columnDefinition = "json", nullable = false) |
||||
|
private List<DishItemDTO> ingredient; |
||||
|
|
||||
|
@Column(name = "marks", nullable = false, length = 45) |
||||
|
private String marks; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "operate", length = 45) |
||||
|
private String operate; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "created") |
||||
|
private Instant created; |
||||
|
|
||||
|
@Column(name = "modify") |
||||
|
private Instant modify; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,41 @@ |
|||||
|
package com.mathvision.diet.entity; |
||||
|
|
||||
|
import com.alibaba.fastjson2.annotation.JSONField; |
||||
|
import lombok.*; |
||||
|
|
||||
|
import javax.persistence.*; |
||||
|
import java.time.Instant; |
||||
|
|
||||
|
@Builder |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Entity |
||||
|
@Table(name = "food_category") |
||||
|
public class FoodCategory { |
||||
|
@JSONField(serialize = false) |
||||
|
@Id |
||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
|
@Column(name = "id", nullable = false) |
||||
|
private Long id; |
||||
|
|
||||
|
@Column(name = "`key`", nullable = false, length = 32) |
||||
|
private String key; |
||||
|
|
||||
|
@Column(name = "name", nullable = false, length = 32) |
||||
|
private String value; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "operate", length = 45) |
||||
|
private String operate; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "created") |
||||
|
private Instant created; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "modify") |
||||
|
private Instant modify; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,41 @@ |
|||||
|
package com.mathvision.diet.entity; |
||||
|
|
||||
|
import com.alibaba.fastjson2.annotation.JSONField; |
||||
|
import lombok.*; |
||||
|
|
||||
|
import javax.persistence.*; |
||||
|
import java.time.Instant; |
||||
|
|
||||
|
@Builder |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Entity |
||||
|
@Table(name = "food_mark") |
||||
|
public class FoodMark { |
||||
|
@JSONField(serialize = false) |
||||
|
@Id |
||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
|
@Column(name = "id", nullable = false) |
||||
|
private Long id; |
||||
|
|
||||
|
@Column(name = "`key`", nullable = false, length = 20) |
||||
|
private String key; |
||||
|
|
||||
|
@Column(name = "name", nullable = false, length = 20) |
||||
|
private String value; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "operate", length = 45) |
||||
|
private String operate; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "created") |
||||
|
private Instant created; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "modify") |
||||
|
private Instant modify; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,48 @@ |
|||||
|
package com.mathvision.diet.entity; |
||||
|
|
||||
|
import com.alibaba.fastjson2.annotation.JSONField; |
||||
|
import lombok.*; |
||||
|
|
||||
|
import javax.persistence.*; |
||||
|
import java.math.BigDecimal; |
||||
|
import java.time.Instant; |
||||
|
|
||||
|
@Builder |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Entity |
||||
|
@Table(name = "food_nutrient") |
||||
|
public class FoodNutrient { |
||||
|
@JSONField(serialize = false) |
||||
|
@Id |
||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
|
@Column(name = "id", nullable = false) |
||||
|
private Long id; |
||||
|
|
||||
|
@Column(name = "`key`", nullable = false, length = 20) |
||||
|
private String key; |
||||
|
|
||||
|
@Column(name = "name", nullable = false, length = 64) |
||||
|
private String value; |
||||
|
|
||||
|
@Column(name = "measurement", nullable = false, length = 10) |
||||
|
private String measurement; |
||||
|
|
||||
|
@Column(name = "nrv", nullable = false, precision = 5, scale = 2) |
||||
|
private BigDecimal nrv; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "operate", length = 45) |
||||
|
private String operate; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "created") |
||||
|
private Instant created; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "modify") |
||||
|
private Instant modify; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,64 @@ |
|||||
|
package com.mathvision.diet.entity; |
||||
|
|
||||
|
import com.alibaba.fastjson2.annotation.JSONField; |
||||
|
import com.vladmihalcea.hibernate.type.json.JsonStringType; |
||||
|
import lombok.*; |
||||
|
import org.hibernate.annotations.Type; |
||||
|
import org.hibernate.annotations.TypeDef; |
||||
|
|
||||
|
import javax.persistence.*; |
||||
|
import java.math.BigDecimal; |
||||
|
import java.time.Instant; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
@Builder |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Entity |
||||
|
@TypeDef(name = "json", typeClass = JsonStringType.class) |
||||
|
@Table(name = "ingredient", schema = "diet") |
||||
|
public class Ingredient { |
||||
|
public Ingredient(String key, String name) { |
||||
|
setKey(key); |
||||
|
setName(name); |
||||
|
} |
||||
|
|
||||
|
public Ingredient(String key, String name, String type) { |
||||
|
setKey(key); |
||||
|
setName(name); |
||||
|
setType(type); |
||||
|
} |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Id |
||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
|
@Column(name = "id", nullable = false) |
||||
|
private Long id; |
||||
|
|
||||
|
@Column(name = "`key`", nullable = false, length = 20) |
||||
|
private String key; |
||||
|
|
||||
|
@Column(name = "name", nullable = false, length = 64) |
||||
|
private String name; |
||||
|
|
||||
|
@Column(name = "type", nullable = false, length = 64) |
||||
|
private String type; |
||||
|
|
||||
|
@Type(type = "json") |
||||
|
@Column(name = "nutrient", columnDefinition = "json", nullable = false) |
||||
|
private Map<String, BigDecimal> nutrient; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "operate", length = 45) |
||||
|
private String operate; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "created") |
||||
|
private Instant created; |
||||
|
|
||||
|
@Column(name = "modify") |
||||
|
private Instant modify; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,50 @@ |
|||||
|
package com.mathvision.diet.entity; |
||||
|
|
||||
|
import com.alibaba.fastjson2.JSONWriter; |
||||
|
import com.alibaba.fastjson2.annotation.JSONField; |
||||
|
import com.mathvision.diet.convert.MarkTypeConvert; |
||||
|
import com.mathvision.diet.domian.MarkType; |
||||
|
import lombok.*; |
||||
|
|
||||
|
import javax.persistence.*; |
||||
|
import java.time.Instant; |
||||
|
|
||||
|
@Builder |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Entity |
||||
|
@Table(name = "ingredient_mark", schema = "diet") |
||||
|
public class IngredientMark { |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Id |
||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
|
@Column(name = "id", nullable = false) |
||||
|
private Long id; |
||||
|
|
||||
|
@Column(name = "ingredient", nullable = false, length = 20) |
||||
|
private String ingredient; |
||||
|
|
||||
|
@Column(name = "vender", nullable = false) |
||||
|
private Long vender; |
||||
|
|
||||
|
@JSONField(serializeFeatures = { JSONWriter.Feature.WriteEnumUsingToString }) |
||||
|
@Convert(converter = MarkTypeConvert.class) |
||||
|
@Column(name = "mark", columnDefinition="ENUM('常用','忌用')", nullable = false) |
||||
|
private MarkType mark; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "operate", length = 45) |
||||
|
private String operate; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "created") |
||||
|
private Instant created; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "modify") |
||||
|
private Instant modify; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,82 @@ |
|||||
|
package com.mathvision.diet.entity; |
||||
|
|
||||
|
import com.alibaba.fastjson2.JSONWriter; |
||||
|
import com.alibaba.fastjson2.annotation.JSONField; |
||||
|
import com.mathvision.diet.convert.MenuStatusConvert; |
||||
|
import com.mathvision.diet.domian.MenuStatus; |
||||
|
import com.vladmihalcea.hibernate.type.json.JsonStringType; |
||||
|
import lombok.*; |
||||
|
import org.hibernate.annotations.Type; |
||||
|
import org.hibernate.annotations.TypeDef; |
||||
|
|
||||
|
import javax.persistence.*; |
||||
|
import java.time.Instant; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
@Builder |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Entity |
||||
|
@TypeDef(name = "json", typeClass = JsonStringType.class) |
||||
|
@Table(name = "menu") |
||||
|
public class Menu { |
||||
|
@Id |
||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
|
@Column(name = "id", nullable = false) |
||||
|
private Long id; |
||||
|
|
||||
|
@Column(name = "vender", nullable = false) |
||||
|
private Long vender; |
||||
|
|
||||
|
@Column(name = "nutrient", nullable = false) |
||||
|
private Long nutrient; |
||||
|
|
||||
|
@Column(name = "name", nullable = false, length = 20) |
||||
|
private String name; |
||||
|
|
||||
|
@Column(name = "day", columnDefinition = "int UNSIGNED not null") |
||||
|
private Long day; |
||||
|
|
||||
|
@Type(type = "json") |
||||
|
@Column(name = "meals", columnDefinition = "json", nullable = false) |
||||
|
private List<String> meals; |
||||
|
|
||||
|
@Type(type = "json") |
||||
|
@Column(name = "crows", columnDefinition = "json", nullable = false) |
||||
|
private List<String> crows; |
||||
|
|
||||
|
@Type(type = "json") |
||||
|
@Column(name = "scale", columnDefinition = "json", nullable = false) |
||||
|
private Map<String, Integer> scale; |
||||
|
|
||||
|
@Type(type = "json") |
||||
|
@Column(name = "month", columnDefinition = "json", nullable = false) |
||||
|
private List<Integer> month; |
||||
|
|
||||
|
@JSONField(serializeFeatures = { JSONWriter.Feature.WriteEnumUsingOrdinal }) |
||||
|
@Convert(converter = MenuStatusConvert.class) |
||||
|
@Column(name = "status", columnDefinition = "tinyint UNSIGNED not null") |
||||
|
private MenuStatus status; |
||||
|
|
||||
|
@Column(name = "approve") |
||||
|
private String approve; |
||||
|
|
||||
|
@Column(name = "start_time") |
||||
|
private Instant startTime; |
||||
|
|
||||
|
@Column(name = "end_time") |
||||
|
private Instant endTime; |
||||
|
|
||||
|
@Column(name = "operate", length = 45) |
||||
|
private String operate; |
||||
|
|
||||
|
@Column(name = "created") |
||||
|
private Instant created; |
||||
|
|
||||
|
@Column(name = "modify") |
||||
|
private Instant modify; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,43 @@ |
|||||
|
package com.mathvision.diet.entity; |
||||
|
|
||||
|
import com.alibaba.fastjson2.annotation.JSONField; |
||||
|
import lombok.*; |
||||
|
|
||||
|
import javax.persistence.*; |
||||
|
import java.time.Instant; |
||||
|
|
||||
|
@Builder |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Entity |
||||
|
@Table(name = "menu_approve") |
||||
|
public class MenuApprove { |
||||
|
@Id |
||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
|
@Column(name = "id", nullable = false) |
||||
|
private Long id; |
||||
|
|
||||
|
@Column(name = "menu", nullable = false) |
||||
|
private Long menu; |
||||
|
|
||||
|
@Column(name = "approve", length = 16) |
||||
|
private String approve; |
||||
|
|
||||
|
@Column(name = "pass", nullable = false) |
||||
|
private Boolean pass = false; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "operate", length = 45) |
||||
|
private String operate; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "created") |
||||
|
private Instant created; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "modify") |
||||
|
private Instant modify; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,65 @@ |
|||||
|
package com.mathvision.diet.entity; |
||||
|
|
||||
|
import com.alibaba.fastjson2.annotation.JSONField; |
||||
|
import com.mathvision.diet.domian.MenuDishItemDTO; |
||||
|
import com.vladmihalcea.hibernate.type.json.JsonStringType; |
||||
|
import lombok.*; |
||||
|
import org.hibernate.annotations.Type; |
||||
|
import org.hibernate.annotations.TypeDef; |
||||
|
|
||||
|
import javax.persistence.*; |
||||
|
import java.time.Instant; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@Builder |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Entity |
||||
|
@TypeDef(name = "json", typeClass = JsonStringType.class) |
||||
|
@Table(name = "menu_dish") |
||||
|
public class MenuDish { |
||||
|
@Id |
||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
|
@Column(name = "id", nullable = false) |
||||
|
private Long id; |
||||
|
|
||||
|
@Column(name = "vender", nullable = false) |
||||
|
private Long vender; |
||||
|
|
||||
|
@Column(name = "menu", nullable = false) |
||||
|
private Long menu; |
||||
|
|
||||
|
@Column(name = "day", columnDefinition = "int UNSIGNED not null") |
||||
|
private Long day; |
||||
|
|
||||
|
@Column(name = "meal", nullable = false, length = 16) |
||||
|
private String meal; |
||||
|
|
||||
|
@Column(name = "dish", nullable = false) |
||||
|
private Long dish; |
||||
|
|
||||
|
@Column(name = "name", nullable = false, length = 64) |
||||
|
private String name; |
||||
|
|
||||
|
@Type(type = "json") |
||||
|
@Column(name = "ingredient", columnDefinition = "json", nullable = false) |
||||
|
private List<MenuDishItemDTO> ingredient; |
||||
|
|
||||
|
@Column(name = "marks", nullable = false, length = 45) |
||||
|
private String marks; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "operate", length = 45) |
||||
|
private String operate; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "created") |
||||
|
private Instant created; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "modify") |
||||
|
private Instant modify; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,63 @@ |
|||||
|
package com.mathvision.diet.entity; |
||||
|
|
||||
|
import com.alibaba.fastjson2.annotation.JSONField; |
||||
|
import com.mathvision.diet.convert.IngredientConvert; |
||||
|
import com.vladmihalcea.hibernate.type.json.JsonStringType; |
||||
|
import lombok.*; |
||||
|
import org.hibernate.annotations.Type; |
||||
|
import org.hibernate.annotations.TypeDef; |
||||
|
|
||||
|
import javax.persistence.*; |
||||
|
import java.math.BigDecimal; |
||||
|
import java.time.Instant; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
@Builder |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Entity |
||||
|
@TypeDef(name = "json", typeClass = JsonStringType.class) |
||||
|
@Table(name = "nutrition", schema = "diet") |
||||
|
public class Nutrition { |
||||
|
@Id |
||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
|
@Column(name = "id", nullable = false) |
||||
|
private Long id; |
||||
|
|
||||
|
@Column(name = "name", nullable = false, length = 20) |
||||
|
private String name; |
||||
|
|
||||
|
@Type( type = "json" ) |
||||
|
@Column(name = "vendors", columnDefinition = "json", nullable = false) |
||||
|
private List<Long> vendors; |
||||
|
|
||||
|
@Type( type = "json" ) |
||||
|
@Column(name = "food_category_day", columnDefinition = "json") |
||||
|
private Map<String, BigDecimal> foodCategoryDay; |
||||
|
|
||||
|
@Type( type = "json" ) |
||||
|
@Column(name = "food_category_week", columnDefinition = "json") |
||||
|
private Map<String, BigDecimal> foodCategoryWeek; |
||||
|
|
||||
|
@Convert(converter = IngredientConvert.class) |
||||
|
@Column(name = "ingredient", columnDefinition = "json") |
||||
|
private Map<String, Map<String, Map<String, BigDecimal>>> ingredient; |
||||
|
|
||||
|
@Column(name = "overflow", nullable = false, precision = 5, scale = 2) |
||||
|
private BigDecimal overflow; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "operate", length = 45) |
||||
|
private String operate; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "created") |
||||
|
private Instant created; |
||||
|
|
||||
|
@Column(name = "modify") |
||||
|
private Instant modify; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,57 @@ |
|||||
|
package com.mathvision.diet.entity; |
||||
|
|
||||
|
import com.alibaba.fastjson2.JSONWriter; |
||||
|
import com.alibaba.fastjson2.annotation.JSONField; |
||||
|
import com.mathvision.diet.convert.RoleTypeConvert; |
||||
|
import com.mathvision.diet.domian.RoleType; |
||||
|
import com.vladmihalcea.hibernate.type.json.JsonStringType; |
||||
|
import lombok.*; |
||||
|
import org.hibernate.annotations.Type; |
||||
|
import org.hibernate.annotations.TypeDef; |
||||
|
|
||||
|
import javax.persistence.*; |
||||
|
import java.time.Instant; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@Builder |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Entity |
||||
|
@TypeDef(name = "json", typeClass = JsonStringType.class) |
||||
|
@Table(name = "role") |
||||
|
public class Role { |
||||
|
@Id |
||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
|
@Column(name = "id", nullable = false) |
||||
|
private Long id; |
||||
|
|
||||
|
@Column(name = "role_name", nullable = false, length = 45) |
||||
|
private String roleName; |
||||
|
|
||||
|
@JSONField(serializeFeatures = { JSONWriter.Feature.WriteEnumUsingToString }) |
||||
|
@Convert(converter = RoleTypeConvert.class) |
||||
|
@Column(name = "role_type", columnDefinition="ENUM('系统','自定义')", nullable = false) |
||||
|
private RoleType roleType; |
||||
|
|
||||
|
@Type(type = "json") |
||||
|
@Column(name = "role_items", columnDefinition = "json", nullable = false) |
||||
|
private List<Long> roleItems; |
||||
|
|
||||
|
@Column(name = "vender", nullable = false) |
||||
|
private Long vender; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "operate", length = 45) |
||||
|
private String operate; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "created") |
||||
|
private Instant created; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "modify") |
||||
|
private Instant modify; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,52 @@ |
|||||
|
package com.mathvision.diet.entity; |
||||
|
|
||||
|
import com.alibaba.fastjson2.JSONWriter; |
||||
|
import com.alibaba.fastjson2.annotation.JSONField; |
||||
|
import com.mathvision.diet.convert.AuthTypeConvert; |
||||
|
import com.mathvision.diet.domian.AuthType; |
||||
|
import lombok.*; |
||||
|
|
||||
|
import javax.persistence.*; |
||||
|
import java.time.Instant; |
||||
|
|
||||
|
@Builder |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Entity |
||||
|
@Table(name = "role_item") |
||||
|
public class RoleItem { |
||||
|
|
||||
|
@Id |
||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
|
@Column(name = "id", nullable = false) |
||||
|
private Long id; |
||||
|
|
||||
|
@Column(name = "item_name", nullable = false, length = 45) |
||||
|
private String itemName; |
||||
|
|
||||
|
@Column(name = "category", nullable = false, length = 50) |
||||
|
private String category; |
||||
|
|
||||
|
@JSONField(serializeFeatures = { JSONWriter.Feature.WriteEnumUsingToString }) |
||||
|
@Convert(converter = AuthTypeConvert.class) |
||||
|
@Column(name = "item_type", columnDefinition="ENUM('管理端','业务端')", nullable = false) |
||||
|
private AuthType itemType; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "item_value", nullable = false) |
||||
|
private String itemValue; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "operate", length = 45) |
||||
|
private String operate; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "created") |
||||
|
private Instant created; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "modify") |
||||
|
private Instant modify; |
||||
|
} |
||||
@ -0,0 +1,64 @@ |
|||||
|
package com.mathvision.diet.entity; |
||||
|
|
||||
|
import com.alibaba.fastjson2.JSONWriter; |
||||
|
import com.alibaba.fastjson2.annotation.JSONField; |
||||
|
import com.mathvision.diet.convert.GenderTypeConvert; |
||||
|
import com.mathvision.diet.domian.GenderType; |
||||
|
import lombok.*; |
||||
|
|
||||
|
import javax.persistence.*; |
||||
|
import java.time.Instant; |
||||
|
|
||||
|
@Builder |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Entity |
||||
|
@Table(name = "user") |
||||
|
public class User { |
||||
|
@JSONField(serialize = false) |
||||
|
@Id |
||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
|
@Column(name = "id", nullable = false) |
||||
|
private Long id; |
||||
|
|
||||
|
@Column(name = "uid", nullable = false, length = 16) |
||||
|
private String uid; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "pwd", nullable = false, length = 16) |
||||
|
private String pwd; |
||||
|
|
||||
|
@Column(name = "status", nullable = false) |
||||
|
private Boolean status = false; |
||||
|
|
||||
|
@Column(name = "name", nullable = false, length = 64) |
||||
|
private String name; |
||||
|
|
||||
|
@Column(name = "phone", length = 16) |
||||
|
private String phone; |
||||
|
|
||||
|
@JSONField(serializeFeatures = { JSONWriter.Feature.WriteEnumUsingToString }) |
||||
|
@Convert(converter = GenderTypeConvert.class) |
||||
|
@Column(name = "gender", columnDefinition="ENUM('男','女')") |
||||
|
private GenderType gender; |
||||
|
|
||||
|
@Column(name = "email", length = 64) |
||||
|
private String email; |
||||
|
|
||||
|
@Column(name = "address", length = 64) |
||||
|
private String address; |
||||
|
|
||||
|
@Column(name = "flag") |
||||
|
private Boolean flag; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "created") |
||||
|
private Instant created; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "modify") |
||||
|
private Instant modify; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,41 @@ |
|||||
|
package com.mathvision.diet.entity; |
||||
|
|
||||
|
import com.alibaba.fastjson2.JSONWriter; |
||||
|
import com.alibaba.fastjson2.annotation.JSONField; |
||||
|
import com.mathvision.diet.domian.ClientType; |
||||
|
import lombok.*; |
||||
|
|
||||
|
import javax.persistence.*; |
||||
|
import java.time.Instant; |
||||
|
|
||||
|
@Builder |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Entity |
||||
|
@Table(name = "user_log") |
||||
|
public class UserLog { |
||||
|
@Id |
||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
|
@Column(name = "id", nullable = false) |
||||
|
private Long id; |
||||
|
|
||||
|
@Column(name = "uid", nullable = false, length = 18) |
||||
|
private String uid; |
||||
|
|
||||
|
@JSONField(serializeFeatures = { JSONWriter.Feature.WriteEnumsUsingName }) |
||||
|
@Enumerated(EnumType.STRING) |
||||
|
@Column(name = "client_type", columnDefinition="ENUM('web','android','ios')", nullable = false) |
||||
|
private ClientType clientType; |
||||
|
|
||||
|
@Column(name = "client_version", nullable = false) |
||||
|
private String clientVersion; |
||||
|
|
||||
|
@Column(name = "login", nullable = false) |
||||
|
private Instant login; |
||||
|
|
||||
|
@Column(name = "logout") |
||||
|
private Instant logout; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,47 @@ |
|||||
|
package com.mathvision.diet.entity; |
||||
|
|
||||
|
import com.alibaba.fastjson2.JSONWriter; |
||||
|
import com.alibaba.fastjson2.annotation.JSONField; |
||||
|
import com.mathvision.diet.domian.MessageType; |
||||
|
import lombok.*; |
||||
|
|
||||
|
import javax.persistence.*; |
||||
|
import java.time.Instant; |
||||
|
|
||||
|
@Builder |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Entity |
||||
|
@Table(name = "user_message") |
||||
|
public class UserMessage { |
||||
|
@Id |
||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
|
@Column(name = "id", nullable = false) |
||||
|
private Long id; |
||||
|
|
||||
|
@Column(name = "uid", nullable = false, length = 45) |
||||
|
private String uid; |
||||
|
|
||||
|
@JSONField(serializeFeatures = { JSONWriter.Feature.WriteEnumsUsingName }) |
||||
|
@Enumerated(EnumType.STRING) |
||||
|
@Column(name = "type", columnDefinition="ENUM('code','notify','message')", nullable = false) |
||||
|
private MessageType type; |
||||
|
|
||||
|
@Column(name = "content", nullable = false, length = 128) |
||||
|
private String content; |
||||
|
|
||||
|
@Column(name = "status", nullable = false) |
||||
|
private Boolean status = false; |
||||
|
|
||||
|
@Column(name = "operate", length = 18) |
||||
|
private String operate; |
||||
|
|
||||
|
@Column(name = "created") |
||||
|
private Instant created; |
||||
|
|
||||
|
@Column(name = "modify") |
||||
|
private Instant modify; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,44 @@ |
|||||
|
package com.mathvision.diet.entity; |
||||
|
|
||||
|
import com.alibaba.fastjson2.annotation.JSONField; |
||||
|
import lombok.*; |
||||
|
|
||||
|
import javax.persistence.*; |
||||
|
import java.time.Instant; |
||||
|
|
||||
|
@Builder |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Entity |
||||
|
@Table(name = "user_role") |
||||
|
public class UserRole { |
||||
|
@JSONField(serialize = false) |
||||
|
@Id |
||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
|
@Column(name = "id", nullable = false) |
||||
|
private Long id; |
||||
|
|
||||
|
@Column(name = "uid", nullable = false, length = 18) |
||||
|
private String uid; |
||||
|
|
||||
|
@Column(name = "role_id", columnDefinition = "int UNSIGNED not null") |
||||
|
private Long roleId; |
||||
|
|
||||
|
@Column(name = "vender", nullable = false) |
||||
|
private Long vender; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "operate", length = 45) |
||||
|
private String operate; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "created") |
||||
|
private Instant created; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "modify") |
||||
|
private Instant modify; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,50 @@ |
|||||
|
package com.mathvision.diet.entity; |
||||
|
|
||||
|
import com.alibaba.fastjson2.JSONWriter; |
||||
|
import com.alibaba.fastjson2.annotation.JSONField; |
||||
|
import com.mathvision.diet.domian.ClientType; |
||||
|
import lombok.*; |
||||
|
|
||||
|
import javax.persistence.*; |
||||
|
import java.time.Instant; |
||||
|
|
||||
|
@Builder |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Entity |
||||
|
@Table(name = "user_session") |
||||
|
public class UserSession { |
||||
|
@JSONField(serialize = false) |
||||
|
@Id |
||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
|
@Column(name = "id", nullable = false) |
||||
|
private Long id; |
||||
|
|
||||
|
@Column(name = "uid", nullable = false, length = 18) |
||||
|
private String uid; |
||||
|
|
||||
|
@Column(name = "vender", nullable = false, length = 64) |
||||
|
private String vender; |
||||
|
|
||||
|
@JSONField(serializeFeatures = { JSONWriter.Feature.WriteEnumsUsingName }) |
||||
|
@Enumerated(EnumType.STRING) |
||||
|
@Column(name = "client_type", columnDefinition="ENUM('web','android','ios')", nullable = false) |
||||
|
private ClientType clientType; |
||||
|
|
||||
|
@Column(name = "client_version", nullable = false, length = 45) |
||||
|
private String clientVersion; |
||||
|
|
||||
|
@Column(name = "expired_time", nullable = false) |
||||
|
private Long expiredTime; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "created") |
||||
|
private Instant created; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "modify") |
||||
|
private Instant modify; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,90 @@ |
|||||
|
package com.mathvision.diet.entity; |
||||
|
|
||||
|
import com.alibaba.fastjson2.JSONWriter; |
||||
|
import com.alibaba.fastjson2.annotation.JSONField; |
||||
|
import com.mathvision.diet.convert.VenderTypeConvert; |
||||
|
import com.mathvision.diet.domian.VenderType; |
||||
|
import lombok.*; |
||||
|
|
||||
|
import javax.persistence.*; |
||||
|
import java.time.Instant; |
||||
|
|
||||
|
@Builder |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Entity |
||||
|
@Table(name = "vender") |
||||
|
public class Vender { |
||||
|
|
||||
|
public Vender (Long id, String name, String account, VenderType category) { |
||||
|
setId(id); |
||||
|
setAccount(account); |
||||
|
setName(name); |
||||
|
setCategory(category); |
||||
|
} |
||||
|
|
||||
|
@Id |
||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
|
@Column(name = "id", nullable = false) |
||||
|
private Long id; |
||||
|
|
||||
|
@Column(name = "account", length = 16, nullable = false) |
||||
|
private String account; |
||||
|
|
||||
|
@Column(name = "name", nullable = false, length = 64) |
||||
|
private String name; |
||||
|
|
||||
|
@JSONField(serializeFeatures = { JSONWriter.Feature.WriteEnumUsingToString }) |
||||
|
@Convert(converter = VenderTypeConvert.class) |
||||
|
@Column(name = "category", columnDefinition="ENUM('学校','医院','事业单位','其他')") |
||||
|
private VenderType category; |
||||
|
|
||||
|
@Column(name = "status", nullable = false) |
||||
|
private Boolean status = false; |
||||
|
|
||||
|
@Lob |
||||
|
@Column(name = "icon") |
||||
|
private String icon; |
||||
|
|
||||
|
@Column(name = "url", length = 64) |
||||
|
private String url; |
||||
|
|
||||
|
@Column(name = "province", length = 20) |
||||
|
private String province; |
||||
|
|
||||
|
@Column(name = "city", length = 20) |
||||
|
private String city; |
||||
|
|
||||
|
@Column(name = "area", length = 20) |
||||
|
private String area; |
||||
|
|
||||
|
@Column(name = "address", length = 64) |
||||
|
private String address; |
||||
|
|
||||
|
@Column(name = "phone", length = 16) |
||||
|
private String phone; |
||||
|
|
||||
|
@Column(name = "contacts", length = 16) |
||||
|
private String contacts; |
||||
|
|
||||
|
@Column(name = "email", length = 32) |
||||
|
private String email; |
||||
|
|
||||
|
@Column(name = "expire") |
||||
|
private Instant expire; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "operate", length = 18) |
||||
|
private String operate; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "created") |
||||
|
private Instant created; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "modify") |
||||
|
private Instant modify; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,51 @@ |
|||||
|
package com.mathvision.diet.entity; |
||||
|
|
||||
|
import com.alibaba.fastjson2.annotation.JSONField; |
||||
|
import lombok.*; |
||||
|
|
||||
|
import javax.persistence.*; |
||||
|
import java.math.BigDecimal; |
||||
|
import java.time.Instant; |
||||
|
|
||||
|
@Builder |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Entity |
||||
|
@Table(name = "vender_config") |
||||
|
public class VenderConfig { |
||||
|
@JSONField(serialize = false) |
||||
|
@Id |
||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
|
@Column(name = "id", nullable = false) |
||||
|
private Long id; |
||||
|
|
||||
|
@Column(name = "vender", nullable = false) |
||||
|
private Long vender; |
||||
|
|
||||
|
@JSONField(format = "#0.00") |
||||
|
@Column(name = "breakfast", precision = 5, scale = 2) |
||||
|
private BigDecimal breakfast; |
||||
|
|
||||
|
@JSONField(format = "#0.00") |
||||
|
@Column(name = "lunch", precision = 5, scale = 2) |
||||
|
private BigDecimal lunch; |
||||
|
|
||||
|
@JSONField(format = "#0.00") |
||||
|
@Column(name = "dinner", precision = 5, scale = 2) |
||||
|
private BigDecimal dinner; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "operate", length = 45) |
||||
|
private String operate; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "created") |
||||
|
private Instant created; |
||||
|
|
||||
|
@JSONField(serialize = false) |
||||
|
@Column(name = "modify") |
||||
|
private Instant modify; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,26 @@ |
|||||
|
package com.mathvision.diet.repository; |
||||
|
|
||||
|
import com.mathvision.diet.entity.Dish; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
import org.springframework.data.jpa.repository.Query; |
||||
|
|
||||
|
import java.util.Collection; |
||||
|
import java.util.List; |
||||
|
|
||||
|
public interface DishRepository extends JpaRepository<Dish, Long>, JpaSpecificationExecutor<Dish> { |
||||
|
long deleteByVender(Long vender); |
||||
|
@Query("select new Dish(d.id, d.name, d.marks, d.ingredient) from Dish d where d.name like ?1 order by d.id DESC") |
||||
|
List<Dish> findByNameLikeOrderByIdDesc(String name); |
||||
|
boolean existsByVenderAndNameAndIdNot(Long vender, String name, Long id); |
||||
|
boolean existsByVenderAndIdIn(Long vender, Collection<Long> ids); |
||||
|
long deleteByIdInAndVender(Collection<Long> ids, Long vender); |
||||
|
@Query("select d from Dish d where d.vender = ?1 and d.id in ?2") |
||||
|
List<Dish> findByVenderAndIdIn(Long vender, Collection<Long> ids); |
||||
|
@Query("select d from Dish d where d.id in ?1") |
||||
|
List<Dish> findByIdIn(Collection<Long> ids); |
||||
|
@Query("select d from Dish d where d.vender = ?1") |
||||
|
List<Dish> findByVender(Long vender); |
||||
|
Dish findByIdAndVender(Long id, Long vender); |
||||
|
boolean existsByVenderAndName(Long vender, String name); |
||||
|
} |
||||
@ -0,0 +1,8 @@ |
|||||
|
package com.mathvision.diet.repository; |
||||
|
|
||||
|
import com.mathvision.diet.entity.FoodCategory; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
|
||||
|
public interface FoodCategoryRepository extends JpaRepository<FoodCategory, Long>, JpaSpecificationExecutor<FoodCategory> { |
||||
|
} |
||||
@ -0,0 +1,8 @@ |
|||||
|
package com.mathvision.diet.repository; |
||||
|
|
||||
|
import com.mathvision.diet.entity.FoodMark; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
|
||||
|
public interface FoodMarkRepository extends JpaRepository<FoodMark, Long>, JpaSpecificationExecutor<FoodMark> { |
||||
|
} |
||||
@ -0,0 +1,8 @@ |
|||||
|
package com.mathvision.diet.repository; |
||||
|
|
||||
|
import com.mathvision.diet.entity.FoodNutrient; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
|
||||
|
public interface FoodNutrientRepository extends JpaRepository<FoodNutrient, Long>, JpaSpecificationExecutor<FoodNutrient> { |
||||
|
} |
||||
@ -0,0 +1,16 @@ |
|||||
|
package com.mathvision.diet.repository; |
||||
|
|
||||
|
import com.mathvision.diet.domian.IngredientDTO; |
||||
|
import com.mathvision.diet.entity.IngredientMark; |
||||
|
import org.springframework.data.domain.Page; |
||||
|
import org.springframework.data.domain.Pageable; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
import org.springframework.data.jpa.repository.Query; |
||||
|
|
||||
|
public interface IngredientDTORepository extends JpaRepository<IngredientDTO, String> , JpaSpecificationExecutor<IngredientMark> { |
||||
|
@Query(countQuery = "SELECT count(1) FROM ingredient a left join (select ingredient as `key`, mark from ingredient_mark where vender = ?1) b on a.key = b.key where 1=1 and if(IFNULL(?2,'') ='', 1=1, a.type = ?2) and if(IFNULL(?3,'') ='', 1=1, b.mark = ?3) and if(IFNULL(?4,'') ='', 1=1, (a.key like concat(?4, '%') or a.name like concat(?4, '%') ))", |
||||
|
value = "SELECT a.key, a.name, a.type, a.nutrient, b.mark, a.modify FROM ingredient a left join (select ingredient as `key`, mark from ingredient_mark where vender = ?1) b on a.key = b.key where 1=1 and if(IFNULL(?2,'') ='', 1=1, a.type = ?2) and if(IFNULL(?3,'') ='', 1=1, b.mark = ?3) and if(IFNULL(?4,'') ='', 1=1, (a.key like concat(?4, '%') or a.name like concat(?4, '%') )) order by a.id DESC", nativeQuery = true) |
||||
|
Page<IngredientDTO> findByVenderAndTypeAndMarkAndKey(Long vender, String type, String mark, String key, Pageable pageable); |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,20 @@ |
|||||
|
package com.mathvision.diet.repository; |
||||
|
|
||||
|
import com.mathvision.diet.domian.MarkType; |
||||
|
import com.mathvision.diet.entity.IngredientMark; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
import org.springframework.data.jpa.repository.Modifying; |
||||
|
import org.springframework.data.jpa.repository.Query; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
|
||||
|
public interface IngredientMarkRepository extends JpaRepository<IngredientMark, Long>, JpaSpecificationExecutor<IngredientMark> { |
||||
|
long deleteByIngredient(String ingredient); |
||||
|
long deleteByVender(Long vender); |
||||
|
@Transactional |
||||
|
@Modifying |
||||
|
@Query("update IngredientMark i set i.mark = ?1, i.operate = ?2 where i.vender = ?3 and i.ingredient = ?4") |
||||
|
int updateMarkAndOperateByVenderAndIngredient(MarkType mark, String operate, Long vender, String ingredient); |
||||
|
@Transactional |
||||
|
long deleteByVenderAndIngredient(Long vender, String ingredient); |
||||
|
} |
||||
@ -0,0 +1,42 @@ |
|||||
|
package com.mathvision.diet.repository; |
||||
|
|
||||
|
import com.mathvision.diet.entity.Ingredient; |
||||
|
import org.springframework.data.domain.Page; |
||||
|
import org.springframework.data.domain.Pageable; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
import org.springframework.data.jpa.repository.Query; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
|
||||
|
import java.util.Collection; |
||||
|
import java.util.List; |
||||
|
|
||||
|
public interface IngredientRepository extends JpaRepository<Ingredient, Long>, JpaSpecificationExecutor<Ingredient> { |
||||
|
@Query("select new Ingredient(i.key,i.name,i.type) from Ingredient i where i.key like concat(?1, '%') or i.name like concat(?1, '%') order by i.key") |
||||
|
List<Ingredient> findByKeyStartsWithOrNameStartsWithOrderByKeyAsc(String keyword); |
||||
|
@Query("select new Ingredient(i.key,i.name,i.type) from Ingredient i where i.key in ?1 order by i.key") |
||||
|
List<Ingredient> findByKeyInOrderByKeyAsc(Collection<String> keys); |
||||
|
@Query("select new Ingredient(i.key,i.name) from Ingredient i") |
||||
|
List<Ingredient> findKeyNameMap(); |
||||
|
|
||||
|
@Query("select i from Ingredient i where i.key in ?1") |
||||
|
List<Ingredient> findByKeyIn(Collection<String> keys); |
||||
|
boolean existsByKey(String key); |
||||
|
@Transactional |
||||
|
long deleteByKey(String key); |
||||
|
Ingredient findByKey(String key); |
||||
|
|
||||
|
Page<Ingredient> findByTypeOrderByIdDesc(String type, Pageable pageable); |
||||
|
|
||||
|
Page<Ingredient> findByKeyStartsWithOrderByIdDesc(String key, Pageable pageable); |
||||
|
|
||||
|
Page<Ingredient> findByNameStartsWithOrderByIdDesc(String name, Pageable pageable); |
||||
|
|
||||
|
Page<Ingredient> findByTypeAndKeyStartsWithOrderByIdDesc(String type, String key, Pageable pageable); |
||||
|
|
||||
|
Page<Ingredient> findByTypeAndNameStartsWithOrderByIdDesc(String type, String name, Pageable pageable); |
||||
|
|
||||
|
@Query("select (count(i) > 0) from Ingredient i where i.id <> ?1 and (i.key = ?2 or i.name = ?3)") |
||||
|
boolean existsByIdNotAndKeyOrName(Long id, String key, String name); |
||||
|
boolean existsByKeyOrName(String key, String name); |
||||
|
} |
||||
@ -0,0 +1,15 @@ |
|||||
|
package com.mathvision.diet.repository; |
||||
|
|
||||
|
import com.mathvision.diet.entity.MenuApprove; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
import org.springframework.data.jpa.repository.Modifying; |
||||
|
import org.springframework.data.jpa.repository.Query; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
|
||||
|
public interface MenuApproveRepository extends JpaRepository<MenuApprove, Long>, JpaSpecificationExecutor<MenuApprove> { |
||||
|
@Transactional |
||||
|
@Modifying |
||||
|
@Query("delete from MenuApprove m where DATEDIFF(NOW(), m.modify) > 30") |
||||
|
void scanExpired(); |
||||
|
} |
||||
@ -0,0 +1,20 @@ |
|||||
|
package com.mathvision.diet.repository; |
||||
|
|
||||
|
import com.mathvision.diet.entity.MenuDish; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
|
||||
|
import java.util.Collection; |
||||
|
import java.util.List; |
||||
|
|
||||
|
public interface MenuDishRepository extends JpaRepository<MenuDish, Long>, JpaSpecificationExecutor<MenuDish> { |
||||
|
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> findByMenuAndVender(Long menu, Long vender); |
||||
|
MenuDish findByIdAndVender(Long id, Long vender); |
||||
|
List<MenuDish> findByMenu(Long menu); |
||||
|
long deleteByMenuAndDayGreaterThanAndMealNotIn(Long menu, Long day, Collection<String> meals); |
||||
|
long deleteByMenuAndVender(Long menu, Long vender); |
||||
|
long deleteByMenu(Long menu); |
||||
|
} |
||||
@ -0,0 +1,53 @@ |
|||||
|
package com.mathvision.diet.repository; |
||||
|
|
||||
|
import com.mathvision.diet.domian.MenuCountDTO; |
||||
|
import com.mathvision.diet.domian.MenuStatus; |
||||
|
import com.mathvision.diet.entity.Menu; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
import org.springframework.data.jpa.repository.Modifying; |
||||
|
import org.springframework.data.jpa.repository.Query; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
|
||||
|
import java.time.Instant; |
||||
|
import java.util.Collection; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
public interface MenuRepository extends JpaRepository<Menu, Long>, JpaSpecificationExecutor<Menu> { |
||||
|
List<Menu> findByNutrient(Long nutrient); |
||||
|
long deleteByVender(Long vender); |
||||
|
@Query(value = "select m.id from menu m " + |
||||
|
"where m.vender = ?1 and m.status = ?2 and (WEEKDAY(NOW()) + 1) <= m.day and now() between m.start_time and m.end_time " + |
||||
|
"order by m.id DESC " + |
||||
|
"limit 1", nativeQuery = true) |
||||
|
Long findCurrentMenu(Long vender, int 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); |
||||
|
@Query("select count(m) from Menu m where m.vender = ?1") |
||||
|
long countByVender(Long vender); |
||||
|
@Transactional |
||||
|
@Modifying |
||||
|
@Query("update Menu m set m.status = ?1, m.scale = ?2, m.startTime = ?3, m.endTime = ?4 where m.id = ?5") |
||||
|
int updateStatusAndScaleAndStartTimeAndEndTimeById(MenuStatus status, Map<String, Integer> scale, Instant startTime, Instant endTime, Long id); |
||||
|
@Transactional |
||||
|
@Modifying |
||||
|
@Query("update Menu m set m.status = ?1, m.approve = ?2, m.operate = ?3 where m.id = ?4") |
||||
|
int updateStatusAndApproveAndOperateById(MenuStatus status, String approve, String operate, Long id); |
||||
|
@Transactional |
||||
|
@Modifying |
||||
|
@Query("update Menu m set m.status = ?1, m.operate = ?2 where m.id = ?3") |
||||
|
int updateStatusAndOperateById(MenuStatus status, String operate, Long id); |
||||
|
@Transactional |
||||
|
@Modifying |
||||
|
@Query("update Menu m set m.status = ?1, m.startTime = ?2, m.endTime = ?3, m.operate = ?4 where m.id = ?5") |
||||
|
int updateStatusAndStartTimeAndEndTimeAndOperateById(MenuStatus status, Instant startTime, Instant endTime, String operate, Long id); |
||||
|
long deleteByIdAndVender(Long id, Long vender); |
||||
|
|
||||
|
Menu findByIdAndVender(Long id, Long vender); |
||||
|
|
||||
|
@Transactional |
||||
|
@Modifying |
||||
|
@Query(value = "update menu v set v.status = 2 where v.status = 5 and v.end_time < now()", nativeQuery = true) |
||||
|
void scanExpired(); |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
package com.mathvision.diet.repository; |
||||
|
|
||||
|
import com.mathvision.diet.entity.Nutrition; |
||||
|
import org.springframework.data.domain.Page; |
||||
|
import org.springframework.data.domain.Pageable; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
|
||||
|
public interface NutritionRepository extends JpaRepository<Nutrition, Long>, JpaSpecificationExecutor<Nutrition> { |
||||
|
boolean existsByName(String name); |
||||
|
Page<Nutrition> findByNameStartsWithOrderByIdDesc(String name, Pageable pageable); |
||||
|
Nutrition findByName(String name); |
||||
|
} |
||||
@ -0,0 +1,8 @@ |
|||||
|
package com.mathvision.diet.repository; |
||||
|
|
||||
|
import com.mathvision.diet.entity.RoleItem; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
|
||||
|
public interface RoleItemRepository extends JpaRepository<RoleItem, Long>, JpaSpecificationExecutor<RoleItem> { |
||||
|
} |
||||
@ -0,0 +1,35 @@ |
|||||
|
package com.mathvision.diet.repository; |
||||
|
|
||||
|
import com.mathvision.diet.domian.RoleType; |
||||
|
import com.mathvision.diet.entity.Role; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
import org.springframework.data.jpa.repository.Modifying; |
||||
|
import org.springframework.data.jpa.repository.Query; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
public interface RoleRepository extends JpaRepository<Role, Long>, JpaSpecificationExecutor<Role> { |
||||
|
@Transactional |
||||
|
long deleteByVender(Long vender); |
||||
|
|
||||
|
boolean existsByIdAndVender(Long id, Long vender); |
||||
|
|
||||
|
@Transactional |
||||
|
@Modifying |
||||
|
@Query("update Role r set r.roleItems = ?1, r.operate = ?2 where r.id = ?3 and r.vender = ?4 and r.roleType != '系统'") |
||||
|
int updateRoleItemsAndOperateByIdAndVenderAndRoleTypeNot(String roleItems, String operate, Long id, Long vender); |
||||
|
|
||||
|
@Transactional |
||||
|
@Modifying |
||||
|
@Query("update Role r set r.roleName = ?1, r.operate = ?2 where r.id = ?3 and r.vender = ?4 and r.roleType != '系统'") |
||||
|
int updateRoleNameAndOperateByIdAndVenderAndRoleTypeNot(String roleName, String operate, Long id, Long vender); |
||||
|
|
||||
|
@Query("DELETE FROM Role d WHERE d.id = ?1 AND d.vender = ?2 AND ?3 != d.roleType") |
||||
|
long deleteByIdAndVenderAndRoleTypeNot(Long id, Long vender, RoleType roleType); |
||||
|
List<Role> findByVender(Long vender); |
||||
|
|
||||
|
@Query("SELECT d FROM Role d WHERE d.vender = ?1 AND ?2= d.roleType") |
||||
|
Role findByVenderForDefaultRole(Long vender, RoleType roleType); |
||||
|
} |
||||
@ -0,0 +1,23 @@ |
|||||
|
package com.mathvision.diet.repository; |
||||
|
|
||||
|
import com.mathvision.diet.entity.UserLog; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
import org.springframework.data.jpa.repository.Modifying; |
||||
|
import org.springframework.data.jpa.repository.Query; |
||||
|
import org.springframework.lang.NonNull; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
|
||||
|
import java.time.Instant; |
||||
|
|
||||
|
public interface UserLogRepository extends JpaRepository<UserLog, Long>, JpaSpecificationExecutor<UserLog> { |
||||
|
@Transactional |
||||
|
@Modifying |
||||
|
@Query(value = "update user_log u set u.logout = ?1 where u.uid = ?2 order by u.id desc limit 1", nativeQuery = true) |
||||
|
void updateLogoutByUid(@NonNull Instant logout, @NonNull String uid); |
||||
|
|
||||
|
@Transactional |
||||
|
@Modifying |
||||
|
@Query("delete from UserLog m where DATEDIFF(NOW(), m.login) > 30") |
||||
|
void scanExpired(); |
||||
|
} |
||||
@ -0,0 +1,16 @@ |
|||||
|
package com.mathvision.diet.repository; |
||||
|
|
||||
|
import com.mathvision.diet.entity.UserMessage; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
import org.springframework.data.jpa.repository.Modifying; |
||||
|
import org.springframework.data.jpa.repository.Query; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
|
||||
|
public interface UserMessageRepository extends JpaRepository<UserMessage, Long>, JpaSpecificationExecutor<UserMessage> { |
||||
|
|
||||
|
@Transactional |
||||
|
@Modifying |
||||
|
@Query("delete from UserMessage m where DATEDIFF(NOW(), m.modify) > 30") |
||||
|
void scanExpired(); |
||||
|
} |
||||
@ -0,0 +1,30 @@ |
|||||
|
package com.mathvision.diet.repository; |
||||
|
|
||||
|
import com.mathvision.diet.entity.User; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
import org.springframework.data.jpa.repository.Modifying; |
||||
|
import org.springframework.data.jpa.repository.Query; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
|
||||
|
import java.util.Collection; |
||||
|
import java.util.List; |
||||
|
|
||||
|
public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> { |
||||
|
@Transactional |
||||
|
long deleteByUidIn(Collection<String> uid); |
||||
|
List<User> findByUidIn(Collection<String> uid); |
||||
|
boolean existsByUidAndPwdAndStatus(String uid, String pwd, Boolean status); |
||||
|
@Transactional |
||||
|
@Modifying |
||||
|
@Query("update User u set u.pwd = ?1 where u.uid = ?2") |
||||
|
int updatePwdByUid(String pwd, String uid); |
||||
|
@Transactional |
||||
|
@Modifying |
||||
|
@Query("update User u set u.name = ?1 where u.uid = ?2") |
||||
|
int updateNameByUid(String name, String uid); |
||||
|
@Transactional |
||||
|
long deleteByUid(String uid); |
||||
|
boolean existsByUid(String uid); |
||||
|
User findByUidAndStatus(String uid, Boolean status); |
||||
|
} |
||||
@ -0,0 +1,26 @@ |
|||||
|
package com.mathvision.diet.repository; |
||||
|
|
||||
|
import com.mathvision.diet.entity.UserRole; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
import org.springframework.data.jpa.repository.Modifying; |
||||
|
import org.springframework.data.jpa.repository.Query; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
public interface UserRoleRepository extends JpaRepository<UserRole, Long>, JpaSpecificationExecutor<UserRole> { |
||||
|
@Transactional |
||||
|
long deleteByVender(Long vender); |
||||
|
@Transactional |
||||
|
long deleteByUidAndVender(String uid, Long vender); |
||||
|
long countByRoleIdAndVenderAndUidNot(Long roleId, Long vender, String uid); |
||||
|
boolean existsByUidAndVender(String uid, Long vender); |
||||
|
@Transactional |
||||
|
@Modifying |
||||
|
@Query("update UserRole u set u.roleId = ?1, u.operate = ?2 where u.uid = ?3 and u.vender = ?4") |
||||
|
int updateRoleIdAndOperateByUidAndVender(Long roleId, String operate, String uid, Long vender); |
||||
|
boolean existsByRoleIdAndVender(Long roleId, Long vender); |
||||
|
List<UserRole> findByVender(Long vender); |
||||
|
UserRole findByUid(String uid); |
||||
|
} |
||||
@ -0,0 +1,16 @@ |
|||||
|
package com.mathvision.diet.repository; |
||||
|
|
||||
|
import com.mathvision.diet.entity.UserSession; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
import org.springframework.data.jpa.repository.Modifying; |
||||
|
import org.springframework.data.jpa.repository.Query; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
|
||||
|
public interface UserSessionRepository extends JpaRepository<UserSession, Long>, JpaSpecificationExecutor<UserSession> { |
||||
|
|
||||
|
@Transactional |
||||
|
@Modifying |
||||
|
@Query("delete from UserSession m where DATEDIFF(NOW(), m.modify) > 30") |
||||
|
void scanExpired(); |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
package com.mathvision.diet.repository; |
||||
|
|
||||
|
import com.mathvision.diet.entity.VenderConfig; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
import org.springframework.data.jpa.repository.Modifying; |
||||
|
import org.springframework.data.jpa.repository.Query; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
|
||||
|
import java.math.BigDecimal; |
||||
|
|
||||
|
public interface VenderConfigRepository extends JpaRepository<VenderConfig, Long>, JpaSpecificationExecutor<VenderConfig> { |
||||
|
@Transactional |
||||
|
@Modifying |
||||
|
@Query("update VenderConfig v set v.breakfast = ?1, v.lunch = ?2, v.dinner = ?3, v.operate = ?4 where v.vender = ?5") |
||||
|
int updateBreakfastAndLunchAndDinnerAndOperateByVender(BigDecimal breakfast, BigDecimal lunch, BigDecimal dinner, String operate, Long vender); |
||||
|
long deleteByVender(Long vender); |
||||
|
VenderConfig findByVender(Long vender); |
||||
|
} |
||||
@ -0,0 +1,33 @@ |
|||||
|
package com.mathvision.diet.repository; |
||||
|
|
||||
|
import com.mathvision.diet.domian.VenderType; |
||||
|
import com.mathvision.diet.entity.Vender; |
||||
|
import org.springframework.data.domain.Page; |
||||
|
import org.springframework.data.domain.PageRequest; |
||||
|
import org.springframework.data.domain.Pageable; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
import org.springframework.data.jpa.repository.Modifying; |
||||
|
import org.springframework.data.jpa.repository.Query; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
|
||||
|
import java.util.Collection; |
||||
|
import java.util.List; |
||||
|
|
||||
|
public interface VenderRepository extends JpaRepository<Vender, Long>, JpaSpecificationExecutor<Vender> { |
||||
|
Page<Vender> findByCategoryAndNameLikeOrderByIdDesc(VenderType category, String name, Pageable pageable); |
||||
|
Page<Vender> findByCategoryOrderByIdDesc(VenderType category, Pageable pageable); |
||||
|
@Query("select new Vender(v.id, v.name, v.account, v.category) from Vender v where v.id in ?1") |
||||
|
List<Vender> findByIdIn(Collection<Long> ids); |
||||
|
@Query("select new Vender(v.id, v.name, v.account, v.category) from Vender v where v.status = ?1 and v.name like concat(?2, '%') order by v.name DESC") |
||||
|
List<Vender> findByStatusAndNameLikeOrderByNameDesc(Boolean status, String name); |
||||
|
@Query("select new Vender(v.id, v.name, v.account, v.category) from Vender v where v.status = ?1 order by v.name") |
||||
|
List<Vender> findByStatusOrderByNameAsc(Boolean status); |
||||
|
Page<Vender> findByNameLikeOrderByIdDesc(String name, PageRequest pageRequest); |
||||
|
boolean existsByName(String name); |
||||
|
@Transactional |
||||
|
@Modifying |
||||
|
@Query("update Vender v set v.status = false where v.status = true and v.expire < now()") |
||||
|
void scanExpired(); |
||||
|
Vender findByIdAndStatus(Long vender, boolean status); |
||||
|
} |
||||
@ -0,0 +1,65 @@ |
|||||
|
package com.mathvision.diet.utils; |
||||
|
|
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
|
||||
|
import java.io.ByteArrayInputStream; |
||||
|
import java.io.ByteArrayOutputStream; |
||||
|
import java.util.zip.GZIPInputStream; |
||||
|
import java.util.zip.GZIPOutputStream; |
||||
|
|
||||
|
@Slf4j |
||||
|
public class GZIPUtils { |
||||
|
|
||||
|
public static final String GZIP_ENCODE_UTF_8 = "UTF-8"; |
||||
|
|
||||
|
public static String compress(String str) { |
||||
|
return compress(str, GZIP_ENCODE_UTF_8); |
||||
|
} |
||||
|
|
||||
|
public static String compress(String str, String encoding) { |
||||
|
if (str == null || str.length() == 0) { |
||||
|
return null; |
||||
|
} |
||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
||||
|
GZIPOutputStream gzip; |
||||
|
try { |
||||
|
gzip = new GZIPOutputStream(out); |
||||
|
gzip.write(str.getBytes(encoding)); |
||||
|
gzip.close(); |
||||
|
} catch (Exception e) { |
||||
|
log.error("gzip compress error.", e); |
||||
|
} |
||||
|
return out.toString(); |
||||
|
} |
||||
|
|
||||
|
public static String decompress(String str) { |
||||
|
return decompressToString(str, GZIP_ENCODE_UTF_8); |
||||
|
} |
||||
|
|
||||
|
public static String decompressToString(String str, String encoding) { |
||||
|
if (str == null || str.length() == 0) { |
||||
|
return null; |
||||
|
} |
||||
|
try { |
||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
||||
|
ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes(encoding)); |
||||
|
GZIPInputStream unGzip = new GZIPInputStream(in); |
||||
|
byte[] buffer = new byte[256]; |
||||
|
int n; |
||||
|
while ((n = unGzip.read(buffer)) >= 0) { |
||||
|
out.write(buffer, 0, n); |
||||
|
} |
||||
|
return out.toString(encoding); |
||||
|
} catch (Exception e) { |
||||
|
log.error("gzip decompressToString to string error.", e); |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
String str ="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD//gAXR2VuZXJhdGVkIGJ5IFNuaXBhc3Rl/9sAhAAKBwcIBwYKCAgICwoKCw4YEA4NDQ4dFRYRGCMfJSQiHyIhJis3LyYpNCkhIjBBMTQ5Oz4+PiUuRElDPEg3PT47AQoLCw4NDhwQEBw7KCIoOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozv/wAARCABAAF0DAREAAhEBAxEB/8QBogAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoLEAACAQMDAgQDBQUEBAAAAX0BAgMABBEFEiExQQYTUWEHInEUMoGRoQgjQrHBFVLR8CQzYnKCCQoWFxgZGiUmJygpKjQ1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4eLj5OXm5+jp6vHy8/T19vf4+foBAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKCxEAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwCjXjnzAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQBe0/RNT1SNpbK0eZEO0sCAM+nNXGnKWyNYUalRXiiWLw5q811NbR2TNNBt81Qy/LnkZ5pqlNu1ilQqNuKWqHy+FdcgieWTTpAiAsxDKcAewNDozSvYbw1VK7iZNZnOWr3TL3T5I47u3aJpVDIDzuB+lVKEo7mk6c4NKSGXthdadP5F5A8MmM7WHUetKUXF2YpwlB2krEG0kE4OB1NIkkjtriWJ5Y4JHjj5d1QkL9T2ppN6jUW1dIYil3VF6scDnFISVya7srixdEuY/LZ0DryDlT0PH0puLjuVKEoaMgpEBQB6R8Ov8AkX5/+vpv/QErvw3wP1PYwH8J+v8AkNa+/wCEZ8U3kl+rLY6iVdJwMhWA6HH1P6Uc3s6j5tmHP7Cs3LaRpXWv2V7aTW+nTC6meJs7ASsYwcsx6D6dzxVupFq0dTaVeEk1B3ZyPgC2WbVblzMYykHQBTuBYZ6g8cCuXDq8mefgo3m9eh288V1f2sM3lvbvgNsSVdw9ASVP6frXY05K56bUppPb+vQpXxOoTzaZcvLC0lmzq0cisGAOGB+Xr0qJe8+V9jKfvtwlpoYHhbW9Ei8PTaXqMggLlt+VJ8wN3yB1HT8BWNKpBQ5ZHLhq1JUnCeh0Gj2mlp4durSxNxcWjeYrkoQ75HOOBnjjNbwUeRqOx10o01ScY3aOL16xi03T44V0Oa13yZS7nnDO4HYqOB9K5KkVFW5bHm1oKEbclvNs54knqSawOQKACgDqvCmr6tBYyadpGnLcSNMZGlkJ2oCAOenp6100ZzS5Yo78NVqKPJTjc6YWviyaMi5k0mRW6wyIxX8eP8a6LVXvY7OXENe9Yl+0/wBn6fPbXmmxWAaNgslvgwscdMgAqfqPxp35VZqxXNyRalG3psed6DcwWmrRy3CvJFgqY1UnfnjGARn9foa4KbSlqeRRkozTZ6Vd6pY6Vahb54YoGQgQlclh2ATHT6gD2r0HOMVqezKpCmve2/rocre6sus2M2racGtdRtNyOsY5a3JwCfpkfTn2rmlPnXNHRr8jhnV9pF1IaSX5GfpPitdG01Le30q3a5Ukm4c5LZOegGfQde1RCtyRslqY0sT7OFlHXuaVr4s1W80LWJ5JMSwrH5TRx4Cbmwefp0zWirTcJM3jiakqc2+ljkbi7ubtt1zcSzEd5HLfzrlbb3PPlKUt3cipEhQAUAa+j+J9R0O3e3tPKMbvvIkTODjHr7CtYVZQVkdFLETpK0TQ/wCFg61/ctf+/Z/xq/rMzb69V8iK78c6xd2slu/2dFlUqxWPnB69SaTxE2rEyxlWSaMCCeS2nSeFykkbBlYdiKwTad0cibi7oW4uJrudp7iVpZXOWdzkmm227scpOTuxI5pYd3lSvHvUq21iNwPUH2pJtbCTa2GUCLdvql3bafcWEbgQXOPMUqDkj3/CqU2k49zSNSUYuK2ZUqTMKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgD//Z"; |
||||
|
System.out.println("原数据:" + str); |
||||
|
System.out.println("压缩后数据:" + (str)); |
||||
|
System.out.println("解压缩后字符串:" + decompress(compress(str))); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,65 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
|
<modelVersion>4.0.0</modelVersion> |
||||
|
|
||||
|
<parent> |
||||
|
<groupId>com.mathvision.diet</groupId> |
||||
|
<artifactId>diet</artifactId> |
||||
|
<version>1.0-SNAPSHOT</version> |
||||
|
</parent> |
||||
|
<artifactId>diet-web</artifactId> |
||||
|
<packaging>jar</packaging> |
||||
|
|
||||
|
<properties> |
||||
|
<maven.compiler.source>8</maven.compiler.source> |
||||
|
<maven.compiler.target>8</maven.compiler.target> |
||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
||||
|
</properties> |
||||
|
|
||||
|
<dependencies> |
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-web</artifactId> |
||||
|
<exclusions> |
||||
|
<exclusion> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-json</artifactId> |
||||
|
</exclusion> |
||||
|
</exclusions> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>com.auth0</groupId> |
||||
|
<artifactId>java-jwt</artifactId> |
||||
|
<version>4.3.0</version> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>com.mathvision.diet</groupId> |
||||
|
<artifactId>diet-core</artifactId> |
||||
|
<version>1.0-SNAPSHOT</version> |
||||
|
</dependency> |
||||
|
</dependencies> |
||||
|
|
||||
|
<build> |
||||
|
<resources> |
||||
|
<resource> |
||||
|
<directory>src/main/resources</directory> |
||||
|
<includes> |
||||
|
<include>*.properties</include> |
||||
|
<include>*.xml</include> |
||||
|
<include>*.yml</include> |
||||
|
<include>**/*.so</include> |
||||
|
<include>**/*.dll</include> |
||||
|
<include>static/**</include> |
||||
|
</includes> |
||||
|
</resource> |
||||
|
</resources> |
||||
|
<plugins> |
||||
|
<plugin> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-maven-plugin</artifactId> |
||||
|
</plugin> |
||||
|
</plugins> |
||||
|
</build> |
||||
|
</project> |
||||
@ -0,0 +1,13 @@ |
|||||
|
package com.mathvision.diet; |
||||
|
|
||||
|
import org.springframework.boot.SpringApplication; |
||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication; |
||||
|
import org.springframework.scheduling.annotation.EnableScheduling; |
||||
|
|
||||
|
@EnableScheduling |
||||
|
@SpringBootApplication |
||||
|
public class Application { |
||||
|
public static void main(String[] args) { |
||||
|
SpringApplication.run(Application.class, args); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,79 @@ |
|||||
|
package com.mathvision.diet.aspect; |
||||
|
|
||||
|
import com.alibaba.fastjson2.JSON; |
||||
|
import com.mathvision.diet.domain.Result; |
||||
|
import com.mathvision.diet.exception.DietException; |
||||
|
import lombok.SneakyThrows; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.stereotype.Controller; |
||||
|
import org.springframework.web.bind.MissingPathVariableException; |
||||
|
import org.springframework.web.bind.MissingRequestCookieException; |
||||
|
import org.springframework.web.bind.MissingRequestHeaderException; |
||||
|
import org.springframework.web.bind.MissingServletRequestParameterException; |
||||
|
import org.springframework.web.bind.annotation.ControllerAdvice; |
||||
|
import org.springframework.web.bind.annotation.ExceptionHandler; |
||||
|
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
import org.springframework.web.context.request.RequestContextHolder; |
||||
|
import org.springframework.web.context.request.ServletRequestAttributes; |
||||
|
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; |
||||
|
import org.springframework.web.multipart.MultipartException; |
||||
|
|
||||
|
import javax.servlet.http.HttpServletRequest; |
||||
|
import java.nio.charset.StandardCharsets; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Controller |
||||
|
@ControllerAdvice |
||||
|
public class GlobalExceptionHandler { |
||||
|
|
||||
|
@ResponseBody |
||||
|
@ExceptionHandler(DietException.class) |
||||
|
public Result errorHandler(DietException e) { |
||||
|
return _innerHandler(e.getResult(), e, false); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@ExceptionHandler({IllegalArgumentException.class}) |
||||
|
public Result errorHandler(IllegalArgumentException e) { |
||||
|
return _innerHandler(Result.ILLEGAL_ARGUMENT, e, false); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@ExceptionHandler({MissingServletRequestParameterException.class, MissingPathVariableException.class, MissingRequestCookieException.class, MissingRequestHeaderException.class, MissingServletRequestParameterException.class}) |
||||
|
public Result errorHandler(MissingServletRequestParameterException e) { |
||||
|
return _innerHandler(Result.ILLEGAL_ARGUMENT, e, false); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@ExceptionHandler({MultipartException.class, MethodArgumentTypeMismatchException.class}) |
||||
|
public Result errorHandler(MultipartException e) { |
||||
|
return _innerHandler(Result.NOT_SUPPORT, e, false); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@ExceptionHandler |
||||
|
public Result errorHandler(Exception e) { |
||||
|
return _innerHandler(Result.ERROR, e, true); |
||||
|
} |
||||
|
|
||||
|
@SneakyThrows |
||||
|
private Result _innerHandler(Result result, Exception e, boolean needExceptionTrace) { |
||||
|
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
||||
|
if (requestAttributes != null) { |
||||
|
if (requestAttributes.getResponse() != null) { |
||||
|
byte[] response = JSON.toJSONString(new Result(result.getCode(), result.equals(Result.ERROR) ? "系统异常!" : e.getMessage())).getBytes(StandardCharsets.UTF_8); |
||||
|
requestAttributes.getResponse().setContentType("text/json; charset=utf-8"); |
||||
|
requestAttributes.getResponse().setStatus(result.getCode()); |
||||
|
requestAttributes.getResponse().setContentLength(response.length); |
||||
|
requestAttributes.getResponse().getOutputStream().write(response); |
||||
|
} |
||||
|
HttpServletRequest request = requestAttributes.getRequest(); |
||||
|
if (!needExceptionTrace) { |
||||
|
log.error(String.format("[GlobalExceptionHandler:%s] %s %s exception=%s", request.getMethod(), request.getServletPath(), JSON.toJSONString(request.getParameterMap()), e.getMessage())); |
||||
|
} else { |
||||
|
log.error(String.format("[GlobalExceptionHandler:%s] %s %s exception=%s", request.getMethod(), request.getServletPath(), JSON.toJSONString(request.getParameterMap()), e.getMessage()), e); |
||||
|
} |
||||
|
} |
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,87 @@ |
|||||
|
package com.mathvision.diet.aspect; |
||||
|
|
||||
|
import com.alibaba.fastjson2.JSON; |
||||
|
import com.mathvision.diet.constant.Constant; |
||||
|
import com.mathvision.diet.domain.Result; |
||||
|
import com.mathvision.diet.domain.UserDO; |
||||
|
import com.mathvision.diet.domian.AuthType; |
||||
|
import com.mathvision.diet.entity.RoleItem; |
||||
|
import com.mathvision.diet.exception.DietException; |
||||
|
import com.mathvision.diet.service.EnumService; |
||||
|
import com.mathvision.diet.utils.JWTUtils; |
||||
|
import lombok.NonNull; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.context.annotation.Configuration; |
||||
|
import org.springframework.web.bind.annotation.ControllerAdvice; |
||||
|
import org.springframework.web.servlet.HandlerInterceptor; |
||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
||||
|
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import javax.servlet.http.HttpServletRequest; |
||||
|
import javax.servlet.http.HttpServletResponse; |
||||
|
import java.io.IOException; |
||||
|
import java.nio.charset.StandardCharsets; |
||||
|
import java.util.List; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Configuration |
||||
|
@ControllerAdvice |
||||
|
public class GlobalRequestAspect implements HandlerInterceptor, WebMvcConfigurer { |
||||
|
|
||||
|
@Resource |
||||
|
EnumService enumService; |
||||
|
|
||||
|
@Override |
||||
|
public void addInterceptors(InterceptorRegistry registry) { |
||||
|
registry.addInterceptor(this) |
||||
|
.addPathPatterns("/api/**") |
||||
|
.excludePathPatterns("/api/login") |
||||
|
.excludePathPatterns("/api/logout"); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler) throws IOException { |
||||
|
if (handler instanceof ResourceHttpRequestHandler) { |
||||
|
response.setStatus(Result.NOT_SUPPORT.getCode()); |
||||
|
response.setCharacterEncoding("UTF-8"); |
||||
|
response.setContentType("text/json; charset=utf-8"); |
||||
|
response.getOutputStream().write(JSON.toJSONString(Result.NOT_SUPPORT).getBytes(StandardCharsets.UTF_8)); |
||||
|
return false; |
||||
|
} |
||||
|
return verifySession(request, response); |
||||
|
} |
||||
|
|
||||
|
private boolean verifySession(HttpServletRequest request, HttpServletResponse response) { |
||||
|
UserDO userDO = (UserDO)request.getSession().getAttribute(Constant.SESSION_USER_KEY); |
||||
|
if (userDO == null) { |
||||
|
String token = request.getHeader(Constant.TOKEN_HEADER_KEY); |
||||
|
if (StringUtils.isBlank(token)) { |
||||
|
throw new DietException(Result.NOT_LOGIN); |
||||
|
} |
||||
|
userDO = JWTUtils.verify(token); |
||||
|
if (userDO == null) { |
||||
|
throw new DietException(Result.NOT_LOGIN); |
||||
|
} else { |
||||
|
userDO.setRoleItems(enumService.getRoleItems(userDO.getRoleItems().stream().map(RoleItem::getId).collect(Collectors.toList()))); |
||||
|
request.getSession().setMaxInactiveInterval(Constant.TOKEN_EXPIRE_SECOND); |
||||
|
request.getSession().setAttribute(Constant.SESSION_USER_KEY, userDO); |
||||
|
request.getSession().setAttribute(Constant.SESSION_USER_TYPE, userDO.isAdmin()); |
||||
|
} |
||||
|
} |
||||
|
if (!hasRole(userDO.isAdmin() ? AuthType.SERVER : AuthType.CLIENT, userDO.getRoleItems(), request.getRequestURI().toLowerCase(), request.getMethod().toLowerCase())) { |
||||
|
throw new DietException(Result.NOT_PRIVILEGED); |
||||
|
} |
||||
|
response.setHeader(Constant.TOKEN_HEADER_KEY, JWTUtils.getToken(userDO)); |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
private boolean hasRole(AuthType authType, List<RoleItem> roleItems, String url, String method) { |
||||
|
return !enumService.getAuthItems(authType).contains(url) || roleItems.parallelStream() |
||||
|
.map(item -> item.getItemValue().split(":", 2)) |
||||
|
.anyMatch(item -> item.length == 2 && item[0].contains(method) && url.matches("^/api/(" + item[1] + ")$")); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,53 @@ |
|||||
|
package com.mathvision.diet.aspect; |
||||
|
|
||||
|
import com.alibaba.fastjson.serializer.SerializerFeature; |
||||
|
import com.alibaba.fastjson.support.config.FastJsonConfig; |
||||
|
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; |
||||
|
import com.mathvision.diet.constant.Constant; |
||||
|
import com.mathvision.diet.domain.Result; |
||||
|
import com.mathvision.diet.domain.ResultCode; |
||||
|
import lombok.NonNull; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.context.annotation.Bean; |
||||
|
import org.springframework.context.annotation.Configuration; |
||||
|
import org.springframework.core.MethodParameter; |
||||
|
import org.springframework.http.MediaType; |
||||
|
import org.springframework.http.converter.HttpMessageConverter; |
||||
|
import org.springframework.http.server.ServerHttpRequest; |
||||
|
import org.springframework.http.server.ServerHttpResponse; |
||||
|
import org.springframework.web.bind.annotation.ControllerAdvice; |
||||
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; |
||||
|
|
||||
|
import java.nio.charset.StandardCharsets; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Configuration |
||||
|
@ControllerAdvice |
||||
|
public class GlobalResponseAspect implements ResponseBodyAdvice<Object> { |
||||
|
|
||||
|
@Override |
||||
|
public boolean supports(@NonNull MethodParameter methodParameter, @NonNull Class clazz) { |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
@Bean |
||||
|
FastJsonHttpMessageConverter fastJsonHttpMessageConverters() { |
||||
|
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); |
||||
|
FastJsonConfig jsonConfig = new FastJsonConfig(); |
||||
|
jsonConfig.setCharset(StandardCharsets.UTF_8); |
||||
|
jsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss"); |
||||
|
jsonConfig.setSerializerFeatures(SerializerFeature.BrowserCompatible); |
||||
|
converter.setFastJsonConfig(jsonConfig); |
||||
|
converter.setDefaultCharset(StandardCharsets.UTF_8); |
||||
|
converter.setSupportedMediaTypes(Constant.SUPPORTED_MEDIA_TYPES); |
||||
|
return converter; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Object beforeBodyWrite(Object result, @NonNull MethodParameter methodParameter, @NonNull MediaType mediaType, @NonNull Class<? extends HttpMessageConverter<?>> clazz, @NonNull ServerHttpRequest serverHttpRequest, @NonNull ServerHttpResponse serverHttpResponse) { |
||||
|
if (result instanceof Result) { |
||||
|
return result; |
||||
|
} |
||||
|
return new Result(ResultCode.success, result); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,32 @@ |
|||||
|
package com.mathvision.diet.constant; |
||||
|
|
||||
|
import com.google.common.collect.Lists; |
||||
|
import org.springframework.http.MediaType; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
public class Constant { |
||||
|
public static final int TOKEN_EXPIRE_SECOND = 7200; |
||||
|
public static final String TOKEN_HEADER_KEY = "Authorization"; |
||||
|
public static final String SESSION_USER_KEY = "user"; |
||||
|
public static final String SESSION_USER_TYPE = "type"; |
||||
|
|
||||
|
public static final List<MediaType> SUPPORTED_MEDIA_TYPES = Lists.newArrayList( |
||||
|
MediaType.APPLICATION_JSON, |
||||
|
MediaType.APPLICATION_ATOM_XML, |
||||
|
MediaType.APPLICATION_FORM_URLENCODED, |
||||
|
MediaType.APPLICATION_OCTET_STREAM, |
||||
|
MediaType.APPLICATION_PDF, |
||||
|
MediaType.APPLICATION_RSS_XML, |
||||
|
MediaType.APPLICATION_XHTML_XML, |
||||
|
MediaType.APPLICATION_XML, |
||||
|
MediaType.IMAGE_GIF, |
||||
|
MediaType.IMAGE_JPEG, |
||||
|
MediaType.IMAGE_PNG, |
||||
|
MediaType.TEXT_EVENT_STREAM, |
||||
|
MediaType.TEXT_HTML, |
||||
|
MediaType.TEXT_MARKDOWN, |
||||
|
MediaType.TEXT_PLAIN, |
||||
|
MediaType.TEXT_XML |
||||
|
); |
||||
|
} |
||||
@ -0,0 +1,58 @@ |
|||||
|
package com.mathvision.diet.controller; |
||||
|
|
||||
|
import com.mathvision.diet.domain.Result; |
||||
|
import com.mathvision.diet.domain.UserDO; |
||||
|
import com.mathvision.diet.domian.ClientType; |
||||
|
import com.mathvision.diet.entity.Vender; |
||||
|
import com.mathvision.diet.exception.DietException; |
||||
|
import com.mathvision.diet.service.UserService; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.stereotype.Controller; |
||||
|
import org.springframework.util.Assert; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMethod; |
||||
|
import org.springframework.web.bind.annotation.RequestParam; |
||||
|
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import java.time.Instant; |
||||
|
|
||||
|
@RequestMapping("/api") |
||||
|
@Controller |
||||
|
public class AuthController extends BaseController { |
||||
|
@Resource |
||||
|
private UserService userService; |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping("login") |
||||
|
public UserDO login(@RequestParam String uid, @RequestParam String pwd, @RequestParam(required = false, defaultValue = "WEB") String clientType, @RequestParam(required = false, defaultValue = "1.0") String clientVersion) { |
||||
|
if(StringUtils.isBlank(uid) || StringUtils.isBlank(pwd)) { |
||||
|
throw new DietException(Result.ILLEGAL_ARGUMENT); |
||||
|
} |
||||
|
UserDO userDO = userService.login(uid, pwd, ClientType.valueOf(clientType), clientVersion); |
||||
|
if (userDO == null) { |
||||
|
throw new DietException(Result.INVALID_USER_PASS); |
||||
|
} |
||||
|
Vender vender = userDO.getVender(); |
||||
|
if (vender != null && (!vender.getStatus() || Instant.now().isAfter(vender.getExpire()))) { |
||||
|
throw new DietException(Result.EXPIRED); |
||||
|
} |
||||
|
setSession(userDO); |
||||
|
return userDO; |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(value = "password", method = RequestMethod.POST) |
||||
|
public void modUser(@RequestParam String oldPassword, @RequestParam String password) { |
||||
|
Assert.isTrue(userService.checkUser(getUid(), oldPassword), "[参数错误]用户名密码错误!"); |
||||
|
userService.changeUser(getUid(), null, password, null, getVender(), getUid()); |
||||
|
delSession(); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping("logout") |
||||
|
public void logout() { |
||||
|
userService.logout(getUid()); |
||||
|
delSession(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,97 @@ |
|||||
|
package com.mathvision.diet.controller; |
||||
|
|
||||
|
import com.mathvision.diet.constant.Constant; |
||||
|
import com.mathvision.diet.domain.Result; |
||||
|
import com.mathvision.diet.domain.UserDO; |
||||
|
import com.mathvision.diet.exception.DietException; |
||||
|
import com.mathvision.diet.utils.JWTUtils; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.apache.commons.lang3.time.DateUtils; |
||||
|
import org.springframework.web.context.request.RequestContextHolder; |
||||
|
import org.springframework.web.context.request.ServletRequestAttributes; |
||||
|
|
||||
|
import javax.servlet.http.HttpServletRequest; |
||||
|
import javax.servlet.http.HttpServletResponse; |
||||
|
import javax.servlet.http.HttpSession; |
||||
|
import java.util.Date; |
||||
|
import java.util.Objects; |
||||
|
|
||||
|
@Slf4j |
||||
|
public class BaseController { |
||||
|
|
||||
|
public HttpServletRequest getRequest() { |
||||
|
return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); |
||||
|
} |
||||
|
|
||||
|
public HttpServletResponse getResponse() { |
||||
|
return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getResponse(); |
||||
|
} |
||||
|
|
||||
|
public UserDO getSession() { |
||||
|
return (UserDO)getRequest().getSession().getAttribute(Constant.SESSION_USER_KEY); |
||||
|
} |
||||
|
|
||||
|
public void setSession(UserDO userDO) { |
||||
|
HttpSession httpSession = getRequest().getSession(); |
||||
|
if (httpSession == null) { |
||||
|
return; |
||||
|
} |
||||
|
httpSession.setMaxInactiveInterval(Constant.TOKEN_EXPIRE_SECOND); |
||||
|
httpSession.setAttribute(Constant.SESSION_USER_KEY, userDO); |
||||
|
httpSession.setAttribute(Constant.SESSION_USER_TYPE, userDO.isAdmin()); |
||||
|
|
||||
|
getResponse().setHeader(Constant.TOKEN_HEADER_KEY, JWTUtils.getToken(userDO)); |
||||
|
} |
||||
|
|
||||
|
public void delSession() { |
||||
|
HttpSession httpSession = getRequest().getSession(); |
||||
|
if (httpSession == null) { |
||||
|
return; |
||||
|
} |
||||
|
httpSession.removeAttribute(Constant.SESSION_USER_KEY); |
||||
|
httpSession.removeAttribute(Constant.SESSION_USER_TYPE); |
||||
|
getResponse().setHeader(Constant.TOKEN_HEADER_KEY, null); |
||||
|
} |
||||
|
|
||||
|
public String getUid() { |
||||
|
UserDO userDO = getSession(); |
||||
|
return userDO == null ? null : userDO.getUid(); |
||||
|
} |
||||
|
|
||||
|
public Long getRoleId() { |
||||
|
UserDO userDO = getSession(); |
||||
|
return userDO == null ? null : userDO.getRoleId(); |
||||
|
} |
||||
|
|
||||
|
public Long getVender() { |
||||
|
return isAdmin() ? 0 : getSession().getVender().getId(); |
||||
|
} |
||||
|
|
||||
|
public boolean isAdmin() { |
||||
|
return (Boolean) getRequest().getSession().getAttribute(Constant.SESSION_USER_TYPE); |
||||
|
} |
||||
|
|
||||
|
public Long auth(Long venderId) { |
||||
|
if (venderId == null) { |
||||
|
venderId = getVender(); |
||||
|
} |
||||
|
if (!isAdmin() && !venderId.equals(getVender())) { |
||||
|
throw new DietException(Result.NOT_PRIVILEGED); |
||||
|
} |
||||
|
return venderId; |
||||
|
} |
||||
|
|
||||
|
public Date parseDate(String dateTime) { |
||||
|
return parseDate(dateTime, null); |
||||
|
} |
||||
|
|
||||
|
public Date parseDate(String dateTime, Date defaultDate) { |
||||
|
try { |
||||
|
return StringUtils.isBlank(dateTime) ? defaultDate : DateUtils.parseDate(dateTime, "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"); |
||||
|
} catch (Exception e) { |
||||
|
log.error("[BaseController] parseDate exception for input:" + dateTime); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,24 @@ |
|||||
|
package com.mathvision.diet.controller; |
||||
|
|
||||
|
import com.mathvision.diet.service.EnumService; |
||||
|
import org.springframework.stereotype.Controller; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import java.util.Collection; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
@RequestMapping("/api") |
||||
|
@Controller |
||||
|
public class BasicController extends BaseController { |
||||
|
|
||||
|
@Resource |
||||
|
private EnumService enumService; |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping("enum") |
||||
|
public Map<String, Collection<?>> all() { |
||||
|
return enumService.getAll(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,136 @@ |
|||||
|
package com.mathvision.diet.controller; |
||||
|
|
||||
|
import com.alibaba.fastjson2.JSON; |
||||
|
import com.alibaba.fastjson2.TypeReference; |
||||
|
import com.google.common.collect.Lists; |
||||
|
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.Ingredient; |
||||
|
import com.mathvision.diet.service.DishService; |
||||
|
import com.mathvision.diet.service.EnumService; |
||||
|
import com.mathvision.diet.service.IngredientService; |
||||
|
import com.mathvision.diet.service.VenderService; |
||||
|
import org.apache.commons.collections4.CollectionUtils; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.data.domain.PageRequest; |
||||
|
import org.springframework.data.domain.Sort; |
||||
|
import org.springframework.stereotype.Controller; |
||||
|
import org.springframework.util.Assert; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMethod; |
||||
|
import org.springframework.web.bind.annotation.RequestParam; |
||||
|
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import java.time.Instant; |
||||
|
import java.util.List; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
@RequestMapping("/api/dish") |
||||
|
@Controller |
||||
|
public class DishController extends BaseController { |
||||
|
|
||||
|
@Resource |
||||
|
EnumService enumService; |
||||
|
|
||||
|
@Resource |
||||
|
DishService dishService; |
||||
|
|
||||
|
@Resource |
||||
|
VenderService venderService; |
||||
|
|
||||
|
@Resource |
||||
|
private IngredientService ingredientService; |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.PUT) |
||||
|
public void add(@RequestParam String name, @RequestParam(required = false) List<Long> vendors, @RequestParam(required = false) String icon, @RequestParam(required = false) List<Integer> month, @RequestParam String mark, @RequestParam String ingredient) { |
||||
|
Assert.isTrue(StringUtils.isNotBlank(name), "[参数错误]菜品名称必填!"); |
||||
|
Assert.isTrue(StringUtils.isNotBlank(mark), "[参数错误]菜品标签必填!"); |
||||
|
Assert.isTrue(enumService.checkMark(mark), "[参数错误]菜品标签不在取值范围内!"); |
||||
|
Assert.isTrue(CollectionUtils.isNotEmpty(month) && month.stream().allMatch(x -> Range.closed(1, 12).contains(x)), "[参数错误]请选择正确的月份!"); |
||||
|
Assert.isTrue(JSON.isValid(ingredient), "[参数错误]食材列表必填!"); |
||||
|
|
||||
|
List<DishItemDTO> items = JSON.parseObject(ingredient, new TypeReference<List<DishItemDTO>>(){}); |
||||
|
Assert.isTrue(items.stream().allMatch(x -> ingredientService.existsIngredientByKey(x.getKey())), "[参数错误]请选择系统存在的食材!"); |
||||
|
if (isAdmin()) { |
||||
|
Assert.isTrue(CollectionUtils.isNotEmpty(vendors), "[参数错误]单位列表必填!"); |
||||
|
} else { |
||||
|
vendors = Lists.newArrayList(getVender()); |
||||
|
} |
||||
|
Assert.isTrue(vendors.stream().allMatch(venderService::exists), "[[参数错误]请选择存在的单位!]"); |
||||
|
|
||||
|
Instant dateTime = Instant.now(); |
||||
|
dishService.add(vendors.stream().map(vender -> Dish.builder().name(name).vender(vender).marks(mark).month(month).icon(icon).ingredient(items).operate(getUid()).created(dateTime).modify(dateTime).build()).collect(Collectors.toList()), getUid()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.DELETE) |
||||
|
public void delete(@RequestParam List<Long> ids) { |
||||
|
Assert.isTrue(CollectionUtils.isNotEmpty(ids), "[参数错误]菜品编号必填!"); |
||||
|
Assert.isTrue(isAdmin() || dishService.exists(getVender(), ids), "[参数错误]菜品不存在!"); |
||||
|
dishService.delete(ids, getVender(), getUid()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.POST) |
||||
|
public void update(@RequestParam Long id, @RequestParam(required = false) String name, @RequestParam(required = false) String icon, @RequestParam(required = false) List<Integer> month, @RequestParam(required = false) String mark, @RequestParam(required = false) String ingredient) { |
||||
|
Dish dish = isAdmin()? dishService.get(id) : dishService.get(id, getVender()); |
||||
|
Assert.isTrue(dish != null, "[参数错误]菜品不存在!"); |
||||
|
|
||||
|
boolean flag = false; |
||||
|
if(StringUtils.isNotBlank(name) && !StringUtils.equals(name, dish.getName())) { |
||||
|
Assert.isTrue(!dishService.exists(id, name, dish.getVender()), "[参数错误]菜品名称已存在!"); |
||||
|
dish.setName(name); |
||||
|
flag = true; |
||||
|
} |
||||
|
if(StringUtils.isNotBlank(icon) && !icon.equals(dish.getIcon())) { |
||||
|
dish.setIcon(icon); |
||||
|
flag = true; |
||||
|
} |
||||
|
if(StringUtils.isNotBlank(mark) && !mark.equals(dish.getMarks())) { |
||||
|
Assert.isTrue(enumService.checkMark(mark), "[参数错误]菜品标签不在取值范围内!"); |
||||
|
dish.setMarks(mark); |
||||
|
flag = true; |
||||
|
} |
||||
|
if(CollectionUtils.isNotEmpty(month)) { |
||||
|
Assert.isTrue(CollectionUtils.isNotEmpty(month) && month.stream().allMatch(x -> Range.closed(1, 12).contains(x)), "[参数错误]请选择正确的月份!"); |
||||
|
dish.setMonth(month); |
||||
|
flag = true; |
||||
|
} |
||||
|
if (JSON.isValid(ingredient)) { |
||||
|
List<DishItemDTO> items = JSON.parseObject(ingredient, new TypeReference<List<DishItemDTO>>(){}); |
||||
|
if(CollectionUtils.isNotEmpty(items)) { |
||||
|
Assert.isTrue(items.stream().allMatch(x -> ingredientService.existsIngredientByKey(x.getKey())), "[参数错误]请选择系统存在的食材!"); |
||||
|
dish.setIngredient(items); |
||||
|
flag = true; |
||||
|
} |
||||
|
} |
||||
|
if (flag) { |
||||
|
dishService.update(dish, getUid()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.GET) |
||||
|
public Object query(@RequestParam(required = false) String keyword, @RequestParam(required = false) Long id, @RequestParam(required = false) String mark, @RequestParam(required = false, defaultValue = "0") int pageNo, @RequestParam(required = false, defaultValue = "20") int pageSize) { |
||||
|
if (id != null) { |
||||
|
return isAdmin() ? dishService.get(id) : dishService.get(id, getVender()); |
||||
|
} |
||||
|
return dishService.list(getVender(), keyword, mark, PageRequest.of(pageNo, pageSize).withSort(Sort.by(Sort.Direction.DESC, "id"))); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(value = "select", method = RequestMethod.GET) |
||||
|
public Object query(@RequestParam(required = false) String keyword) { |
||||
|
return dishService.query(keyword); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(value = "label", method = RequestMethod.GET) |
||||
|
public List<DishLabelDO> label(@RequestParam(required = false) List<Long> ids) { |
||||
|
return dishService.label(ids, getVender()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,137 @@ |
|||||
|
package com.mathvision.diet.controller; |
||||
|
|
||||
|
import com.alibaba.fastjson2.JSON; |
||||
|
import com.alibaba.fastjson2.TypeReference; |
||||
|
import com.mathvision.diet.domian.MarkType; |
||||
|
import com.mathvision.diet.entity.Ingredient; |
||||
|
import com.mathvision.diet.service.IngredientService; |
||||
|
import org.apache.commons.collections4.CollectionUtils; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.data.domain.Page; |
||||
|
import org.springframework.data.domain.PageRequest; |
||||
|
import org.springframework.data.domain.Sort; |
||||
|
import org.springframework.stereotype.Controller; |
||||
|
import org.springframework.util.Assert; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMethod; |
||||
|
import org.springframework.web.bind.annotation.RequestParam; |
||||
|
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
import org.springframework.web.multipart.MultipartFile; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import javax.servlet.http.HttpServletResponse; |
||||
|
import java.math.BigDecimal; |
||||
|
import java.net.URLEncoder; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
@RequestMapping("/api/ingredient") |
||||
|
@Controller |
||||
|
public class IngredientController extends BaseController { |
||||
|
@Resource |
||||
|
IngredientService ingredientService; |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.PUT) |
||||
|
public void addIngredient(@RequestParam String key, @RequestParam String name, @RequestParam String type, @RequestParam String nutrient) { |
||||
|
Assert.isTrue(isAdmin(), "[参数错误]无操作权限!"); |
||||
|
Assert.isTrue(JSON.isValid(nutrient), "[参数错误]营养素列表JSON解析错误!"); |
||||
|
Assert.isTrue(!ingredientService.existsIngredientByKey(key), "[参数错误]食材编号重复!"); |
||||
|
|
||||
|
ingredientService.addIngredient(Ingredient.builder().key(key).name(name).type(type).nutrient(JSON.parseObject(nutrient, new TypeReference<Map<String, BigDecimal>>(){})).build(), getUid()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.DELETE) |
||||
|
public void delIngredient(@RequestParam List<String> keys) { |
||||
|
Assert.isTrue(isAdmin(), "[参数错误]无操作权限!"); |
||||
|
keys = keys.stream().filter(key -> ingredientService.existsIngredientByKey(key)).collect(Collectors.toList()); |
||||
|
Assert.isTrue(CollectionUtils.isNotEmpty(keys), "[参数错误]请提供食材编号!"); |
||||
|
|
||||
|
ingredientService.delIngredient(keys, getUid()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.POST) |
||||
|
public void modIngredient(@RequestParam String key, @RequestParam(required = false) String name, @RequestParam(required = false) String type, @RequestParam(required = false) String nutrient) { |
||||
|
Assert.isTrue(isAdmin(), "[参数错误]无操作权限!"); |
||||
|
Assert.isTrue(nutrient == null || JSON.isValid(nutrient), "[参数错误]营养素列表JSON解析错误!"); |
||||
|
Ingredient ingredient = ingredientService.queryIngredientByKey(key); |
||||
|
Assert.isTrue(ingredient != null, "[参数错误]食材不存在!"); |
||||
|
|
||||
|
boolean flag = false; |
||||
|
if(StringUtils.isNotBlank(type) && !StringUtils.equals(type, ingredient.getType())) { |
||||
|
ingredient.setType(type); |
||||
|
flag = true; |
||||
|
} |
||||
|
if(StringUtils.isNotBlank(name) && !StringUtils.equals(name, ingredient.getName())) { |
||||
|
ingredient.setName(name); |
||||
|
flag = true; |
||||
|
} |
||||
|
if (JSON.isValid(nutrient)) { |
||||
|
ingredient.setNutrient(JSON.parseObject(nutrient, new TypeReference<Map<String, BigDecimal>>(){})); |
||||
|
flag = true; |
||||
|
} |
||||
|
if (flag) { |
||||
|
ingredientService.modIngredient(ingredient, getUid()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.GET) |
||||
|
public Page<?> queryIngredient(@RequestParam(required = false) String keyword, @RequestParam(required = false) String type, @RequestParam(required = false) String mark, @RequestParam(required = false, defaultValue = "0") int pageNo, @RequestParam(required = false, defaultValue = "20") int pageSize) { |
||||
|
return ingredientService.queryIngredientByMark(getVender(), mark, keyword, type, PageRequest.of(pageNo, pageSize).withSort(Sort.by(Sort.Direction.DESC, "id"))); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(value = "select", method = RequestMethod.GET) |
||||
|
public List<Ingredient> queryIngredient(@RequestParam(required = false) List<String> keys, @RequestParam(required = false) String keyword) { |
||||
|
return CollectionUtils.isNotEmpty(keys) ? ingredientService.getByKeys(keys) : ingredientService.getByKeyword(keyword); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(value = "mark", method = RequestMethod.PUT) |
||||
|
public void addMark(@RequestParam String mark, @RequestParam String key) { |
||||
|
MarkType markType = MarkType.toType(mark); |
||||
|
Assert.isTrue(!isAdmin(), "[参数错误]无操作权限!"); |
||||
|
Assert.notNull(markType, "[参数错误]标记取值:常用/忌用!"); |
||||
|
Assert.isTrue(ingredientService.existsIngredientByKey(key), "[参数错误]食材不存在!"); |
||||
|
ingredientService.addMark(markType, key, getVender(), getUid()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(value = "mark", method = RequestMethod.DELETE) |
||||
|
public void delMark(@RequestParam String key) { |
||||
|
Assert.isTrue(!isAdmin(), "[参数错误]无操作权限!"); |
||||
|
Assert.isTrue(ingredientService.existsIngredientByKey(key), "[参数错误]食材不存在!"); |
||||
|
ingredientService.delMark(key, getVender(), getUid()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(value = "excel", method = RequestMethod.PUT) |
||||
|
public void addIngredient(@RequestParam MultipartFile file) throws Exception { |
||||
|
Assert.isTrue(isAdmin(), "[参数错误]无操作权限!"); |
||||
|
Assert.notNull(file, "[参数错误]请选择要导入的食材文件!"); |
||||
|
HttpServletResponse response = getResponse(); |
||||
|
|
||||
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
||||
|
response.setCharacterEncoding("utf-8"); |
||||
|
response.setHeader("Content-Disposition", "attachment;filename*=utf-8''"+ URLEncoder.encode("[导入结果]" + file.getOriginalFilename(), "UTF-8")); |
||||
|
|
||||
|
ingredientService.upload(file.getInputStream(), response.getOutputStream(), getUid()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(value = "excel", method = RequestMethod.GET) |
||||
|
public void template() throws Exception { |
||||
|
Assert.isTrue(isAdmin(), "[参数错误]无操作权限!"); |
||||
|
HttpServletResponse response = getResponse(); |
||||
|
|
||||
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
||||
|
response.setCharacterEncoding("utf-8"); |
||||
|
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + URLEncoder.encode("导入模板.xlsx", "UTF-8")); |
||||
|
|
||||
|
ingredientService.template(response.getOutputStream()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,143 @@ |
|||||
|
package com.mathvision.diet.controller; |
||||
|
|
||||
|
import com.google.common.collect.Lists; |
||||
|
import com.google.common.collect.Range; |
||||
|
import com.mathvision.diet.domian.MealType; |
||||
|
import com.mathvision.diet.domian.MenuStatus; |
||||
|
import com.mathvision.diet.entity.Menu; |
||||
|
import com.mathvision.diet.entity.Nutrition; |
||||
|
import com.mathvision.diet.service.MenuService; |
||||
|
import com.mathvision.diet.service.NutritionService; |
||||
|
import com.mathvision.diet.service.VenderService; |
||||
|
import org.apache.commons.collections4.CollectionUtils; |
||||
|
import org.apache.commons.collections4.MapUtils; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.data.domain.PageRequest; |
||||
|
import org.springframework.data.domain.Sort; |
||||
|
import org.springframework.stereotype.Controller; |
||||
|
import org.springframework.util.Assert; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMethod; |
||||
|
import org.springframework.web.bind.annotation.RequestParam; |
||||
|
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.Map; |
||||
|
import java.util.Set; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
@RequestMapping("/api/menu") |
||||
|
@Controller |
||||
|
public class MenuController extends BaseController { |
||||
|
|
||||
|
@Resource |
||||
|
VenderService venderService; |
||||
|
|
||||
|
@Resource |
||||
|
NutritionService nutritionService; |
||||
|
|
||||
|
@Resource |
||||
|
MenuService menuService; |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.PUT) |
||||
|
public List<Long> add(@RequestParam String name, @RequestParam(required = false) List<Long> vendors, @RequestParam Long nutrient, @RequestParam Integer day, @RequestParam List<String> meals, @RequestParam List<Integer> month, @RequestParam List<String> crows) { |
||||
|
Assert.isTrue(StringUtils.isNotBlank(name), "[参数错误]食谱名称必填!"); |
||||
|
Assert.isTrue(Range.closed(1, 7).contains(day), "[参数错误]天数取值[1~7]!"); |
||||
|
Assert.isTrue(CollectionUtils.isNotEmpty(month) && month.stream().allMatch(x -> Range.closed(1, 12).contains(x)), "[参数错误]请选择正确的月份!"); |
||||
|
Assert.isTrue(CollectionUtils.isNotEmpty(meals), "[参数错误]餐次必填!"); |
||||
|
Assert.isTrue(meals.stream().allMatch(x -> MealType.toType(x) != null), "[参数错误]餐次取值[早餐,午餐,晚餐]!"); |
||||
|
|
||||
|
Nutrition nutrition = nutritionService.get(nutrient); |
||||
|
Assert.notNull(nutrition, "[参数错误]营养计划必选!"); |
||||
|
Set<String> allCrows = nutrition.getIngredient().keySet(); |
||||
|
vendors = isAdmin() ? (vendors == null ? Lists.newArrayList() : vendors.stream().filter(venderService::exists).collect(Collectors.toList())) : Lists.newArrayList(getVender()); |
||||
|
|
||||
|
Assert.isTrue(CollectionUtils.isNotEmpty(vendors), "[参数错误]营养计划不适用于所选单位!"); |
||||
|
Assert.isTrue(new HashSet<>(nutrition.getVendors()).containsAll(vendors), "[参数错误]营养计划不适用于所选单位!"); |
||||
|
Assert.isTrue(CollectionUtils.isNotEmpty(crows), "[参数错误]人群取值[参照营养计划]必填!"); |
||||
|
Assert.isTrue(allCrows.containsAll(crows), "[参数错误]营养计划不包含所选的人群!"); |
||||
|
Instant dateTime = Instant.now(); |
||||
|
List<Menu> menus = vendors.stream().map(v -> Menu.builder().name(name).meals(meals).crows(crows).scale(crows.stream().collect(Collectors.toMap(x -> x, x -> 0))).day(Long.valueOf(day)).nutrient(nutrient).month(month).vender(v).status(MenuStatus.draft).operate(getUid()).created(dateTime).modify(dateTime).build()).collect(Collectors.toList()); |
||||
|
return menuService.add(menus).stream().map(Menu::getId).collect(Collectors.toList()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.DELETE) |
||||
|
public void delete(@RequestParam Long id) { |
||||
|
Menu menu = menuService.get(id); |
||||
|
Assert.notNull(menu, "[参数错误]食谱不存在!"); |
||||
|
Assert.isTrue(menu.getVender().equals(getVender()) || isAdmin(), "[参数错误]食谱不存在!"); |
||||
|
Assert.isTrue(menu.getStatus() != MenuStatus.publish, "[参数错误]该食谱已发布,不可删除!"); |
||||
|
menuService.delete(id, getVender(), getUid()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.POST) |
||||
|
public void update(@RequestParam Long id, @RequestParam(required = false) String name, @RequestParam(required = false) List<Integer> month, @RequestParam Long nutrient, @RequestParam(required = false) Integer day, @RequestParam(required = false) List<String> meals, @RequestParam(required = false) List<String> crows) { |
||||
|
Menu menu = menuService.get(id); |
||||
|
Assert.notNull(menu, "[参数错误]食谱不存在!"); |
||||
|
Assert.isTrue(menu.getVender().equals(getVender()) || isAdmin(), "[参数错误]食谱不存在!"); |
||||
|
Assert.isTrue(menu.getStatus() != MenuStatus.publish, "[参数错误]该食谱已发布,不可编辑!"); |
||||
|
Assert.isTrue(CollectionUtils.isEmpty(month) || month.stream().allMatch(x -> Range.closed(1, 12).contains(x)), "[参数错误]请选择正确的月份!"); |
||||
|
Assert.isTrue(day == null || Range.closed(1, 7).contains(day), "[参数错误]天数取值[1~7]!"); |
||||
|
|
||||
|
boolean flag = false; |
||||
|
boolean check = false; |
||||
|
if (StringUtils.isNotBlank(name) && !StringUtils.equals(menu.getName(), name)) { |
||||
|
menu.setName(name); |
||||
|
flag = true; |
||||
|
} |
||||
|
if (CollectionUtils.isNotEmpty(month) && !menu.getMonth().equals(month)) { |
||||
|
menu.setMonth(month); |
||||
|
flag = true; |
||||
|
} |
||||
|
if (CollectionUtils.isNotEmpty(meals) && !meals.equals(menu.getMeals())) { |
||||
|
Assert.isTrue(meals.stream().allMatch(x -> MealType.toType(x) != null), "[参数错误]餐次取值[早餐,午餐,晚餐]!"); |
||||
|
menu.setMeals(meals); |
||||
|
flag = true; |
||||
|
} |
||||
|
if (day != null && !day.equals(menu.getDay().intValue())) { |
||||
|
menu.setDay(day.longValue()); |
||||
|
flag = true; |
||||
|
} |
||||
|
if (nutrient != null && !nutrient.equals(menu.getNutrient())) { |
||||
|
menu.setNutrient(nutrient); |
||||
|
flag = true; |
||||
|
check = true; |
||||
|
} |
||||
|
if (CollectionUtils.isNotEmpty(crows) && !crows.equals(menu.getCrows())) { |
||||
|
menu.setCrows(crows); |
||||
|
check = true; |
||||
|
} |
||||
|
if (flag) { |
||||
|
if (check) { |
||||
|
Nutrition nutrition = nutritionService.get(menu.getNutrient()); |
||||
|
Assert.notNull(nutrition, "[参数错误]营养计划不存在!"); |
||||
|
Assert.isTrue(nutrition.getVendors().contains(menu.getVender()), "[参数错误]营养计划不适用于该企业!"); |
||||
|
Assert.isTrue(nutrition.getIngredient().keySet().containsAll(crows), "[参数错误]营养计划不包含所选的人群!"); |
||||
|
if (MapUtils.isNotEmpty(menu.getScale())) { |
||||
|
Map<String, Integer> scale = menu.getScale(); |
||||
|
menu.setScale(menu.getCrows().stream().collect(Collectors.toMap(x -> x, x -> scale.getOrDefault(x, 0)))); |
||||
|
} |
||||
|
} |
||||
|
Instant dateTime = Instant.now(); |
||||
|
menu.setStatus(MenuStatus.draft); |
||||
|
menu.setOperate(getUid()); |
||||
|
menu.setModify(dateTime); |
||||
|
menuService.update(menu); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.GET) |
||||
|
public Object query(@RequestParam(required = false) Long id, @RequestParam(required = false) String name, @RequestParam(required = false) Long vender, @RequestParam(required = false) Long status, @RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime, @RequestParam(required = false, defaultValue = "0") int pageNo, @RequestParam(required = false, defaultValue = "20") int pageSize) { |
||||
|
if (id != null) { |
||||
|
return isAdmin() ? menuService.get(id) : menuService.get(id, getVender()); |
||||
|
} |
||||
|
return menuService.list(isAdmin() ? vender : getVender(), name, MenuStatus.toType(status), parseDate(startTime), parseDate(endTime), PageRequest.of(pageNo, pageSize).withSort(Sort.by(Sort.Direction.DESC, "id"))); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,194 @@ |
|||||
|
package com.mathvision.diet.controller; |
||||
|
|
||||
|
import com.alibaba.fastjson2.JSON; |
||||
|
import com.alibaba.fastjson2.JSONObject; |
||||
|
import com.google.common.collect.Range; |
||||
|
import com.mathvision.diet.domian.MenuDishItemDTO; |
||||
|
import com.mathvision.diet.domian.MenuStatus; |
||||
|
import com.mathvision.diet.entity.Dish; |
||||
|
import com.mathvision.diet.entity.Menu; |
||||
|
import com.mathvision.diet.entity.MenuDish; |
||||
|
import com.mathvision.diet.service.*; |
||||
|
import com.mathvision.diet.vo.MenuDishVO; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.stereotype.Controller; |
||||
|
import org.springframework.util.Assert; |
||||
|
import org.springframework.web.bind.annotation.*; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import javax.servlet.http.HttpServletResponse; |
||||
|
import java.io.IOException; |
||||
|
import java.net.URLEncoder; |
||||
|
import java.time.Instant; |
||||
|
import java.time.LocalDate; |
||||
|
import java.util.Arrays; |
||||
|
import java.util.HashSet; |
||||
|
import java.util.List; |
||||
|
import java.util.Set; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
@RequestMapping("/api/menu/dish") |
||||
|
@Controller |
||||
|
public class MenuDishController extends BaseController { |
||||
|
|
||||
|
@Resource |
||||
|
EnumService enumService; |
||||
|
|
||||
|
@Resource |
||||
|
DishService dishService; |
||||
|
|
||||
|
@Resource |
||||
|
MenuService menuService; |
||||
|
|
||||
|
@Resource |
||||
|
MenuDishService menuDishService; |
||||
|
|
||||
|
@Resource |
||||
|
private IngredientService ingredientService; |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.PUT) |
||||
|
public Long add(@RequestParam Long menuId, @RequestParam Long dishId, @RequestParam Integer day, @RequestParam String meal, @RequestParam(required = false) String mark, @RequestParam String ingredient) { |
||||
|
Menu menu = checkAndConvert(menuId, MenuStatus.pass, MenuStatus.publish); |
||||
|
return menuDishService.add(checkAndConvert(menu, checkAndConvert(dishId), day, meal, mark, parseItems(ingredient, new HashSet<>(menu.getCrows())))).getId(); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(value = "batch", method = RequestMethod.PUT) |
||||
|
public List<Long> addAll(@RequestBody MenuDishVO menuDishVO) { |
||||
|
menuDishVO.getMenuIds().forEach(menuId -> checkAndConvert(menuId, MenuStatus.pass, MenuStatus.publish)); |
||||
|
return menuDishService.addAll(menuDishVO.getMenuIds().stream().map(menuId -> { |
||||
|
Menu menu = checkAndConvert(menuId, MenuStatus.pass, MenuStatus.publish); |
||||
|
return menuDishVO.getDishes().stream().map(dish -> checkAndConvert(menu, checkAndConvert(dish.getDish()), dish.getDay(), dish.getMeal(), dish.getMark(), dish.getItems().stream().map(item -> MenuDishItemDTO.builder().isMain(item.getIsMain()).key(item.getKey()).value(item.getValue()).build()).collect(Collectors.toList()))).collect(Collectors.toList()); |
||||
|
}).flatMap(List::stream).collect(Collectors.toList())).stream().map(MenuDish::getId).collect(Collectors.toList()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.DELETE) |
||||
|
public void delete(@RequestParam(required = false) Long menuDishId, @RequestParam Long menuId) { |
||||
|
checkAndConvert(menuId, MenuStatus.pass, MenuStatus.publish); |
||||
|
if (menuDishId != null) { |
||||
|
MenuDish dish = isAdmin() ? menuDishService.get(menuDishId) : menuDishService.get(menuDishId, getVender()); |
||||
|
Assert.notNull(dish, "[参数错误]菜品不存在!"); |
||||
|
menuDishService.delete(menuDishId); |
||||
|
} else { |
||||
|
menuDishService.deleteAll(menuId); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.POST) |
||||
|
public void update(@RequestParam Long menuDishId, @RequestParam Long menuId, @RequestParam(required = false) String mark, @RequestParam(required = false) String ingredient) { |
||||
|
Menu menu = checkAndConvert(menuId, MenuStatus.pass, MenuStatus.publish); |
||||
|
MenuDish dish = isAdmin() ? menuDishService.get(menuDishId) : menuDishService.get(menuDishId, getVender()); |
||||
|
Assert.notNull(dish, "[参数错误]菜品不存在!"); |
||||
|
|
||||
|
boolean flag = false; |
||||
|
if (StringUtils.isNotBlank(mark) && !StringUtils.equals(mark, dish.getMarks())) { |
||||
|
Assert.isTrue(enumService.checkMark(mark), "[参数错误]菜品标签不在取值范围内!"); |
||||
|
dish.setMarks(mark); |
||||
|
flag= true; |
||||
|
} |
||||
|
if (StringUtils.isNotBlank(ingredient) && JSON.isValid(ingredient)) { |
||||
|
dish.setIngredient(parseItems(ingredient, new HashSet<>(menu.getCrows()))); |
||||
|
flag = true; |
||||
|
} |
||||
|
if (flag) { |
||||
|
Instant dateTime = Instant.now(); |
||||
|
dish.setOperate(getUid()); |
||||
|
dish.setModify(dateTime); |
||||
|
menuDishService.add(dish); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(value = "export", method = RequestMethod.GET) |
||||
|
public void export(@RequestParam Long id) throws IOException { |
||||
|
Menu menu = isAdmin() ? menuService.get(id) : menuService.get(id, getVender()); |
||||
|
Assert.notNull(menu, "[参数错误]食谱不存在!"); |
||||
|
|
||||
|
HttpServletResponse response = getResponse(); |
||||
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
||||
|
response.setCharacterEncoding("utf-8"); |
||||
|
response.setHeader("Content-Disposition", "attachment;filename*=utf-8''"+ URLEncoder.encode("[食谱导出]" + menu.getId() + "-"+ menu.getName() + ".xlsx", "UTF-8")); |
||||
|
|
||||
|
menuDishService.export(menu, response.getOutputStream()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(value = "analysis", method = RequestMethod.GET) |
||||
|
public JSONObject analysis(@RequestParam Long id, @RequestParam(required = false) Long day, @RequestParam(required = false) String crow) { |
||||
|
Menu menu = isAdmin() ? menuService.get(id) : menuService.get(id, getVender()); |
||||
|
Assert.notNull(menu, "[参数错误]食谱不存在!"); |
||||
|
day = (day == null || day < 1 || day > menu.getDay()) ? (long) LocalDate.now().getDayOfWeek().getValue() : day; |
||||
|
crow = StringUtils.isBlank(crow) || !menu.getCrows().contains(crow) ? menu.getCrows().get(0) : crow; |
||||
|
List<MenuDish> dishes = menuDishService.query(id, menu.getVender(), day); |
||||
|
return menuDishService.assess(menu, day, crow, dishes); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(value = "analysis/types", method = RequestMethod.GET) |
||||
|
public JSONObject types(@RequestParam Long id) { |
||||
|
Menu menu = isAdmin() ? menuService.get(id) : menuService.get(id, getVender()); |
||||
|
Assert.notNull(menu, "[参数错误]食谱不存在!"); |
||||
|
List<MenuDish> dishes = menuDishService.query(id, menu.getVender()); |
||||
|
return menuDishService.types(menu, dishes); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(value = "analysis/energy", method = RequestMethod.GET) |
||||
|
public JSONObject energy(@RequestParam Long id, @RequestParam(required = false) Long day, @RequestParam(required = false) String crow) { |
||||
|
Menu menu = isAdmin() ? menuService.get(id) : menuService.get(id, getVender()); |
||||
|
Assert.notNull(menu, "[参数错误]食谱不存在!"); |
||||
|
day = (day == null || day < 1 || day > menu.getDay()) ? (long) LocalDate.now().getDayOfWeek().getValue() : day; |
||||
|
crow = StringUtils.isBlank(crow) || !menu.getCrows().contains(crow) ? menu.getCrows().get(0) : crow; |
||||
|
List<MenuDish> dishes = menuDishService.query(id, menu.getVender(), day); |
||||
|
return menuDishService.energy(day, crow, dishes); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.GET) |
||||
|
public List<MenuDish> query(@RequestParam(required = false) Long menuId) { |
||||
|
if (menuId == null) { |
||||
|
Long currentMenuId = menuService.current(getVender()); |
||||
|
return menuDishService.query(currentMenuId, getVender(), (long) LocalDate.now().getDayOfWeek().getValue()); |
||||
|
} |
||||
|
return isAdmin() ? menuDishService.query(menuId) : menuDishService.query(menuId, getVender()); |
||||
|
} |
||||
|
|
||||
|
private MenuDish checkAndConvert(Menu menu, Dish dish, Integer day, String meal, String mark, List<MenuDishItemDTO> ingredient) { |
||||
|
Assert.isTrue(Range.closed(1, menu.getDay().intValue()).contains(day), "[参数错误]天数不在食谱的设置范围内!"); |
||||
|
Assert.isTrue(menu.getMeals().contains(meal), "[参数错误]餐次不在食谱的设置范围内!"); |
||||
|
|
||||
|
mark = StringUtils.isBlank(mark) ? dish.getMarks() : mark; |
||||
|
Assert.isTrue(enumService.checkMark(mark), "[参数错误]菜品标签不在取值范围内!"); |
||||
|
|
||||
|
Instant dateTime = Instant.now(); |
||||
|
return MenuDish.builder().vender(menu.getVender()).menu(menu.getId()).dish(dish.getId()).day(day.longValue()).meal(meal).name(dish.getName()).marks(mark).ingredient(ingredient).operate(getUid()).created(dateTime).modify(dateTime).build(); |
||||
|
} |
||||
|
|
||||
|
private Menu checkAndConvert(Long menuId, MenuStatus ... statuses) { |
||||
|
Menu menu = isAdmin() ? menuService.get(menuId) : menuService.get(menuId, getVender()); |
||||
|
Assert.notNull(menu, "[参数错误]食谱不存在, menuId:" + menuId); |
||||
|
if (statuses != null && statuses.length > 0) { |
||||
|
Assert.isTrue(Arrays.stream(statuses).noneMatch(menuStatus -> menu.getStatus().equals(menuStatus)), "[参数错误]食谱已通过审核或者发布, menuId:" + menuId); |
||||
|
} |
||||
|
return menu; |
||||
|
} |
||||
|
|
||||
|
private Dish checkAndConvert(Long dishId) { |
||||
|
Assert.notNull(dishId, "[参数错误]菜品不存在: dish=" + dishId); |
||||
|
Dish dish = isAdmin() ? dishService.get(dishId) : dishService.get(dishId, getVender()); |
||||
|
Assert.notNull(dish, "[参数错误]菜品不存在: dish=" + dishId); |
||||
|
return dish; |
||||
|
} |
||||
|
|
||||
|
private List<MenuDishItemDTO> parseItems(String ingredient, Set<String> crows) { |
||||
|
Assert.isTrue(JSON.isValid(ingredient), "[参数错误]菜品成分解析失败!"); |
||||
|
List<MenuDishItemDTO> items = JSON.parseArray(ingredient, MenuDishItemDTO.class); |
||||
|
Assert.isTrue(items.stream().allMatch(item -> crows.containsAll(item.getValue().keySet())), "[参数错误]人群不在食谱的设置范围内!"); |
||||
|
Assert.isTrue(items.stream().allMatch(item -> ingredientService.existsIngredientByKey(item.getKey())), "[参数错误]请选择系统存在的食材!"); |
||||
|
return items; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,58 @@ |
|||||
|
package com.mathvision.diet.controller; |
||||
|
|
||||
|
import com.mathvision.diet.domian.MenuStatus; |
||||
|
import com.mathvision.diet.entity.Menu; |
||||
|
import com.mathvision.diet.service.MenuReleaseService; |
||||
|
import com.mathvision.diet.service.MenuService; |
||||
|
import org.apache.commons.lang3.time.DateUtils; |
||||
|
import org.springframework.data.domain.Page; |
||||
|
import org.springframework.data.domain.PageRequest; |
||||
|
import org.springframework.data.domain.Sort; |
||||
|
import org.springframework.stereotype.Controller; |
||||
|
import org.springframework.util.Assert; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMethod; |
||||
|
import org.springframework.web.bind.annotation.RequestParam; |
||||
|
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import java.util.Date; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
@RequestMapping("/api/menu/release") |
||||
|
@Controller |
||||
|
public class MenuReleaseController extends BaseController { |
||||
|
@Resource |
||||
|
MenuService menuService; |
||||
|
@Resource |
||||
|
MenuReleaseService menuReleaseService; |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.PUT) |
||||
|
public void publish(@RequestParam Long id, @RequestParam(required = false) Map<String, Integer> scale, @RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime) { |
||||
|
Menu menu = menuService.get(id); |
||||
|
Assert.notNull(menu, "[参数错误]食谱不存在!"); |
||||
|
Assert.isTrue(menu.getVender().equals(getVender()) || isAdmin(), "[参数错误]食谱不存在!"); |
||||
|
Assert.isTrue(menu.getStatus() == MenuStatus.pass, "[参数错误]该食谱当前非审批通过状态,不可发布!"); |
||||
|
menu.getScale().entrySet().forEach(x -> x.setValue(scale.getOrDefault(x.getKey(), 0))); |
||||
|
menuReleaseService.publish(id, menu.getScale(), parseDate(startTime, new Date()), parseDate(endTime, DateUtils.addDays(new Date(), menu.getDay().intValue())), getUid()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.DELETE) |
||||
|
public void cancel(@RequestParam Long id) { |
||||
|
Menu menu = menuService.get(id); |
||||
|
Assert.notNull(menu, "[参数错误]食谱不存在!"); |
||||
|
Assert.isTrue(menu.getVender().equals(getVender()) || isAdmin(), "[参数错误]食谱不存在!"); |
||||
|
Assert.isTrue(menu.getStatus() == MenuStatus.publish, "[参数错误]该食谱非发布状态, 不可撤销发布!"); |
||||
|
menuReleaseService.cancel(id, getUid()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.GET) |
||||
|
public Page<Menu> query(@RequestParam(required = false) String name, @RequestParam(required = false) Long vender, @RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime, @RequestParam(required = false, defaultValue = "0") int pageNo, @RequestParam(required = false, defaultValue = "20") int pageSize) { |
||||
|
return menuReleaseService.list(vender, name, parseDate(startTime), parseDate(endTime), PageRequest.of(pageNo, pageSize).withSort(Sort.by(Sort.Direction.DESC, "id"))); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
@ -0,0 +1,77 @@ |
|||||
|
package com.mathvision.diet.controller; |
||||
|
|
||||
|
import com.mathvision.diet.domian.MenuStatus; |
||||
|
import com.mathvision.diet.entity.Menu; |
||||
|
import com.mathvision.diet.service.MenuReviewService; |
||||
|
import com.mathvision.diet.service.MenuService; |
||||
|
import org.springframework.data.domain.Page; |
||||
|
import org.springframework.data.domain.PageRequest; |
||||
|
import org.springframework.data.domain.Sort; |
||||
|
import org.springframework.stereotype.Controller; |
||||
|
import org.springframework.util.Assert; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMethod; |
||||
|
import org.springframework.web.bind.annotation.RequestParam; |
||||
|
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
|
||||
|
@RequestMapping("/api/menu/review") |
||||
|
@Controller |
||||
|
public class MenuReviewController extends BaseController { |
||||
|
|
||||
|
@Resource |
||||
|
MenuService menuService; |
||||
|
|
||||
|
@Resource |
||||
|
MenuReviewService menuReviewService; |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.PUT) |
||||
|
public void submit(@RequestParam Long id) { |
||||
|
Menu menu = menuService.get(id); |
||||
|
Assert.notNull(menu, "[参数错误]食谱不存在!"); |
||||
|
Assert.isTrue(menu.getVender().equals(getVender()) || isAdmin(), "[参数错误]食谱不存在!"); |
||||
|
Assert.isTrue(menu.getStatus() == MenuStatus.draft || menu.getStatus() == MenuStatus.reject, "[参数错误]提交审核失败: 该食谱当前非草稿或者审核失败状态!"); |
||||
|
menuReviewService.submit(id, getUid()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.DELETE) |
||||
|
public void disable(@RequestParam Long id) { |
||||
|
Assert.isTrue(isAdmin(), "[参数错误]无审批权限!"); |
||||
|
Menu menu = menuService.get(id); |
||||
|
Assert.notNull(menu, "[参数错误]食谱不存在!"); |
||||
|
Assert.isTrue(menu.getVender().equals(getVender()) || isAdmin(), "[参数错误]食谱不存在!"); |
||||
|
Assert.isTrue(menu.getStatus() != MenuStatus.pass && menu.getStatus() != MenuStatus.publish, "[参数错误]禁用食谱失败: 该食谱当前非审批通过或者发布状态!"); |
||||
|
menuReviewService.disable(id, getUid()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.POST) |
||||
|
public void approve(@RequestParam Long id, @RequestParam boolean pass, @RequestParam(required = false) String reason) { |
||||
|
Assert.isTrue(isAdmin(), "[参数错误]无审批权限!"); |
||||
|
Menu menu = menuService.get(id); |
||||
|
Assert.notNull(menu, "[参数错误]食谱不存在!"); |
||||
|
Assert.isTrue(menu.getVender().equals(getVender()) || isAdmin(), "[参数错误]食谱不存在!"); |
||||
|
Assert.isTrue(menu.getStatus() == MenuStatus.submit || menu.getStatus() == MenuStatus.reject, "[参数错误]审批食谱失败: 该食谱当前非提交审核状态!"); |
||||
|
if(pass){ |
||||
|
menuReviewService.pass(id, reason, getUid()); |
||||
|
} else { |
||||
|
menuReviewService.reject(id, reason, getUid()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.GET) |
||||
|
public Page<Menu> query(@RequestParam(required = false) String name, @RequestParam(required = false) Integer status, @RequestParam(required = false) Long vender, @RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime, @RequestParam(required = false, defaultValue = "0") int pageNo, @RequestParam(required = false, defaultValue = "20") int pageSize) { |
||||
|
return menuReviewService.list(vender, MenuStatus.toType(status), name, parseDate(startTime), parseDate(endTime), PageRequest.of(pageNo, pageSize).withSort(Sort.by(Sort.Direction.DESC, "id"))); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(value = "count", method = RequestMethod.GET) |
||||
|
public Object count() { |
||||
|
Assert.isTrue(isAdmin(), "[参数错误]无审批权限!"); |
||||
|
return menuReviewService.count(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,112 @@ |
|||||
|
package com.mathvision.diet.controller; |
||||
|
|
||||
|
import com.alibaba.fastjson2.JSON; |
||||
|
import com.alibaba.fastjson2.TypeReference; |
||||
|
import com.mathvision.diet.entity.Nutrition; |
||||
|
import com.mathvision.diet.service.NutritionService; |
||||
|
import org.apache.commons.collections4.CollectionUtils; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.data.domain.Page; |
||||
|
import org.springframework.data.domain.PageRequest; |
||||
|
import org.springframework.data.domain.Sort; |
||||
|
import org.springframework.stereotype.Controller; |
||||
|
import org.springframework.util.Assert; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMethod; |
||||
|
import org.springframework.web.bind.annotation.RequestParam; |
||||
|
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import java.math.BigDecimal; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
@RequestMapping("/api/nutrition") |
||||
|
@Controller |
||||
|
public class NutritionController extends BaseController { |
||||
|
@Resource |
||||
|
NutritionService nutritionService; |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.PUT) |
||||
|
public Nutrition add(@RequestParam String name, @RequestParam List<Long> vendors, @RequestParam BigDecimal overflow) { |
||||
|
Assert.isTrue(isAdmin(), "[参数错误]无操作权限!"); |
||||
|
Assert.isTrue(StringUtils.isNotBlank(name), "[参数错误]营养计划名称必填!"); |
||||
|
Assert.notNull(overflow, "[参数错误]溢出范围必填!"); |
||||
|
Assert.isTrue(CollectionUtils.isNotEmpty(vendors), "[参数错误]单位列表必填!"); |
||||
|
Assert.isTrue(nutritionService.notExists(name), "[参数错误]营养计划名称已存在!"); |
||||
|
return nutritionService.add(Nutrition.builder().name(name).overflow(overflow).vendors(vendors).build(), getUid()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.DELETE) |
||||
|
public void delete(@RequestParam Long id) { |
||||
|
Assert.isTrue(isAdmin(), "[参数错误]无操作权限!"); |
||||
|
Assert.notNull(id, "[参数错误]营养计划必填!"); |
||||
|
Assert.isTrue(nutritionService.exists(id), "[参数错误]营养计划不存在!"); |
||||
|
|
||||
|
nutritionService.delete(id, getUid()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@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) { |
||||
|
Assert.isTrue(isAdmin(), "[参数错误]无操作权限!"); |
||||
|
Assert.isTrue(foodCategoryDay == null || JSON.isValid(foodCategoryDay), "[参数错误]日标准JSON解析错误!"); |
||||
|
Assert.isTrue(foodCategoryWeek == null || JSON.isValid(foodCategoryWeek), "[参数错误]周标准JSON解析错误!"); |
||||
|
Assert.isTrue(ingredient == null || JSON.isValid(ingredient), "[参数错误]食材JSON解析错误!"); |
||||
|
|
||||
|
Nutrition nutrition = nutritionService.get(id); |
||||
|
Assert.isTrue(nutrition != null, "[参数错误]营养计划不存在!"); |
||||
|
|
||||
|
boolean flag = false; |
||||
|
if(StringUtils.isNotBlank(name) && !StringUtils.equals(name, nutrition.getName()) && nutritionService.notExists(name)) { |
||||
|
nutrition.setName(name); |
||||
|
flag = true; |
||||
|
} |
||||
|
if(overflow != null && !nutrition.getOverflow().equals(overflow)) { |
||||
|
nutrition.setOverflow(overflow); |
||||
|
flag = true; |
||||
|
} |
||||
|
if(CollectionUtils.isNotEmpty(vendors)) { |
||||
|
nutrition.setVendors(vendors); |
||||
|
flag = true; |
||||
|
} |
||||
|
if (JSON.isValid(foodCategoryDay)) { |
||||
|
nutrition.setFoodCategoryDay(JSON.parseObject(foodCategoryDay, new TypeReference<Map<String, BigDecimal>>(){})); |
||||
|
flag = true; |
||||
|
} |
||||
|
if (JSON.isValid(foodCategoryWeek)) { |
||||
|
nutrition.setFoodCategoryWeek(JSON.parseObject(foodCategoryWeek, new TypeReference<Map<String, BigDecimal>>(){})); |
||||
|
flag = true; |
||||
|
} |
||||
|
if (JSON.isValid(foodCategoryWeek)) { |
||||
|
nutrition.setFoodCategoryWeek(JSON.parseObject(foodCategoryWeek, new TypeReference<Map<String, BigDecimal>>(){})); |
||||
|
flag = true; |
||||
|
} |
||||
|
if (JSON.isValid(ingredient)) { |
||||
|
nutrition.setIngredient(JSON.parseObject(ingredient, new TypeReference<Map<String, Map<String, Map<String, BigDecimal>>>>(){})); |
||||
|
flag = true; |
||||
|
} |
||||
|
if (flag) { |
||||
|
nutrition = nutritionService.update(nutrition, getUid()); |
||||
|
} |
||||
|
return nutrition; |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.GET) |
||||
|
public Page<Nutrition> query(@RequestParam(required = false) String keyword, @RequestParam(required = false, defaultValue = "0") int pageNo, @RequestParam(required = false, defaultValue = "20") int pageSize) { |
||||
|
Assert.isTrue(isAdmin(), "[参数错误]无操作权限!"); |
||||
|
return nutritionService.list(keyword, PageRequest.of(pageNo, pageSize).withSort(Sort.by(Sort.Direction.DESC, "id"))); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(value="select", method = RequestMethod.GET) |
||||
|
public Object query(@RequestParam(required = false) Long id, @RequestParam(required = false) String keyword, @RequestParam(required = false) Long vender) { |
||||
|
if (id != null) { |
||||
|
return nutritionService.get(id); |
||||
|
} |
||||
|
return nutritionService.query(isAdmin() ? vender : getVender(), keyword); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,53 @@ |
|||||
|
package com.mathvision.diet.controller; |
||||
|
|
||||
|
import com.mathvision.diet.domian.AuthType; |
||||
|
import com.mathvision.diet.domian.RoleType; |
||||
|
import com.mathvision.diet.entity.Role; |
||||
|
import com.mathvision.diet.entity.RoleItem; |
||||
|
import com.mathvision.diet.service.UserService; |
||||
|
import org.springframework.stereotype.Controller; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMethod; |
||||
|
import org.springframework.web.bind.annotation.RequestParam; |
||||
|
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@RequestMapping("/api/role") |
||||
|
@Controller |
||||
|
public class RoleController extends BaseController { |
||||
|
|
||||
|
@Resource |
||||
|
private UserService userService; |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(value = "item") |
||||
|
public List<RoleItem> listRoleItems() { |
||||
|
return userService.listRoleItems(isAdmin() ? AuthType.SERVER : AuthType.CLIENT); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.PUT) |
||||
|
public void addRole(@RequestParam String roleName, @RequestParam(required = false) List<Long> items) { |
||||
|
userService.addRole(roleName, items, RoleType.CUSTOM, isAdmin() ? AuthType.SERVER : AuthType.CLIENT, getVender(), getUid()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.DELETE) |
||||
|
public void delRole(@RequestParam Long roleId) { |
||||
|
userService.delRole(roleId, getVender(), getUid()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.POST) |
||||
|
public void modRole(@RequestParam Long roleId, @RequestParam(required = false) String roleName, @RequestParam(required = false) List<Long> items) { |
||||
|
userService.changeRole(roleId, roleName, items, isAdmin() ? AuthType.SERVER : AuthType.CLIENT, getVender(), getUid()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.GET) |
||||
|
public List<Role> listRole() { |
||||
|
return userService.listRole(getVender()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,54 @@ |
|||||
|
package com.mathvision.diet.controller; |
||||
|
|
||||
|
import com.mathvision.diet.domain.UserDO; |
||||
|
import com.mathvision.diet.service.UserService; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.stereotype.Controller; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMethod; |
||||
|
import org.springframework.web.bind.annotation.RequestParam; |
||||
|
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@RequestMapping("/api/user") |
||||
|
@Controller |
||||
|
public class UserController extends BaseController { |
||||
|
|
||||
|
@Resource |
||||
|
private UserService userService; |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(value = "check") |
||||
|
public boolean check(@RequestParam String uid) { |
||||
|
return userService.checkUser(uid); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.PUT) |
||||
|
public void addUser(@RequestParam String uid, @RequestParam String name, @RequestParam String password, @RequestParam Long roleId) { |
||||
|
userService.addUser(uid, name, password, roleId, getVender(), getUid()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.DELETE) |
||||
|
public void delUser(@RequestParam String uid) { |
||||
|
userService.delUser(uid, getVender(), getUid()); |
||||
|
if (StringUtils.isBlank(uid)) { |
||||
|
delSession(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.POST) |
||||
|
public void modUser(@RequestParam String uid, @RequestParam(required = false) String name, @RequestParam(required = false) String password, @RequestParam(required = false) Long roleId) { |
||||
|
userService.changeUser(uid, name, password, roleId, getVender(), getUid()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.GET) |
||||
|
public List<UserDO> listUser() { |
||||
|
return userService.listUser(getVender()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,164 @@ |
|||||
|
package com.mathvision.diet.controller; |
||||
|
|
||||
|
import com.mathvision.diet.domain.UserDO; |
||||
|
import com.mathvision.diet.domian.VenderType; |
||||
|
import com.mathvision.diet.entity.Vender; |
||||
|
import com.mathvision.diet.entity.VenderConfig; |
||||
|
import com.mathvision.diet.service.UserService; |
||||
|
import com.mathvision.diet.service.VenderService; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.data.domain.Page; |
||||
|
import org.springframework.data.domain.PageRequest; |
||||
|
import org.springframework.data.domain.Sort; |
||||
|
import org.springframework.format.annotation.DateTimeFormat; |
||||
|
import org.springframework.stereotype.Controller; |
||||
|
import org.springframework.util.Assert; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMethod; |
||||
|
import org.springframework.web.bind.annotation.RequestParam; |
||||
|
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import java.math.BigDecimal; |
||||
|
import java.time.Instant; |
||||
|
import java.util.Date; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@RequestMapping("/api/vender") |
||||
|
@Controller |
||||
|
public class VenderController extends BaseController { |
||||
|
|
||||
|
@Resource |
||||
|
private VenderService venderService; |
||||
|
|
||||
|
@Resource |
||||
|
private UserService userService; |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(value = "check/account") |
||||
|
public boolean checkAccount(@RequestParam String account) { |
||||
|
return venderService.checkAccount(account); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(value = "check/name") |
||||
|
public boolean checkName(@RequestParam String name) { |
||||
|
return venderService.checkName(name); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(value = "config", method = RequestMethod.GET) |
||||
|
public VenderConfig queryConfig() { |
||||
|
Assert.isTrue(!isAdmin(), "[无权限]仅业务端可以操作!"); |
||||
|
return venderService.queryConfig(getVender()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(value = "config", method = RequestMethod.POST) |
||||
|
public void modConfig(@RequestParam BigDecimal breakfast, @RequestParam BigDecimal lunch, @RequestParam BigDecimal dinner) { |
||||
|
Assert.isTrue(!isAdmin(), "[无权限]仅业务端可以操作!"); |
||||
|
venderService.modConfig(getVender(), breakfast, lunch, dinner, getUid()); |
||||
|
} |
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.PUT) |
||||
|
public void addVender(@RequestParam String account, @RequestParam String password, @RequestParam String name, @RequestParam String category, @RequestParam @DateTimeFormat(pattern="yyyy-MM-dd") Date expire, @RequestParam(required = false) String icon, @RequestParam(required = false) String address, @RequestParam(required = false) String contacts, @RequestParam(required = false) String phone, @RequestParam(required = false) String email) { |
||||
|
VenderType venderType = VenderType.toType(category); |
||||
|
Assert.isTrue(isAdmin(), "[无权限]仅系统管理员可以操作!"); |
||||
|
Assert.notNull(venderType, "[参数错误]单位类型必选!"); |
||||
|
Assert.isTrue(StringUtils.isNotBlank(password), "[参数错误]初始密码必填!"); |
||||
|
Assert.isTrue(Instant.now().isBefore(expire.toInstant()), "[参数错误]过期时间不能小于现在!"); |
||||
|
Assert.isTrue(StringUtils.isNotBlank(name) && checkName(name), "[参数错误]单位名称必填,且不能重复!"); |
||||
|
Assert.isTrue(StringUtils.isNotBlank(account) && checkAccount(account), "[参数错误]账号必填,且不能重复!"); |
||||
|
Vender vender = Vender.builder().account(account).name(name).category(venderType).expire(expire.toInstant()).icon(icon).address(address).phone(phone).email(email).contacts(contacts).build(); |
||||
|
venderService.addVender(vender, password, getUid()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.DELETE) |
||||
|
public void delVender(@RequestParam Long venderId) { |
||||
|
Assert.isTrue(isAdmin(), "[无权限]仅系统管理员可以操作!"); |
||||
|
venderService.delVender(venderId, getUid()); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.POST) |
||||
|
public void modVender(@RequestParam Long venderId, @RequestParam(required = false) String account, @RequestParam(required = false) String name, @RequestParam(required = false) String category, @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd") Date expire, @RequestParam(required = false) Boolean status, @RequestParam(required = false) String icon, @RequestParam(required = false) String address, @RequestParam(required = false) String contacts, @RequestParam(required = false) String phone, @RequestParam(required = false) String email) { |
||||
|
venderId = auth(venderId); |
||||
|
VenderType venderType = VenderType.toType(category); |
||||
|
boolean change = false; |
||||
|
Vender vender = venderService.queryVender(venderId); |
||||
|
Assert.isTrue(vender != null, "[参数错误]单位不存在!"); |
||||
|
Assert.isTrue(vender.getAccount().equals(getUid()) || isAdmin(), "[无权限]仅主账号或者系统管理员可以操作!"); |
||||
|
|
||||
|
if (expire != null && !expire.toInstant().equals(vender.getExpire()) && Instant.now().isBefore(expire.toInstant())) { |
||||
|
Assert.isTrue(isAdmin(), "[无权限]仅系统管理员可以操作!"); |
||||
|
vender.setExpire(expire.toInstant()); |
||||
|
change = true; |
||||
|
} |
||||
|
if(status != null && !status.equals(vender.getStatus())) { |
||||
|
Assert.isTrue(isAdmin(), "[无权限]仅系统管理员可以操作!"); |
||||
|
vender.setStatus(status); |
||||
|
change = true; |
||||
|
} |
||||
|
if (StringUtils.isNotBlank(name) && !StringUtils.equals(name, vender.getName()) && checkName(name)) { |
||||
|
vender.setName(name); |
||||
|
change = true; |
||||
|
} |
||||
|
if(venderType != null && !venderType.equals(vender.getCategory())) { |
||||
|
vender.setCategory(venderType); |
||||
|
change = true; |
||||
|
} |
||||
|
if (StringUtils.isNotBlank(account) && !StringUtils.equals(account, vender.getAccount())) { |
||||
|
UserDO userDO = userService.queryUser(account); |
||||
|
Assert.isTrue(userDO != null && venderId.equals(userDO.getVender().getId()), "[参数错误]绑定账户不存在!"); |
||||
|
Long adminRoleId = isAdmin() ? userService.queryUser(vender.getAccount()).getRoleId() : getRoleId(); |
||||
|
Assert.isTrue(adminRoleId != null, "[参数错误]单位管理员角色缺失,请联系系统管理员处理!"); |
||||
|
if (!adminRoleId.equals(userDO.getRoleId())) { |
||||
|
userService.changeUser(account, null, null, adminRoleId, venderId, getUid()); |
||||
|
} |
||||
|
vender.setAccount(account); |
||||
|
change = true; |
||||
|
} |
||||
|
if(icon != null && !StringUtils.equals(icon, vender.getIcon())) { |
||||
|
vender.setIcon(icon); |
||||
|
change = true; |
||||
|
} |
||||
|
if(icon != null && !StringUtils.equals(icon, vender.getIcon())) { |
||||
|
vender.setIcon(icon); |
||||
|
change = true; |
||||
|
} |
||||
|
if(phone != null && !StringUtils.equals(phone, vender.getPhone())) { |
||||
|
vender.setPhone(phone); |
||||
|
change = true; |
||||
|
} |
||||
|
if(email != null && !StringUtils.equals(email, vender.getEmail())) { |
||||
|
vender.setEmail(email); |
||||
|
change = true; |
||||
|
} |
||||
|
if(contacts != null && !StringUtils.equals(contacts, vender.getContacts())) { |
||||
|
vender.setContacts(contacts); |
||||
|
change = true; |
||||
|
} |
||||
|
if(address != null && !StringUtils.equals(address, vender.getAddress())) { |
||||
|
vender.setAddress(address); |
||||
|
change = true; |
||||
|
} |
||||
|
if (change) { |
||||
|
venderService.modVender(vender, getUid()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(method = RequestMethod.GET) |
||||
|
public Page<Vender> pageVender(@RequestParam(required = false) String keyword, @RequestParam(required = false) String category, @RequestParam(required = false, defaultValue = "0") int pageNo, @RequestParam(required = false, defaultValue = "20") int pageSize) { |
||||
|
Assert.isTrue(isAdmin(), "[无权限]仅系统管理员可以操作!"); |
||||
|
return venderService.pageVender(keyword, VenderType.toType(category), PageRequest.of(pageNo, pageSize).withSort(Sort.by(Sort.Direction.DESC, "id"))); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@RequestMapping(value = "select", method = RequestMethod.GET) |
||||
|
public List<Vender> listVender(@RequestParam(required = false) String keyword, @RequestParam(required = false) List<Long> vendors) { |
||||
|
Assert.isTrue(isAdmin(), "[无权限]仅系统管理员可以操作!"); |
||||
|
return venderService.listVender(keyword, vendors); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,64 @@ |
|||||
|
package com.mathvision.diet.domain; |
||||
|
|
||||
|
import lombok.Getter; |
||||
|
import lombok.Setter; |
||||
|
import lombok.ToString; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
|
||||
|
/** |
||||
|
* 统一输出定义 |
||||
|
* |
||||
|
* @author caoyiwen |
||||
|
* @creation 2015年9月2日 |
||||
|
*/ |
||||
|
@ToString |
||||
|
public class Result implements Serializable { |
||||
|
|
||||
|
private static final long serialVersionUID = 1L; |
||||
|
public static final Result NOT_SUPPORT = new Result(ResultCode.not_support_operate); |
||||
|
public static final Result NOT_PRIVILEGED = new Result(ResultCode.not_privileged); |
||||
|
public static final Result INVALID_USER_PASS = new Result(ResultCode.invalid_user_password); |
||||
|
public static final Result EXPIRED = new Result(ResultCode.expired_vender); |
||||
|
public static final Result ILLEGAL_ARGUMENT = new Result(ResultCode.illegal_argument); |
||||
|
public static final Result FAILURE = new Result(ResultCode.operate_failure); |
||||
|
public static final Result ERROR = new Result(ResultCode.system_error); |
||||
|
public static final Result NOT_LOGIN = new Result(ResultCode.need_login); |
||||
|
public static final Result SUCCESS = new Result(ResultCode.success); |
||||
|
|
||||
|
@Getter |
||||
|
@Setter |
||||
|
private int code; |
||||
|
@Getter |
||||
|
@Setter |
||||
|
private String desc; |
||||
|
@Getter |
||||
|
@Setter |
||||
|
private Object body; |
||||
|
|
||||
|
public Result(ResultCode resultCode) { |
||||
|
setCode(resultCode.getCode()); |
||||
|
setDesc(resultCode.getDesc()); |
||||
|
} |
||||
|
|
||||
|
public Result(int code, String msg) { |
||||
|
setCode(code); |
||||
|
setDesc(msg); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 使用通用结果码生成对象 |
||||
|
*/ |
||||
|
public Result(ResultCode resultCode, Object body) { |
||||
|
setCode(resultCode.getCode()); |
||||
|
setDesc(resultCode.getDesc()); |
||||
|
setBody(body); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 判断执行结果是否成功 |
||||
|
*/ |
||||
|
public boolean isSuccess() { |
||||
|
return ResultCode.success.getCode() == code; |
||||
|
} |
||||
|
} |
||||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue