package com.xcgl.utils; import java.util.Date; import java.util.List; import java.util.Map; import com.daju.mix.dao.service.ITBCarScheduleTaskService; import org.jeecgframework.core.common.exception.BusinessException; import org.jeecgframework.core.util.ApplicationContextUtil; import org.jeecgframework.core.util.DateUtils; import org.jeecgframework.core.util.StringUtil; import org.jeecgframework.web.system.service.SystemService; import com.xcgl.taskplatform.service.TaskServiceI; public class OrderNumTools { //流水号位数 private static int numLength = 3; /** * prefix:前缀固定字母,长度必须= 2 * * */ public static String generateNextBillCode(String localMaxCode,String prefix) { String newcode = ""; String date = DateUtils.formatDate(new Date(), "yyyyMMdd"); if (localMaxCode == null || localMaxCode =="") { String num = getStrNum(1); newcode = prefix+date + num; } else { String after_code = localMaxCode.substring(localMaxCode.length() - 1 - numLength,localMaxCode.length()); Integer after_code_num = Integer.parseInt(after_code.substring(2)); String nextNum = ""; if (after_code_num == getMaxNumByLength(numLength)) { throw new BusinessException("超出最大单据编号,请联系管理员。"); //= getNextStrNum(0) } else { nextNum = getNextStrNum(after_code_num); } newcode = prefix + date + nextNum; } return newcode; } /** * prefix:前缀固定字母,长度必须= 2 * serialLength:流水号长度 * * */ public static String generateNextBillCode(String localMaxCode,String prefix,int serialLength) { String newcode = ""; String date = DateUtils.formatDate(new Date(), "yyyyMMdd"); if (localMaxCode == null || localMaxCode =="") { String num = getStrNum(1,serialLength); newcode = prefix+date + num; } else { // String after_code = localMaxCode.substring(10,localMaxCode.length()); //前缀2位+日期8位,共10位,所以取10位之后的字符转换成流水号 Integer after_code_num = Integer.parseInt(localMaxCode.substring(8 + prefix.length(), localMaxCode.length())); String nextNum = ""; if (after_code_num == getMaxNumByLength(serialLength)) { throw new BusinessException("超出最大单据编号,请联系管理员。"); //= getNextStrNum(0) } else { nextNum = getNextStrNum(after_code_num,serialLength); } newcode = prefix + date + nextNum; } return newcode; } /** * 将数字前面位数补零 * * @param num * @return */ private static String getNextStrNum(int num) { return getStrNum(getNextNum(num)); } private static String getNextStrNum(int num,int numLength) { return getStrNum(getNextNum(num),numLength); } /** * 将数字前面位数补零 * * @param num * @return */ private static String getStrNum(int num) { String s = String.format("%0" + numLength + "d", num); return s; } /** * 将数字前面位数补零 * * @param num * @return */ private static String getStrNum(int num,int numLength) { String s = String.format("%0" + numLength + "d", num); return s; } /** * 递增获取下个数字 * * @param num * @return */ private static int getNextNum(int num) { num++; return num; } /** * 递增获取下个字母 * * @param num * @return */ private static char getNextZiMu(char zimu) { if (zimu == 'Z') { return 'A'; } zimu++; return zimu; } /** * 根据数字位数获取最大值 * @param length * @return */ private static int getMaxNumByLength(int length){ if(length==0){ return 0; } String max_num = ""; for (int i=0;i> objMapList = ApplicationContextUtil.getContext().getBean(SystemService.class).findForJdbc(sb.toString(), 1, 1); String returnCode = null; if(objMapList!=null && objMapList.size()>0){ returnCode = (String)objMapList.get(0).get(codeFiled); } return returnCode; } public static String generateNextBillCode(String prefix,int prefixLength,String tablename,String codeFiled,int serialLength) { String localMaxCode= getMaxLocalCode(tablename,codeFiled,serialLength+prefixLength,prefix,prefixLength); String newcode = ""; if (localMaxCode == null || localMaxCode =="") { String num = getStrNum(1,serialLength); newcode = prefix + num; } else { // String after_code = localMaxCode.substring(10,localMaxCode.length()); //前缀2位+日期8位,共10位,所以取10位之后的字符转换成流水号 Integer after_code_num = Integer.parseInt(localMaxCode.substring(prefixLength,localMaxCode.length())); String nextNum = ""; if (after_code_num == getMaxNumByLength(serialLength)) { throw new BusinessException("超出最大编号,请联系管理员。"); //= getNextStrNum(0) } else { nextNum = getNextStrNum(after_code_num,serialLength); } newcode = prefix + nextNum; } return newcode; } private static String getMaxLocalCode(String tablename,String codeFiled,int codeLength,String prefix,int prefixLength){ StringBuilder sb = new StringBuilder(); sb.append("SELECT "+codeFiled+" FROM "+tablename); sb.append(" where length("+codeFiled+") = "+codeLength+" and left("+codeFiled+","+prefixLength+") = '"+prefix+"'" +" and "+System.currentTimeMillis()+" = '"+System.currentTimeMillis()+"'"); sb.append(" ORDER BY "+codeFiled+" DESC"); List> objMapList = ApplicationContextUtil.getContext().getBean(ITBCarScheduleTaskService.class).findForJdbc(sb.toString(), 1, 1); String returnCode = null; if(objMapList!=null && objMapList.size()>0){ returnCode = (String)objMapList.get(0).get(codeFiled); } return returnCode; } }