caoyiwen 1 year ago
parent
commit
024c7af5b5
  1. 2
      diet-core/src/main/java/com/mathvision/diet/domain/ComponentAnalysisDO.java
  2. 5
      diet-core/src/main/java/com/mathvision/diet/excel/IngredientModel.java
  3. 42
      diet-core/src/main/java/com/mathvision/diet/service/DishService.java
  4. 15
      diet-core/src/main/java/com/mathvision/diet/service/IngredientService.java
  5. 2
      diet-web/src/main/java/com/mathvision/diet/controller/IngredientController.java
  6. 6
      diet-web/src/main/java/com/mathvision/diet/controller/VenderController.java
  7. 36
      diet-web/src/main/resources/static/vender.html
  8. 42
      doc/vender.md

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

@ -10,7 +10,7 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
public class ComponentAnalysisDO {
String key;
String name;
String nutrition;
String nvr;

5
diet-core/src/main/java/com/mathvision/diet/excel/IngredientModel.java

@ -1,10 +1,7 @@
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 com.alibaba.excel.annotation.write.style.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

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

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

15
diet-core/src/main/java/com/mathvision/diet/service/IngredientService.java

@ -4,6 +4,8 @@ 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.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.google.common.collect.Lists;
import com.mathvision.diet.domian.MarkType;
import com.mathvision.diet.entity.Ingredient;
@ -17,6 +19,8 @@ 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.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
@ -51,7 +55,12 @@ public class IngredientService {
private IngredientMarkRepository ingredientMarkRepository;
public void template(OutputStream outputStream) {
EasyExcel.write(outputStream).head(IngredientModel.class).excelType(ExcelTypeEnum.XLSX).sheet("模板").doWrite(Lists.newArrayList());
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
headWriteCellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
EasyExcel.write(outputStream).registerWriteHandler(new HorizontalCellStyleStrategy(headWriteCellStyle, new WriteCellStyle()))
.head(IngredientModel.class).excelType(ExcelTypeEnum.XLSX).sheet("食材导入模板").doWrite(Lists.newArrayList());
}
public void upload(InputStream inputStream, OutputStream outputStream, String operator) {
@ -63,7 +72,9 @@ public class IngredientService {
@Override
public void onException(Exception exception, AnalysisContext context) {
Assert.isTrue(exception == null, "导入异常:" + exception.getMessage());
if (exception != null) {
throw new IllegalArgumentException("导入异常:" + exception.getMessage());
}
}
@Override

2
diet-web/src/main/java/com/mathvision/diet/controller/IngredientController.java

@ -130,7 +130,7 @@ public class IngredientController extends BaseController {
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"));
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + URLEncoder.encode("食材导入模板.xlsx", "UTF-8"));
ingredientService.template(response.getOutputStream());
}

6
diet-web/src/main/java/com/mathvision/diet/controller/VenderController.java

@ -148,6 +148,12 @@ public class VenderController extends BaseController {
}
}
@ResponseBody
@RequestMapping(value = "info", method = RequestMethod.GET)
public Vender info() {
return isAdmin() ? null : venderService.queryVender(getVender());
}
@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) {

36
diet-web/src/main/resources/static/vender.html

@ -188,4 +188,40 @@ vendors=1,2,3 //
&quot;success&quot;: true
}
</code></pre>
<h1>10. 获取当前企业信息</h1>
<blockquote>
<p>GET /api/vender/info</p>
<p>管理端接口</p>
</blockquote>
<h3>输入:</h3>
<pre><code></code></pre>
<h3>输出:</h3>
<pre><code>{
&quot;body&quot;: {
&quot;content&quot;: [
{
&quot;account&quot;: &quot;xxx&quot;,
&quot;address&quot;: &quot;百仁路&quot;,
&quot;area&quot;: &quot;青羊区&quot;,
&quot;category&quot;: &quot;小学&quot;,
&quot;city&quot;: &quot;成都市&quot;,
&quot;contacts&quot;: &quot;曹先生&quot;,
&quot;expire&quot;: 1695033585000,
&quot;icon&quot;: &quot;1232334234.jpg&quot;,
&quot;id&quot;: 1,
&quot;name&quot;: &quot;成都实验小学&quot;,
&quot;phone&quot;: &quot;13919103408&quot;,
&quot;province&quot;: &quot;四川省&quot;,
&quot;status&quot;: true
}
],
&quot;number&quot;: 0,
&quot;size&quot;: 20,
&quot;totalElements&quot;: 1,
&quot;totalPages&quot;: 1
},
&quot;code&quot;: 200,
&quot;desc&quot;: &quot;成功&quot;,
&quot;success&quot;: true
}</code></pre>

42
doc/vender.md

@ -218,4 +218,44 @@ vendors=1,2,3 // 根据ID批量获取单位信息
"desc": "成功",
"success": true
}
```
```
# 10. 获取当前企业信息
> GET /api/vender/info
>
> 管理端接口
### 输入:
```
```
### 输出:
```
{
"body": {
"content": [
{
"account": "xxx",
"address": "百仁路",
"area": "青羊区",
"category": "小学",
"city": "成都市",
"contacts": "曹先生",
"expire": 1695033585000,
"icon": "1232334234.jpg",
"id": 1,
"name": "成都实验小学",
"phone": "13919103408",
"province": "四川省",
"status": true
}
],
"number": 0,
"size": 20,
"totalElements": 1,
"totalPages": 1
},
"code": 200,
"desc": "成功",
"success": true
}
Loading…
Cancel
Save