浏览代码

maxLid值获取调整

ellisran 10 月之前
父节点
当前提交
f33755bf5f
共有 2 个文件被更改,包括 36 次插入8 次删除
  1. 33 0
      app/base/base_bills_service.js
  2. 3 8
      app/base/base_tree_service.js

+ 33 - 0
app/base/base_bills_service.js

@@ -787,6 +787,39 @@ class BaseBillsSerivce extends TreeService {
         ledgerBills.is_tp = ledgerExtra ? ledgerExtra.is_tp : 0;
         return ledgerBills
     }
+
+    /**
+     * 获取最大节点id
+     *
+     * @param {Number} mid - master id
+     * @return {Number}
+     * @private
+     */
+    async _getMaxLid(mid) {
+        const cacheKey = this.setting.keyPre + mid;
+        let maxId = this.setting.cacheKey ? parseInt(await this.cache.get(cacheKey)) : undefined;
+        if (this.setting.cacheKey) {
+            // 判断是否存在变更新增部位maxId,有则取两者最大值
+            const changeReviseCacheKey = 'change_ledger_maxLid2:' + mid;
+            const changeReviseMaxId = await this.cache.get(changeReviseCacheKey);
+            if (changeReviseMaxId) {
+                maxId = !maxId ? parseInt(changeReviseMaxId) : Math.max(maxId, parseInt(changeReviseMaxId));
+            }
+        }
+        if (!maxId) {
+            const sql = 'SELECT Max(??) As max_id FROM ?? Where ' + this.setting.mid + ' = ?';
+            const sqlParam = [this.setting.kid, this.tableName, mid];
+            const queryResult = await this.db.queryOne(sql, sqlParam);
+            maxId = queryResult.max_id || 0;
+            const sql1 = 'SELECT Max(??) As max_id FROM ?? Where ' + this.setting.mid + ' = ?';
+            const sqlParam1 = [this.setting.kid, this.ctx.service.changeLedger.tableName, mid];
+            const queryResult1 = await this.db.queryOne(sql1, sqlParam1);
+            const maxId1 = queryResult1.max_id || 0;
+            maxId = Math.max(maxId, maxId1);
+            if (this.setting.cacheKey) this.cache.set(cacheKey, maxId, 'EX', this.ctx.app.config.cacheTime);
+        }
+        return maxId;
+    }
 }
 
 module.exports = BaseBillsSerivce;

+ 3 - 8
app/base/base_tree_service.js

@@ -35,6 +35,7 @@ class TreeService extends Service {
         super(ctx);
         this.tableName = setting.tableName;
         this.setting = setting;
+        if (this.setting.cacheKey === undefined) this.setting.cacheKey = true;
         // 以下字段仅可通过树结构操作改变,不可直接通过update方式从接口提交,发现时过滤
         this.readOnlyFields = ['id'];
         this.readOnlyFields.push(this.setting.mid);
@@ -332,19 +333,13 @@ class TreeService extends Service {
      */
     async _getMaxLid(mid) {
         const cacheKey = this.setting.keyPre + mid;
-        let maxId = parseInt(await this.cache.get(cacheKey));
-        // 判断是否存在变更新增部位maxId,有则取两者最大值
-        const changeReviseCacheKey = 'change_ledger_maxLid2:' + mid;
-        const changeReviseMaxId = await this.cache.get(changeReviseCacheKey);
-        if (changeReviseMaxId) {
-            maxId = !maxId ? parseInt(changeReviseMaxId) : Math.max(maxId, parseInt(changeReviseMaxId));
-        }
+        let maxId = this.setting.cacheKey ? parseInt(await this.cache.get(cacheKey)) : undefined;
         if (!maxId) {
             const sql = 'SELECT Max(??) As max_id FROM ?? Where ' + this.setting.mid + ' = ?';
             const sqlParam = [this.setting.kid, this.tableName, mid];
             const queryResult = await this.db.queryOne(sql, sqlParam);
             maxId = queryResult.max_id || 0;
-            this.cache.set(cacheKey, maxId, 'EX', this.ctx.app.config.cacheTime);
+            if (this.setting.cacheKey) this.cache.set(cacheKey, maxId, 'EX', this.ctx.app.config.cacheTime);
         }
         return maxId;
     }