public static Map<Object, Object> invoiceNumberMap;// 票号
public static final int FOUR_COUNT = 4; // 4位序列号(0001-9999)
static {
invoiceNumberMap = new HashMap<Object, Object>();
}
/**
* 得到新的批次号或编号的序列号(001-999)
* @param map
* @param key
* @return
* @throws Exception
*/
private Object getNewSerial(Object serialNum, int count) throws Exception {
String prefix = "";
if (serialNum == null) {
for (int i = 0; i < count - 1; i++) {
prefix += "0";
}
return prefix + "1";
}
String aSerialNum = serialNum.toString();
for (int i=0; i<aSerialNum.length(); i++) {
if (aSerialNum.charAt(i) == '0') {
prefix += "0";
} else {
break;
}
}
int suffix = Integer.parseInt(aSerialNum.substring(prefix.length())) + 1;
prefix = "";
for (int i=0; i<count - (suffix + "").length(); i++) {
prefix += "0";
}
return prefix + suffix;
}
/**
*
*取收据的票号 生成规则: 例:
*规则:SD+YYYYMMDDD+自增值。示例:201808290003
/
public synchronized String getinNum () throws Exception {
StringBuffer serialNo = new StringBuffer("SD");
serialNo.append(getCurDate());
Object newSearial = this.getNewSerial(invoiceNumberMap.get(serialNo.toString()), FOUR_COUNT);
serialNo.append(newSearial);
addSerialToSerialMapForServiceNo(invoiceNumberMap, serialNo.toString());
return serialNo.toString();
}
private void addSerialToSerialMapForServiceNo(Map<Object, Object> map, String serialNo) {
int index = serialNo.length() - 4;
String key = serialNo.substring(0, index);
String value = serialNo.substring(index + 1);
map.put(key, value);
}