From 97deafb62d4361a87dd858fce81c2dc9ee4f8372 Mon Sep 17 00:00:00 2001 From: xiezhijun <15270898033@163.com> Date: Wed, 3 Mar 2021 16:07:52 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A3=9F=E8=B0=B1=E8=AE=A1=E5=88=92=E7=94=9F?= =?UTF-8?q?=E6=88=90=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/stdiet/common/utils/DateUtils.java | 11 ++ .../custom/mapper/SysOrderPauseMapper.java | 7 ++ .../custom/service/ISysOrderPauseService.java | 7 ++ .../impl/SysOrderPauseServiceImpl.java | 10 ++ .../impl/SysRecipesPlanServiceImpl.java | 109 ++++++++++++------ .../mapper/custom/SysOrderPauseMapper.xml | 6 + .../mapper/custom/SysRecipesPlanMapper.xml | 20 ++-- 7 files changed, 127 insertions(+), 43 deletions(-) diff --git a/stdiet-common/src/main/java/com/stdiet/common/utils/DateUtils.java b/stdiet-common/src/main/java/com/stdiet/common/utils/DateUtils.java index b85390db6..642eaff7a 100644 --- a/stdiet-common/src/main/java/com/stdiet/common/utils/DateUtils.java +++ b/stdiet-common/src/main/java/com/stdiet/common/utils/DateUtils.java @@ -7,6 +7,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.Date; import org.apache.commons.lang3.time.DateFormatUtils; @@ -186,4 +187,14 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault()); return Date.from(zonedDateTime.toInstant()); } + + public static String localDateToString(LocalDate date, String pattern){ + DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern); + return date.format(fmt); + } + + public static LocalDate stringToLocalDate(String date, String pattern){ + DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern); + return LocalDate.parse(date, fmt); + } } diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/mapper/SysOrderPauseMapper.java b/stdiet-custom/src/main/java/com/stdiet/custom/mapper/SysOrderPauseMapper.java index 6805e9e5f..f12b8a48d 100644 --- a/stdiet-custom/src/main/java/com/stdiet/custom/mapper/SysOrderPauseMapper.java +++ b/stdiet-custom/src/main/java/com/stdiet/custom/mapper/SysOrderPauseMapper.java @@ -73,4 +73,11 @@ public interface SysOrderPauseMapper * @return */ int deletePauseByOrderId(Long[] orderIds); + + /** + * 根据订单ID获取暂停计划 + * @param orderId + * @return + */ + List getPauseListByOrderId(@Param("orderId")Long orderId); } \ No newline at end of file diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/service/ISysOrderPauseService.java b/stdiet-custom/src/main/java/com/stdiet/custom/service/ISysOrderPauseService.java index 0f98bdc97..142f990ae 100644 --- a/stdiet-custom/src/main/java/com/stdiet/custom/service/ISysOrderPauseService.java +++ b/stdiet-custom/src/main/java/com/stdiet/custom/service/ISysOrderPauseService.java @@ -72,4 +72,11 @@ public interface ISysOrderPauseService * @return */ int deletePauseByOrderId(Long[] orderIds); + + /** + * 根据订单ID获取暂停记录 + * @param orderId + * @return + */ + List getPauseListByOrderId(Long orderId); } \ No newline at end of file diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysOrderPauseServiceImpl.java b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysOrderPauseServiceImpl.java index 70228dd13..1d6c24ed5 100644 --- a/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysOrderPauseServiceImpl.java +++ b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysOrderPauseServiceImpl.java @@ -139,4 +139,14 @@ public class SysOrderPauseServiceImpl implements ISysOrderPauseService public int deletePauseByOrderId(Long[] orderIds){ return sysOrderPauseMapper.deletePauseByOrderId(orderIds); } + + /** + * 根据订单ID获取暂停计划 + * @param orderId + * @return + */ + @Override + public List getPauseListByOrderId(Long orderId){ + return sysOrderPauseMapper.getPauseListByOrderId(orderId); + } } \ No newline at end of file diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysRecipesPlanServiceImpl.java b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysRecipesPlanServiceImpl.java index 29ba4025d..b55065f17 100644 --- a/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysRecipesPlanServiceImpl.java +++ b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysRecipesPlanServiceImpl.java @@ -1,6 +1,7 @@ package com.stdiet.custom.service.impl; import com.stdiet.common.utils.DateUtils; +import com.stdiet.common.utils.StringUtils; import com.stdiet.common.utils.SynchrolockUtil; import com.stdiet.common.utils.sign.Md5Utils; import com.stdiet.custom.domain.SysOrder; @@ -18,9 +19,7 @@ import org.springframework.transaction.annotation.Transactional; import java.time.LocalDate; import java.time.temporal.ChronoUnit; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; +import java.util.*; /** * 食谱计划Service业务层处理 @@ -150,10 +149,8 @@ public class SysRecipesPlanServiceImpl implements ISysRecipesPlanService { SysRecipesPlan queryParam = new SysRecipesPlan(); queryParam.setOrderId(orderId); List oldRecipesPlanList = sysRecipesPlanMapper.selectSysRecipesPlanList(queryParam); - SysOrderPause pauseParam = new SysOrderPause(); - pauseParam.setOrderId(sysOrder.getOrderId()); - //暂停记录列表 - List pauseList = sysOrderPauseService.selectSysOrderPauseList(pauseParam); + //暂停记录列表,按暂停开始时间顺序 + List pauseList = sysOrderPauseService.getPauseListByOrderId(sysOrder.getOrderId()); List planList = generatePlan(sysOrder, oldRecipesPlanList, DateUtils.dateToLocalDate(sysOrder.getStartTime()), DateUtils.dateToLocalDate(sysOrder.getServerEndTime()), pauseList); if(oldRecipesPlanList != null && oldRecipesPlanList.size() > 0){ updateOrAddRecipesPlan(oldRecipesPlanList, planList); @@ -184,9 +181,12 @@ public class SysRecipesPlanServiceImpl implements ISysRecipesPlanService { List delList = new ArrayList<>(); for (SysRecipesPlan plan : oldRecipesPlanList) { if(index < newSize){ - plan.setStartDate(newRecipesPlanList.get(index).getStartDate()); - plan.setEndDate(newRecipesPlanList.get(index).getEndDate()); - updateList.add(plan); + if(plan.getStartDate().compareTo(newRecipesPlanList.get(index).getStartDate()) != 0 + || plan.getEndDate().compareTo(newRecipesPlanList.get(index).getEndDate()) != 0){ + plan.setStartDate(newRecipesPlanList.get(index).getStartDate()); + plan.setEndDate(newRecipesPlanList.get(index).getEndDate()); + updateList.add(plan); + } }else{ delList.add(plan.getId()); } @@ -197,6 +197,7 @@ public class SysRecipesPlanServiceImpl implements ISysRecipesPlanService { } //更新 if(updateList.size() > 0){ + System.out.println("更新数量:"+updateList.size()); for (SysRecipesPlan plan : updateList) { sysRecipesPlanMapper.updateSysRecipesPlan(plan); } @@ -230,19 +231,26 @@ public class SysRecipesPlanServiceImpl implements ISysRecipesPlanService { oldStartNumDay = oldRecipesPlanList.get(0).getStartNumDay() - 1; } List planList = new ArrayList<>(); - boolean breakFlag = false; - LocalDate planStartDate = serverStartDate; - LocalDate planEndDate = planStartDate.plusDays(6); - while (true) { + LocalDate planStartDate = null; + LocalDate planEndDate = serverStartDate.plusDays(-1); + boolean breakFlag = true; + do { SysRecipesPlan sysRecipesPlan = new SysRecipesPlan(); + planStartDate = planEndDate.plusDays(1); + planEndDate = planStartDate.plusDays(6); //判断是否大于服务到期时间 if (ChronoUnit.DAYS.between(planEndDate, serverEndDate) <= 0) { planEndDate = serverEndDate; - breakFlag = true; + breakFlag = false; + } + String[] pauseResult = dealPlanPause(planStartDate, planEndDate, pauseList); + //开始时间变更为暂停结束之后的日期,因为暂停开始时间与食谱计划开始时间相同 + if(StringUtils.isNotEmpty(pauseResult[0])){ + planStartDate = DateUtils.stringToLocalDate(pauseResult[0],"yyyyMMdd"); + planEndDate = DateUtils.stringToLocalDate(pauseResult[1],"yyyyMMdd"); } - long pauseDay = getPauseDayeCount(planStartDate, planEndDate, pauseList); //加上暂停时间 - planEndDate = planEndDate.plusDays(pauseDay); + planEndDate = planEndDate.plusDays(Integer.parseInt(pauseResult[2])); sysRecipesPlan.setStartDate(DateUtils.localDateToDate(planStartDate)); sysRecipesPlan.setEndDate(DateUtils.localDateToDate(planEndDate)); sysRecipesPlan.setOrderId(sysOrder.getOrderId()); @@ -252,30 +260,27 @@ public class SysRecipesPlanServiceImpl implements ISysRecipesPlanService { sysRecipesPlan.setStartNumDay(oldStartNumDay); oldStartNumDay += 6; sysRecipesPlan.setEndNumDay(oldStartNumDay); + //添加暂停范围内的日期 planList.add(sysRecipesPlan); - - planStartDate = planEndDate.plusDays(1); - planEndDate = planStartDate.plusDays(6); - - if (breakFlag) { - break; - } - } + }while (breakFlag); return planList; } /** - * 根据食谱开始时间、结束时间、暂停列表获取在食谱计划范围内的暂停天数 - * - * @param planStartDate 计划开始时间 - * @param planEndDate 计划结束时间 - * @param pauseList 暂停列表 + * 根据食谱开始时间、结束时间、暂停列表获取在食谱计划范围内的暂停天数,以及返回调整之后的计划开始、结束时间,以及暂停日期 + * @param planStartDate 原先的计划开始时间 + * @param planEndDate 原先计划结束时间 + * @param pauseList 暂停列表集合 * @return */ - private long getPauseDayeCount(LocalDate planStartDate, LocalDate planEndDate, List pauseList) { + private String[] dealPlanPause(LocalDate planStartDate, LocalDate planEndDate, List pauseList) { + //分别为计划开始时间、计划结束时间,范围内暂停天数,具体暂停日期,使用|隔开 + String[] result = {"","","0",""}; long pauseDay = 0; + Set pauseDateString = new TreeSet<>(new MyComparator()); //判断这个时间内是否存在暂停 if (pauseList != null && pauseList.size() > 0) { + //每条暂停时间的范围不会重叠,在添加暂停时做了限制 for (SysOrderPause sysOrderPause : pauseList) { LocalDate pauseStartDate = DateUtils.dateToLocalDate(sysOrderPause.getPauseStartDate()); LocalDate pauseEndDate = DateUtils.dateToLocalDate(sysOrderPause.getPauseEndDate()); @@ -288,10 +293,48 @@ public class SysRecipesPlanServiceImpl implements ISysRecipesPlanService { if (ChronoUnit.DAYS.between(planEndDate, pauseEndDate) > 0) { pauseEndDate = planEndDate; } - pauseDay += ChronoUnit.DAYS.between(pauseStartDate, pauseEndDate); + //判断暂停记录是否从食谱计划开始时间开始的 + if(ChronoUnit.DAYS.between(pauseStartDate, planStartDate) == 0){ + //记录该条暂停记录结束之后的第一天日期,如果两条记录连着,则取最后暂停结束之后的第一天 + if("".equals(result[0]) || result[0].equals(DateUtils.localDateToString(pauseStartDate,"yyyyMMdd"))){ + planStartDate = pauseEndDate.plusDays(1); + planEndDate = planStartDate.plusDays(6); + result[0] = DateUtils.localDateToString(planStartDate,"yyyyMMdd"); + result[1] = DateUtils.localDateToString(planEndDate,"yyyyMMdd"); + } + }else{ + pauseDay += ChronoUnit.DAYS.between(pauseStartDate, pauseEndDate) + 1; + pauseDateString.addAll(getPauseDateString(pauseStartDate, pauseEndDate)); + } } } - return pauseDay; + result[2] = pauseDay+""; + result[3] = pauseDateString.size() > 0 ? StringUtils.join(pauseDateString,"|") : ""; + return result; + } + + /** + * 根据暂停时间范围获取范围内的日期 + * @return + */ + private Set getPauseDateString(LocalDate pauseStartDate, LocalDate pauseEndDate){ + Set pauseDateList = new HashSet<>(); + long daysBetween = ChronoUnit.DAYS.between(pauseStartDate, pauseEndDate); + for(int i = 0; i <= daysBetween; i++){ + pauseDateList.add(DateUtils.localDateToString(pauseStartDate.plusDays(i),"yyyyMMdd")); + } + return pauseDateList; + } + + /** + * 集合排序key值比较器 + * */ + class MyComparator implements Comparator{ + + @Override + public int compare(String o1, String o2) { + return Integer.parseInt(o1) - Integer.parseInt(o2); + } } /** diff --git a/stdiet-custom/src/main/resources/mapper/custom/SysOrderPauseMapper.xml b/stdiet-custom/src/main/resources/mapper/custom/SysOrderPauseMapper.xml index 6b175989b..8cc719ed4 100644 --- a/stdiet-custom/src/main/resources/mapper/custom/SysOrderPauseMapper.xml +++ b/stdiet-custom/src/main/resources/mapper/custom/SysOrderPauseMapper.xml @@ -120,4 +120,10 @@ + + + \ No newline at end of file diff --git a/stdiet-custom/src/main/resources/mapper/custom/SysRecipesPlanMapper.xml b/stdiet-custom/src/main/resources/mapper/custom/SysRecipesPlanMapper.xml index fcee81005..1d429715e 100644 --- a/stdiet-custom/src/main/resources/mapper/custom/SysRecipesPlanMapper.xml +++ b/stdiet-custom/src/main/resources/mapper/custom/SysRecipesPlanMapper.xml @@ -132,16 +132,16 @@ where id = #{id} and del_flag = 0 - - delete from sys_recipes_plan where id = #{id} - + + update sys_recipes_plan set del_flag = 1 where id = #{id} + - - delete from sys_recipes_plan where id in + + update sys_recipes_plan set del_flag = 1 where id in #{id} - + @@ -156,7 +156,7 @@ - delete from sys_recipes_plan where order_id in + update sys_recipes_plan set del_flag = 1 where order_id in #{orderId} @@ -217,7 +217,7 @@ - select id, start_date, end_date, start_num_day, end_num_day, recipes_id from sys_recipes_plan where out_id=#{outId} + select id, start_date, end_date, start_num_day, end_num_day, recipes_id from sys_recipes_plan where out_id=#{outId} and del_flag = 0 @@ -244,7 +244,7 @@