IdGen.nextCode 优化,修正某些情况下的问题。

This commit is contained in:
thinkgem
2019-08-30 10:13:21 +08:00
parent bb409f6fd7
commit 64cc31f8b1

View File

@@ -7,7 +7,6 @@ import java.security.SecureRandom;
import java.util.UUID;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.lang.NumberUtils;
import com.jeesite.common.lang.StringUtils;
/**
@@ -65,59 +64,59 @@ public class IdGenerate {
public static String nextCode(String code){
if (code != null){
String str = code.trim();
int lastNotNumIndex = -1;
int len = str.length() - 1;
int lastNotNumIndex = 0;
for (int i = len; i >= 0; i--) {
if (!(str.charAt(i) >= '0' && str.charAt(i) <= '9')) {
if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
lastNotNumIndex = i;
}else{
break;
}
}
// 如果最后一位是数字并且last索引位置还在最后则代表是纯数字则最后一个不是数字的索引为-1
if ((str.charAt(len) >= '0' && str.charAt(len) <= '9') && (lastNotNumIndex == len)) {
lastNotNumIndex = -1;
String prefix = str;
String prevNum = "000";
if (lastNotNumIndex != -1){
prefix = str.substring(0, lastNotNumIndex);
prevNum = str.substring(lastNotNumIndex, str.length());
}
String prefix = str.substring(0, lastNotNumIndex + 1);
String numStr = str.substring(lastNotNumIndex + 1, str.length());
long num = NumberUtils.isCreatable(numStr) ? Long.valueOf(numStr) : 0;
// System.out.println("处理前:"+str);
str = prefix + StringUtils.leftPad(String.valueOf(num + 1), numStr.length(), "0");
// System.out.println("处理后:"+str);
String nextNum = String.valueOf(Long.valueOf(prevNum) + 1);
str = prefix + StringUtils.leftPad(nextNum, prevNum.length(), "0");
return str;
}
return null;
}
// public static void main(String[] args) {
// System.out.println(nextCode("8") + " = 9");
// System.out.println(nextCode("09") + " = 10");
// System.out.println(nextCode("009") + " = 010");
// System.out.println(nextCode("T09") + " = T10");
// System.out.println(nextCode("TG09") + " = TG10");
// System.out.println(nextCode("TG0101") + " = TG0102");
// System.out.println(nextCode("TG0109") + " = TG0110");
// System.out.println(nextCode("TG02T03") + " = TG02T04");
// System.out.println(nextCode("TG02T099") + " = TG02T100");
// System.out.println(nextCode("TG02T100") + " = TG02T101");
// System.out.println(nextCode("TG02T10A") + " = TG02T10A001");
// System.out.println(nextCode("1123117153417957377") + " = 1123117153417957379");
// System.out.println(nextCode("0040009") + " = 0040010");
// System.out.println(uuid());
// System.out.println(nextId());
// System.out.println(nextCode("8"));
// System.out.println(nextCode("09"));
// System.out.println(nextCode("009"));
// System.out.println(nextCode("E09"));
// System.out.println(nextCode("EC09"));
// System.out.println(nextCode("EC0101"));
// System.out.println(nextCode("EC0109"));
// System.out.println(nextCode("EC02T03"));
// System.out.println(nextCode("EC02T099"));
// System.out.println(nextCode("EC02T100"));
// System.out.println(nextCode("EC02T10A"));
// System.out.println(nextCode("1123117153417957377"));
//// // 数值型ID重复验证测试
//// Set<String> set = SetUtils.newHashSet();
//// try{
//// for (int i=0; i<100; i++){
//// String id = String.valueOf(nextId());
//// if (set.contains(id)){
//// throw new Exception(id + " exists");
//// }
//// set.add(id);
//// System.out.println(id);
//// Thread.sleep(100);
//// }
//// }catch (Exception e) {
//// e.printStackTrace();
//// }
// // 数值型ID重复验证测试
// Set<String> set = SetUtils.newHashSet();
// try{
// for (int i=0; i<100; i++){
// String id = String.valueOf(nextId());
// if (set.contains(id)){
// throw new Exception(id + " exists");
// }
// set.add(id);
// System.out.println(id);
// Thread.sleep(100);
// }
// }catch (Exception e) {
// e.printStackTrace();
// }
// }
}