caoyiwen 1 year ago
parent
commit
570c4d25cc
  1. 38
      diet-core/src/main/java/com/mathvision/diet/service/MenuDishService.java
  2. 82
      diet-core/src/main/java/com/mathvision/diet/service/MenuReportService.java
  3. 33
      diet-web/src/main/java/com/mathvision/diet/controller/MenuDishController.java
  4. 2
      diet-web/src/main/java/com/mathvision/diet/controller/MenuReportController.java
  5. 6
      sql/update.sql

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

@ -1,32 +1,29 @@
package com.mathvision.diet.service; package com.mathvision.diet.service;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.mathvision.diet.domian.MealType; import com.mathvision.diet.domian.MealType;
import com.mathvision.diet.domian.MenuDishItemDTO; import com.mathvision.diet.domian.MenuDishItemDTO;
import com.mathvision.diet.entity.*; import com.mathvision.diet.entity.Dish;
import com.mathvision.diet.entity.Menu;
import com.mathvision.diet.entity.MenuDish;
import com.mathvision.diet.repository.DishRepository; import com.mathvision.diet.repository.DishRepository;
import com.mathvision.diet.repository.MenuDishRepository; import com.mathvision.diet.repository.MenuDishRepository;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.OutputStream;
import java.time.Instant; import java.time.Instant;
import java.util.*; import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -114,7 +111,7 @@ public class MenuDishService {
return menuDishRepository.distinctDaysByMenuAndVender(menuId, vender); return menuDishRepository.distinctDaysByMenuAndVender(menuId, vender);
} }
public void export(Menu menu, OutputStream outputStream) { public void export(Menu menu, ExcelWriter excelWriter) {
Map<String, Map<Long, List<MenuDish>>> menuDishes = menuDishRepository.findByMenu(menu.getId()).stream().collect(Collectors.groupingBy(MenuDish::getMeal, Collectors.groupingBy(MenuDish::getDay))); Map<String, Map<Long, List<MenuDish>>> menuDishes = menuDishRepository.findByMenu(menu.getId()).stream().collect(Collectors.groupingBy(MenuDish::getMeal, Collectors.groupingBy(MenuDish::getDay)));
List<String> allMeals = new ArrayList<>(menuDishes.keySet()).stream().sorted(Comparator.comparing(MealType::toType)).collect(Collectors.toList()); List<String> allMeals = new ArrayList<>(menuDishes.keySet()).stream().sorted(Comparator.comparing(MealType::toType)).collect(Collectors.toList());
List<Long> allDays = menuDishes.entrySet().stream().flatMap(kv -> kv.getValue().keySet().stream()).distinct().sorted().collect(Collectors.toList()); List<Long> allDays = menuDishes.entrySet().stream().flatMap(kv -> kv.getValue().keySet().stream()).distinct().sorted().collect(Collectors.toList());
@ -170,21 +167,6 @@ public class MenuDishService {
}); });
}); });
}); });
excelWriter.write(contents, EasyExcel.writerSheet(menu.getName()).head(headers).build());
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
headWriteCellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
EasyExcel.write(outputStream).head(headers)
.registerWriteHandler(new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle))
.registerWriteHandler(new SimpleColumnWidthStyleStrategy(25))
.registerWriteHandler(new SimpleRowHeightStyleStrategy((short)30, (short)20))
.excelType(ExcelTypeEnum.XLSX).sheet(menu.getName()).doWrite(contents);
} }
} }

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

