7994b19e85562315cd0bdcf81b0f43afdbea30eb.svn-base 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package org.jeecg.common.util;
  2. import io.netty.util.internal.StringUtil;
  3. /**
  4. * 流水号生成规则(按默认规则递增,数字从1-99开始递增,数字到99,递增字母;位数不够增加位数)
  5. * A001
  6. * A001A002
  7. * @Author zhangdaihao
  8. *
  9. */
  10. public class YouBianCodeUtil {
  11. // 数字位数(默认生成3位的数字)
  12. private static final int numLength = 2;//代表数字位数
  13. public static final int zhanweiLength = 1+numLength;
  14. /**
  15. * 根据前一个code,获取同级下一个code
  16. * 例如:当前最大code为D01A04,下一个code为:D01A05
  17. *
  18. * @param code
  19. * @return
  20. */
  21. public static synchronized String getNextYouBianCode(String code) {
  22. String newcode = "";
  23. if (oConvertUtils.isEmpty(code)) {
  24. String zimu = "A";
  25. String num = getStrNum(1);
  26. newcode = zimu + num;
  27. } else {
  28. String before_code = code.substring(0, code.length() - 1- numLength);
  29. String after_code = code.substring(code.length() - 1 - numLength,code.length());
  30. char after_code_zimu = after_code.substring(0, 1).charAt(0);
  31. Integer after_code_num = Integer.parseInt(after_code.substring(1));
  32. // org.jeecgframework.core.util.LogUtil.info(after_code);
  33. // org.jeecgframework.core.util.LogUtil.info(after_code_zimu);
  34. // org.jeecgframework.core.util.LogUtil.info(after_code_num);
  35. String nextNum = "";
  36. char nextZimu = 'A';
  37. // 先判断数字等于999*,则计数从1重新开始,递增
  38. if (after_code_num == getMaxNumByLength(numLength)) {
  39. nextNum = getNextStrNum(0);
  40. } else {
  41. nextNum = getNextStrNum(after_code_num);
  42. }
  43. // 先判断数字等于999*,则字母从A重新开始,递增
  44. if(after_code_num == getMaxNumByLength(numLength)) {
  45. nextZimu = getNextZiMu(after_code_zimu);
  46. }else{
  47. nextZimu = after_code_zimu;
  48. }
  49. // 例如Z99,下一个code就是Z99A01
  50. if ('Z' == after_code_zimu && getMaxNumByLength(numLength) == after_code_num) {
  51. newcode = code + (nextZimu + nextNum);
  52. } else {
  53. newcode = before_code + (nextZimu + nextNum);
  54. }
  55. }
  56. return newcode;
  57. }
  58. /**
  59. * 根据父亲code,获取下级的下一个code
  60. *
  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 parentCode + getNextYouBianCode(localCode);
  72. return getNextYouBianCode(localCode);
  73. }else{
  74. parentCode = parentCode + "A"+ getNextStrNum(0);
  75. }
  76. return parentCode;
  77. }
  78. /**
  79. * 将数字前面位数补零
  80. *
  81. * @param num
  82. * @return
  83. */
  84. private static String getNextStrNum(int num) {
  85. return getStrNum(getNextNum(num));
  86. }
  87. /**
  88. * 将数字前面位数补零
  89. *
  90. * @param num
  91. * @return
  92. */
  93. private static String getStrNum(int num) {
  94. String s = String.format("%0" + numLength + "d", num);
  95. return s;
  96. }
  97. /**
  98. * 递增获取下个数字
  99. *
  100. * @param num
  101. * @return
  102. */
  103. private static int getNextNum(int num) {
  104. num++;
  105. return num;
  106. }
  107. /**
  108. * 递增获取下个字母
  109. *
  110. * @param num
  111. * @return
  112. */
  113. private static char getNextZiMu(char zimu) {
  114. if (zimu == 'Z') {
  115. return 'A';
  116. }
  117. zimu++;
  118. return zimu;
  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 max_num = "";
  130. for (int i=0;i<length;i++){
  131. max_num = max_num + "9";
  132. }
  133. return Integer.parseInt(max_num);
  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()/(numLength+1);
  141. String[] cutcode = new String[c];
  142. for(int i =0 ; i <c;i++){
  143. cutcode[i] = code.substring(0,(i+1)*(numLength+1));
  144. }
  145. return cutcode;
  146. }
  147. }
  148. // public static void main(String[] args) {
  149. // // org.jeecgframework.core.util.LogUtil.info(getNextZiMu('C'));
  150. // // org.jeecgframework.core.util.LogUtil.info(getNextNum(8));
  151. // // org.jeecgframework.core.util.LogUtil.info(cutYouBianCode("C99A01B01")[2]);
  152. // }
  153. }