日期:2014-05-17  浏览次数:20817 次

请教一个问题,系统自动生成的一个流水码,由今年年份数字4位+从0001-9999每次递增1的4位数字组成的8位字符串。。。
该编号由当年年份的4位数字(如2010)+0001-9999的4位每次递增1的4位数字(如0002)组成的8位字符串(20100002)
这个是写在DaoImpl内的。。具体该怎么做。。感谢指导

------解决方案--------------------
我这有个差不多的,利用数据库生成的,可以参考下
Java code

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);