package com.yx.face.boot.uitls;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Pattern;

/**
 * @author cyh
 * @Description
 * @date 2021/4/9 13:58
 */
public class SMSOrIdCardUtils {

    //校验手机是否合规 2020年最全的国内手机号格式
    private static final String REGEX_MOBILE = "^1\\d{10}$";

    //校验身份证号码
    private static final String REGEX_ID = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|" + "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";


    /**
     * 校验手机号
     *
     * @param phone 手机号
     * @return boolean true:是  false:否
     */
    public static boolean isMobile(String phone) {
        return Pattern.matches(REGEX_MOBILE, phone);
    }

    /**
     * 校验身份证
     *
     * @param IDNumber
     * @return boolean true:是  false:否
     */
    public static boolean isIdCard(String IDNumber){
        if (IDNumber == null || "".equals(IDNumber)) {
            return false;
        }
        boolean matches = IDNumber.matches(REGEX_ID);

        //判断第18位校验值
        if (matches) {

            if (IDNumber.length() == 18) {
                try {
                    char[] charArray = IDNumber.toCharArray();
                    //前十七位加权因子
                    int[] idCardWi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
                    //这是除以11后，可能产生的11位余数对应的验证码
                    String[] idCardY = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
                    int sum = 0;
                    for (int i = 0; i < idCardWi.length; i++) {
                        int current = Integer.parseInt(String.valueOf(charArray[i]));
                        int count = current * idCardWi[i];
                        sum += count;
                    }
                    char idCardLast = charArray[17];
                    int idCardMod = sum % 11;
                    if (idCardY[idCardMod].toUpperCase().equals(String.valueOf(idCardLast).toUpperCase())) {
                        return true;
                    } else {
                        return false;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
            }
        }
        return matches;
    }

    /*
    隐藏手机号中间的部分
     */
    public static String hidePhone(String phone){
        return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    }


    /*
    隐藏身份证中间的部分
     */
    public static String hideIdNumber(String idNumber){
      //  return idNumber.substring(0, 4) +"*****" + idNumber.substring(14);
        if (StringUtils.isEmpty(idNumber) || (idNumber.length() < 8)) {
            return idNumber;
        }
        return idNumber.replaceAll("(?<=\\w{4})\\w(?=\\w{4})", "*");
    }

    public static void main(String[] args) {
        String s = hideIdNumber("52020119960112363X");
        System.out.println(s);
    }
    /*
      隐藏名字的第一个字
      */
    public static String hideName(String name){
        String sub ="";
        if(name.length()>=2){
            for (int i = 0; i < name.length()-1; i++) {
                sub+="*";
            }
        }
        return name.substring(0,1)+sub;
    }
}
