|
@@ -0,0 +1,131 @@
|
|
|
+package com.boman.web.core.utils;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.boman.common.core.utils.DateUtils;
|
|
|
+import com.boman.common.core.utils.SpringUtils;
|
|
|
+import com.boman.common.redis.service.RedisService;
|
|
|
+import com.boman.web.core.service.common.ICommonService;
|
|
|
+
|
|
|
+import static com.boman.common.core.utils.obj.ObjectUtils.*;
|
|
|
+import static com.boman.common.redis.RedisKey.BILL_SQE;
|
|
|
+import static com.boman.domain.constant.BillConst.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author shiqian
|
|
|
+ * @description 单据编号生成
|
|
|
+ * @date 2021年03月23日 16:04
|
|
|
+ **/
|
|
|
+public class BillRuleUtils {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 功能描述: 获取最新的单据编号,根据sys_seq定义的规则
|
|
|
+ *
|
|
|
+ * @param tableName 业务表
|
|
|
+ * @param seqName gen_table_column中业务表有单据的列对应的seqName
|
|
|
+ * @param columnName 业务表有单据的列
|
|
|
+ * @return java.lang.String
|
|
|
+ */
|
|
|
+ public static String getRules(String tableName, String seqName, String columnName) {
|
|
|
+ requireNonNull(tableName, "tableName is empty");
|
|
|
+ requireNonNull(seqName, "seqName is empty");
|
|
|
+ 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 currentValue;
|
|
|
+ if (isEmpty(rule)) {
|
|
|
+ rule = getSysSeqFromDb(seqName);
|
|
|
+ if (isEmpty(rule)) {
|
|
|
+ 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);
|
|
|
+ // 最最原始的数据
|
|
|
+ currentValue = car + rules + last;
|
|
|
+ } else {
|
|
|
+ // 这种情况几乎不会有,除非redis中数据被人手动清空了
|
|
|
+ String beforeValue = newest.getString(columnName);
|
|
|
+ currentValue = resolveRules(rule, beforeValue);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ currentValue = rule.getString(CURRENT_VALUE);
|
|
|
+ currentValue = resolveRules(rule, currentValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ rule.put(CURRENT_VALUE, currentValue);
|
|
|
+ redisService.setCacheObject(sequencesKey, rule);
|
|
|
+ return currentValue;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 功能描述: 把001 -> 002
|
|
|
+ *
|
|
|
+ * @param rule sys_seq中的数据
|
|
|
+ * @param beforeValue 现在数据库最大的值
|
|
|
+ * @return java.lang.String
|
|
|
+ */
|
|
|
+ private static String resolveRules(JSONObject rule, String beforeValue) {
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 功能描述: getSysSeqFromDb
|
|
|
+ *
|
|
|
+ * @param seqName seqName
|
|
|
+ * @return com.alibaba.fastjson.JSONObject
|
|
|
+ */
|
|
|
+ private static JSONObject getSysSeqFromDb(String seqName) {
|
|
|
+ // 查数据库
|
|
|
+ JSONObject condition = new JSONObject();
|
|
|
+ condition.put(SEQ_NAME, seqName);
|
|
|
+ ICommonService commonService = SpringUtils.getBean(ICommonService.class);
|
|
|
+ return commonService.getOneByMap(SYS_SEQ_TABLE_NAME, condition);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 功能描述: 根据sys_seq 中对应的format来获取数据
|
|
|
+ *
|
|
|
+ * @param format 规则
|
|
|
+ * @return java.lang.String
|
|
|
+ */
|
|
|
+ private static String getRules(String format) {
|
|
|
+ if (YEAR.equals(format)) {
|
|
|
+ return DateUtils.getYear();
|
|
|
+ } else if (MONTH.equals(format)) {
|
|
|
+ return DateUtils.getYyyyMm();
|
|
|
+ } else if (DAY.equals(format)) {
|
|
|
+ return DateUtils.getYyyyMmDd();
|
|
|
+ }
|
|
|
+
|
|
|
+ throw new IllegalArgumentException("sys_seq表中format配置错误,format = " + format);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 功能描述: eg:
|
|
|
+ * 001 -> 002
|
|
|
+ * 0001 -> 0002
|
|
|
+ * ......
|
|
|
+ *
|
|
|
+ * @param in 输入
|
|
|
+ * @param increaseNum 增量
|
|
|
+ * @return java.lang.String
|
|
|
+ */
|
|
|
+ public static String strPlusOne(String in, Integer increaseNum) {
|
|
|
+ requireNonNull(in, "redisStr is empty");
|
|
|
+ requireNonNull(increaseNum, "increaseNum is empty");
|
|
|
+ int i = Integer.parseInt(in) + increaseNum;
|
|
|
+ return String.format("%0" + in.length() + "d", i);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|