package com.hai.common;

import java.security.SecureRandom;
import java.util.Calendar;
import java.util.Random;

/**
 * @Auther: 胡锐
 * @Description:
 * @Date: 2021/3/18 21:07
 */
public class GenerateCode {

    private static final Random RANDOM = new SecureRandom();

    private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    /**
     * @Author 胡锐
     * @Description 生成卡卷销售码  21位  毫秒+随机字符串
     * @Date 2021/3/18 21:08
     **/
    public static String couponSalesCode(Long merchantId,Long couponId) {
        // 默认3位时间毫秒
        String val = merchantId + "" + couponId;
        Random random = new Random();
        for (int i = 0; i < 21; i++) {
            // 输出字母还是数字
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
                if ("char".equalsIgnoreCase(charOrNum)) {
                // 取得大写字母还是小写字母
                int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
                    val += (char) (choice + random.nextInt(26));
                } else if ("num".equalsIgnoreCase(charOrNum)) { // 数字
                    val += String.valueOf(random.nextInt(10));
                }
        }
        return val;
    }

    /**
     * 生成兑换码
     * @param couponAgentRelId
     * @return
     */
    public static String convertCode(Long couponAgentRelId) {
        // 兑换码总长度
        int countLength = 20;
        // 需要生成的长度
        int generateLength = countLength - couponAgentRelId.toString().length();

        char[] nonceChars = new char[generateLength];
        for (int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
        }
        return new String(nonceChars) + couponAgentRelId;
    }

}
