|
@@ -53,6 +53,7 @@ module.exports = app => {
|
|
|
insertData.push(tmp);
|
|
|
}
|
|
|
const operate = await this.transaction.insert(this.tableName, insertData);
|
|
|
+ this.transaction.commit();
|
|
|
result = operate.affectedRows > 0;
|
|
|
} else {
|
|
|
// 对象则单个插入
|
|
@@ -156,6 +157,19 @@ module.exports = app => {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 从数据库获取标段的最大节点id
|
|
|
+ * @param {Number} tenderId - 标段id
|
|
|
+ * @return {Number}
|
|
|
+ * @private
|
|
|
+ */
|
|
|
+ async _getMaxNodeId(tenderId) {
|
|
|
+ const sql = 'SELECT Max(??) As max_id FROM ?? Where tender_id = ' + tenderId;
|
|
|
+ const sqlParam = ['template_id', this.tableName];
|
|
|
+ const queryResult = await this.db.queryOne(sql, sqlParam);
|
|
|
+ return queryResult.max_id;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 根据selectData, data 新增数据
|
|
|
* @param {Number} tenderId - 标段id
|
|
|
* @param {Object} selectData - 选中节点的数据
|
|
@@ -170,14 +184,23 @@ module.exports = app => {
|
|
|
if (!data) {
|
|
|
data = {};
|
|
|
}
|
|
|
+ const cacheKey = 'tender_node_maxId:' + tenderId;
|
|
|
+ let maxId = parseInt(await this.cache.get(cacheKey));
|
|
|
+ if (!maxId) {
|
|
|
+ maxId = await this._getMaxNodeId(tenderId);
|
|
|
+ this.cache.set(cacheKey, maxId, 'EX', this.ctx.app.config.cacheTime);
|
|
|
+ }
|
|
|
+
|
|
|
data.tender_id = tenderId;
|
|
|
- data.template_id = 15;
|
|
|
+ data.template_id = maxId + 1;
|
|
|
data.template_pid = selectData.template_pid;
|
|
|
data.level = selectData.level;
|
|
|
data.order = selectData.order + 1;
|
|
|
data.full_path = selectData.full_path.replace(selectData.template_id, data.template_id);
|
|
|
const result = await this.transaction.insert(this.tableName, data);
|
|
|
|
|
|
+ this.cache.set(cacheKey, maxId + 1, 'EX', this.ctx.app.config.cacheTime);
|
|
|
+
|
|
|
return result;
|
|
|
}
|
|
|
|
|
@@ -190,32 +213,44 @@ module.exports = app => {
|
|
|
*/
|
|
|
async addNode(tenderId, selectId, data) {
|
|
|
if ((tenderId <= 0) || (selectId <= 0)) {
|
|
|
- return undefined;
|
|
|
+ return [];
|
|
|
}
|
|
|
const selectData = await this.getDataByNodeId(tenderId, selectId);
|
|
|
this.transaction = await this.db.beginTransaction();
|
|
|
|
|
|
- let resultData = [];
|
|
|
if (selectData) {
|
|
|
try {
|
|
|
// 选中节点的所有后兄弟节点,order+1
|
|
|
- const updateData = await this._updateSelectNextsOrder(selectData);
|
|
|
- console.log(updateData);
|
|
|
+ await this._updateSelectNextsOrder(selectData);
|
|
|
// 数据库创建新增节点数据
|
|
|
- const addData = await this._addNodeData(tenderId, selectData, data);
|
|
|
- console.log(addData);
|
|
|
- resultData = await this.transaction.commit();
|
|
|
- console.log(resultData);
|
|
|
-
|
|
|
- resultData.concat(updateData, addData);
|
|
|
+ await this._addNodeData(tenderId, selectData, data);
|
|
|
+ await this.transaction.commit();
|
|
|
} catch (err) {
|
|
|
await this.transaction.rollback();
|
|
|
throw err;
|
|
|
}
|
|
|
- } else {
|
|
|
- throw '新增节点数据错误';
|
|
|
+
|
|
|
+ // 查询应返回的结果
|
|
|
+ this.initSqlBuilder();
|
|
|
+ this.sqlBuilder.setAndWhere('tender_id', {
|
|
|
+ value: selectData.tender_id,
|
|
|
+ operate: '=',
|
|
|
+ });
|
|
|
+ this.sqlBuilder.setAndWhere('template_pid', {
|
|
|
+ value: selectData.template_pid,
|
|
|
+ operate: '=',
|
|
|
+ });
|
|
|
+ this.sqlBuilder.setAndWhere('order', {
|
|
|
+ value: selectData.order,
|
|
|
+ operate: '>',
|
|
|
+ });
|
|
|
+ const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
|
|
|
+ const resultData = this.db.query(sql, sqlParam);
|
|
|
+
|
|
|
+ return resultData;
|
|
|
}
|
|
|
- return resultData;
|
|
|
+ throw '新增节点数据错误';
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
|