日期:2014-05-17 浏览次数:20817 次
package com.hhh.platform.beton.tm.share; import java.util.Calendar; import java.util.HashMap; import java.util.Map; import com.hhh.platform.beton.tm.dao.current_number.Current_numberDao; /** * 编号生成器(暂用,以后再做进一步修改) * 默认生成格式为:yyyy-xxxxx格式 * @author 3hlgw * */ public class NumberGenerater { private static final String TIME_FORMATER = "yyyy"; //时间格式 private static final String SERIAL_NUMBER = "XXXXX"; //流水号格式 private static final String NUMBER_FORMATER = TIME_FORMATER+"-"+SERIAL_NUMBER; //编码格式 private static final String SERIAL_CHAR="X"; private static NumberGenerater numberGenerater = null; private NumberGenerater(){ } /** * 取得NumberGenerater的单例实现 * @return */ public static NumberGenerater getInstance(){ if(numberGenerater==null){ synchronized(NumberGenerater.class){ if(numberGenerater==null){ numberGenerater = new NumberGenerater(); } }; } return numberGenerater; } /** * 生成下一个编号 * @param type * @param customer_id * @return */ public synchronized String geneterNextNumber(int type,String customer_id){ Current_numberDao current_numberDao = new Current_numberDao(); //判断项目是否已经有编号存在 boolean isExist = this.isExistNumber(type, customer_id); //取得序列号格式化字符串 // int serial_start_index = NUMBER_FORMATER.indexOf(SERIAL_CHAR); // int serial_end_index = NUMBER_FORMATER.lastIndexOf(SERIAL_CHAR); int count =SERIAL_NUMBER.length(); StringBuilder sb = new StringBuilder(); for(int i=0;i<count;i++){ sb.append("0"); } java.text.DecimalFormat df = new java.text.DecimalFormat(sb.toString()); //如果存在 if(isExist){ //取得数据库中现存的编号 Map<String,Object> parametersMap = new HashMap<String,Object>(); parametersMap.put("TYPE", type); parametersMap.put("CUSTOMER_ID", customer_id); Map resultMap = current_numberDao.getCurrent_number(parametersMap); String old_current_number = (String) resultMap.get("CURRENT_NUMBER"); //数据库所存的编号的年 String year_str = old_current_number.substring(0,4); int current_number_year = Integer.parseInt(year_str); //数据库所存的编号的流水号 String seriou_num_str = old_current_number.substring(5,10); int current_number_seriou = Integer.parseInt(seriou_num_str); //当前系统时间的年份 Calendar nowCalendar = Calendar.getInstance(); int year = nowCalendar.get(Calendar.YEAR); if(year>current_number_year){ int current_serial =1 ; //生成当前的流水号 String current_serial_number = df.format(current_serial); String current_number = year+"-"+current_serial_number; this.updateCurrent_Number(type, customer_id, current_number); return current_number; }else{ int current_serial =1+current_number_seriou ; //生成当前的流水号 String current_serial_number = df.format(current_serial); String current_number = year+"-"+current_serial_number; this.updateCurrent_Number(type, customer_id, current_number);