package com.bonus.common.core.constant;



/**
 * @className:ValidateUtils
 * @author:cwchen
 * @date:2024-06-28-14:52
 * @version:1.0
 * @description:校验规则工具类
 */
public class ValidateUtils {

    /**
     * 密码校验规则
     */
    public static int MAX_LENGTH=20;

    public static int MIN_LENGTH=8;

    public static String CHECKPWD_ERROR = "密码必须包含大小写字母、数字、特殊字符，且长度在%s-%s位之间";
    /**
     * 密码校验
     *
     * @param password
     * @return String
     * @author cwchen
     * @date 2024/6/28 15:01
     */
    public static String isPwd(String password) {

        if (password.length() < MIN_LENGTH) {
            return String.format(CHECKPWD_ERROR,MIN_LENGTH,MAX_LENGTH);
        }
        if (password.length() > MAX_LENGTH) {
            return String.format(CHECKPWD_ERROR,MIN_LENGTH,MAX_LENGTH);
        }
        boolean hasUpperCase = false;
        boolean hasLowerCase = false;
        boolean hasDigit = false;
        boolean hasSpecialChar = false;

        for (char c : password.toCharArray()) {
            if (Character.isUpperCase(c)) {
                hasUpperCase = true;
            } else if (Character.isLowerCase(c)) {
                hasLowerCase = true;
            } else if (Character.isDigit(c)) {
                hasDigit = true;
            } else {
                hasSpecialChar = true;
            }
        }
        if(hasUpperCase && hasLowerCase && hasDigit && hasSpecialChar){
                return null;
        }
        return String.format(CHECKPWD_ERROR,MIN_LENGTH,MAX_LENGTH);
    }
}
