Browse Source

强制上下移

MaiXinRong 1 năm trước cách đây
mục cha
commit
2860da509c
1 tập tin đã thay đổi với 38 bổ sung5 xóa
  1. 38 5
      app/service/sub_project.js

+ 38 - 5
app/service/sub_project.js

@@ -96,8 +96,13 @@ module.exports = app => {
         }
 
         async getLastChild(tree_pid) {
-            const result = await this.getAllDataByCondition({ where: { tree_pid }, orders: [['tree_order', 'desc']], limit: 1, offset: 0 });
-            return result[0];
+            if (tree_pid === -1) {
+                const result = await this.getAllDataByCondition({ where: { tree_pid, project_id: this.ctx.session.sessionProject.id }, orders: [['tree_order', 'asc']], limit: 1, offset: 0 });
+                return result[0];
+            } else {
+                const result = await this.getAllDataByCondition({ where: { tree_pid }, orders: [['tree_order', 'asc']], limit: 1, offset: 0 });
+                return result[0];
+            }
         }
 
         async getPosterityData(id){
@@ -122,7 +127,7 @@ module.exports = app => {
                     step = step + 1;
                 }
             }
-            return await this.getAllDataByCondition({ where: { tree_pid: node.tree_pid, tree_order }, orders: [['tree_order', 'desc']]});
+            return await this.getAllDataByCondition({ where: { tree_pid: node.tree_pid, tree_order, project_id: this.ctx.session.sessionProject.id }, orders: [['tree_order', 'desc']]});
         }
 
         async addFolder(data) {
@@ -237,6 +242,34 @@ module.exports = app => {
             }
         }
 
+        async _siblingMoveForce(node, step) {
+            const sibling = await this.getAllDataByCondition({ where: { tree_pid: node.tree_pid, project_id: this.ctx.session.sessionProject.id }, orders: [['tree_order', 'asc']] });
+            const nodeIndex = sibling.findIndex(x => { return x.id === node.id });
+            if (nodeIndex + step < 0) throw '移动数据结构错误';
+            if (nodeIndex + step > sibling.length - 1) throw '移动数据结构错误';
+
+            const conn = await this.db.beginTransaction();
+            try {
+                const updateData = [];
+                updateData.push({ id: node.id, tree_order: sibling[nodeIndex + step].tree_order });
+                while(step) {
+                    const stepNode = sibling[nodeIndex + step];
+                    if (step > 0) {
+                        updateData.push({ id: stepNode.id, tree_order: sibling[nodeIndex + step - 1].tree_order });
+                        step = step - 1;
+                    } else {
+                        updateData.push({ id: stepNode.id, tree_order: sibling[nodeIndex + step + 1].tree_order});
+                        step = step + 1;
+                    }
+                }
+                await conn.updateRows(this.tableName, updateData);
+                await conn.commit();
+            } catch (error) {
+                await conn.rollback();
+                throw error;
+            }
+        }
+
         async _topMove(node) {
             const lastChild = await this.getLastChild(-1);
             const posterity = await this.getPosterityData(node.id);
@@ -265,8 +298,8 @@ module.exports = app => {
             if (!node) throw '移动数据结构错误';
 
             switch(data.type) {
-                case 'up': await this._siblingMove(node, -1); break;
-                case 'down': await this._siblingMove(node, 1); break;
+                case 'up': await this._siblingMoveForce(node, -1); break;
+                case 'down': await this._siblingMoveForce(node, 1); break;
                 case 'top': await this._topMove(node); break;
                 default: throw '未知移动类型';
             }