package com.xkrs.utilsnew; import com.xkrs.model.bean.DataWrapper2; import com.xkrs.model.bean.VipTimeRangeBean; import com.xkrs.model.entity.SysUserEntity; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; /** * 时间范围工具类 */ public class TimeRangeUtils { private TimeRangeUtils() { } public static boolean checkIfAllExpired(SysUserEntity sysUserEntity) { boolean probationExpired = checkIfProbationExpired(sysUserEntity.getAddTime(), sysUserEntity.getDayNum());//试用期是否过期 boolean vipExpired = checkIfVipExpired(sysUserEntity.getVipTimeRangeJson());//VIP是否过期 return probationExpired && vipExpired; } /** * 检查VIP是否有效 */ public static boolean checkIfVipValid(String vipTimeRangeJson) { LocalDateTime now = LocalDateTime.now(); List> vipRangeList = getVipRangeList(vipTimeRangeJson); for (DataWrapper2 vipRange : vipRangeList) { if (checkIfInTargetRange(vipRange.getData1(), vipRange.getData2(), now)) { return true; } } return false; } /** * 检查VIP是否过期(OK) */ public static boolean checkIfVipExpired(String vipTimeRangeJson) { return !checkIfVipValid(vipTimeRangeJson); } /** * 获取VIP范围列表(OK) */ public static List> getVipRangeList(String vipTimeRangeJson) { List> vipRangeList = new ArrayList<>();//VIP范围结果列表 List list = new ArrayList<>();//Json解析的VIP范围列表 try { VipTimeRangeBean vipTimeRangeBean = JsonUtils.deserialize(vipTimeRangeJson, VipTimeRangeBean.class); if (vipTimeRangeBean.getList() == null || vipTimeRangeBean.getList().size() == 0) { throw new RuntimeException("VipTimeRangeJson Error"); } list.addAll(vipTimeRangeBean.getList()); } catch (Exception e) { e.printStackTrace(); } for (VipTimeRangeBean.VipTimeRangeItemBean item : list) { DateTimeUtils.checkDateTimeFormat(item.getStartTime()); DateTimeUtils.checkDateTimeFormat(item.getEndTime()); LocalDateTime start = DateTimeUtils.formatToLocalDateTime(item.getStartTime()); LocalDateTime end = DateTimeUtils.formatToLocalDateTime(item.getEndTime()); vipRangeList.add(new DataWrapper2<>(start, end)); } return vipRangeList; } /** * 检查试用期是否有效 */ public static boolean checkIfProbationValid(String addTime, Long dayNum) { LocalDateTime now = LocalDateTime.now(); DataWrapper2 probationRange = getProbationRange(addTime, dayNum); return checkIfInTargetRange(probationRange.getData1(), probationRange.getData2(), now); } /** * 检查试用期是否过期(OK) */ public static boolean checkIfProbationExpired(String addTime, Long dayNum) { return !checkIfProbationValid(addTime, dayNum); } /** * 获取试用期范围(OK) */ public static DataWrapper2 getProbationRange(String addTime, Long dayNum) { DateTimeUtils.checkDateTimeFormat(addTime); LocalDateTime start = DateTimeUtils.formatToLocalDateTime(addTime); LocalDateTime end = start.plusDays(dayNum); return new DataWrapper2<>(start, end); } /** * 判断test是否在rangeStart和rangeEnd之间 */ public static boolean checkIfInTargetRange(LocalDateTime rangeStart, LocalDateTime rangeEnd, LocalDateTime test) { return rangeStart.isBefore(test) && rangeEnd.isAfter(test); } }