OrderNumTools.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package com.xcgl.utils;
  2. import java.util.Date;
  3. import java.util.List;
  4. import java.util.Map;
  5. import com.daju.mix.dao.service.ITBCarScheduleTaskService;
  6. import org.jeecgframework.core.common.exception.BusinessException;
  7. import org.jeecgframework.core.util.ApplicationContextUtil;
  8. import org.jeecgframework.core.util.DateUtils;
  9. import org.jeecgframework.core.util.StringUtil;
  10. import org.jeecgframework.web.system.service.SystemService;
  11. import com.xcgl.taskplatform.service.TaskServiceI;
  12. public class OrderNumTools {
  13. //流水号位数
  14. private static int numLength = 3;
  15. /**
  16. * prefix:前缀固定字母,长度必须= 2
  17. *
  18. * */
  19. public static String generateNextBillCode(String localMaxCode,String prefix) {
  20. String newcode = "";
  21. String date = DateUtils.formatDate(new Date(), "yyyyMMdd");
  22. if (localMaxCode == null || localMaxCode =="") {
  23. String num = getStrNum(1);
  24. newcode = prefix+date + num;
  25. } else {
  26. String after_code = localMaxCode.substring(localMaxCode.length() - 1 - numLength,localMaxCode.length());
  27. Integer after_code_num = Integer.parseInt(after_code.substring(2));
  28. String nextNum = "";
  29. if (after_code_num == getMaxNumByLength(numLength)) {
  30. throw new BusinessException("超出最大单据编号,请联系管理员。"); //= getNextStrNum(0)
  31. } else {
  32. nextNum = getNextStrNum(after_code_num);
  33. }
  34. newcode = prefix + date + nextNum;
  35. }
  36. return newcode;
  37. }
  38. /**
  39. * prefix:前缀固定字母,长度必须= 2
  40. * serialLength:流水号长度
  41. *
  42. * */
  43. public static String generateNextBillCode(String localMaxCode,String prefix,int serialLength) {
  44. String newcode = "";
  45. String date = DateUtils.formatDate(new Date(), "yyyyMMdd");
  46. if (localMaxCode == null || localMaxCode =="") {
  47. String num = getStrNum(1,serialLength);
  48. newcode = prefix+date + num;
  49. } else {
  50. // String after_code = localMaxCode.substring(10,localMaxCode.length());
  51. //前缀2位+日期8位,共10位,所以取10位之后的字符转换成流水号
  52. Integer after_code_num = Integer.parseInt(localMaxCode.substring(8 + prefix.length(), localMaxCode.length()));
  53. String nextNum = "";
  54. if (after_code_num == getMaxNumByLength(serialLength)) {
  55. throw new BusinessException("超出最大单据编号,请联系管理员。"); //= getNextStrNum(0)
  56. } else {
  57. nextNum = getNextStrNum(after_code_num,serialLength);
  58. }
  59. newcode = prefix + date + nextNum;
  60. }
  61. return newcode;
  62. }
  63. /**
  64. * 将数字前面位数补零
  65. *
  66. * @param num
  67. * @return
  68. */
  69. private static String getNextStrNum(int num) {
  70. return getStrNum(getNextNum(num));
  71. }
  72. private static String getNextStrNum(int num,int numLength) {
  73. return getStrNum(getNextNum(num),numLength);
  74. }
  75. /**
  76. * 将数字前面位数补零
  77. *
  78. * @param num
  79. * @return
  80. */
  81. private static String getStrNum(int num) {
  82. String s = String.format("%0" + numLength + "d", num);
  83. return s;
  84. }
  85. /**
  86. * 将数字前面位数补零
  87. *
  88. * @param num
  89. * @return
  90. */
  91. private static String getStrNum(int num,int numLength) {
  92. String s = String.format("%0" + numLength + "d", num);
  93. return s;
  94. }
  95. /**
  96. * 递增获取下个数字
  97. *
  98. * @param num
  99. * @return
  100. */
  101. private static int getNextNum(int num) {
  102. num++;
  103. return num;
  104. }
  105. /**
  106. * 递增获取下个字母
  107. *
  108. * @param num
  109. * @return
  110. */
  111. private static char getNextZiMu(char zimu) {
  112. if (zimu == 'Z') {
  113. return 'A';
  114. }
  115. zimu++;
  116. return zimu;
  117. }
  118. /**
  119. * 根据数字位数获取最大值
  120. * @param length
  121. * @return
  122. */
  123. private static int getMaxNumByLength(int length){
  124. if(length==0){
  125. return 0;
  126. }
  127. String max_num = "";
  128. for (int i=0;i<length;i++){
  129. max_num = max_num + "9";
  130. }
  131. return Integer.parseInt(max_num);
  132. }
  133. public static String[] cutYouBianCode(String code){
  134. if(code==null||StringUtil.isEmpty(code)){
  135. return null;
  136. }else{
  137. //获取标准长度为numLength+1,截取的数量为code.length/numLength+1
  138. int c = code.length()/(numLength+1);
  139. String[] cutcode = new String[c];
  140. for(int i =0 ; i <c;i++){
  141. cutcode[i] = code.substring(0,(i+1)*(numLength+1));
  142. }
  143. return cutcode;
  144. }
  145. }
  146. public static String generateNextBillCode(String prefix,String tablename,String codeFiled,int serialLength) {
  147. String localMaxCode= getMaxLocalCode(tablename,codeFiled,serialLength+10,prefix);
  148. String newcode = "";
  149. String date = DateUtils.formatDate(new Date(), "yyyyMMdd");
  150. if (localMaxCode == null || localMaxCode =="") {
  151. String num = getStrNum(1,serialLength);
  152. newcode = prefix+date + num;
  153. } else {
  154. // String after_code = localMaxCode.substring(10,localMaxCode.length());
  155. //前缀2位+日期8位,共10位,所以取10位之后的字符转换成流水号
  156. Integer after_code_num = Integer.parseInt(localMaxCode.substring(10,localMaxCode.length()));
  157. String nextNum = "";
  158. if (after_code_num == getMaxNumByLength(serialLength)) {
  159. throw new BusinessException("超出最大单据编号,请联系管理员。"); //= getNextStrNum(0)
  160. } else {
  161. nextNum = getNextStrNum(after_code_num,serialLength);
  162. }
  163. newcode = prefix + date + nextNum;
  164. }
  165. return newcode;
  166. }
  167. private static String getMaxLocalCode(String tablename,String codeFiled,int codeLength,String prefix){
  168. StringBuilder sb = new StringBuilder();
  169. sb.append("SELECT "+codeFiled+" FROM "+tablename);
  170. sb.append(" where left(create_date,10) = '"+ DateUtils.date_sdf.format(new Date())+"' and length("+codeFiled+") = "+codeLength+" and left("+codeFiled+",2) = '"+prefix+"'");
  171. sb.append(" ORDER BY "+codeFiled+" DESC");
  172. List<Map<String, Object>> objMapList = ApplicationContextUtil.getContext().getBean(SystemService.class).findForJdbc(sb.toString(), 1, 1);
  173. String returnCode = null;
  174. if(objMapList!=null && objMapList.size()>0){
  175. returnCode = (String)objMapList.get(0).get(codeFiled);
  176. }
  177. return returnCode;
  178. }
  179. public static String generateNextBillCode(String prefix,int prefixLength,String tablename,String codeFiled,int serialLength) {
  180. String localMaxCode= getMaxLocalCode(tablename,codeFiled,serialLength+prefixLength,prefix,prefixLength);
  181. String newcode = "";
  182. if (localMaxCode == null || localMaxCode =="") {
  183. String num = getStrNum(1,serialLength);
  184. newcode = prefix + num;
  185. } else {
  186. // String after_code = localMaxCode.substring(10,localMaxCode.length());
  187. //前缀2位+日期8位,共10位,所以取10位之后的字符转换成流水号
  188. Integer after_code_num = Integer.parseInt(localMaxCode.substring(prefixLength,localMaxCode.length()));
  189. String nextNum = "";
  190. if (after_code_num == getMaxNumByLength(serialLength)) {
  191. throw new BusinessException("超出最大编号,请联系管理员。"); //= getNextStrNum(0)
  192. } else {
  193. nextNum = getNextStrNum(after_code_num,serialLength);
  194. }
  195. newcode = prefix + nextNum;
  196. }
  197. return newcode;
  198. }
  199. private static String getMaxLocalCode(String tablename,String codeFiled,int codeLength,String prefix,int prefixLength){
  200. StringBuilder sb = new StringBuilder();
  201. sb.append("SELECT "+codeFiled+" FROM "+tablename);
  202. sb.append(" where length("+codeFiled+") = "+codeLength+" and left("+codeFiled+","+prefixLength+") = '"+prefix+"'" +" and "+System.currentTimeMillis()+" = '"+System.currentTimeMillis()+"'");
  203. sb.append(" ORDER BY "+codeFiled+" DESC");
  204. List<Map<String, Object>> objMapList = ApplicationContextUtil.getContext().getBean(ITBCarScheduleTaskService.class).findForJdbc(sb.toString(), 1, 1);
  205. String returnCode = null;
  206. if(objMapList!=null && objMapList.size()>0){
  207. returnCode = (String)objMapList.get(0).get(codeFiled);
  208. }
  209. return returnCode;
  210. }
  211. }