数字编号递增,递减,重复等生成,靓号

package cn.malong.common.core.utils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**

  • @author Wei Yuzhe

  • @Description:

  • @date 2021/12/6 10:40
    */
    public class CodeGenerateUtil {
    private static final int RECOMMEND_CODE_SCALE = 2;
    private static final int DEMAND_CODE_SCALE = 3;
    private static final int INVITE_CODE_SCALE = 3;
    private static final int RECRUIT_CODE_SCALE = 5;
    private static final int TENGLONG_CODE_SCALE = 5;
    private static final int XUNLONG_CODE_SCALE = 5;
    private static final int MALONG_CODE_SCALE = 6;

    /**

    • 匹配5位顺增或顺降
      */
      private static final String ascOrDeac = “(?😦?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){4}|(?:9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){4})\d”;

    /**

    • 匹配5位连续的数字
      */
      private static final String repeat = “([\d])\1{4,}”;

    private final static AtomicInteger ai = new AtomicInteger(0);

    /**
    *

    • 生成规则:从1000001开始自增,7位数字、保留靓号
    • 靓号规则:1、5位以上正序或反序数列:如1012345、1076543
    • 2、5位以上数字重复:如 1011111、1111111
    • 数据类型:varchar(12)
    • @param scale 1-5之间的整数 (以下该参数均写si)
    • @return 序列号
      /
      public static String malongSeqGenerate() {
      /
      if (scale < 0 || scale > 5) {
      throw new IllegalArgumentException(“scale应为[1-5]之间的整数!”);
      }*/
      StringBuilder dateTime = new StringBuilder(“1”);
      ai.compareAndSet(Integer.MAX_VALUE, 1);
      int incrementAndGet = ai.incrementAndGet();
      StringBuilder str = new StringBuilder(String.format("%0" + MALONG_CODE_SCALE + “d”, incrementAndGet));
      String code = dateTime.append(str.substring(str.length() - MALONG_CODE_SCALE, str.length())).toString();
      // 判断是否5位以上正序或反序数列
      boolean flag = isAcsOrDescOrRepeatNumeric(code, ascOrDeac);
      // 5位以上数字重复
      boolean flag1 = isAcsOrDescOrRepeatNumeric(code, repeat);
      // 只要有一种情况满足,则重新生成
      if (flag || flag1) {
      String malongCode = malongSeqGenerate();
      return malongCode;
      } else {
      return code;
      }
      }

    /**
    *

    • 生成规则:从100000开始自增,6位数字
    • 数据类型:varchar(12)
    • @param scale 1-5之间的整数
    • @return 序列号
      /
      public static String tenglongSeqGenerate() {
      /
      if (scale < 0 || scale > 5) {
      throw new IllegalArgumentException(“scale应为[1-5]之间的整数!”);
      }*/
      StringBuilder dateTime = new StringBuilder(“1”);
      String code = getCode(TENGLONG_CODE_SCALE, dateTime);
      return code;
      }

    /**
    *

    • 生成规则:从200000开始自增,6位数字
    • 数据类型:varchar(12)
    • @param scale 1-5之间的整数
    • @return 序列号
      /
      public static String xunlongSeqGenerate() {
      /
      if (scale < 0 || scale > 5) {
      throw new IllegalArgumentException(“scale应为[1-5]之间的整数!”);
      }*/
      StringBuilder dateTime = new StringBuilder(“2”);
      String code = getCode(XUNLONG_CODE_SCALE, dateTime);
      return code;
      }

    /**
    *
    *

    • 生成规则:从300000开始自增,6位数字 即scale为3
    • 数据类型:varchar(12)
    • @param scale 1-5之间的整数
    • @return 序列号
      /
      public static String recruitSeqGenerate() {
      /
      if (scale < 0 || scale > 5) {
      throw new IllegalArgumentException(“scale应为[1-5]之间的整数!”);
      }*/
      StringBuilder dateTime = new StringBuilder(“3”);
      String code = getCode(RECRUIT_CODE_SCALE, dateTime);
      return code;
      }

    /**

    • 需求编号
    • 生成规则:1{编号}{yyMMddHHmmss}{三位随机数} 即scale为3
    • 注:1开头代表需求单
    • 数据类型:varchar(32)
    • @param scale 1-5之间的整数
    • @return 序列号
      /
      public static String demandSeqGenerate(String tenglongCode) {
      /
      if (scale < 0 || scale > 5) {
      throw new IllegalArgumentException(“scale应为[1-5]之间的整数!”);
      }*/
      String format = getFormat();
      StringBuilder dateTime = new StringBuilder(“1” + tenglongCode + format);
      String code = getCode(DEMAND_CODE_SCALE, dateTime);
      return code;
      }

    /**

    • 受理编号
    • 生成规则:2{编号}{yyMMddHHmmss}{三位随机数} 即scale为3
    • 注:2开头代表邀约单
    • 数据类型:varchar(32)
    • @param scale 1-5之间的整数
    • @return 序列号
      /
      public static String inviteSeqGenerate(String xunlongCode) {
      /
      if (scale < 0 || scale > 5) {
      throw new IllegalArgumentException(“scale应为[1-5]之间的整数!”);
      }*/
      String format = getFormat();
      StringBuilder dateTime = new StringBuilder(“2” + xunlongCode + format);
      String code = getCode(INVITE_CODE_SCALE, dateTime);
      return code;
      }

    /**

    • 推荐记录编号
    • 含义:面试人员的面试编号
    • 生成规则:3{编号0}{编号1}{编号3}{yyMMddHHmmss}{两位随机数} 即scale为2
    • 注:3开头代表推荐单
    • @param scale 1-5之间的整数
    • @return 序列号
      */
      public static String recommendSeqGenerate(String… code) {
      /if (scale < 0 || scale > 5) {
      throw new IllegalArgumentException(“scale应为[1-5]之间的整数!”);
      }
      /
      String format = getFormat();
      StringBuilder dateTime = new StringBuilder(“3” + Arrays.stream(code).map(String::valueOf).collect(Collectors.joining()) + format);
      String recommendCode = getCode(RECOMMEND_CODE_SCALE, dateTime);
      return recommendCode;
      }

    private static String getFormat() {
    LocalDateTime now = LocalDateTime.now();
    return now.format(DateTimeFormatter.ofPattern(“yyMMddHHmmss”));
    }

    private static String getCode(int scale, StringBuilder dateTime) {
    int incrementAndGet = ai.incrementAndGet();
    ai.compareAndSet(Integer.MAX_VALUE, 0);
    StringBuilder str = new StringBuilder(String.format("%0" + scale + “d”, incrementAndGet - 1));
    return dateTime.append(str.substring(str.length() - scale, str.length())).toString();
    }

    /**

    • ascOrDeac:不能是连续的数字–递增或递减(如:123456、234567)
    • repeat:不能全是相同的数字(如:1000001、1111111)
    • @param mc
    • @return 连续数字返回true
      */
      public static boolean isAcsOrDescOrRepeatNumeric(String mc, String regular) {
      Pattern pa = Pattern.compile(regular);
      // 去头和去尾
      String substring0 = mc.substring(0, mc.length() - 1);
      String substring1 = substring0.substring(1);
      Matcher ma = pa.matcher(substring1);
      // 去头两位
      String substring2 = mc.substring(2);
      Matcher ma2 = pa.matcher(substring2);
      // 去尾两位
      String substring3 = mc.substring(0, mc.length() - 2);
      Matcher ma3 = pa.matcher(substring3);
      return ma.matches() || ma2.matches() || ma3.matches();
      }

    /* public static void main(String[] args) {
    for (int i = 0; i < 99999; i++) {
    String s = malongSeqGenerate();
    System.out.println(i+":"+s);
    }
    // }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值