您好,登錄后才能下訂單哦!
/**
* 編碼生成器
*/
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();
/**
* 業(yè)務(wù)碼
* @return
*/
public abstract String getBusinessCode();
/**
* 順序號(hào)
* @return
*/
public abstract int getSerialNo() throws Exception ;
/**
* 順序號(hào)最少位數(shù)
* @param length
*/
protected void setLeastSerialNo(int length){
this.leastSerialNoLength = length;
}
/**
* 順序號(hào)不足補(bǔ)位
* @return
*/
protected String getWrapperSerialNo() throws Exception {
String format = "%0"+ leastSerialNoLength +"d";
return String.format(format, getSerialNo());
}
}
/**
* 通過數(shù)據(jù)庫的索引機(jī)制,建表生成相關(guān)數(shù)據(jù)
*/
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");
}
/**
* 獲取順序號(hào)。注:數(shù)據(jù)庫建立的唯一索引機(jī)制,獲取多并發(fā)的情況會(huì)報(bào)錯(cuò),因此嘗試第二次獲取。
* @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("更新計(jì)數(shù)器報(bào)錯(cuò)");
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("生成客戶編碼失敗");
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。