时间范围通用查询

时间范围通用查询

时间范围枚举

/**
 * @author: yc
 * @des:  时间范围枚举
 * @date: 2024/06/04 11:26
 */
@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;
    }

    /**
     * @author: yc
     * @des:  格式校验
     * @date: 2024/06/04 11:34
     */
    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;
    }

    /**
     * @author: yc
     * @des:  指定格式校验
     * @date: 2024/06/04 11:44
     */
    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;
    }

    /**
     * @author: yc
     * @des: 校验前空格式
     * @date: 2024/06/04 15:32
     */
    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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值