|
@@ -1,10 +1,7 @@
|
|
|
'use strict';
|
|
|
|
|
|
/**
|
|
|
- * 提供基础操作:
|
|
|
- * 1. 增删改查
|
|
|
- * 2. 粘贴整块
|
|
|
- * 3. 简易导入
|
|
|
+ * 提供基础操作:增删改查
|
|
|
*
|
|
|
* @author Mai
|
|
|
* @date
|
|
@@ -14,6 +11,7 @@
|
|
|
const Service = require('./base_service');
|
|
|
// sql拼装器
|
|
|
const SqlBuilder = require('../lib/sql_builder');
|
|
|
+const rootId = -1;
|
|
|
|
|
|
class TreeService extends Service {
|
|
|
/**
|
|
@@ -53,7 +51,7 @@ class TreeService extends Service {
|
|
|
if (condition.mid) result[this.setting.mid] = condition.mid;
|
|
|
if (condition.kid) result[this.setting.kid] = condition.kid;
|
|
|
if (condition.pid) result[this.setting.pid] = condition.pid;
|
|
|
- if (condition[this.setting.order]) result[this.setting.order] = condition[this.setting.order];
|
|
|
+ if (condition.order) result[this.setting.order] = condition.order;
|
|
|
if (condition.level) result[this.setting.level] = condition.level;
|
|
|
if (condition.fullPath) result[this.setting.fullPath] = condition.fullPath;
|
|
|
if (condition.isLeaf) result[this.setting.isLeaf] = condition.isLeaf;
|
|
@@ -65,10 +63,24 @@ class TreeService extends Service {
|
|
|
* @param {Number} mid - masterId
|
|
|
* @returns {Promise<void>}
|
|
|
*/
|
|
|
- async getData(mid) {
|
|
|
- return await this.db.select(this.tableName, {
|
|
|
- where: this.getCondition({mid: mid})
|
|
|
- });
|
|
|
+ async getData(mid, level) {
|
|
|
+ if (level) {
|
|
|
+ this.initSqlBuilder();
|
|
|
+ this.sqlBuilder.setAndWhere('list_id', {
|
|
|
+ operate: '=',
|
|
|
+ value: mid,
|
|
|
+ });
|
|
|
+ this.sqlBuilder.setAndWhere('level', {
|
|
|
+ operate: '<=',
|
|
|
+ value: level,
|
|
|
+ });
|
|
|
+ const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
|
|
|
+ return await this.db.query(sql, sqlParam);
|
|
|
+ } else {
|
|
|
+ return await this.db.select(this.tableName, {
|
|
|
+ where: this.getCondition({mid: mid})
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -77,7 +89,7 @@ class TreeService extends Service {
|
|
|
* @param {Number} id
|
|
|
* @returns {Promise<void>}
|
|
|
*/
|
|
|
- async getDataByLid (mid, kid) {
|
|
|
+ async getDataByKid (mid, kid) {
|
|
|
return await this.db.get(this.tableName, this.getCondition({
|
|
|
mid: mid, kid: kid,
|
|
|
}));
|
|
@@ -163,6 +175,52 @@ class TreeService extends Service {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 根据full_path获取数据 full_path Like ‘1.2.3%’(传参full_path = '1.2.3%')
|
|
|
+ * @param {Number} tenderId - 标段id
|
|
|
+ * @param {String} full_path - 路径
|
|
|
+ * @return {Promise<void>}
|
|
|
+ */
|
|
|
+ async getDataByFullPath(mid, full_path) {
|
|
|
+ this.initSqlBuilder();
|
|
|
+ this.sqlBuilder.setAndWhere(this.setting.mid, {
|
|
|
+ value: mid,
|
|
|
+ operate: '=',
|
|
|
+ });
|
|
|
+ this.sqlBuilder.setAndWhere(this.setting.fullPath, {
|
|
|
+ value: this.db.escape(full_path),
|
|
|
+ operate: 'Like',
|
|
|
+ });
|
|
|
+ const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
|
|
|
+ const resultData = await this.db.query(sql, sqlParam);
|
|
|
+ return resultData;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 根据full_path检索自己及所有父项
|
|
|
+ * @param {Number} tenderId - 标段id
|
|
|
+ * @param {Array|String} fullPath - 节点完整路径
|
|
|
+ * @return {Promise<*>}
|
|
|
+ * @private
|
|
|
+ */
|
|
|
+ async getFullLevelDataByFullPath(mid, fullPath) {
|
|
|
+ const explodePath = this.ctx.helper.explodePath(fullPath);
|
|
|
+
|
|
|
+ this.initSqlBuilder();
|
|
|
+ this.sqlBuilder.setAndWhere(this.setting.mid, {
|
|
|
+ value: mid,
|
|
|
+ operate: '=',
|
|
|
+ });
|
|
|
+ this.sqlBuilder.setAndWhere(this.setting.fullPath, {
|
|
|
+ value: explodePath,
|
|
|
+ operate: 'in',
|
|
|
+ });
|
|
|
+
|
|
|
+ const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
|
|
|
+ const data = await this.db.query(sql, sqlParam);
|
|
|
+
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 获取最大节点id
|
|
|
*
|
|
|
* @param {Number} mid - master id
|
|
@@ -174,7 +232,7 @@ class TreeService extends Service {
|
|
|
let maxId = parseInt(await this.cache.get(cacheKey));
|
|
|
if (!maxId) {
|
|
|
const sql = 'SELECT Max(??) As max_id FROM ?? Where ' + this.setting.mid + ' = ?';
|
|
|
- const sqlParam = ['ledger_id', this.tableName, 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);
|
|
@@ -230,7 +288,7 @@ class TreeService extends Service {
|
|
|
/**
|
|
|
* 新增数据(新增为selectData的后项,该方法不可单独使用)
|
|
|
*
|
|
|
- * @param {Number} mid - master-id
|
|
|
+ * @param {Number} mid - 台账id
|
|
|
* @param {Object} select - 选中节点的数据
|
|
|
* @param {Object} data - 新增节点的初始数据
|
|
|
* @return {Object} - 新增结果
|
|
@@ -242,13 +300,13 @@ class TreeService extends Service {
|
|
|
}
|
|
|
const maxId = await this._getMaxLid(mid);
|
|
|
|
|
|
- data.id = this.uuid.v4();
|
|
|
+ if (this.setting.uuid) data.id = this.uuid.v4();
|
|
|
data[this.setting.kid] = maxId + 1;
|
|
|
- data[this.setting.pid] = select[this.setting.pid];
|
|
|
+ data[this.setting.pid] = select ? select[this.setting.pid] : rootId;
|
|
|
data[this.setting.mid] = mid;
|
|
|
- data[this.setting.level] = select[this.setting.level];
|
|
|
- data[this.setting.order] = select[this.setting.order] + 1;
|
|
|
- data[this.setting.fullPath] = select[this.setting.fullPath].replace('.' + select[this.setting.kid], '.' + data[this.setting.kid]);
|
|
|
+ data[this.setting.level] = select ? select[this.setting.level] : 1;
|
|
|
+ data[this.setting.order] = select ? select[this.setting.order] + 1 : 1;
|
|
|
+ data[this.setting.fullPath] = data[this.setting.level] > 1 ? select[this.setting.fullPath].replace('.' + select[this.setting.kid], '.' + data[this.setting.kid]) : data[this.setting.kid] + '';
|
|
|
data[this.setting.isLeaf] = true;
|
|
|
const result = await this.transaction.insert(this.tableName, data);
|
|
|
|
|
@@ -259,24 +317,20 @@ class TreeService extends Service {
|
|
|
|
|
|
/**
|
|
|
* 新增节点
|
|
|
- * @param {Number} mid - master id
|
|
|
+ * @param {Number} mid - 台账id
|
|
|
* @param {Number} kid - 清单节点id
|
|
|
* @returns {Promise<void>}
|
|
|
*/
|
|
|
async addNode(mid, kid, data) {
|
|
|
- if (!mid || !kid) return null;
|
|
|
- const select = await this.getDataByLid(mid, kid);
|
|
|
- if (!select) {
|
|
|
- throw '新增节点数据错误';
|
|
|
- }
|
|
|
+ if (!mid) return null;
|
|
|
+ const select = kid ? await this.getDataByKid(mid, kid) : null;
|
|
|
+ if (kid && !select) throw '新增节点数据错误';
|
|
|
|
|
|
this.transaction = await this.db.beginTransaction();
|
|
|
try {
|
|
|
- await this._updateChildrenOrder(mid, select[this.setting.pid], select[this.setting.order]+1);
|
|
|
+ if (select) await this._updateChildrenOrder(mid, select[this.setting.pid], select[this.setting.order]+1);
|
|
|
const newNode = await this._addNodeData(mid, select, data);
|
|
|
- if (newNode.affectedRows !== 1) {
|
|
|
- throw '新增节点数据额错误';
|
|
|
- }
|
|
|
+ if (newNode.affectedRows !== 1) throw '新增节点数据额错误';
|
|
|
await this.transaction.commit();
|
|
|
this.transaction = null;
|
|
|
} catch (err) {
|
|
@@ -285,15 +339,20 @@ class TreeService extends Service {
|
|
|
throw err;
|
|
|
}
|
|
|
|
|
|
- const createData = await this.getDataByParentAndOrder(mid, select[this.setting.pid], [select[this.setting.order] + 1]);
|
|
|
- const updateData = await this.getNextsData(mid, select[this.setting.pid], select[this.setting.order] + 1);
|
|
|
- return {create: createData, update: updateData};
|
|
|
+ if (select) {
|
|
|
+ const createData = await this.getDataByParentAndOrder(mid, select[this.setting.pid], [select[this.setting.order] + 1]);
|
|
|
+ const updateData = await this.getNextsData(mid, select[this.setting.pid], select[this.setting.order] + 1);
|
|
|
+ return {create: createData, update: updateData};
|
|
|
+ } else {
|
|
|
+ const createData = await this.getDataByParentAndOrder(mid, -1, [1]);
|
|
|
+ return {create: createData};
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除节点
|
|
|
- * @param {Number} mid - master id
|
|
|
- * @param {Object} deleteNode - 删除节点数据
|
|
|
+ * @param {Number} tenderId - 标段id
|
|
|
+ * @param {Object} deleteData - 删除节点数据
|
|
|
* @return {Promise<*>}
|
|
|
* @private
|
|
|
*/
|
|
@@ -314,17 +373,17 @@ class TreeService extends Service {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 删除选中节点及其子节点
|
|
|
+ * tenderId标段中, 删除选中节点及其子节点
|
|
|
*
|
|
|
- * @param {Number} mid - master id
|
|
|
- * @param {Number} kid - 选中节点id
|
|
|
+ * @param {Number} tenderId - 标段id
|
|
|
+ * @param {Number} selectId - 选中节点id
|
|
|
* @return {Array} - 被删除的数据
|
|
|
*/
|
|
|
async deleteNode(mid, kid) {
|
|
|
if ((mid <= 0) || (kid <= 0)) return [];
|
|
|
- const select = await this.getDataByNodeId(mid, kid);
|
|
|
+ const select = await this.getDataByKid(mid, kid);
|
|
|
if (!select) throw '删除节点数据错误';
|
|
|
- const parent = await this.getDataByNodeId(mid, select[this.setting.pid]);
|
|
|
+ const parent = await this.getDataByKid(mid, select[this.setting.pid]);
|
|
|
// 获取将要被删除的数据
|
|
|
const deleteData = await this.getDataByFullPath(mid, select[this.setting.fullPath] + '%');
|
|
|
if (deleteData.length === 0) throw '删除节点数据错误';
|
|
@@ -343,18 +402,21 @@ class TreeService extends Service {
|
|
|
}
|
|
|
}
|
|
|
// 选中节点--全部后节点 order--
|
|
|
- await this._updateSelectNextsOrder(select, -1);
|
|
|
+ await this._updateChildrenOrder(mid, select[this.setting.pid], select[this.setting.order] + 1, -1);
|
|
|
+ //await this._updateSelectNextsOrder(select, -1);
|
|
|
// 删除部位明细
|
|
|
//await this.ctx.service.pos.deletePosData(this.transaction, tenderId, this._.map(deleteData, 'id'));
|
|
|
await this.transaction.commit();
|
|
|
+ this.transaction = null;
|
|
|
} catch (err) {
|
|
|
await this.transaction.rollback();
|
|
|
+ this.transaction = null;
|
|
|
throw err;
|
|
|
}
|
|
|
// 查询结果
|
|
|
const updateData = await this.getNextsData(mid, select[this.setting.pid], select[this.setting.order] - 1);
|
|
|
if (parent) {
|
|
|
- const updateData1 = await this.getDataByNodeId(mid, select[this.setting.pid]);
|
|
|
+ const updateData1 = await this.getDataByKid(mid, select[this.setting.pid]);
|
|
|
if (updateData1[this.setting.isLeaf]) {
|
|
|
updateData.push(updateData1);
|
|
|
}
|
|
@@ -371,7 +433,7 @@ class TreeService extends Service {
|
|
|
*/
|
|
|
async upMoveNode(mid, kid) {
|
|
|
if (!mid || !kid) return null;
|
|
|
- const select = await this.getDataByLid(mid, kid);
|
|
|
+ const select = await this.getDataByKid(mid, kid);
|
|
|
if (!select) {
|
|
|
throw '上移节点数据错误';
|
|
|
}
|
|
@@ -385,8 +447,10 @@ class TreeService extends Service {
|
|
|
const sData = await this.transaction.update(this.tableName, { id: select.id, order: select[this.setting.order] - 1 });
|
|
|
const pData = await this.transaction.update(this.tableName, { id: pre.id, order: pre[this.setting.order] + 1 });
|
|
|
await this.transaction.commit();
|
|
|
+ this.transaction = null;
|
|
|
} catch (err) {
|
|
|
await this.transaction.rollback();
|
|
|
+ this.transaction = null;
|
|
|
throw err;
|
|
|
}
|
|
|
|
|
@@ -403,7 +467,7 @@ class TreeService extends Service {
|
|
|
*/
|
|
|
async downMoveNode(mid, kid) {
|
|
|
if (!mid || !kid) return null;
|
|
|
- const select = await this.getDataByLid(mid, kid);
|
|
|
+ const select = await this.getDataByKid(mid, kid);
|
|
|
if (!select) {
|
|
|
throw '下移节点数据错误';
|
|
|
}
|
|
@@ -417,8 +481,10 @@ class TreeService extends Service {
|
|
|
const sData = await this.transaction.update(this.tableName, { id: select.id, order: select[this.setting.order] + 1 });
|
|
|
const pData = await this.transaction.update(this.tableName, { id: next.id, order: next[this.setting.order] - 1 });
|
|
|
await this.transaction.commit();
|
|
|
+ this.transaction = null;
|
|
|
} catch (err) {
|
|
|
await this.transaction.rollback();
|
|
|
+ this.transaction = null;
|
|
|
throw err;
|
|
|
}
|
|
|
|
|
@@ -435,11 +501,11 @@ class TreeService extends Service {
|
|
|
async _syncUplevelChildren(select) {
|
|
|
this.initSqlBuilder();
|
|
|
this.sqlBuilder.setAndWhere(this.setting.mid, {
|
|
|
- value: selectData.tender_id,
|
|
|
+ value: select[this.setting.mid],
|
|
|
operate: '=',
|
|
|
});
|
|
|
this.sqlBuilder.setAndWhere(this.setting.fullPath, {
|
|
|
- value: this.db.escape(select[this.setting.fullPath] + '.%'),
|
|
|
+ value: this.db.escape(select[this.setting.fullPath] + '-%'),
|
|
|
operate: 'like',
|
|
|
});
|
|
|
this.sqlBuilder.setUpdateData(this.setting.level, {
|
|
@@ -521,16 +587,16 @@ class TreeService extends Service {
|
|
|
/**
|
|
|
* 升级节点
|
|
|
*
|
|
|
- * @param {Number} mid - master id
|
|
|
- * @param {Number} kid - 选中节点id
|
|
|
+ * @param {Number} tenderId - 标段id
|
|
|
+ * @param {Number} selectId - 选中节点id
|
|
|
* @return {Array} - 发生改变的数据
|
|
|
*/
|
|
|
async upLevelNode(mid, kid) {
|
|
|
if ((mid <= 0) || (kid <= 0)) return [];
|
|
|
|
|
|
- const select = await this.getDataByNodeId(mid, kid);
|
|
|
+ const select = await this.getDataByKid(mid, kid);
|
|
|
if (!select) throw '升级节点数据错误';
|
|
|
- const parent = await this.getDataByNodeId(mid, select[this.setting.pid]);
|
|
|
+ const parent = await this.getDataByKid(mid, select[this.setting.pid]);
|
|
|
if (!parent) throw '升级节点数据错误';
|
|
|
|
|
|
this.transaction = await this.db.beginTransaction();
|
|
@@ -544,12 +610,13 @@ class TreeService extends Service {
|
|
|
});
|
|
|
}
|
|
|
// 选中节点--父节点--全部后兄弟节点 order+1
|
|
|
- await this._updateSelectNextsOrder(parent);
|
|
|
+ await this._updateChildrenOrder(mid, parent[this.setting.pid], parent[this.setting.order] + 1);
|
|
|
+ //await this._updateSelectNextsOrder(parent);
|
|
|
// 选中节点 修改pid, order, full_path, level, is_leaf, 清空计算项
|
|
|
const updateData = { id: select.id };
|
|
|
updateData[this.setting.pid] = parent[this.setting.pid];
|
|
|
updateData[this.setting.order] = parent[this.setting.order] + 1;
|
|
|
- updateData[this.setting.level] = select[this.setting.level] + 1;
|
|
|
+ updateData[this.setting.level] = select[this.setting.level] - 1;
|
|
|
updateData[this.setting.fullPath] = newFullPath;
|
|
|
|
|
|
const nexts = await this.getNextsData(mid, parent[this.setting.kid], select[this.setting.order]);
|
|
@@ -567,14 +634,16 @@ class TreeService extends Service {
|
|
|
// 选中节点--全部后兄弟节点 收编为子节点 修改pid, order, full_path
|
|
|
await this._syncUpLevelNexts(select);
|
|
|
await this.transaction.commit();
|
|
|
+ this.transaction = null;
|
|
|
} catch (err) {
|
|
|
await this.transaction.rollback();
|
|
|
+ this.transaction = null;
|
|
|
throw err;
|
|
|
}
|
|
|
|
|
|
// 查询修改的数据
|
|
|
const resultData1 = await this.getDataByFullPath(mid, newFullPath + '%');
|
|
|
- const resultData2 = await this.getNextsData(mid, parent[this.setting.pid], parent[this.setting.order] + 1);
|
|
|
+ const resultData2 = await this.getNextsData(mid, parent[this.setting.pid], parent[this.setting.order] - 1);
|
|
|
return { update: resultData1.concat(resultData2) };
|
|
|
}
|
|
|
|
|
@@ -592,7 +661,7 @@ class TreeService extends Service {
|
|
|
operate: '=',
|
|
|
});
|
|
|
this.sqlBuilder.setAndWhere(this.setting.fullPath, {
|
|
|
- value: this.db.escape(select[this.setting.fullPath] + '.%'),
|
|
|
+ value: this.db.escape(select[this.setting.fullPath] + '-%'),
|
|
|
operate: 'like',
|
|
|
});
|
|
|
this.sqlBuilder.setUpdateData(this.setting.level, {
|
|
@@ -600,7 +669,7 @@ class TreeService extends Service {
|
|
|
selfOperate: '+',
|
|
|
});
|
|
|
this.sqlBuilder.setUpdateData(this.setting.fullPath, {
|
|
|
- value: [this.setting.fullPath, this.db.escape('.' + select[this.setting.kid]), this.db.escape('.' + pre[this.setting.kid] + '.' + select[this.setting.kid])],
|
|
|
+ value: [this.setting.fullPath, this.db.escape(select[this.setting.kid] + '.'), this.db.escape(pre[this.setting.kid] + '.' + select[this.setting.kid] + '.')],
|
|
|
literal: 'Replace',
|
|
|
});
|
|
|
const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
|
|
@@ -612,13 +681,13 @@ class TreeService extends Service {
|
|
|
/**
|
|
|
* 降级节点
|
|
|
*
|
|
|
- * @param {Number} mid - master id
|
|
|
- * @param {Number} kid - 选中节点id
|
|
|
+ * @param {Number} tenderId - 标段id
|
|
|
+ * @param {Number} selectId - 选中节点id
|
|
|
* @return {Array} - 发生改变的数据
|
|
|
*/
|
|
|
async downLevelNode(mid, kid) {
|
|
|
if ((mid <= 0) || (kid <= 0)) return [];
|
|
|
- const select = await this.getDataByNodeId(mid, kid);
|
|
|
+ const select = await this.getDataByKid(mid, kid);
|
|
|
if (!select) throw '降级节点数据错误';
|
|
|
const pre = await this.getDataByParentAndOrder(mid, select[this.setting.pid], select[this.setting.order] - 1);
|
|
|
if (!pre) throw '节点不可降级';
|
|
@@ -630,10 +699,10 @@ class TreeService extends Service {
|
|
|
const newFullPath = select.full_path.replace(orgLastPath, newLastPath);
|
|
|
try {
|
|
|
// 选中节点--全部后节点 order--
|
|
|
- await this._updateSelectNextsOrder(select, -1);
|
|
|
+ await this._updateChildrenOrder(mid, select[this.setting.pid], select[this.setting.order] + 1, -1);
|
|
|
// 选中节点 修改pid, level, order, full_path
|
|
|
const updateData = { id: select.id };
|
|
|
- updateData[this.setting.kid] = pre[this.setting.kid];
|
|
|
+ updateData[this.setting.pid] = pre[this.setting.kid];
|
|
|
updateData[this.setting.order] = preLastChild ? preLastChild[this.setting.order] + 1 : 1;
|
|
|
updateData[this.setting.level] = select[this.setting.level] + 1;
|
|
|
updateData[this.setting.fullPath] = newFullPath;
|
|
@@ -650,8 +719,10 @@ class TreeService extends Service {
|
|
|
// updateData2.deal_tp = null;
|
|
|
await this.transaction.update(this.tableName, updateData2);
|
|
|
await this.transaction.commit();
|
|
|
+ this.transaction = null;
|
|
|
} catch (err) {
|
|
|
await this.transaction.rollback();
|
|
|
+ this.transaction = null;
|
|
|
throw err;
|
|
|
}
|
|
|
|
|
@@ -659,7 +730,7 @@ class TreeService extends Service {
|
|
|
// 选中节点及子节点
|
|
|
const resultData1 = await this.getDataByFullPath(mid, newFullPath + '%');
|
|
|
// 选中节点--原前兄弟节点&全部后兄弟节点
|
|
|
- const resultData2 = await this.getNextsData(mid, pre[this.setting.pid], pre[this.setting.order]);
|
|
|
+ const resultData2 = await this.getNextsData(mid, pre[this.setting.pid], pre[this.setting.order] - 1);
|
|
|
return { update: resultData1.concat(resultData2) };
|
|
|
}
|
|
|
|
|
@@ -682,34 +753,33 @@ class TreeService extends Service {
|
|
|
|
|
|
/**
|
|
|
* 提交多条数据 - 不影响计算等未提交项
|
|
|
- * @param {Number} mid - master id
|
|
|
+ * @param {Number} tenderId - 标段id
|
|
|
* @param {Array} datas - 提交数据
|
|
|
* @return {Array} - 提交后的数据
|
|
|
*/
|
|
|
async updateInfos(mid, datas) {
|
|
|
if (mid <= 0) throw '数据错误';
|
|
|
- for (const data of datas) {
|
|
|
- if (mid !== data[this.setting.mid]) throw '提交数据错误';
|
|
|
- }
|
|
|
|
|
|
- this.transaction = await this.db.beginTransaction();
|
|
|
- try {
|
|
|
- for (const data of datas) {
|
|
|
- const updateNode = await this.getDataById(data.id);
|
|
|
- if (!updateNode || mid !== updateNode[this.setting.mid] || data.kid !== updateNode[this.setting.kid]) {
|
|
|
- throw '提交数据错误';
|
|
|
- }
|
|
|
- const updateData = this._filterUpdateInvalidField(updateNode.id, data);
|
|
|
- await this.transaction.update(this.tableName, updateData);
|
|
|
+ if (Array.isArray(datas)) {
|
|
|
+ const updateDatas = [];
|
|
|
+ for (const d of datas) {
|
|
|
+ if (mid !== d[this.setting.mid]) throw '提交数据错误';
|
|
|
+ const node = await this.getDataById(d.id);
|
|
|
+ if (!node || mid !== node[this.setting.mid] || d[this.setting.kid] !== node[this.setting.kid]) throw '提交数据错误';
|
|
|
+ updateDatas.push(this._filterUpdateInvalidField(node.id, d));
|
|
|
}
|
|
|
- await this.transaction.commit();
|
|
|
- } catch (err) {
|
|
|
- await this.transaction.rollback();
|
|
|
- throw err;
|
|
|
- }
|
|
|
+ await this.db.updateRows(this.tableName, updateDatas);
|
|
|
|
|
|
- const resultData = await this.getDataById(this._.map(datas, 'id'));
|
|
|
- return resultData;
|
|
|
+ const resultData = await this.getDataById(this._.map(datas, 'id'));
|
|
|
+ return resultData;
|
|
|
+ } else {
|
|
|
+ if (mid !== datas[this.setting.mid]) throw '提交数据错误';
|
|
|
+ const node = await this.getDataById(datas.id);
|
|
|
+ if (!node || mid !== node[this.setting.mid] || datas[this.setting.kid] !== node[this.setting.kid]) throw '提交数据错误';
|
|
|
+ const updateData = this._filterUpdateInvalidField(node.id, datas);
|
|
|
+ await this.db.update(this.tableName, updateData);
|
|
|
+ return await this.getDataById(datas.id);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|