校验传入的时间是否在指定的VIP时间范围内

This commit is contained in:
liuchengqian 2023-02-14 16:28:20 +08:00
parent a9849d48b8
commit 31aadb17a6
3 changed files with 170 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -0,0 +1,45 @@
package com.xkrs.straw.model.bean;
import java.util.List;
public class VipTimeRangeBean {
private List<VipTimeRangeItemBean> 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<VipTimeRangeItemBean> getList() {
return list;
}
public void setList(List<VipTimeRangeItemBean> list) {
this.list = list;
}
}

View File

@ -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<VipLocalDateTimeRangePair> 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<VipLocalDateTimeRangePair> obtainSortedVipLocalDateTimeRangePairList(List<VipTimeRangeBean.VipTimeRangeItemBean> list) {
List<VipLocalDateTimeRangePair> 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<VipLocalDateTimeRangePair> 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;
}
}