|
@@ -100,6 +100,31 @@ module.exports = app => {
|
|
return result[0];
|
|
return result[0];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ async getPosterityData(id){
|
|
|
|
+ const result = [];
|
|
|
|
+ let cur = await this.getAllDataByCondition({ where: { tree_pid: id } });
|
|
|
|
+ let iLevel = 1;
|
|
|
|
+ while (cur.length > 0 && iLevel < 6) {
|
|
|
|
+ result.push(...cur);
|
|
|
|
+ cur = await this.getAllDataByCondition({ where: { tree_pid: cur.map(x => { return x.id })} });
|
|
|
|
+ iLevel += 1;
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ async getStepNode(node, step) {
|
|
|
|
+ const tree_order = [];
|
|
|
|
+ while(step) {
|
|
|
|
+ tree_order.push(node.tree_order + step);
|
|
|
|
+ if (step > 0) {
|
|
|
|
+ step = step - 1;
|
|
|
|
+ } else {
|
|
|
|
+ step = step + 1;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return await this.getAllDataByCondition({ where: { tree_pid: node.tree_pid, tree_order }, orders: [['tree_order', 'desc']]});
|
|
|
|
+ }
|
|
|
|
+
|
|
async addFolder(data) {
|
|
async addFolder(data) {
|
|
const parent = await this.getDataById(data.tree_pid);
|
|
const parent = await this.getDataById(data.tree_pid);
|
|
if (parent && !parent.is_folder) throw '添加数据结构错误';
|
|
if (parent && !parent.is_folder) throw '添加数据结构错误';
|
|
@@ -163,18 +188,6 @@ module.exports = app => {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- async getPosterityData(id){
|
|
|
|
- const result = [];
|
|
|
|
- let cur = await this.getAllDataByCondition({ where: { tree_pid: id } });
|
|
|
|
- let iLevel = 1;
|
|
|
|
- while (cur.length > 0 && iLevel < 6) {
|
|
|
|
- result.push(...cur);
|
|
|
|
- cur = await this.getAllDataByCondition({ where: { tree_pid: cur.map(x => { return x.id })} });
|
|
|
|
- iLevel += 1;
|
|
|
|
- }
|
|
|
|
- return result;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
async dragTo(data) {
|
|
async dragTo(data) {
|
|
const dragNode = await this.getDataById(data.drag_id);
|
|
const dragNode = await this.getDataById(data.drag_id);
|
|
const dropNode = await this.getDataById(data.drop_id);
|
|
const dropNode = await this.getDataById(data.drop_id);
|
|
@@ -206,6 +219,61 @@ module.exports = app => {
|
|
return await this.getSubProject(this.ctx.session.sessionProject.id, this.ctx.session.sessionUser.accountId, this.ctx.session.sessionUser.is_admin);
|
|
return await this.getSubProject(this.ctx.session.sessionProject.id, this.ctx.session.sessionUser.accountId, this.ctx.session.sessionUser.is_admin);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ async _siblingMove(node, step) {
|
|
|
|
+ const stepNode = await this.getStepNode(node, step);
|
|
|
|
+
|
|
|
|
+ const conn = await this.db.beginTransaction();
|
|
|
|
+ try {
|
|
|
|
+ const updateData = [];
|
|
|
|
+ updateData.push({ id: node.id, tree_order: node.tree_order + step });
|
|
|
|
+ for (const sn of stepNode) {
|
|
|
|
+ updateData.push({ id: node.id, tree_order: step > 0 ? sn.tree_order - 1 : sn.tree_order + 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);
|
|
|
|
+
|
|
|
|
+ const conn = await this.db.beginTransaction();
|
|
|
|
+ try {
|
|
|
|
+ const updateData = { id: node.id, tree_pid: -1, tree_level: 1, tree_order: lastChild ? lastChild.tree_order + 1 : 1 };
|
|
|
|
+ await conn.update(this.tableName, updateData);
|
|
|
|
+ if (node.tree_level !== 1 && posterity.length > 0) {
|
|
|
|
+ const posterityUpdateData = posterity.map(x => {
|
|
|
|
+ return { id: x.id, tree_level: x.tree_level - node.tree_level + 1 }
|
|
|
|
+ });
|
|
|
|
+ await conn.updateRows(this.tableName, posterityUpdateData);
|
|
|
|
+ }
|
|
|
|
+ // 升级原来的后项的order
|
|
|
|
+ await conn.query(`UPDATE ${this.tableName} SET tree_order = tree_order-1 WHERE tree_pid = ? AND tree_order > ?`, [node.tree_pid, node.tree_order]);
|
|
|
|
+ await conn.commit();
|
|
|
|
+ } catch (error) {
|
|
|
|
+ await conn.rollback();
|
|
|
|
+ throw error;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ async move(data) {
|
|
|
|
+ const node = await this.getDataById(data.id);
|
|
|
|
+ if (!node) throw '移动数据结构错误';
|
|
|
|
+
|
|
|
|
+ switch(data.type) {
|
|
|
|
+ case 'up': await this._siblingMove(node, -1); break;
|
|
|
|
+ case 'down': await this._siblingMove(node, 1); break;
|
|
|
|
+ case 'top': await this._topMove(node); break;
|
|
|
|
+ default: throw '未知移动类型';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return await this.getSubProject(this.ctx.session.sessionProject.id, this.ctx.session.sessionUser.accountId, this.ctx.session.sessionUser.is_admin);
|
|
|
|
+ }
|
|
|
|
+
|
|
async del(id) {
|
|
async del(id) {
|
|
const node = await this.getDataById(id);
|
|
const node = await this.getDataById(id);
|
|
if (!node) throw '删除的数据不存在';
|
|
if (!node) throw '删除的数据不存在';
|