码迷,mamicode.com
首页 > 其他好文 > 详细

模板方法模式 生成唯一编号

时间:2018-06-28 16:46:34      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:efi   编号   creat   tran   etl   TE   try   obj   build   

模板抽象类
/**
 * 编码生成器
 */
public abstract class AbstractNumberGenerator {

    private int leastSerialNoLength = 6;

    /**
     * 生成编码
     * @return
     */
    public String generateNumber() throws Exception {
        return new StringBuilder().append(getPrefix()).append(getBusinessCode()).append(getWrapperSerialNo()).toString();
    }

    /**
     * 前缀
     * @return
     */
    public abstract String getPrefix();

    /**
     * 业务码
     * @return
     */
    public abstract String getBusinessCode();

    /**
     * 顺序号
     * @return
     */
    public abstract int getSerialNo() throws Exception ;

    /**
     * 顺序号最少位数
     * @param length
     */
    protected void setLeastSerialNo(int length){
        this.leastSerialNoLength = length;
    }

    /**
     * 顺序号不足补位
     * @return
     */
    protected String getWrapperSerialNo() throws Exception {
        String format = "%0"+ leastSerialNoLength +"d";
        return String.format(format, getSerialNo());
    }
}

数据库实现顺序号

/**
 * 通过数据库的索引机制,建表生成相关数据
 */
public class CustomerNumberCounterGenerator extends AbstractNumberGenerator {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerNumberCounterGenerator.class);

    @Override
    public String getPrefix() {
        return "K";
    }

    @Override
    public String getBusinessCode() {
        return DateUtils.format(new Date(), "yyMMdd");
    }

    /**
     * 获取顺序号。注:数据库建立的唯一索引机制,获取多并发的情况会报错,因此尝试第二次获取。
     * @return
     * @throws Exception
     */
    @Override
    public int getSerialNo() throws Exception {
        int serialNo = Integer.MIN_VALUE;
        try {
            serialNo = getCustomSerialNo();
        } catch (Exception e) {
            try {
                serialNo = getCustomSerialNo();
            } catch (Exception e1) {
                throw new Exception("getSerialNo is failed.", e1);
            }
        }
        return serialNo;
    }

    public int getCustomSerialNo() throws Exception {
        String prefix = getPrefix();
        String businessCode = getBusinessCode();
        CustomerNumberCounter numberCounter = CustomerNumberCounter.findFirst(CustomerNumberCounter.class, "prefix=? and timeCode=?", new Object[]{prefix, businessCode});
        if (null == numberCounter){
            numberCounter = new CustomerNumberCounter();
            numberCounter.setPrefix(prefix);
            numberCounter.setTimeCode(businessCode);
            numberCounter.setSerialNo(1);
            numberCounter.create();
            return 1;
        }
        try {
            BaseDomain.beginTransaction(CustomerNumberCounter.class);
            int execute = BaseDomain.execute(BaseDomain.class, "update t_customer_number_counter set serialNo = serialNo+1 where id = ?", new Object[]{numberCounter.getId()});
            if (execute != 1)
                throw new IllegalAccessException("更新计数器报错");
            return CustomerNumberCounter.find(CustomerNumberCounter.class, numberCounter.getId()).getSerialNo();
        }catch (Exception e){
            BaseDomain.rollback(CustomerNumberCounter.class);
            if (LOGGER.isErrorEnabled())
                LOGGER.error("to generate customer number occur error.", e);
        }finally {
            if (BaseDomain.getCurrentTransaction(CustomerNumberCounter.class) != null)
                BaseDomain.commit(CustomerNumberCounter.class);
        }
        throw new IllegalAccessException("生成客户编码失败");
    }

}

模板方法模式 生成唯一编号

标签:efi   编号   creat   tran   etl   TE   try   obj   build   

原文地址:http://blog.51cto.com/881206524/2133764

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!