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