package com.changfa.custom;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.util.ObjectUtils;

import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExCondition<T> extends QueryWrapper<T>{


    private static Pattern humpPattern = Pattern.compile("[A-Z]");


    public static ExCondition create(){
        return new ExCondition();
    }

    public QueryWrapper<T> eq(String column, Object params) {
        if(ObjectUtils.isEmpty(params)){
            return this;
        }
        return super.eq(true, humpToLine(column), params);
    }

    public QueryWrapper<T> ne(String column, Object params) {
        if(ObjectUtils.isEmpty(params)){
            return this;
        }
        return super.ne(true, humpToLine(column), params);
    }

    public QueryWrapper<T> in(String column, Collection<?> params) {
        if(ObjectUtils.isEmpty(params)){
            return this;
        }
        return super.in(true, humpToLine(column), params);
    }

    public QueryWrapper<T> likeRight(String column, Object params) {
        if(ObjectUtils.isEmpty(params)){
            return this;
        }
        return super.likeRight(true, humpToLine(column), params);
    }
    public QueryWrapper<T> like(String column, Object params) {
        if(ObjectUtils.isEmpty(params)){
            return this;
        }
        return super.like(true, humpToLine(column), params);
    }

    public QueryWrapper<T> between(String column, Object params, Object params1) {
        if(ObjectUtils.isEmpty(params) || ObjectUtils.isEmpty(params1)){
            return this;
        }
        return super.between(true, humpToLine(column), params,params1);
    }
    /**
     * 驼峰转下划线,最后转为大写
     * @param str
     * @return
     */
    public static String humpToLine(String str) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static void main(String[] args){
        System.out.println(humpToLine("user_name"));
    }
}
