package com.zt.plat.module.qms.core.code;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;

public class SampleEncryptUtil {

    private static final String[] ID_CHAR_SET = {
            "0","1","2","3","4","5","6","7","8","9",
            "A","B","C","D","E","F","G","H","I","J",
            "K","L","M","N","P","Q","R","S","T","U",
            "V","W","X","Y","Z"};

    public static String sampleEncrypt() {
        long id = IdWorker.getId();
        return sampleEncrypt(id);
    }
    
    public static String sampleEncrypt(long id) {
        String pivotStr = pivotStr(id + "");
        String to35Num = to35Num(pivotStr);
        return StringUtils.leftPad(to35Num, 13, '0');
    }
    
    private static String to35Num(String l) {
        List<String> al = new ArrayList<>();
        BigInteger y = new BigInteger(l);
        BigInteger t;
        while (y.compareTo(BigInteger.ZERO) > 0) {
            t = y.mod(new BigInteger(ID_CHAR_SET.length + ""));
            y = y.divide(new BigInteger(ID_CHAR_SET.length + ""));
            al.add(0, ID_CHAR_SET[t.intValue()]);
        }
        StringBuffer sb = new StringBuffer();
        for (String c : al) {
            sb.append(c);
        }
        return sb.toString();
    }
    
    /**
     * 字符串反转
     * @param str
     * @return
     */
    private static String pivotStr(String str) {
        StringBuilder sb = new StringBuilder();
        char[] charArray = str.toCharArray();
        for (int i = charArray.length; i-- > 0; ) {
            sb.append(charArray[i]);
        }
        return sb.toString();
    }
}
