时间范围通用查询
时间范围枚举
@Getter
public enum TimeRangeEnum implements BaseEnum{
OPEN_OPEN("OPEN_OPEN","前开后开","(",")"),
OPEN_CLOSE("OPEN_CLOSE","前开后闭","(","]"),
CLOSE_CLOSE("CLOSE_CLOSE","前闭后闭","[","]"),
CLOSE_OPEN("CLOSE_OPEN","前闭后开","[",")"),
EMPTY_OPEN_OPEN("EMPTY_OPEN_OPEN","空前开后开","(",")"),
EMPTY_OPEN_CLOSE("EMPTY_OPEN_CLOSE","空前开后闭","(","]"),
EMPTY_CLOSE_CLOSE("EMPTY_CLOSE_CLOSE","空前闭后闭","[","]"),
EMPTY_CLOSE_OPEN("EMPTY_CLOSE_OPEN","空前闭后开","[",")");
private String code;
private String name;
private String startChar;
private String endChar;
TimeRangeEnum(String code,String name,String startChar,String endChar){
this.code = code;
this.name = name;
this.startChar = startChar;
this.endChar = endChar;
}
public static TimeRangeEnum getEnumByTimeRange (String timeRange){
String prefix = timeRange.substring(0, 1);
String suffix = timeRange.substring(timeRange.length() - 1);
String times = timeRange.substring(1, timeRange.length() - 1);
String[] split = times.split(StrUtil.COMMA);
for (TimeRangeEnum timeRangeEnum : TimeRangeEnum.values()) {
if(timeRangeEnum.getStartChar().equals(prefix) && timeRangeEnum.getEndChar().equals(suffix)){
if(split.length == 2){
String startTime = split[0];
String endTime = split[1];
if(StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)){
if (!timeRangeEnum.getCode().startsWith("EMPTY")) {
return timeRangeEnum;
}
} else if(StringUtils.isBlank(startTime) && StringUtils.isNotBlank(endTime)) {
if (timeRangeEnum.getCode().startsWith("EMPTY")) {
return timeRangeEnum;
}
} else {
if (timeRangeEnum.getCode().endsWith("EMPTY")) {
return timeRangeEnum;
}
}
}
}
}
return null;
}
public static String verify(String timeRange){
String msg = null;
if(StringUtils.isBlank(timeRange)){
msg = "时间范围不能为空";
}
String[] timeArray = timeRange.split(StrUtil.COMMA);
if(timeArray.length < 2){
msg = "时间段格式不正确";
}
String timeStart = timeArray[0];
String timeEnd = timeArray[1];
if(!timeStart.contains("(") && !timeStart.contains("[")){
msg = "开始时间错误,需包含‘(’或'['";
}
if(!timeEnd.contains(")") && !timeEnd.contains("]")){
msg = "结束时间错误,需包含‘)’或']'";
}
return msg;
}
public static String verify(String timeRange,TimeRangeEnum timeRangeEnum){
String msg = null;
msg = verify(timeRange);
if(StringUtils.isNotBlank(msg)){
return msg;
}
String[] timeArray = timeRange.split(StrUtil.COMMA);
String timeStart = timeArray[0];
String timeEnd = timeArray[1];
if(!(timeStart.contains(timeRangeEnum.getStartChar()) && timeEnd.contains(timeRangeEnum.getEndChar()))){
msg = "请传入正确的" + timeRangeEnum.getName() + "格式";
}
return msg;
}
public static String verify(String timeRange,String validTime,String timeType,TimeRangeEnum timeRangeEnum){
if (StringUtils.isBlank(validTime) || StringUtils.isBlank(timeType)) {
return verify(timeRange,timeRangeEnum);
}
String msg = verify(timeRange,timeRangeEnum);
String[] timeArray = timeRange.split(StrUtil.COMMA);
String timeEnd = timeArray[1];
try {
DateUtils.getCurrentYMDHMSDate(timeEnd.substring(0,timeEnd.length() - 1));
}catch (Exception e){
msg = "后半区时间格式不正确";
}
try {
Integer.parseInt(validTime);
}catch (Exception e){
msg = "时效格式不正确";
}
Integer type = -1;
try {
type = Integer.parseInt(timeType);
}catch (Exception e){
msg = "时效类型格式不正确";
}
if(type < 0 || type > 3){
msg = "时效类型参数不正确";
}
return msg;
}
通用写法示例
if(StringUtils.isNotBlank(req.getTimeRange())){
String verify = TimeRangeEnum.verify(req.getTimeRange());
if(StringUtils.isNotBlank(verify)){
return Result.fail(RespBizCodeEnum.BIZ_10001.getCode(),verify);
}
qw.apply(StrUtils.getTimeRangeSql(req.getTimeRange(),"init_time"));
}
StrUtils
StrUtils
DateUtils
DateUtils