瀏覽代碼

添加分类,默认名称

MaiXinRong 2 年之前
父節點
當前提交
592b3677f3
共有 2 個文件被更改,包括 14 次插入2 次删除
  1. 1 1
      app/public/js/file_detail.js
  2. 13 1
      app/service/filing.js

+ 1 - 1
app/public/js/file_detail.js

@@ -106,7 +106,7 @@ $(document).ready(function() {
             postData('filing/add', { tree_pid: node.tree_pid, tree_pre_id: node.id }, function(result) {
                 const refreshData = self.dragTree.loadPostData(result);
                 const newNode = refreshData.create[0];
-                self.filingTree.addNodes(node.getParentNode(), node.getIndex(), [{ id: newNode.id, tree_pid: newNode.tree_pid, name: newNode.name, spid: newNode.spid, source_node: newNode}]);
+                self.filingTree.addNodes(node.getParentNode(), node.getIndex() + 1, [{ id: newNode.id, tree_pid: newNode.tree_pid, name: newNode.name, spid: newNode.spid, source_node: newNode}]);
             });
         }
         addChildFiling(node) {

+ 13 - 1
app/service/filing.js

@@ -85,6 +85,17 @@ module.exports = app => {
             if (data.is_fixed) throw '固定分类,不可编辑';
         }
 
+        async getNewName(spid, name = '新增文件类别') {
+            const data = await this.db.query(`SELECT * FROM ${this.tableName} WHERE spid = '${spid}' AND name LIKE '${name}%'`);
+            if (data.length === 0) return name;
+
+            const _ = this._;
+            const names = data.map(x => { return _.toInteger(x.name.replace(name, '')) });
+            const filterNames = names.filter(x => { return x > 0 });
+            const max = filterNames.reduce((pre, cur) => { return Math.max(pre, cur); }, 0);
+            return max >= 0 ? name + (max + 1) : name;
+        }
+
         async add(data) {
             const parent = await this.getDataById(data.tree_pid);
             if (!parent) throw '添加数据结构错误';
@@ -101,10 +112,11 @@ module.exports = app => {
                 const sessionProject = this.ctx.session.sessionProject;
 
                 const tree_order = preChild ? preChild.tree_order + 1 : (sibling.length > 0 ? sibling[sibling.length - 1].tree_order + 1 : 1);
+                const name = await this.getNewName(parent.spid);
                 const insertData = {
                     id: this.uuid.v4(), spid: parent.spid, add_user_id: sessionUser.accountId,
                     tree_pid: parent.id, tree_level: parent.tree_level + 1, tree_order,
-                    name: 'xx文件', filing_type: parent.filing_type,
+                    name, filing_type: parent.filing_type,
                 };
                 const operate = await conn.insert(this.tableName, insertData);
                 if (operate.affectedRows === 0) throw '新增文件夹失败';