@ -1,7 +1,10 @@
package com.mathvision.diet.service; package com.mathvision.diet.service;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Range; import com.google.common.collect.Range;
@ -21,6 +24,7 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -305,6 +309,84 @@ public class MenuReportService {
return result; return result;
} }
public void export(Menu menu, ExcelWriter excelWriter) {
List<String> crows = menu.getCrows();
crows.forEach(crow -> {
List<List<String>> headers = Lists.newArrayList();
headers.add(Lists.newArrayList("日均营养分析", "营养素"));
headers.add(Lists.newArrayList("日均营养分析", "实际摄入"));
headers.add(Lists.newArrayList("日均营养分析", "标准范围"));
headers.add(Lists.newArrayList("日均营养分析", "UL值"));
headers.add(Lists.newArrayList("日均营养分析", "溢出范围"));
headers.add(Lists.newArrayList("日均营养分析", "评价"));
JSONObject nutrition = nutrition(menu, 0, crow);
JSONArray ingredient = nutrition.getJSONArray("ingredient");
if (ingredient == null) {
return;
}
List<List<Object>> contents = ingredient.stream().map(x -> (JSONObject)x).map(x -> Lists.newArrayList(x.get("nutrition"), x.get("virtual"), x.get("standard"), x.get("ul"), x.get("overload"), x.get("conclusion"))).collect(Collectors.toList());
excelWriter.write(contents, EasyExcel.writerSheet("日均营养分析-" + crow).head(headers).build());
});
crows.forEach(crow -> {
List<List<String>> headers = Lists.newArrayList();
headers.add(Lists.newArrayList("日均能量来源分布", "能量占比"));
headers.add(Lists.newArrayList("日均能量来源分布", "要求(%)"));
headers.add(Lists.newArrayList("日均能量来源分布", "实际摄入(%)"));
headers.add(Lists.newArrayList("日均能量来源分布", "评价"));
JSONArray energy = energy(0, crow, menuDishService.query(menu.getId(), menu.getVender())).getJSONArray("energy");
if (energy == null) {
return;
}
List<List<Object>> contents = energy.stream().map(x -> (JSONObject)x).map(x -> Lists.newArrayList(x.get("name"), x.get("standard"), x.get("value"), x.get("conclusion"))).collect(Collectors.toList());
excelWriter.write(contents, EasyExcel.writerSheet("日均能量来源分布-" + crow).head(headers).build());
});
crows.forEach(crow -> {
List<List<String>> headers = Lists.newArrayList();
headers.add(Lists.newArrayList("食材种类统计", ""));
headers.add(Lists.newArrayList("食材种类统计", "种类名称"));
headers.add(Lists.newArrayList("食材种类统计", "至少需要"));
headers.add(Lists.newArrayList("食材种类统计", "当前含有"));
headers.add(Lists.newArrayList("食材种类统计", "还需"));
JSONObject types = types(menu, crow, menuDishService.query(menu.getId(), menu.getVender()));
if (types == null) {
return;
}
List<List<Object>> contents = Lists.newArrayList();
JSONArray weekRule = types.getJSONArray("weekRule");
if (weekRule != null) {
contents.addAll(weekRule.stream().map(x -> (JSONObject)x).map(x -> Lists.newArrayList("周合计", x.get("name"), MeasurementType.quantity.name().equals(x.getString("measurement")) ? x.get("standard") + "种" : x.get("standard") + "克", x.get("supplied"), x.get("lack"))).collect(Collectors.toList()));
}
Map<Long, List<JSONObject>> dayRule = types.getObject("dayRule", new TypeReference<Map<Long, List<JSONObject>>>() {});
if (dayRule != null) {
dayRule.forEach((k, v) -> contents.addAll(v.stream().map(x -> Lists.newArrayList("周" + x.getString("day"), x.get("name"), MeasurementType.quantity.name().equals(x.getString("measurement")) ? x.get("standard") + "种" : x.get("standard") + "克", x.get("supplied"), x.get("lack"))).collect(Collectors.toList())));
}
excelWriter.write(contents, EasyExcel.writerSheet("食材种类统计-" + crow).head(headers).build());
});
crows.forEach(crow -> {
List<List<String>> headers = Lists.newArrayList();
headers.add(Lists.newArrayList("烹饪方式统计", ""));
headers.add(Lists.newArrayList("烹饪方式统计", "烹饪方式"));
headers.add(Lists.newArrayList("烹饪方式统计", "菜品数量"));
JSONObject polys = poly(crow, menuDishService.query(menu.getId(), menu.getVender()));
if (polys == null) {
return;
}
List<List<Object>> contents = polys.entrySet().stream().filter(kv -> !StringUtils.equals(kv.getKey(), "crow")).map(kv -> ((Map<Object, Object>)kv.getValue()).entrySet().stream().map(x -> Lists.newArrayList(kv.getKey().equals("0") ? "周合计" : "周" + kv.getKey(), x.getKey(), x.getValue())).collect(Collectors.toList())).flatMap(Collection::stream).collect(Collectors.toList());
excelWriter.write(contents, EasyExcel.writerSheet("烹饪方式统计-" + crow).head(headers).build());
});
}
private JSONObject toEnergyContent(BigDecimal total, String key, BigDecimal value, Range<Integer> standard) { private JSONObject toEnergyContent(BigDecimal total, String key, BigDecimal value, Range<Integer> standard) {
BigDecimal percentage = total.intValue() == 0 ? BigDecimal.ZERO : value.divide(total, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)); BigDecimal percentage = total.intValue() == 0 ? BigDecimal.ZERO : value.divide(total, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
percentage = percentage.setScale(2, RoundingMode.HALF_UP); percentage = percentage.setScale(2, RoundingMode.HALF_UP);

33
diet-web/src/main/java/com/mathvision/diet/controller/MenuDishController.java

@ -1,5 +1,12 @@
package com.mathvision.diet.controller; package com.mathvision.diet.controller;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.mathvision.diet.domian.MenuDishItemDTO; import com.mathvision.diet.domian.MenuDishItemDTO;
import com.mathvision.diet.domian.MenuStatus; import com.mathvision.diet.domian.MenuStatus;
@ -9,6 +16,9 @@ import com.mathvision.diet.entity.MenuDish;
import com.mathvision.diet.service.*; import com.mathvision.diet.service.*;
import com.mathvision.diet.vo.MenuDishVO; import com.mathvision.diet.vo.MenuDishVO;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -40,6 +50,9 @@ public class MenuDishController extends BaseController {
@Resource @Resource
MenuDishService menuDishService; MenuDishService menuDishService;
@Resource
MenuReportService menuReportService;
@Resource @Resource
private IngredientService ingredientService; private IngredientService ingredientService;
@ -114,7 +127,25 @@ public class MenuDishController extends BaseController {
response.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment;filename*=utf-8''"+ URLEncoder.encode("[食谱导出]" + menu.getId() + "-"+ menu.getName() + ".xlsx", "UTF-8")); response.setHeader("Content-Disposition", "attachment;filename*=utf-8''"+ URLEncoder.encode("[食谱导出]" + menu.getId() + "-"+ menu.getName() + ".xlsx", "UTF-8"));
menuDishService.export(menu, response.getOutputStream()); WriteCellStyle headWriteCellStyle = new WriteCellStyle();
headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
headWriteCellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream())
.registerWriteHandler(new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle))
.registerWriteHandler(new SimpleColumnWidthStyleStrategy(25))
.registerWriteHandler(new SimpleRowHeightStyleStrategy((short)30, (short)20))
.excelType(ExcelTypeEnum.XLSX).build();
menuDishService.export(menu, excelWriter);
menuReportService.export(menu, excelWriter);
excelWriter.finish();
} }
@ResponseBody @ResponseBody

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

