diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/cook/controller/CookRecipeController.java b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/cook/controller/CookRecipeController.java index 44c4c23..3ff2fd0 100644 --- a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/cook/controller/CookRecipeController.java +++ b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/cook/controller/CookRecipeController.java @@ -4,6 +4,7 @@ import java.util.List; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; +import cn.hutool.core.util.ObjectUtil; import com.bonus.canteen.core.cook.dto.CookRecipeBindDTO; import com.bonus.canteen.core.cook.dto.CookRecipeDTO; import com.bonus.canteen.core.cook.dto.CookRecipeDetailDTO; @@ -14,12 +15,8 @@ import com.bonus.common.log.enums.OperaType; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.*; import com.bonus.common.log.annotation.SysLog; import com.bonus.canteen.core.cook.domain.CookRecipe; import com.bonus.canteen.core.cook.service.ICookRecipeService; @@ -27,6 +24,7 @@ import com.bonus.common.core.web.controller.BaseController; import com.bonus.common.core.web.domain.AjaxResult; import com.bonus.common.core.utils.poi.ExcelUtil; import com.bonus.common.core.web.page.TableDataInfo; +import org.springframework.web.multipart.MultipartFile; /** * 菜品计划信息Controller @@ -148,6 +146,23 @@ public class CookRecipeController extends BaseController { return AjaxResult.success(); } + @PostMapping({"/recipe/import/dishes"}) + @ApiOperation("批量导入菜谱中的菜品信息") + @Transactional + public AjaxResult recipeImportDishes(@RequestParam("file") MultipartFile excel, @RequestParam("recipeId") Long recipeId) { + try{ + return ObjectUtil.isNull(excel) ? AjaxResult.error("请选择导入文件 ^_^") : + AjaxResult.success(this.cookRecipeService.recipeImportDishes(excel, recipeId)); + }catch (Exception e){ + String message = e.toString(); + //取倒数第二个:之后的信息 + String finalMessage = message.substring(message.lastIndexOf(":") - 7); + String errorMes = finalMessage.replace("Error:", ""); + System.err.println("errorMes="+errorMes); + return error("导入失败:"+errorMes); + } + } + /** * 删除菜品计划信息 */ diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/cook/service/ICookRecipeService.java b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/cook/service/ICookRecipeService.java index 5672815..20d96cc 100644 --- a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/cook/service/ICookRecipeService.java +++ b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/cook/service/ICookRecipeService.java @@ -1,12 +1,15 @@ package com.bonus.canteen.core.cook.service; import java.util.List; +import java.util.Set; + import com.bonus.canteen.core.cook.domain.CookRecipe; import com.bonus.canteen.core.cook.dto.CookRecipeBindDTO; import com.bonus.canteen.core.cook.dto.CookRecipeDTO; import com.bonus.canteen.core.cook.dto.CookRecipeDetailDTO; import com.bonus.canteen.core.cook.vo.CookRecipeDetailVO; import com.bonus.canteen.core.cook.vo.CookRecipeVO; +import org.springframework.web.multipart.MultipartFile; /** * 菜品计划信息Service接口 @@ -57,6 +60,8 @@ public interface ICookRecipeService { public void bindCookRecipe(CookRecipeBindDTO cookRecipeDTO); + public Set recipeImportDishes(MultipartFile excel, Long recipeId); + /** * 批量删除菜品计划信息 * diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/cook/service/impl/CookRecipeServiceImpl.java b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/cook/service/impl/CookRecipeServiceImpl.java index 382af21..e063511 100644 --- a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/cook/service/impl/CookRecipeServiceImpl.java +++ b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/cook/service/impl/CookRecipeServiceImpl.java @@ -9,6 +9,10 @@ import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; import cn.hutool.core.util.ObjectUtil; +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.ExcelReader; +import com.alibaba.excel.read.metadata.ReadSheet; +import com.bonus.canteen.core.android.vo.CookMealtimeTypeEnum; import com.bonus.canteen.core.common.utils.DateUtil; import com.bonus.canteen.core.common.utils.MqUtil; import com.bonus.canteen.core.cook.domain.CookAppRecipe; @@ -21,6 +25,7 @@ import com.bonus.canteen.core.cook.enums.RecipeDetailTypeEnum; import com.bonus.canteen.core.cook.enums.RecipeTypeEnum; import com.bonus.canteen.core.cook.mapper.CookRecipeDetailMapper; import com.bonus.canteen.core.cook.mapper.CookRecipeDishesMapper; +import com.bonus.canteen.core.cook.utils.CookRecipeImportListener; import com.bonus.canteen.core.cook.vo.*; import com.bonus.common.core.exception.ServiceException; import com.bonus.common.core.utils.DateUtils; @@ -37,6 +42,7 @@ import com.bonus.canteen.core.cook.domain.CookRecipe; import com.bonus.canteen.core.cook.service.ICookRecipeService; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; +import org.springframework.web.multipart.MultipartFile; /** * 菜品计划信息Service业务层处理 @@ -423,6 +429,42 @@ public class CookRecipeServiceImpl implements ICookRecipeService { } } + @Override + public Set recipeImportDishes(MultipartFile excel, Long recipeId) { + ExcelReader excelReader = null; + try { + Set notImportDishesNames = new HashSet(); + List listeners = new ArrayList(); + + excelReader = EasyExcel.read(excel.getInputStream()).build(); + List readSheets = excelReader.excelExecutor().sheetList(); + for (ReadSheet readSheet : readSheets) { + String sheetName = readSheet.getSheetName(); + Integer mealtimeType = CookMealtimeTypeEnum.getKeyByDesc(sheetName); + if (!Objects.isNull(mealtimeType)) { + CookRecipeImportListener cookRecipeImportListener = new CookRecipeImportListener(sheetName, recipeId, mealtimeType.longValue()); + listeners.add(cookRecipeImportListener); + readSheet.getCustomReadListenerList().add(cookRecipeImportListener); + } + } + excelReader.read(readSheets); + notImportDishesNames.addAll( + listeners.stream() + .flatMap(listener -> listener.getNotImportDishesNames().stream()) + .collect(Collectors.toList()) + ); + + return notImportDishesNames; + }catch (Exception e) { + throw new ServiceException(e.toString()); + } finally { + if (excelReader != null) { + excelReader.finish(); + } + + } + } + /** * 批量删除菜品计划信息 * diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/cook/utils/CookRecipeImportListener.java b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/cook/utils/CookRecipeImportListener.java index 088fc82..7c4e39d 100644 --- a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/cook/utils/CookRecipeImportListener.java +++ b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/cook/utils/CookRecipeImportListener.java @@ -13,14 +13,9 @@ import com.bonus.canteen.core.cook.dto.ImportRecipeDishesDTO; import com.bonus.canteen.core.cook.mapper.CookDishesMapper; import com.bonus.canteen.core.cook.mapper.CookRecipeDetailMapper; import com.bonus.canteen.core.cook.mapper.CookRecipeDishesMapper; -import com.bonus.canteen.core.cook.service.ICookDishesService; -import com.bonus.canteen.core.cook.service.ICookRecipeDetailService; -import com.bonus.canteen.core.cook.service.ICookRecipeDishesService; import com.bonus.common.core.exception.ServiceException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; - import java.math.BigDecimal; import java.time.LocalDate; import java.util.*; @@ -109,7 +104,7 @@ public class CookRecipeImportListener extends AnalysisEventListener dates = new HashSet(this.dateMap.values()); if (!dates.isEmpty()) { - ICookRecipeDetailService menuRecipeDetailService = SpringContextHolder.getBean(ICookRecipeDetailService.class); + //ICookRecipeDetailService menuRecipeDetailService = SpringContextHolder.getBean(ICookRecipeDetailService.class); CookRecipeDetailMapper cookRecipeDetailMapper = SpringContextHolder.getBean(CookRecipeDetailMapper.class); CookRecipeDishesMapper cookRecipeDishesMapper = SpringContextHolder.getBean(CookRecipeDishesMapper.class); //List byRecipeIdAndDates = menuRecipeDetailService.getByRecipeIdAndDatesAndIntervalId(this.recipeId, dates, this.mealtimeType); @@ -123,50 +118,50 @@ public class CookRecipeImportListener extends AnalysisEventListener menuDishesList = cookDishesMapper.selectCookDishesList(cookDishes); + List cookDishesList = cookDishesMapper.selectCookDishesList(cookDishes); - Set difference = DifferenceCalculator.calculateDifference(this.dishesNames, menuDishesList); + Set difference = DifferenceCalculator.calculateDifference(this.dishesNames, cookDishesList); System.out.println("差集结果: " + difference); if (difference != null && difference.size() > 0) { throw new ServiceException("以下菜品不存在:" + difference); } - Map nameMap = menuDishesList.stream().collect(Collectors.groupingBy(CookDishes::getDishesName, Collectors.collectingAndThen(Collectors.toList(), (list) -> { + Map nameMap = cookDishesList.stream().collect(Collectors.groupingBy(CookDishes::getDishesName, Collectors.collectingAndThen(Collectors.toList(), (list) -> { return list.get(0); }))); - ICookRecipeDishesService menuRecipeDishesService = SpringContextHolder.getBean(ICookRecipeDishesService.class); + //ICookRecipeDishesService menuRecipeDishesService = SpringContextHolder.getBean(ICookRecipeDishesService.class); List inserData = new ArrayList<>(); for (LocalDate date : dates) { - CookRecipeDetail menuRecipeDetail = dateDetailMap.get(date); - if (menuRecipeDetail == null) { - menuRecipeDetail = new CookRecipeDetail(); - menuRecipeDetail.setRecipeId(this.recipeId); - menuRecipeDetail.setApplyDate(date); - menuRecipeDetail.setMealtimeType(this.mealtimeType); + CookRecipeDetail cookRecipeDetail = dateDetailMap.get(date); + if (cookRecipeDetail == null) { + cookRecipeDetail = new CookRecipeDetail(); + cookRecipeDetail.setRecipeId(this.recipeId); + cookRecipeDetail.setApplyDate(date); + cookRecipeDetail.setMealtimeType(this.mealtimeType); //menuRecipeDetail.setDetailId(Id.next()); //menuRecipeDetailService.save(menuRecipeDetail); - cookRecipeDetailMapper.insertCookRecipeDetail(menuRecipeDetail); + cookRecipeDetailMapper.insertCookRecipeDetail(cookRecipeDetail); } else { //menuRecipeDishesService.deleteByDetailId(menuRecipeDetail.getDetailId()); - Long[] recipeDetailIds = {menuRecipeDetail.getRecipeDetailId()}; + Long[] recipeDetailIds = {cookRecipeDetail.getRecipeDetailId()}; cookRecipeDishesMapper.deleteCookRecipeDishesByCookRecipeDetailIds(recipeDetailIds); } List importRecipeDishesDtos = this.datas.get(date); - CookRecipeDetail finalMenuRecipeDetail = menuRecipeDetail; + CookRecipeDetail finalCookRecipeDetail = cookRecipeDetail; List saveDatas = importRecipeDishesDtos.stream().filter(distinctByKey(ImportRecipeDishesDTO::getDishesName)).map((importRecipeDishesDto) -> { - CookRecipeDishes menuRecipeDishes = null; - CookDishes menuDishes = nameMap.get(importRecipeDishesDto.getDishesName()); - if (menuDishes != null) { - menuRecipeDishes = BeanUtil.copyProperties(importRecipeDishesDto, CookRecipeDishes.class, new String[0]); - menuRecipeDishes.setRecipeDetailId(finalMenuRecipeDetail.getRecipeDetailId()); - menuRecipeDishes.setSizeType(menuDishes.getSizeType()); - menuRecipeDishes.setDishesId(menuDishes.getDishesId()); + CookRecipeDishes cookRecipeDishes = null; + CookDishes cookDishesValue = nameMap.get(importRecipeDishesDto.getDishesName()); + if (cookDishesValue != null) { + cookRecipeDishes = BeanUtil.copyProperties(importRecipeDishesDto, CookRecipeDishes.class, new String[0]); + cookRecipeDishes.setRecipeDetailId(finalCookRecipeDetail.getRecipeDetailId()); + cookRecipeDishes.setSizeType(cookDishesValue.getSizeType()); + cookRecipeDishes.setDishesId(cookDishesValue.getDishesId()); } else { this.notImportDishesNames.add(importRecipeDishesDto.getDishesName()); } - return menuRecipeDishes; + return cookRecipeDishes; }).filter(Objects::nonNull).collect(Collectors.toList()); inserData.addAll(saveDatas); }