From 64cc31f8b12c81ea7d3914c4ee90f1cd049dec53 Mon Sep 17 00:00:00 2001 From: thinkgem Date: Fri, 30 Aug 2019 10:13:21 +0800 Subject: [PATCH] =?UTF-8?q?IdGen.nextCode=20=E4=BC=98=E5=8C=96=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=9F=90=E4=BA=9B=E6=83=85=E5=86=B5=E4=B8=8B?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/jeesite/common/idgen/IdGenerate.java | 77 +++++++++---------- 1 file changed, 38 insertions(+), 39 deletions(-) diff --git a/common/src/main/java/com/jeesite/common/idgen/IdGenerate.java b/common/src/main/java/com/jeesite/common/idgen/IdGenerate.java index ccd23b02..0374c33a 100644 --- a/common/src/main/java/com/jeesite/common/idgen/IdGenerate.java +++ b/common/src/main/java/com/jeesite/common/idgen/IdGenerate.java @@ -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 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 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(); +// } // } }