Bladeren bron

原始单据生成规则

shiqian 4 jaren geleden
bovenliggende
commit
fb12574eb5

+ 37 - 0
boman-api/boman-domain/src/main/java/com.boman.domain/constant/BillConst.java

@@ -0,0 +1,37 @@
+package com.boman.domain.constant;
+
+
+/** 单据生成规则
+ * @author shiqian
+ * @date 2021年05月19日 13:32
+ **/
+public class BillConst {
+
+
+    /** 表名 */
+    public static final String SYS_SEQ_TABLE_NAME = "sys_seq";
+
+    /** seqName */
+    public static final String SEQ_NAME = "seq_name";
+
+    /** 增量 */
+    public static final String INCREMENTAL = "incremental";
+
+    /** 当前值 */
+    public static final String CURRENT_VALUE = "current_value";
+
+    /** 起始值 */
+    public static final String START_NUMBER = "start_number";
+
+    /** 格式 */
+    public static final String FORMAT = "format";
+
+    /** YEAR */
+    public static final String YEAR = "year";
+
+    /** MONTH */
+    public static final String MONTH = "month";
+
+    /** DAY */
+    public static final String DAY = "day";
+}

+ 15 - 0
boman-common/boman-common-core/src/main/java/com/boman/common/core/utils/DateUtils.java

@@ -21,8 +21,10 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
     public static String YYYY = "yyyy";
 
     public static String YYYY_MM = "yyyy-MM";
+    public static String YYYYMM = "yyyyMM";
 
     public static String YYYY_MM_DD = "yyyy-MM-dd";
+    public static String YYYYMMDD = "yyyyMMdd";
 
     public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
 
@@ -51,6 +53,19 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
         return dateTimeNow(YYYY_MM_DD);
     }
 
+    public static String getYear() {
+        return dateTimeNow(YYYY);
+    }
+
+    public static final String getYyyyMm() {
+        return dateTimeNow(YYYYMM);
+    }
+
+
+    public static final String getYyyyMmDd() {
+        return dateTimeNow(YYYYMMDD);
+    }
+
     public static final String getTime() {
         return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
     }

+ 5 - 0
boman-common/boman-common-redis/src/main/java/com/boman/common/redis/RedisKey.java

@@ -27,4 +27,9 @@ public class RedisKey {
      * gen_table_relation中的数据存到redis中的key
      */
     public static final String RELATION_INFO = "relation:info";
+
+    /**
+     * 单据的seq
+     */
+    public static final String BILL_SQE = "BILL:SEQ:";
 }

+ 9 - 0
boman-web-core/src/main/java/com/boman/web/core/mapper/StandardlyMapper.java

@@ -178,6 +178,15 @@ public interface StandardlyMapper {
             , @Param("packCondition") JSONObject packCondition
             , @Param("limitOne") boolean limitOne);
 
