|
@@ -31,34 +31,36 @@ public class BillRuleUtils {
|
|
|
requireNonNull(columnName, "columnName is empty");
|
|
|
RedisService redisService = SpringUtils.getBean(RedisService.class);
|
|
|
ICommonService commonService = SpringUtils.getBean(ICommonService.class);
|
|
|
- String sequencesKey = BILL_SQE + seqName.toUpperCase();
|
|
|
- JSONObject rule = redisService.getCacheObject(sequencesKey);
|
|
|
+
|
|
|
+ String sequencesKey = packRulesKey(seqName);
|
|
|
+ JSONObject ruleFromRedis = redisService.getCacheObject(sequencesKey);
|
|
|
+
|
|
|
String currentValue;
|
|
|
- if (isEmpty(rule)) {
|
|
|
- rule = getSysSeqFromDb(seqName);
|
|
|
- if (isEmpty(rule)) {
|
|
|
+ if (isEmpty(ruleFromRedis)) {
|
|
|
+ // 数据库中没有 CURRENT_VALUE
|
|
|
+ ruleFromRedis = getSysSeqFromDb(seqName);
|
|
|
+ if (isEmpty(ruleFromRedis)) {
|
|
|
throw new IllegalArgumentException(String.format("表 [%s] 未配置单据生成规则,seqName: %s", tableName, seqName));
|
|
|
}
|
|
|
|
|
|
JSONObject newest = commonService.getNewest(tableName);
|
|
|
if (isEmpty(newest)) {
|
|
|
- String car = rule.getString(SEQ_NAME);
|
|
|
- String rules = getRules(rule.getString(FORMAT));
|
|
|
- String last = rule.getString(START_NUMBER);
|
|
|
+ String car = ruleFromRedis.getString(SEQ_NAME);
|
|
|
+ String rules = getTimeFormat(ruleFromRedis.getString(FORMAT));
|
|
|
+ String last = ruleFromRedis.getString(START_NUMBER);
|
|
|
// 最最原始的数据
|
|
|
currentValue = car + rules + last;
|
|
|
} else {
|
|
|
// 这种情况几乎不会有,除非redis中数据被人手动清空了
|
|
|
- String beforeValue = newest.getString(columnName);
|
|
|
- currentValue = resolveRules(rule, beforeValue);
|
|
|
+ currentValue = resolveRules(ruleFromRedis);
|
|
|
}
|
|
|
} else {
|
|
|
- currentValue = rule.getString(CURRENT_VALUE);
|
|
|
- currentValue = resolveRules(rule, currentValue);
|
|
|
+// String beforeValue = ruleFromRedis.getString(CURRENT_VALUE);
|
|
|
+ currentValue = resolveRules(ruleFromRedis);
|
|
|
}
|
|
|
|
|
|
- rule.put(CURRENT_VALUE, currentValue);
|
|
|
- redisService.setCacheObject(sequencesKey, rule);
|
|
|
+ ruleFromRedis.put(CURRENT_VALUE, currentValue);
|
|
|
+ redisService.setCacheObject(sequencesKey, ruleFromRedis);
|
|
|
return currentValue;
|
|
|
}
|
|
|
|
|
@@ -66,17 +68,34 @@ public class BillRuleUtils {
|
|
|
* 功能描述: 把001 -> 002
|
|
|
*
|
|
|
* @param rule sys_seq中的数据
|
|
|
- * @param beforeValue 现在数据库最大的值
|
|
|
* @return java.lang.String
|
|
|
*/
|
|
|
- private static String resolveRules(JSONObject rule, String beforeValue) {
|
|
|
+ private static String resolveRules(JSONObject rule) {
|
|
|
+ // 2021-05-05
|
|
|
+ String timeFormat = getTimeFormat(rule.getString(FORMAT));
|
|
|
+
|
|
|
+ String beforeValue = rule.getString(CURRENT_VALUE);
|
|
|
+ if (isEmpty(beforeValue)) {
|
|
|
+ return rule.getString(SEQ_NAME) + timeFormat + rule.getString(START_NUMBER);
|
|
|
+ }
|
|
|
+
|
|
|
int totalLength = beforeValue.length();
|
|
|
int suffixLength = rule.getString(START_NUMBER).length();
|
|
|
int prefixLength = totalLength - suffixLength;
|
|
|
- String suffixValue = beforeValue.substring(prefixLength, totalLength);
|
|
|
- String plusOneSuffixValue = strPlusOne(suffixValue, rule.getInteger(INCREMENTAL));
|
|
|
- String rules = getRules(rule.getString(FORMAT));
|
|
|
- return rule.getString(SEQ_NAME) + rules + plusOneSuffixValue;
|
|
|
+
|
|
|
+ // 0001
|
|
|
+ String suffixValue;
|
|
|
+ try {
|
|
|
+ suffixValue = beforeValue.substring(prefixLength, totalLength);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return rule.getString(SEQ_NAME) + timeFormat + rule.getString(START_NUMBER);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 0002
|
|
|
+ String plusOneSuffixValue = strPlusOneByTimeFormat(suffixValue, rule);
|
|
|
+
|
|
|
+ return rule.getString(SEQ_NAME) + timeFormat + plusOneSuffixValue;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -99,7 +118,7 @@ public class BillRuleUtils {
|
|
|
* @param format 规则
|
|
|
* @return java.lang.String
|
|
|
*/
|
|
|
- private static String getRules(String format) {
|
|
|
+ private static String getTimeFormat(String format) {
|
|
|
if (YEAR.equals(format)) {
|
|
|
return DateUtils.getYear();
|
|
|
} else if (MONTH.equals(format)) {
|
|
@@ -117,15 +136,75 @@ public class BillRuleUtils {
|
|
|
* 0001 -> 0002
|
|
|
* ......
|
|
|
*
|
|
|
- * @param in 输入
|
|
|
- * @param increaseNum 增量
|
|
|
+ * @param value 单据编号后缀的值
|
|
|
+ * @param rule 增量
|
|
|
* @return java.lang.String
|
|
|
*/
|
|
|
- public static String strPlusOne(String in, Integer increaseNum) {
|
|
|
- requireNonNull(in, "redisStr is empty");
|
|
|
+ public static String strPlusOneByTimeFormat(String value, JSONObject rule) {
|
|
|
+ requireNonNull(rule, "rule is empty");
|
|
|
+ requireNonNull(value, "suffixValue is empty");
|
|
|
+
|
|
|
+ Integer increaseNum = rule.getInteger(INCREMENTAL);
|
|
|
+ String startNumber = rule.getString(START_NUMBER);
|
|
|
+ String timeFormat = rule.getString(FORMAT);
|
|
|
requireNonNull(increaseNum, "increaseNum is empty");
|
|
|
- int i = Integer.parseInt(in) + increaseNum;
|
|
|
- return String.format("%0" + in.length() + "d", i);
|
|
|
+ requireNonNull(timeFormat, "timeFormat is empty");
|
|
|
+
|
|
|
+ int parseInt;
|
|
|
+ try {
|
|
|
+ parseInt = Integer.parseInt(value);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ // 默认值
|
|
|
+ return startNumber;
|
|
|
+ }
|
|
|
+
|
|
|
+ // trip202002020001
|
|
|
+ String current = rule.getString(CURRENT_VALUE);
|
|
|
+ String seqName = rule.getString(SEQ_NAME);
|
|
|
+ int intValue;
|
|
|
+ if (YEAR.equals(timeFormat)) {
|
|
|
+ String year = DateUtils.getYear();
|
|
|
+ // 2020
|
|
|
+ String timeFormatValue = getTimeFormatValue(current, seqName, 4);
|
|
|
+ intValue = year.equals(timeFormatValue) ? parseInt + increaseNum : Integer.parseInt(startNumber);
|
|
|
+ } else if (MONTH.equals(timeFormat)) {
|
|
|
+ String yyyyMm = DateUtils.getYyyyMm();
|
|
|
+ // 202002
|
|
|
+ String timeFormatValue = getTimeFormatValue(current, seqName, 6);
|
|
|
+ intValue = yyyyMm.equals(timeFormatValue) ? parseInt + increaseNum : Integer.parseInt(startNumber);
|
|
|
+ } else {
|
|
|
+ String yyyyMmDd = DateUtils.getYyyyMmDd();
|
|
|
+ // 20200202
|
|
|
+ String timeFormatValue = getTimeFormatValue(current, seqName, 8);
|
|
|
+ intValue = yyyyMmDd.equals(timeFormatValue) ? parseInt + increaseNum : Integer.parseInt(startNumber);
|
|
|
+ }
|
|
|
+
|
|
|
+ return String.format("%0" + value.length() + "d", intValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String packRulesKey(String seqName) {
|
|
|
+ return (BILL_SQE + seqName.toUpperCase()).trim();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 功能描述: 截取中间的时间
|
|
|
+ * timeFormatLength=4 => 2020
|
|
|
+ * timeFormatLength=6 => 202002
|
|
|
+ * timeFormatLength=8 => 20200202
|
|
|
+ *
|
|
|
+ * @param value trip202002020001
|
|
|
+ * @param seqName trip
|
|
|
+ * @param timeFormatLength 4
|
|
|
+ * @return java.lang.String
|
|
|
+ */
|
|
|
+ public static String getTimeFormatValue(String value, String seqName, int timeFormatLength) {
|
|
|
+ // value = trip202002020001
|
|
|
+ requireNonNull(value, "value is empty");
|
|
|
+ requireNonNull(seqName, "seqName is empty");
|
|
|
+ // substring = 202002020001
|
|
|
+ String substring = value.substring(seqName.length());
|
|
|
+ return substring.substring(0, timeFormatLength);
|
|
|
}
|
|
|
|
|
|
}
|