@ -33,7 +33,7 @@ public class MenuReportController extends BaseController {
@ResponseBody @ResponseBody
@RequestMapping(method = RequestMethod.GET) @RequestMapping(method = RequestMethod.GET)
public JSONObject analysis(@RequestParam Long id, @RequestParam(required = false) Long day, @RequestParam(required = false) String crow) { public JSONObject nutrition(@RequestParam Long id, @RequestParam(required = false) Long day, @RequestParam(required = false) String crow) {
Menu menu = checkAndGetMenu(id); Menu menu = checkAndGetMenu(id);
day = checkAndGetDay(day, menu.getDay()); day = checkAndGetDay(day, menu.getDay());
crow = checkAndGetCrow(crow, menu.getCrows()); crow = checkAndGetCrow(crow, menu.getCrows());

6
sql/update.sql

@ -25,8 +25,4 @@ CREATE TABLE `sugar` (
PRIMARY KEY (`id`) USING BTREE, PRIMARY KEY (`id`) USING BTREE,
INDEX `udx_vender`(`vender` ASC) USING BTREE, INDEX `udx_vender`(`vender` ASC) USING BTREE,
INDEX `idx_time`(`start_time` ASC, `end_time` ASC) USING BTREE INDEX `idx_time`(`start_time` ASC, `end_time` ASC) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci COMMENT = '油盐糖' ROW_FORMAT = Dynamic; ) ENGINE = InnoDB CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci COMMENT = '油盐糖' ROW_FORMAT = Dynamic;
INSERT INTO `sugar` VALUES (1, 1, '2023-50周', 5, 100.00, 100.00, 100.00, 100.00, 100.00, 100.00, '2023-12-11', '2023-12-17', 'system', '2023-12-16 21:38:38', '2023-12-16 21:38:38');
Loading…
Cancel
Save