diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/cook/dto/CookRecipeDateDTO.java b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/cook/dto/CookRecipeDateDTO.java index aca02c1..57336be 100644 --- a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/cook/dto/CookRecipeDateDTO.java +++ b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/cook/dto/CookRecipeDateDTO.java @@ -6,6 +6,8 @@ import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.ToString; + +import java.time.LocalDate; import java.util.Date; import java.util.List; @@ -26,7 +28,7 @@ public class CookRecipeDateDTO extends BaseEntity { @ApiModelProperty(value = "启用时间(天)") @JsonFormat(pattern = "yyyy-MM-dd") @Excel(name = "启用时间(天)", width = 30, dateFormat = "yyyy-MM-dd") - private Date applyDate; + private LocalDate applyDate; /** 启用时间(周) */ @Excel(name = "启用时间(周)") 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 077067b..3f4a79e 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 @@ -1,6 +1,7 @@ package com.bonus.canteen.core.cook.service.impl; -import java.util.List; +import java.time.LocalDate; +import java.util.*; import com.bonus.canteen.core.cook.domain.CookRecipeDetail; import com.bonus.canteen.core.cook.domain.CookRecipeDishes; @@ -77,34 +78,84 @@ public class CookRecipeServiceImpl implements ICookRecipeService { cookRecipeDTO.setCreateTime(DateUtils.getNowDate()); cookRecipeDTO.setCreateBy(SecurityUtils.getUsername()); try { + //TODO 判断名字是否重复 int count = cookRecipeMapper.insertCookRecipe(cookRecipeDTO); //插入菜谱 - List recipeDateList = cookRecipeDTO.getRecipeDateList(); - for (CookRecipeDateDTO recipeDateDTO : recipeDateList) { - List detailList = recipeDateDTO.getDetailList(); - for (CookRecipeDetailDTO detailDTO : detailList) { - if (CollectionUtils.isEmpty(detailDTO.getDishesList())) { - continue; - } - CookRecipeDetail cookRecipeDetail = new CookRecipeDetail(); - BeanUtils.copyProperties(recipeDateDTO, detailDTO); - BeanUtils.copyProperties(detailDTO, cookRecipeDetail); - cookRecipeDetail.setRecipeId(cookRecipeDTO.getRecipeId()); - cookRecipeDetailMapper.insertCookRecipeDetail(cookRecipeDetail); //插入菜谱之执行计划 - List dishesList = detailDTO.getDishesList(); - for (CookRecipeDishesDTO dishesDTO : dishesList) { - CookRecipeDishes cookRecipeDishes = new CookRecipeDishes(); - BeanUtils.copyProperties(dishesDTO, cookRecipeDishes); - cookRecipeDishes.setRecipeDetailId(cookRecipeDetail.getRecipeDetailId()); - cookRecipeDishesMapper.insertCookRecipeDishes(cookRecipeDishes); //插入菜谱之菜品详情 - } - } - } + createRecipeDetails(cookRecipeDTO, true); return count; } catch (Exception e) { throw new ServiceException(e.getMessage()); } } + private void createRecipeDetails(CookRecipeDTO cookRecipeDTO, boolean isCreate) { + List recipeDateList = getCookRecipeDateList(cookRecipeDTO); + insertRecipeDetails(cookRecipeDTO, recipeDateList); + } + + private static List getCookRecipeDateList(CookRecipeDTO cookRecipeDTO) { + List recipeDateList = new ArrayList<>(); + if (1 == cookRecipeDTO.getRecipeType()) { //指定日期 + recipeDateList = cookRecipeDTO.getRecipeDateList(); + } else if (2 == cookRecipeDTO.getRecipeType()) { //每日循环 + CookRecipeDateDTO cookRecipeDateDTO = new CookRecipeDateDTO(); + LocalDate now = LocalDate.now(); + for (int i = 0; i < 7; ++i) { + cookRecipeDateDTO.setApplyDate(now.plusDays(i)); + } + recipeDateList.add(cookRecipeDateDTO); + recipeDateList.add(new CookRecipeDateDTO()); + } else if (3 == cookRecipeDTO.getRecipeType()) { //每周循环 + LocalDate now = LocalDate.now(); + HashMap dateHashMap = new HashMap<>(); + for (int i = 0; i < 7; ++i) { + LocalDate applyWeek = now.plusDays((long) i); + dateHashMap.put(applyWeek.getDayOfWeek().getValue(), applyWeek); + } + for (Integer key : dateHashMap.keySet()) { + CookRecipeDateDTO cookRecipeDateDTO = new CookRecipeDateDTO(); + cookRecipeDateDTO.setApplyWeek(Long.valueOf(key)); + recipeDateList.add(cookRecipeDateDTO); + } + Iterator> iterator = dateHashMap.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + CookRecipeDateDTO cookRecipeDateDTO = new CookRecipeDateDTO(); + cookRecipeDateDTO.setApplyWeek(Long.valueOf(entry.getKey())); + cookRecipeDateDTO.setApplyDate(entry.getValue()); + recipeDateList.add(cookRecipeDateDTO); + } + } + return recipeDateList; + } + + private void insertRecipeDetails(CookRecipeDTO cookRecipeDTO, List recipeDateList) { + for (CookRecipeDateDTO recipeDateDTO : recipeDateList) { + List detailList = recipeDateDTO.getDetailList(); + for (CookRecipeDetailDTO detailDTO : detailList) { + if (1 == cookRecipeDTO.getRecipeType() && CollectionUtils.isEmpty(detailDTO.getDishesList())) { //指定日期,不插入空数据 + continue; + } + CookRecipeDetail cookRecipeDetail = new CookRecipeDetail(); + BeanUtils.copyProperties(recipeDateDTO, detailDTO); + BeanUtils.copyProperties(detailDTO, cookRecipeDetail); + cookRecipeDetail.setRecipeId(cookRecipeDTO.getRecipeId()); + if (Objects.isNull(cookRecipeDetail.getApplyDate())) { + cookRecipeDetail.setDetailType(1L); //模板数据 + } else { + cookRecipeDetail.setDetailType(2L); //详情数据 + } + cookRecipeDetailMapper.insertCookRecipeDetail(cookRecipeDetail); //插入菜谱之执行计划 + List dishesList = detailDTO.getDishesList(); + for (CookRecipeDishesDTO dishesDTO : dishesList) { + CookRecipeDishes cookRecipeDishes = new CookRecipeDishes(); + BeanUtils.copyProperties(dishesDTO, cookRecipeDishes); + cookRecipeDishes.setRecipeDetailId(cookRecipeDetail.getRecipeDetailId()); + cookRecipeDishesMapper.insertCookRecipeDishes(cookRecipeDishes); //插入菜谱之菜品详情 + } + } + } + } + /** * 修改菜品计划信息 *