+    /**
+     * 功能描述: 获取最新的一条数据 create desc
+     *
+     * @param tableName tableName
+     * @return jcom.alibaba.fastjson.JSONObject
+     */
+    @Select("select * from ${tableName} order by create_time desc limit 1")
+    JSONObject getNewest(@Param("tableName") String tableName);
+
     @SuppressWarnings("unchecked")
     public static class SqlProvider {
         static final String[] READONLY_COLUMNS = new String[]{"OWNERID", "OWNERNAME", "OWNERENAME", "CREATIONDATE", "ID"};

+ 10 - 4
boman-web-core/src/main/java/com/boman/web/core/service/TableServiceCmdService.java

@@ -662,15 +662,21 @@ public class TableServiceCmdService {
         // extend的字段标记为true
         markTrueByExtend(allColumns);
         // 所有新增可见的列
-        allColumns = filterData(allColumns, 0, MaskConstant.INSERT_VISIBLE::equals);
+        List<GenTableColumn> insertVisiableColumns = filterData(allColumns, 0, MaskConstant.INSERT_VISIBLE::equals);
+//        insertVisiableColumns.add(IdUtils.getPkColumn(allColumns));
         // 带折叠
         if (BooleanUtils.isTrue(isUi)) {
             // 把孩子放入父亲的怀抱
             for (GenTableColumn hrColumn : parentColumns) {
                 List<GenTableColumn> children = Lists.newArrayListWithCapacity(16);
-                for (GenTableColumn column : allColumns) {
+                for (GenTableColumn column : insertVisiableColumns) {
                     if (hrColumn.getId().equals(column.getHrParentId())) {
+//                        if (GenTableColumn.IS_PK.equals(column.getIsPk())) {
+//                            column.setColumnValue(-1);
+//                        }
+
                         children.add(column);
+
                     }
                 }
                 hrColumn.setHrChildren(children);
@@ -680,13 +686,13 @@ public class TableServiceCmdService {
         } else {
             // 不带折叠
             // packDictDataToColumns(allColumns, ObjectUtils::isNotEmpty);
-            resultCols = allColumns;
+            resultCols = insertVisiableColumns;
         }
 
         JSONObject result = new JSONObject();
         result.put(BUTTON_LIST, getButton(tableName));
         result.put(SHOW_DATA, resultCols);
-        result.put(RULES, packRequireColumn(allColumns));
+        result.put(RULES, packRequireColumn(insertVisiableColumns));
         Integer tableColumn = genTable.getTableColumn();
         if (tableColumn != null){
             result.put(TABLE_COLUMN,tableColumn);

+ 12 - 0
boman-web-core/src/main/java/com/boman/web/core/service/common/CommonServiceImpl.java

@@ -94,4 +94,16 @@ public class CommonServiceImpl implements ICommonService {
         JSONObject packCondition = selectService.packColCondition(genTable.getColumns(), condition);
         return selectService.countByCondition(tableName, condition, packCondition);
     }
+
+    /**
+     * 功能描述: 获取最新的一条数据 create desc
+     *
+     * @param tableName tableName
+     * @return jcom.alibaba.fastjson.JSONObject
+     */
+    @Override
+    public JSONObject getNewest(String tableName) {
+        requireNonNull(tableName, "tableName is empty");
+        return selectService.getNewest(tableName);
+    }
 }

+ 8 - 0
boman-web-core/src/main/java/com/boman/web/core/service/common/ICommonService.java

@@ -48,4 +48,12 @@ public interface ICommonService {
      * @return java.lang.Long
      */
     int count(FormDataDto dto);
+
+    /**
+     * 功能描述: 获取最新的一条数据 create desc
+     *
+     * @param tableName tableName
+     * @return com.alibaba.fastjson.JSONObject
+     */
+    JSONObject getNewest(String tableName);
 }

+ 27 - 5
boman-web-core/src/main/java/com/boman/web/core/service/save/BaseSaveServiceImpl.java

@@ -3,15 +3,15 @@ package com.boman.web.core.service.save;
 import com.alibaba.fastjson.JSONObject;
 import com.boman.common.core.constant.CacheConstants;
 import com.boman.common.core.utils.SecurityUtils;
-import com.boman.common.core.utils.obj.ObjectUtils;
 import com.boman.common.redis.service.RedisService;
 import com.boman.common.security.service.TokenService;
-import com.boman.domain.constant.FormDataConstant;
 import com.boman.domain.GenTableColumn;
+import com.boman.domain.constant.FormDataConstant;
 import com.boman.system.api.model.LoginUser;
 import com.boman.web.core.domain.RowResult;
 import com.boman.web.core.domain.TableContext;
 import com.boman.web.core.mapper.StandardlyMapper;
+import com.boman.web.core.utils.BillRuleUtils;
 import com.boman.web.core.utils.ColumnUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -21,6 +21,8 @@ import org.springframework.stereotype.Component;
 import java.sql.Timestamp;
 import java.util.List;
 
+import static com.boman.common.core.utils.obj.ObjectUtils.*;
+
 /**
  * @author shiqian
  * @description
@@ -50,6 +52,8 @@ public class BaseSaveServiceImpl implements IBaseSaveService {
         ColumnUtils.packUpdateByAndTime(columns, commitData, new Timestamp(System.currentTimeMillis()), true);
         // 处理默认值
         handlerDefaultValue(commitData, columns);
+        // 如果有单据、按照单据编号规则
+        buildBillRule(context.getTableName(), commitData, columns);
 
         commitData.put(context.getPkName(), maxId);
         int ret = mapper.insert(context.getTableName(), commitData);
@@ -75,9 +79,7 @@ public class BaseSaveServiceImpl implements IBaseSaveService {
         JSONObject userEnv = loginUser.getUserEnv();
         for (GenTableColumn column : columns) {
             // 有默认值的列, 并且前台传过来的key中不包含
-            if (ObjectUtils.isNotEmpty(column.getDefaultValue())
-                    && !commitData.containsKey(column.getColumnName().toLowerCase())
-                    && !commitData.containsKey(column.getColumnName().toUpperCase())) {
+            if (isNotEmpty(column.getDefaultValue()) && !commitData.containsKey(column.getColumnName())) {
                 String dbDefaultValue = column.getDefaultValue();
                 String defaultValue = ColumnUtils.parseVariables(dbDefaultValue);
                 String variables = dbDefaultValue.equalsIgnoreCase(defaultValue) ? dbDefaultValue : userEnv.getString(defaultValue);
@@ -86,5 +88,25 @@ public class BaseSaveServiceImpl implements IBaseSaveService {
         }
     }
 
+    /*
+     * 功能描述: 单据规则
+     *
+     * @param commitData commitData
+     * @param allColumns allColumns
+     * @return void
+     */
+    private void buildBillRule(String tableName, JSONObject commitData, List<GenTableColumn> allColumns) {
+        requireNonNull(tableName, "tableName is empty");
+        for (GenTableColumn column : allColumns) {
+            String columnName = column.getColumnName();
+            String seqName = column.getSeqName();
+            if (isEmpty(seqName)) {
+                continue;
+            }
+
+            String rule = BillRuleUtils.getRules(tableName, seqName, columnName);
+            commitData.put(columnName, rule);
+        }
+    }
 
 }

+ 12 - 0
boman-web-core/src/main/java/com/boman/web/core/service/select/BaseSelectServiceImpl.java

@@ -171,4 +171,16 @@ public class BaseSelectServiceImpl implements IBaseSelectService {
 
         return result;
     }
+
+    /**
+     * 功能描述: 获取最新的一条数据 create desc
+     *
+     * @param tableName tableName
+     * @return jcom.alibaba.fastjson.JSONObject
+     */
+    @Override
+    public JSONObject getNewest(String tableName) {
+        requireNonNull(tableName, "tableName is empty");
+        return mapper.getNewest(tableName);
+    }
 }

+ 8 - 0
boman-web-core/src/main/java/com/boman/web/core/service/select/IBaseSelectService.java

@@ -76,4 +76,12 @@ public interface IBaseSelectService {
 
 
     JSONObject packColCondition(List<GenTableColumn> columns, JSONObject condition);
+
+    /**
+     * 功能描述: 获取最新的一条数据 create desc
+     *
+     * @param tableName tableName
+     * @return jcom.alibaba.fastjson.JSONObject
+     */
+    JSONObject getNewest(String tableName);
 }

+ 131 - 0
boman-web-core/src/main/java/com/boman/web/core/utils/BillRuleUtils.java

@@ -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);
+    }
+
+}

+ 13 - 0
boman-web-core/src/main/java/com/boman/web/core/utils/IdUtils.java

@@ -66,4 +66,17 @@ public class IdUtils {
         }
     }
 
+    public static GenTableColumn getPkColumn(List<GenTableColumn> columnList) {
+        if (CollectionUtils.isEmpty(columnList)) {
+            throw new IllegalArgumentException("获取主键失败");
+        }
+
+        for (GenTableColumn tableColumn : columnList) {
+            if (GenTableColumn.IS_PK.equalsIgnoreCase(tableColumn.getIsPk())) {
+                return tableColumn;
+            }
+        }
+
+        throw new IllegalArgumentException("不可能到这里的, 一个表连个主键都没有吗??");
+    }
 }