package foundation.code.segment;
|
|
import foundation.code.CodeRule;
|
import foundation.code.SegmentCreator;
|
import foundation.period.ActivePeriod;
|
import foundation.persist.NamedSQL;
|
import foundation.persist.SQLRunner;
|
import foundation.util.ID;
|
import foundation.variant.provider.IVariantsProvider;
|
|
|
public class MonthlyNoSegment extends SegmentCreator {
|
|
public static String SQL_GetPeriodSequence = "getPeriodSequence";
|
public static String SQL_UpdatePeriodSequence = "updatePeriodSequence";
|
public static String SQL_InsertPeriodSequence = "insertPeriodSequence";
|
private NamedSQL getPeriodSequence;
|
private NamedSQL updatePeriodSequence;
|
private NamedSQL insertPeriodSequence;
|
private String ruleCode;
|
private String cachedValue;
|
private Integer sequenceNo;
|
|
public MonthlyNoSegment() {
|
sequenceNo = null;
|
}
|
|
@Override
|
protected void init(CodeRule rule) {
|
ruleCode = rule.getCode();
|
|
try {
|
getPeriodSequence = NamedSQL.getInstance(SQL_GetPeriodSequence);
|
getPeriodSequence.setParam("code", ruleCode);
|
|
updatePeriodSequence = NamedSQL.getInstance(SQL_UpdatePeriodSequence);
|
updatePeriodSequence.setParam("code", ruleCode);
|
|
insertPeriodSequence = NamedSQL.getInstance(SQL_InsertPeriodSequence);
|
insertPeriodSequence.setParam("code", ruleCode);
|
}
|
catch(Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
@Override
|
public String getNextSequenceValue(String dynamic, IVariantsProvider... entitys) {
|
synchronized (this) {
|
NamedSQL namedSQL = null;
|
int monthNo = ActivePeriod.getMonthNo();
|
|
try {
|
//1. 如果内存中没有记录,就获取一次,并插入一条记录
|
if (sequenceNo == null) {
|
namedSQL = getPeriodSequence.createInstance();
|
namedSQL.setParam("periodNo", monthNo);
|
namedSQL.setParam("code", ruleCode);
|
sequenceNo = SQLRunner.getInteger(namedSQL);
|
}
|
|
//2. 如果数据库中没有记录,就插入一条记录(只有第一次会运行)
|
if (sequenceNo == null) {
|
sequenceNo = 1;
|
|
namedSQL = insertPeriodSequence.createInstance();
|
namedSQL.setParam("id", ID.newValue());
|
namedSQL.setParam("code", ruleCode);
|
namedSQL.setParam("periodNo", monthNo);
|
namedSQL.setParam("sequenceValue", sequenceNo);
|
SQLRunner.execSQL(namedSQL);
|
}
|
|
//3. 如果数据库中没有记录,就插入一条记录(只有第一次会运行)
|
else {
|
sequenceNo = sequenceNo + 1;
|
|
namedSQL = updatePeriodSequence.createInstance();
|
namedSQL.setParam("code", ruleCode);
|
namedSQL.setParam("value", sequenceNo);
|
namedSQL.setParam("periodNo", monthNo);
|
SQLRunner.execSQL(namedSQL);
|
}
|
|
//4. 返回结果
|
String result = String.valueOf(sequenceNo);
|
result = format(result);
|
|
cachedValue = result;
|
return result;
|
}
|
catch (Exception e) {
|
return null;
|
}
|
}
|
}
|
|
@Override
|
public String getCurrSequenceValue(String dynamic, IVariantsProvider... entitys) {
|
synchronized (this) {
|
NamedSQL namedSQL = null;
|
int monthNo = ActivePeriod.getMonthNo();
|
|
try {
|
//1. 如果内存中没有记录,就获取一次,并插入一条记录
|
if (sequenceNo == null) {
|
namedSQL = getPeriodSequence.createInstance();
|
namedSQL.setParam("periodNo", monthNo);
|
sequenceNo = SQLRunner.getInteger(namedSQL);
|
}
|
|
//2. 如果数据库中没有记录,就插入一条记录(只有第一次会运行)
|
if (sequenceNo == null) {
|
sequenceNo = 1;
|
|
namedSQL = insertPeriodSequence.createInstance();
|
namedSQL.setParam("id", ID.newValue());
|
namedSQL.setParam("code", ruleCode);
|
namedSQL.setParam("periodNo", monthNo);
|
namedSQL.setParam("sequenceValue", sequenceNo);
|
SQLRunner.execSQL(namedSQL);
|
}
|
|
//3. 返回结果
|
String result = String.valueOf(sequenceNo);
|
result = format(result);
|
|
cachedValue = result;
|
return result;
|
}
|
catch (Exception e) {
|
return null;
|
}
|
}
|
}
|
|
|
|
@Override
|
protected String format(String value) {
|
if (value == null) {
|
return value;
|
}
|
|
int length = value.length();
|
|
if (length > size-1) {
|
return value.substring(0, size-1);
|
}
|
else if (length == size-1) {
|
return value;
|
}
|
else {
|
while (value.length() < size-1) {
|
value = "0" + value;
|
}
|
|
return value;
|
}
|
}
|
|
@Override
|
public String getTempValue(String dynamic, IVariantsProvider... entitys) {
|
String result = format("#");
|
return result;
|
}
|
|
@Override
|
protected void setCodeRule(CodeRule codeRule) {
|
ruleCode = codeRule.getCode();
|
super.setCodeRule(codeRule);
|
}
|
|
@Override
|
public String toString() {
|
return codeRule.getCode() + " : " + cachedValue;
|
}
|
|
}
|