From a85ad2e82e9f312a3c2754102bba1e423eec2660 Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Sat, 25 Mar 2023 21:26:16 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=94=A8=E6=88=B7=E7=9A=84?= =?UTF-8?q?=E8=BF=87=E6=9C=9F=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xkrs/utilsnew/SysUserConvertUtils.java | 2 +- .../com/xkrs/utilsnew/VipTimeRangeUtils.java | 109 +++++++++++------- 2 files changed, 66 insertions(+), 45 deletions(-) diff --git a/src/main/java/com/xkrs/utilsnew/SysUserConvertUtils.java b/src/main/java/com/xkrs/utilsnew/SysUserConvertUtils.java index 654ad1b..3b4079c 100644 --- a/src/main/java/com/xkrs/utilsnew/SysUserConvertUtils.java +++ b/src/main/java/com/xkrs/utilsnew/SysUserConvertUtils.java @@ -25,7 +25,7 @@ public class SysUserConvertUtils { sysUserVo.setReceiveSms(entity.getReceiveSms()); sysUserVo.setReceiveSmsCount(entity.getReceiveSmsCount()); sysUserVo.setRemark(entity.getRemark()); - sysUserVo.setOverTime(VipTimeRangeUtils.obtainLastOverTime(entity.getVipTimeRangeJson())); + sysUserVo.setOverTime(VipTimeRangeUtils.obtainOverTime(entity.getVipTimeRangeJson(), entity.getAddTime(), entity.getDayNum())); return sysUserVo; } diff --git a/src/main/java/com/xkrs/utilsnew/VipTimeRangeUtils.java b/src/main/java/com/xkrs/utilsnew/VipTimeRangeUtils.java index 03877cf..f100489 100644 --- a/src/main/java/com/xkrs/utilsnew/VipTimeRangeUtils.java +++ b/src/main/java/com/xkrs/utilsnew/VipTimeRangeUtils.java @@ -18,6 +18,71 @@ public class VipTimeRangeUtils { private VipTimeRangeUtils() { } + /** + * 获取用户的过期时间 + */ + public static String obtainOverTime(String oldVipTimeRangeJson, String addTime, Long dayNum) { + // 试用期结束时间 + LocalDateTime lastTryTime = obtainLastTryTime(addTime, dayNum); + // VIP过期时间 + LocalDateTime lastOverTime = obtainLastOverTime(oldVipTimeRangeJson); + // 如果没有设置过VIP时间范围,那就将试用期结束时间返回 + if (lastOverTime == null) { + return DateTimeUtils.localDateTimeToString(lastTryTime); + } + //如果VIP时间范围对应的结束时间更晚,那就将VIP时间范围的结束时间返回 + if (lastOverTime.isAfter(lastTryTime)) { + return DateTimeUtils.localDateTimeToString(lastOverTime); + } + //否则就返回试用期的结束时间 + return DateTimeUtils.localDateTimeToString(lastTryTime); + } + + private static LocalDateTime obtainLastTryTime(String addTime, Long dayNum) { + return LocalDateTime.parse(addTime, DateTimeUtils.DATE_TIME_FORMATTER_1).plusDays(dayNum); + } + + private static LocalDateTime obtainLastOverTime(String oldVipTimeRangeJson) { + //oldVipTimeRangeJson不为空说明不是新注册用户,解析该用户的VIP时间范围 + VipTimeRangeBean vipTimeRangeBean = JsonUtils.deserialize(oldVipTimeRangeJson, VipTimeRangeBean.class); + if (vipTimeRangeBean == null || vipTimeRangeBean.getList() == null || vipTimeRangeBean.getList().size() == 0) { + return null; + } + List list = vipTimeRangeBean.getList(); + if (list == null || list.size() == 0) { + return null; + } + //校验数据合法性 + for (VipTimeRangeBean.VipTimeRangeItemBean vipTimeRangeItemBeanA : list) { + for (VipTimeRangeBean.VipTimeRangeItemBean vipTimeRangeItemBeanB : list) { + if (vipTimeRangeItemBeanA != vipTimeRangeItemBeanB) { + LocalDateTime startLocalDateTimeA = LocalDateTime.parse(vipTimeRangeItemBeanA.getStartTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); + LocalDateTime endLocalDateTimeA = LocalDateTime.parse(vipTimeRangeItemBeanA.getEndTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); + LocalDateTime startLocalDateTimeB = LocalDateTime.parse(vipTimeRangeItemBeanB.getStartTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); + LocalDateTime endLocalDateTimeB = LocalDateTime.parse(vipTimeRangeItemBeanB.getEndTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); + if (startLocalDateTimeB.isBefore(startLocalDateTimeA) && endLocalDateTimeB.isAfter(startLocalDateTimeA)) { + throw new RuntimeException("VIP开始结束时间存在交叉现象1,系统数据错误"); + } + if (startLocalDateTimeB.isBefore(endLocalDateTimeA) && endLocalDateTimeB.isAfter(endLocalDateTimeA)) { + throw new RuntimeException("VIP开始结束时间存在交叉现象2,系统数据错误"); + } + } + } + } + //获取结束时间最大的那个Pair + VipTimeRangeBean.VipTimeRangeItemBean lastVipTimeRangeItemBean = list.get(0); + for (VipTimeRangeBean.VipTimeRangeItemBean vipTimeRangeItemBean : list) { + if (lastVipTimeRangeItemBean != vipTimeRangeItemBean) { + LocalDateTime endLocalDateTime = LocalDateTime.parse(vipTimeRangeItemBean.getEndTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); + LocalDateTime lastEndLocalDateTime = LocalDateTime.parse(lastVipTimeRangeItemBean.getEndTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); + if (endLocalDateTime.isAfter(lastEndLocalDateTime)) { + lastVipTimeRangeItemBean = vipTimeRangeItemBean; + } + } + } + return LocalDateTime.parse(lastVipTimeRangeItemBean.getEndTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); + } + public static boolean checkIfInTryTimeRange(String addTime, Long dayNum) { return checkIfInTryTimeRange(addTime, dayNum, LocalDateTime.now()); } @@ -26,9 +91,6 @@ public class VipTimeRangeUtils { * 校验传入的时间是否在指定的试用期过期时间之前 */ public static boolean checkIfInTryTimeRange(String addTime, Long dayNum, LocalDateTime targetLocalDateTime) { - if (TextUtils.isEmpty(addTime) || dayNum == null || targetLocalDateTime == null) { - return false; - } LocalDateTime addLocalDateTime = LocalDateTime.parse(addTime, DateTimeUtils.DATE_TIME_FORMATTER_1); LocalDateTime tryLocalDateTime = addLocalDateTime.plusDays(dayNum); return targetLocalDateTime.isBefore(tryLocalDateTime); @@ -114,47 +176,6 @@ public class VipTimeRangeUtils { return vipLocalDateTimeRangePairList; } - public static String obtainLastOverTime(String oldVipTimeRangeJson) { - //oldVipTimeRangeJson不为空说明不是新注册用户,解析该用户的VIP时间范围 - VipTimeRangeBean vipTimeRangeBean = JsonUtils.deserialize(oldVipTimeRangeJson, VipTimeRangeBean.class); - if (vipTimeRangeBean == null || vipTimeRangeBean.getList() == null || vipTimeRangeBean.getList().size() == 0) { - throw new RuntimeException("已保存的VIP时间范围反序列化失败"); - } - List list = vipTimeRangeBean.getList(); - if (list == null || list.size() == 0) { - return ""; - } - //校验数据合法性 - for (VipTimeRangeBean.VipTimeRangeItemBean vipTimeRangeItemBeanA : list) { - for (VipTimeRangeBean.VipTimeRangeItemBean vipTimeRangeItemBeanB : list) { - if (vipTimeRangeItemBeanA != vipTimeRangeItemBeanB) { - LocalDateTime startLocalDateTimeA = LocalDateTime.parse(vipTimeRangeItemBeanA.getStartTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); - LocalDateTime endLocalDateTimeA = LocalDateTime.parse(vipTimeRangeItemBeanA.getEndTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); - LocalDateTime startLocalDateTimeB = LocalDateTime.parse(vipTimeRangeItemBeanB.getStartTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); - LocalDateTime endLocalDateTimeB = LocalDateTime.parse(vipTimeRangeItemBeanB.getEndTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); - if (startLocalDateTimeB.isBefore(startLocalDateTimeA) && endLocalDateTimeB.isAfter(startLocalDateTimeA)) { - throw new RuntimeException("VIP开始结束时间存在交叉现象1,系统数据错误"); - } - if (startLocalDateTimeB.isBefore(endLocalDateTimeA) && endLocalDateTimeB.isAfter(endLocalDateTimeA)) { - throw new RuntimeException("VIP开始结束时间存在交叉现象2,系统数据错误"); - } - } - } - } - //获取结束时间最大的那个Pair - VipTimeRangeBean.VipTimeRangeItemBean lastVipTimeRangeItemBean = list.get(0); - for (VipTimeRangeBean.VipTimeRangeItemBean vipTimeRangeItemBean : list) { - if (lastVipTimeRangeItemBean != vipTimeRangeItemBean) { - LocalDateTime endLocalDateTime = LocalDateTime.parse(vipTimeRangeItemBean.getEndTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); - LocalDateTime lastEndLocalDateTime = LocalDateTime.parse(lastVipTimeRangeItemBean.getEndTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); - if (endLocalDateTime.isAfter(lastEndLocalDateTime)) { - lastVipTimeRangeItemBean = vipTimeRangeItemBean; - } - } - } - return lastVipTimeRangeItemBean.getEndTime(); - } - /** * 生成一个新的 VipTimeRangeJson */