| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- 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<length;i++){
- max_num = max_num + "9";
- }
- return Integer.parseInt(max_num);
- }
- public static String[] cutYouBianCode(String code){
- if(code==null||StringUtil.isEmpty(code)){
- return null;
- }else{
- //获取标准长度为numLength+1,截取的数量为code.length/numLength+1
- int c = code.length()/(numLength+1);
- String[] cutcode = new String[c];
- for(int i =0 ; i <c;i++){
- cutcode[i] = code.substring(0,(i+1)*(numLength+1));
- }
- return cutcode;
- }
-
- }
- public static String generateNextBillCode(String prefix,String tablename,String codeFiled,int serialLength) {
- String localMaxCode= getMaxLocalCode(tablename,codeFiled,serialLength+10,prefix);
- 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(10,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;
- }
- private static String getMaxLocalCode(String tablename,String codeFiled,int codeLength,String prefix){
- StringBuilder sb = new StringBuilder();
- sb.append("SELECT "+codeFiled+" FROM "+tablename);
- sb.append(" where left(create_date,10) = '"+ DateUtils.date_sdf.format(new Date())+"' and length("+codeFiled+") = "+codeLength+" and left("+codeFiled+",2) = '"+prefix+"'");
- sb.append(" ORDER BY "+codeFiled+" DESC");
- List<Map<String, Object>> 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<Map<String, Object>> 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;
- }
- }
|