YouBianCodeUtil.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package org.jeecg.common.util;
  2. import io.netty.util.internal.StringUtil;
  3. /**
  4. * 流水号生成规则(按默认规则递增,数字从1-99开始递增,数字到99,递增字母;位数不够增加位数)
  5. * A001
  6. * A001A002
  7. *
  8. * @Author zhangdaihao
  9. */
  10. public class YouBianCodeUtil {
  11. /**
  12. * 代表数字位数
  13. * 数字位数(默认生成3位的数字)
  14. */
  15. private static final int NUM_LENGTH = 2;
  16. public static final int ZHANWEI_LENGTH = 1 + NUM_LENGTH;
  17. /**
  18. * 根据前一个code,获取同级下一个code
  19. * 例如:当前最大code为D01A04,下一个code为:D01A05
  20. *
  21. * @param code
  22. * @return
  23. */
  24. public static synchronized String getNextYouBianCode(String code) {
  25. String newcode = "";
  26. if (code == null || code == "") {
  27. String zimu = "A";
  28. String num = getStrNum(1);
  29. newcode = zimu + num;
  30. } else {
  31. String beforeCode = code.substring(0, code.length() - 1 - NUM_LENGTH);
  32. String afterCode = code.substring(code.length() - 1 - NUM_LENGTH, code.length());
  33. char afterCodeZimu = afterCode.substring(0, 1).charAt(0);
  34. Integer afterCodeNum = Integer.parseInt(afterCode.substring(1));
  35. String nextNum = "";
  36. char nextZimu = 'A';
  37. // 先判断数字等于999*,则计数从1重新开始,递增
  38. if (afterCodeNum == getMaxNumByLength(NUM_LENGTH)) {
  39. nextNum = getNextStrNum(0);
  40. } else {
  41. nextNum = getNextStrNum(afterCodeNum);
  42. }
  43. // 先判断数字等于999*,则字母从A重新开始,递增
  44. if (afterCodeNum == getMaxNumByLength(NUM_LENGTH)) {
  45. nextZimu = getNextZiMu(afterCodeZimu);
  46. } else {
  47. nextZimu = afterCodeZimu;
  48. }
  49. // 例如Z99,下一个code就是Z99A01
  50. if ('Z' == afterCodeZimu && getMaxNumByLength(NUM_LENGTH) == afterCodeZimu) {
  51. newcode = code + (nextZimu + nextNum);
  52. } else {
  53. newcode = beforeCode + (nextZimu + nextNum);
  54. }
  55. }
  56. return newcode;
  57. }
  58. /**
  59. * 根据父亲code,获取下级的下一个code
  60. * <p>
  61. * 例如:父亲CODE:A01
  62. * 当前CODE:A01B03
  63. * 获取的code:A01B04
  64. *
  65. * @param parentCode 上级code
  66. * @param localCode 同级code
  67. * @return
  68. */
  69. public static synchronized String getSubYouBianCode(String parentCode, String localCode) {
  70. if (localCode != null && localCode != "") {
  71. return getNextYouBianCode(localCode);
  72. } else {
  73. parentCode = parentCode + "A" + getNextStrNum(0);
  74. }
  75. return parentCode;
  76. }
  77. /**
  78. * 将数字前面位数补零
  79. *
  80. * @param num
  81. * @return
  82. */
  83. private static String getNextStrNum(int num) {
  84. return getStrNum(getNextNum(num));
  85. }
  86. /**
  87. * 将数字前面位数补零
  88. *
  89. * @param num
  90. * @return
  91. */
  92. private static String getStrNum(int num) {
  93. String s = String.format("%0" + NUM_LENGTH + "d", num);
  94. return s;
  95. }
  96. /**
  97. * 递增获取下个数字
  98. *
  99. * @param num
  100. * @return
  101. */
  102. private static int getNextNum(int num) {
  103. num++;
  104. return num;
  105. }
  106. /**
  107. * 递增获取下个字母
  108. *
  109. * @param zimu
  110. * @return
  111. */
  112. private static char getNextZiMu(char zimu) {
  113. if (zimu == 'Z') {
  114. return 'A';
  115. }
  116. zimu++;
  117. return zimu;
  118. }
  119. /**
  120. * 根据数字位数获取最大值
  121. *
  122. * @param length
  123. * @return
  124. */
  125. private static int getMaxNumByLength(int length) {
  126. if (length == 0) {
  127. return 0;
  128. }
  129. String maxNum = "";
  130. for (int i = 0; i < length; i++) {
  131. maxNum = maxNum + "9";
  132. }
  133. return Integer.parseInt(maxNum);
  134. }
  135. public static String[] cutYouBianCode(String code) {
  136. if (code == null || StringUtil.isNullOrEmpty(code)) {
  137. return null;
  138. } else {
  139. //获取标准长度为numLength+1,截取的数量为code.length/numLength+1
  140. int c = code.length() / (NUM_LENGTH + 1);
  141. String[] cutcode = new String[c];
  142. for (int i = 0; i < c; i++) {
  143. cutcode[i] = code.substring(0, (i + 1) * (NUM_LENGTH + 1));
  144. }
  145. return cutcode;
  146. }
  147. }
  148. }