package com.fzm.mall.server.front.util;

import java.util.Random;

/**
 * @author wangp
 * @date 2021/3/11 10:42
 * @description
 * @since JDK 1.8
 */
public class NewCodeUtil {

    public static final String getAcctUid() {
        return genRandomCode(false, 10);
    }

    public static final String getTranSeqNo() {
        return genRandomCode(false, 22);
    }


    private NewCodeUtil() {
    }

    /**
     * 生成手机短信验证码
     *
     * @return 短信验证码
     */
    public static final String genValidateCode() {
        return genRandomCode(true, 6);
    }

    /**
     * 生成系统用户邀请码
     *
     * @return 用户邀请码
     */
    public static final String genInviteCode() {
        return genRandomCode(false, 6).toLowerCase();
    }

    /**
     * 生成系统用户邀请码
     *
     * @return 用户邀请码
     */
    public static final String genKey() {
        return genRandomCode(false, 8).toLowerCase();
    }

    /**
     * 生成系统用户邀请码
     *
     * @return 用户邀请码
     */
    public static final String getUid() {
        return genRandomCode(false, 7).toLowerCase();
    }

    public static final String genThreeRandomCode() {
        return genRandomCode(false, 3);
    }

    private static final String genRandomCode(boolean numOnly, int length) {
        //0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
        String orginStr = numOnly ? "0123456789" : "0123456789abcdefghijklmnopqrstuvwxyz";
        if (length < 1) {
            return null;
        }

        StringBuilder code = new StringBuilder();
        Random r = new Random();
        while (length > 0) {
            code.append(orginStr.charAt(r.nextInt(orginStr.length())));
            --length;
        }
        return code.toString();
    }

    public static final String getSixRadomCode() {
        return genRandomCode(true, 6);
    }
}
