package cn.pluss.platform.util;

/**
 * @author djh
 */
public class USCCUtil {

    /**
     * checkUSCC
     * [功能]校验18位统一社会信用代码正确性
     * [参数]testUSCC:待校验的统一社会信用代码（要求字母已经保持大写）
     * [返回值]boolean类型，0(false)表示验证未通过，1(true)表示验证通过
     */
    public static boolean checkUSCC(String testUSCC) {
        try {
            if (testUSCC == null) {
                return false;
            }

            if (testUSCC.length() >= 18) {
                testUSCC = testUSCC.substring(0, 18);
                return checkUSCC18(testUSCC);
            }

            if (testUSCC.length() == 15) {
                return "true".equals(checkUSCC15(testUSCC));
            }

            return false;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * checkUSCC
     * [功能]校验18位统一社会信用代码正确性
     * [参数]testUSCC:待校验的统一社会信用代码（要求字母已经保持大写）
     * [返回值]boolean类型，0(false)表示验证未通过，1(true)表示验证通过
     */
    public static boolean checkUSCC18(String testUSCC) {
        if (testUSCC.length() != 18) {
            return false;
        }

        // 用于存放权值
        int[] weight = {1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28};
        //用于计算当前判断的统一社会信用代码位数
        int index = 0;
        //用于存放当前位的统一社会信用代码
        char testc;
        //用于存放代码字符和加权因子乘积之和
        int tempSum = 0;
        int tempNum = 0;
        for (index = 0; index <= 16; index++) {
            testc = testUSCC.charAt(index);

            if (index == 0) {
                if (testc != '1' && testc != '5' && testc != '9' && testc != 'Y') {
                    return false;
                }
            }

            if (index == 1) {
                if (testc != '1' && testc != '2' && testc != '3' && testc != '9') {
                    return false;
                }
            }

            tempNum = charToNum(testc);
            //验证代码中是否有错误字符
            if (tempNum != -1) {
                tempSum += weight[index] * tempNum;
            } else {
                return false;
            }
        }
        tempNum = 31 - tempSum % 31;
        if (tempNum == 31) {
            tempNum = 0;
        }
        return charToNum(testUSCC.charAt(17)) == tempNum;
    }


    /**
     * harToNum
     * [功能]按照GB32100-2015标准代码字符集将用于检验的字符变为相应数字
     * [参数]c:待转换的字符
     * [返回值]转换完成后对应的数字,若无法找到字符集中字符，返回-1
     */
    public static int charToNum(char c) {
        switch (c) {
            case '0':
                return 0;
            case '1':
                return 1;
            case '2':
                return 2;
            case '3':
                return 3;
            case '4':
                return 4;
            case '5':
                return 5;
            case '6':
                return 6;
            case '7':
                return 7;
            case '8':
                return 8;
            case '9':
                return 9;
            case 'A':
                return 10;
            case 'B':
                return 11;
            case 'C':
                return 12;
            case 'D':
                return 13;
            case 'E':
                return 14;
            case 'F':
                return 15;
            case 'G':
                return 16;
            case 'H':
                return 17;
            case 'J':
                return 18;
            case 'K':
                return 19;
            case 'L':
                return 20;
            case 'M':
                return 21;
            case 'N':
                return 22;
            case 'P':
                return 23;
            case 'Q':
                return 24;
            case 'R':
                return 25;
            case 'T':
                return 26;
            case 'U':
                return 27;
            case 'W':
                return 28;
            case 'X':
                return 29;
            case 'Y':
                return 30;
            default:
                return -1;
        }
    }

    /**
     * 营业执照注册号校验正确返回码
     */
    public static String isBusinesslicense = "true";
    private static final String error_Businesslicense_Empty = "请输入营业执照注册号";
    public static String error_Businesslicense = "您输入的营业执照注册号有误，请核对后再输!";
    public static String error_Businesslicense_No = "您输入的营业执照注册号不足15位，请核对后再输!";

    /**
     * 校验 营业执照注册号
     *
     * @param businesslicense
     * @return
     */
    public static String checkUSCC15(String businesslicense) {
        if ("".equals(businesslicense) || " ".equals(businesslicense)) {
            return error_Businesslicense_Empty;
        } else if (businesslicense.length() != 15) {
            return error_Businesslicense_No;
        }
        String businesslicensePrex14 = businesslicense.substring(0, 14);// 获取营业执照注册号前14位数字用来计算校验码
        String businesslicense15 = businesslicense.substring(14);// 获取营业执照号的校验码
        char[] chars = businesslicensePrex14.toCharArray();
        int[] ints = new int[chars.length];
        for (int i = 0; i < chars.length; i++) {
            ints[i] = Integer.parseInt(String.valueOf(chars[i]));
        }
        getCheckCode(ints);
        if (businesslicense15.equals(getCheckCode(ints) + "")) {// 比较 填写的营业执照注册号的校验码和计算的校验码是否一致
            return isBusinesslicense;
        }
        return error_Businesslicense;
    }

    /**
     * 获取 营业执照注册号的校验码
     *
     * @param ints
     * @return
     */
    private static int getCheckCode(int[] ints) {
        if (null != ints && ints.length > 1) {
            int ti = 0;
            int si = 0;// pi|11+ti
            int cj = 0;// （si||10==0？10：si||10）*2
            int pj = 10;// pj=cj|11==0?10:cj|11
            for (int i = 0; i < ints.length; i++) {
                ti = ints[i];
                pj = (cj % 11) == 0 ? 10 : (cj % 11);
                si = pj + ti;
                cj = (0 == si % 10 ? 10 : si % 10) * 2;
                if (i == ints.length - 1) {
                    pj = (cj % 11) == 0 ? 10 : (cj % 11);
                    return pj == 1 ? 1 : 11 - pj;
                }
            }
        }
        return -1;

    }
}
