YouBianCodeUtil.java 4.7 KB

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