diff --git a/src/main/java/com/xkrs/straw/model/bean/VipLocalDateTimeRangePair.java b/src/main/java/com/xkrs/straw/model/bean/VipLocalDateTimeRangePair.java new file mode 100644 index 0000000..8d78cd7 --- /dev/null +++ b/src/main/java/com/xkrs/straw/model/bean/VipLocalDateTimeRangePair.java @@ -0,0 +1,26 @@ +package com.xkrs.straw.model.bean; + +import java.time.LocalDateTime; + +public class VipLocalDateTimeRangePair { + + private LocalDateTime startLocalDateTime; + + private LocalDateTime endLocalDateTime; + + public LocalDateTime getStartLocalDateTime() { + return startLocalDateTime; + } + + public void setStartLocalDateTime(LocalDateTime startLocalDateTime) { + this.startLocalDateTime = startLocalDateTime; + } + + public LocalDateTime getEndLocalDateTime() { + return endLocalDateTime; + } + + public void setEndLocalDateTime(LocalDateTime endLocalDateTime) { + this.endLocalDateTime = endLocalDateTime; + } +} diff --git a/src/main/java/com/xkrs/straw/model/bean/VipTimeRangeBean.java b/src/main/java/com/xkrs/straw/model/bean/VipTimeRangeBean.java new file mode 100644 index 0000000..32d7033 --- /dev/null +++ b/src/main/java/com/xkrs/straw/model/bean/VipTimeRangeBean.java @@ -0,0 +1,45 @@ +package com.xkrs.straw.model.bean; + +import java.util.List; + +public class VipTimeRangeBean { + + private List list; + + public static class VipTimeRangeItemBean { + + /** + * 开始时间 + */ + private String startTime; + + /** + * 结束时间 + */ + private String endTime; + + public String getStartTime() { + return startTime; + } + + public void setStartTime(String startTime) { + this.startTime = startTime; + } + + public String getEndTime() { + return endTime; + } + + public void setEndTime(String endTime) { + this.endTime = endTime; + } + } + + public List getList() { + return list; + } + + public void setList(List list) { + this.list = list; + } +} diff --git a/src/main/java/com/xkrs/straw/utils/VipTimeRangeUtils.java b/src/main/java/com/xkrs/straw/utils/VipTimeRangeUtils.java new file mode 100644 index 0000000..a78cdf0 --- /dev/null +++ b/src/main/java/com/xkrs/straw/utils/VipTimeRangeUtils.java @@ -0,0 +1,99 @@ +package com.xkrs.straw.utils; + +import com.xkrs.straw.model.bean.VipLocalDateTimeRangePair; +import com.xkrs.straw.model.bean.VipTimeRangeBean; +import com.xkrs.utils.DateTimeUtils; +import org.apache.hc.core5.util.TextUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class VipTimeRangeUtils { + + public static Logger log = LoggerFactory.getLogger(VipTimeRangeUtils.class); + + private VipTimeRangeUtils() { + } + + /** + * 校验传入的时间是否在指定的VIP时间范围内 + */ + public static boolean checkIfInVipTimeRange(String vipTimeRangeJson, LocalDateTime targetLocalDateTime) { + try { + if (TextUtils.isEmpty(vipTimeRangeJson) || targetLocalDateTime == null) { + return false; + } + VipTimeRangeBean vipTimeRangeBean = JsonUtils.deserialize(vipTimeRangeJson, VipTimeRangeBean.class); + if (vipTimeRangeBean == null || vipTimeRangeBean.getList() == null || vipTimeRangeBean.getList().size() == 0) { + return false; + } + List vipLocalDateTimeRangePairList = obtainSortedVipLocalDateTimeRangePairList(vipTimeRangeBean.getList()); + if (vipLocalDateTimeRangePairList.size() == 0) { + return false; + } + for (VipLocalDateTimeRangePair vipLocalDateTimeRangePair : vipLocalDateTimeRangePairList) { + LocalDateTime startLocalDateTime = vipLocalDateTimeRangePair.getStartLocalDateTime(); + LocalDateTime endLocalDateTime = vipLocalDateTimeRangePair.getEndLocalDateTime(); + if (startLocalDateTime.isBefore(targetLocalDateTime) && endLocalDateTime.isAfter(targetLocalDateTime)) { + return true; + } + } + } catch (Exception e) { + e.printStackTrace(); + log.info("检查是否属于VIP时间范围时出错,详情=" + e.getMessage()); + } + return false; + } + + /** + * 根据一个字符串格式时间的列表 生成 一个LocalDateTime格式时间的列表 + */ + private static List obtainSortedVipLocalDateTimeRangePairList(List list) { + List vipLocalDateTimeRangePairList = new ArrayList<>(); + try { + if (list == null || list.size() == 0) { + return vipLocalDateTimeRangePairList; + } + for (VipTimeRangeBean.VipTimeRangeItemBean vipTimeRangeItemBean : list) { + String startTime = vipTimeRangeItemBean.getStartTime(); + LocalDateTime startLocalDateTime = LocalDateTime.parse(startTime, DateTimeUtils.DATE_TIME_FORMATTER_1); + String endTime = vipTimeRangeItemBean.getEndTime(); + LocalDateTime endLocalDateTime = LocalDateTime.parse(endTime, DateTimeUtils.DATE_TIME_FORMATTER_1); + + VipLocalDateTimeRangePair vipLocalDateTimeRangePair = new VipLocalDateTimeRangePair(); + vipLocalDateTimeRangePair.setStartLocalDateTime(startLocalDateTime); + vipLocalDateTimeRangePair.setEndLocalDateTime(endLocalDateTime); + vipLocalDateTimeRangePairList.add(vipLocalDateTimeRangePair); + } + List sortedVipLocalDateTimeRangePairList = vipLocalDateTimeRangePairList.stream().sorted((o1, o2) -> { + try { + LocalDateTime startLocalDateTime1 = o1.getStartLocalDateTime(); + LocalDateTime endLocalDateTime1 = o1.getEndLocalDateTime(); + LocalDateTime startLocalDateTime2 = o2.getStartLocalDateTime(); + LocalDateTime endLocalDateTime2 = o2.getEndLocalDateTime(); + if (startLocalDateTime1.isBefore(startLocalDateTime2) && endLocalDateTime1.isBefore(endLocalDateTime2)) { + return 1; + } + if (startLocalDateTime1.isAfter(startLocalDateTime2) && endLocalDateTime1.isAfter(endLocalDateTime2)) { + return -1; + } + return 0; + } catch (Exception e) { + e.printStackTrace(); + } + return 0; + }).collect(Collectors.toList()); + log.info("原始集合size=" + list.size() + ",格式化后的集合size=" + vipLocalDateTimeRangePairList.size() + ",排序后的集合size=" + sortedVipLocalDateTimeRangePairList.size()); + return sortedVipLocalDateTimeRangePairList; + } catch (Exception e) { + e.printStackTrace(); + } + return vipLocalDateTimeRangePairList; + } + + +}