【fix sonar】YouBianCodeUtil文件

This commit is contained in:
梁琦涛
2023-03-06 11:36:22 +08:00
parent 20ddd56f93
commit e14fe992bc
@@ -2,6 +2,8 @@ package com.jero.common.util;
import io.netty.util.internal.StringUtil;
import java.util.Objects;
/**
* 流水号生成规则(按默认规则递增,数字从1-99开始递增,数字到99,递增字母;位数不够增加位数)
* A001
@@ -11,16 +13,20 @@ import io.netty.util.internal.StringUtil;
*/
public class YouBianCodeUtil {
private YouBianCodeUtil(){
}
// 数字位数(默认生成3位的数字)
//代表数字位数
private static final int NUM_LENGTH = 2;
private static final int numLength = 2;//代表数字位数
public static final int zhanweiLength = 1+numLength;
public static final int ZHANWEI_LENGTH = 1 + NUM_LENGTH;
/**
* 根据前一个code,获取同级下一个code
* 例如:当前最大code为D01A04,下一个code为:D01A05
*
*
* @param code
* @return
*/
@@ -31,28 +37,28 @@ public class YouBianCodeUtil {
String num = getStrNum(1);
newcode = zimu + num;
} else {
String before_code = code.substring(0, code.length() - 1- numLength);
String after_code = code.substring(code.length() - 1 - numLength,code.length());
String before_code = code.substring(0, code.length() - 1- NUM_LENGTH);
String after_code = code.substring(code.length() - 1 - NUM_LENGTH,code.length());
char after_code_zimu = after_code.substring(0, 1).charAt(0);
Integer after_code_num = Integer.parseInt(after_code.substring(1));
String nextNum = "";
char nextZimu = 'A';
// 先判断数字等于999*,则计数从1重新开始,递增
if (after_code_num == getMaxNumByLength(numLength)) {
if (after_code_num == getMaxNumByLength(NUM_LENGTH)) {
nextNum = getNextStrNum(0);
} else {
nextNum = getNextStrNum(after_code_num);
}
// 先判断数字等于999*,则字母从A重新开始,递增
if(after_code_num == getMaxNumByLength(numLength)) {
if(after_code_num == getMaxNumByLength(NUM_LENGTH)) {
nextZimu = getNextZiMu(after_code_zimu);
}else{
nextZimu = after_code_zimu;
}
// 例如Z99,下一个code就是Z99A01
if ('Z' == after_code_zimu && getMaxNumByLength(numLength) == after_code_num) {
if ('Z' == after_code_zimu && getMaxNumByLength(NUM_LENGTH) == after_code_num) {
newcode = code + (nextZimu + nextNum);
} else {
newcode = before_code + (nextZimu + nextNum);
@@ -64,17 +70,17 @@ public class YouBianCodeUtil {
/**
* 根据父亲code,获取下级的下一个code
*
*
* 例如:父亲CODE:A01
* 当前CODE:A01B03
* 获取的code:A01B04
*
*
* @param parentCode 上级code
* @param localCode 同级code
* @return
*/
public static synchronized String getSubYouBianCode(String parentCode,String localCode) {
if(localCode!=null && localCode!=""){
if(localCode!=null && Objects.equals("",localCode)){
// return parentCode + getNextYouBianCode(localCode);
return getNextYouBianCode(localCode);
@@ -85,11 +91,11 @@ public class YouBianCodeUtil {
return parentCode;
}
/**
* 将数字前面位数补零
*
*
* @param num
* @return
*/
@@ -99,18 +105,18 @@ public class YouBianCodeUtil {
/**
* 将数字前面位数补零
*
*
* @param num
* @return
*/
private static String getStrNum(int num) {
String s = String.format("%0" + numLength + "d", num);
String s = String.format("%0" + NUM_LENGTH + "d", num);
return s;
}
/**
* 递增获取下个数字
*
*
* @param num
* @return
*/
@@ -121,8 +127,8 @@ public class YouBianCodeUtil {
/**
* 递增获取下个字母
*
* @param num
*
* @param zimu
* @return
*/
private static char getNextZiMu(char zimu) {
@@ -132,7 +138,7 @@ public class YouBianCodeUtil {
zimu++;
return zimu;
}
/**
* 根据数字位数获取最大值
* @param length
@@ -150,16 +156,16 @@ public class YouBianCodeUtil {
}
public static String[] cutYouBianCode(String code){
if(code==null || StringUtil.isNullOrEmpty(code)){
return null;
return new String[0];
}else{
//获取标准长度为numLength+1,截取的数量为code.length/numLength+1
int c = code.length()/(numLength+1);
int c = code.length()/(NUM_LENGTH+1);
String[] cutcode = new String[c];
for(int i =0 ; i <c;i++){
cutcode[i] = code.substring(0,(i+1)*(numLength+1));
cutcode[i] = code.substring(0,(i+1)*(NUM_LENGTH+1));
}
return cutcode;
}
}
}