package com.sckw.core.utils;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zk
 * @description 正则表达式校验
 * @date 2020/05/09 14:37:50
 */
public class RegularUtils {
    /**
     * 手机号
     */
    public static final String PHONE_REG = "^1[3456789]\\d{9}$";

    /**
     * 浮点型(不包括0)保留两位小数
     */
    public static final String DECIMAL_REG = "^[+]?(([1-9]\\d*[.]?)|(0.))(\\d{1,2})?$";

    /**
     * 非负浮点型(包括0)
     */
    public static final String DECIMAL_REG1 = "^\\d+(\\.\\d+)?$";

    /**
     * 数字和字母
     */
    public static final String NUMBER_AND_LATTER = "[0-9A-Za-z]{18}";

    /**
     * 数字和大写字母
     */
    public static final String NUMBER_AND_UPPERCASE_LATTER = "[0-9A-Z]";

    /**
     * 正整数和0
     */
    public static final String NUMBER = "^[1-9]\\d*|0$";

    /**
     * 金额
     */
    public static final String DECIMAL_REG2 = "^(-?\\d+)(\\.\\d+)?$";

    /**
     * ${xxxxx}
     */
    public static final String DOLLAR_BIG_BRACKETS = "(\\$\\{)([\\w]+)(\\})";

    /**
     * 身份证
     */
    public static final String IDCARD = "[0-9A-Za-z]{15}|[0-9A-Za-z]{18}";
    /**[0-9A-Za-z]{18}
     * 车牌号
     */
//    public static final String TRUCK_NUMBER = "^[京津沪冀晋辽吉黑苏浙皖闽赣鲁豫鄂湘粤桂琼川黔云渝藏陕陇青宁新闽粤晋琼使领A_Z]{1}[A_Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$";
//    public static final String TRUCK_NUMBER = "^[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云渝藏陕甘青宁新]{1}[A-Z]{1}[DF]{1}[0-9a-zA-Z]{5}$";
    /**
     * 车牌校验  五位数车牌
     */
    public static final String TRUCK_NUMBER_FIVE = "^[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云渝藏陕甘青宁新使]{1}[a-zA-Z]{1}[0-9a-zA-Z]{5}$";

    /**
     * 车牌校验  六位数车牌
     */
    public static final String TRUCK_NUMBER_SIX = "^[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云渝藏陕甘青宁新使]{1}[a-zA-Z]{1}[0-9a-zA-Z]{6}$";

    /**
     * 中文汉字
     */
    public static final String CHINESE = "^[\\u4e00-\\u9fa5]{1,6}$";

    /**
     * 姓名相关
     * （企业法人、企业代表、员工姓名、司机姓名）
     */
    public static final String NAME = "^[\\u4e00-\\u9fa5]{1,25}$";


    /**
     * 道路运输许可证号
     */
    public static final String ROAD_LICENCE = "(^\\d{12})";

    /**
     * @param
     * @return
     * @description 校验
     * @author zk
     * @date 2020/5/9 14:38
     **/
    public static boolean matchs(String regular, String str) {
        Pattern p = Pattern.compile(regular);
        Matcher m = p.matcher(str);
        boolean isMatch = m.matches();
        return isMatch;
    }

    /**
     * @param regular 正则 str匹配字符串 min 最小长度 max最大长度
     * @desc: 校验（限制长度范围）
     * @author: czh
     * @date: 2023/7/25
     */
    public static boolean matchWithRange(String regular, String str, Integer min, Integer max) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(regular).append("{").append(min).append(",").append(max).append("}$");
        Pattern p = Pattern.compile(stringBuilder.toString());
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * @param regular 正则 str匹配字符串 fixLength 固定长度
     * @desc: 校验（限制固定长度）
     * @author: czh
     * @date: 2023/7/25
     */
    public static boolean matchWithFix(String regular, String str, Integer fixLength) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(regular).append("{").append(fixLength).append("}$");
        Pattern p = Pattern.compile(stringBuilder.toString());
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * @param str 数值 numLength 整数长度  fractionLenth小数长度
     * @desc: 校验（限制固定长度）
     * @author: czh
     * @date: 2023/7/25
     */
    public static boolean matchNumberWithFix(String str, Integer numLength, Integer fractionLenth) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("\\d{1,").append(numLength).append("}([\\.]\\d{0,").append(fractionLenth).append("})?");
        Pattern p = Pattern.compile(stringBuilder.toString());
        Matcher m = p.matcher(str);
        return m.matches();
    }


    public static void main(String[] args) {
        System.out.println(matchs(IDCARD, "22883719241229935X"));
    }
}
