欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

批量生成连续的n个满足000000~ZZZZZZ(不包含4)的号

程序员文章站 2022-06-16 11:48:16
/** * SN号生成器 */public class RescueSnUtil { /** 救助牌号正则 */ private final static String SN_REGEX = "^[012356789A-Z]{6}$"; /** 救助牌号组成 */ private final static String[] SN_ARR = new String[]{"0","1","2","3","5","6","7","8","9","A","B","C","D"....
/**
 * SN号生成器
 */
public class RescueSnUtil {
    /** 救助牌号正则 */
    private final static String SN_REGEX = "^[012356789A-Z]{6}$";
    /** 救助牌号组成 */
    private final static String[] SN_ARR = new String[]{"0","1","2","3","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"} ;
    /** 标准进制串组成 */
    private final static String[] STR_ARR = new String[]{"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y"} ;
    /** 进制 */
    private final static int SN_ARR_LENGTH = SN_ARR.length;
    /** 救助号牌长度 */
    private final static int SN_LENGTH = 6;
    /** 救助号牌对应最大整数 */
    private final static int SN_MIN = 0;
    /** 救助号牌对应最大整数 */
    private final static int SN_MAX = (int)Math.pow(SN_ARR_LENGTH, SN_LENGTH) - 1;

    //批量获取救助号
    public static synchronized WebResultDTO<String[]> getBatchRescueSign(String currentMaxSn, int delta){
        //currentMaxSn,begin
        if(StringUtils.isNotEmpty(currentMaxSn) && !currentMaxSn.matches(SN_REGEX)){
            return new WebResultDTO(400,ArrayUtils.EMPTY_STRING_ARRAY,"传入的最大救助号格式错误");
        }
        int currentMax = StringUtils.isEmpty(currentMaxSn)? -1 : convertSnToNum(currentMaxSn);
        int begin = currentMax + 1;
        if(begin<SN_MIN){
            return new WebResultDTO(400,ArrayUtils.EMPTY_STRING_ARRAY,"传入的当前最大救助号格式错误");
        }
        //delta
        if(delta<=0){
            return new WebResultDTO(400,ArrayUtils.EMPTY_STRING_ARRAY,"要生成的救助号数量格式错误");
        }
        //end
        int end = currentMax + delta;
        if(end>SN_MAX){
            return new WebResultDTO(400,ArrayUtils.EMPTY_STRING_ARRAY,"没有足够的救助号");
        }
        //snArr
        String[] snArr = new String[delta];
        for(int i=begin;i<=end;i++){
            snArr[i-begin] = StringUtils.leftPad(convertNumToSn(i), SN_LENGTH, "0") ;
        }
        return new WebResultDTO(200,snArr,"批量获取救助号成功");
    }

    //num转sn
    private static String convertNumToSn(int num) {
        //num转str
        String str = Integer.toString(num, SN_ARR_LENGTH).toUpperCase();
        //str转sn
        return StringUtils.replaceEach(str,STR_ARR,SN_ARR);
    }

    //sn转num
    private static Integer convertSnToNum(String sn) {
        //sn转str
        String str = StringUtils.replaceEach(sn,SN_ARR,STR_ARR);
        //str转num
        return Integer.valueOf(str, SN_ARR_LENGTH);
    }

    public static void main(String[] args) {
        // WebResultDTO dto = getBatchRescueSign("",SN_MAX + 1);
        System.out.println(new Date());
        WebResultDTO dto = getBatchRescueSign("",10000);
        System.out.println(new Date());
        // WebResultDTO dto = getBatchRescueSign(null,33);
        // WebResultDTO dto = getBatchRescueSign("",2);
        System.out.println(dto.getStatus());
        System.out.println(dto.getMsg());
        System.out.println(StringUtils.join(Arrays.asList((String[])dto.getData())));
    }



}

 

本文地址:https://blog.csdn.net/xx897115293/article/details/112003423

相关标签: 思路