|
@@ -14,6 +14,7 @@ const defaultData = [
|
|
|
{ code: '2', name: '实施阶段', tree_id: 2, tree_pid: -1, tree_level: 1, tree_order: 2, tree_full_path: '2', tree_is_leaf: 1 },
|
|
|
{ code: '3', name: '竣(交)工阶段', tree_id: 3, tree_pid: -1, tree_level: 1, tree_order: 3, tree_full_path: '3', tree_is_leaf: 1 },
|
|
|
];
|
|
|
+const billsUtils = require('../lib/bills_utils');
|
|
|
|
|
|
module.exports = app => {
|
|
|
class SubProjProgress extends app.BaseTreeService {
|
|
@@ -91,6 +92,125 @@ module.exports = app => {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ async addStdNode(subProj, targetId, stdData) {
|
|
|
+ const findPreData = function(list, a) {
|
|
|
+ if (!list || list.length === 0) { return null; }
|
|
|
+ for (let i = 0, iLen = list.length; i < iLen; i++) {
|
|
|
+ if (billsUtils.compareCode(list[i].code, a.code) > 0) {
|
|
|
+ return i > 0 ? list[i - 1] : null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return list[list.length - 1];
|
|
|
+ };
|
|
|
+ let parent = await this.getDataByKid(subProj.id, targetId);
|
|
|
+ if (targetId && !parent) throw '新增节点数据错误,请刷新页面重试';
|
|
|
+ const orgParentId = parent.id;
|
|
|
+ let children = await this.getChildrenByParentId(subProj.id, targetId);
|
|
|
+ const updateParent = children.length === 0;
|
|
|
+
|
|
|
+ const insertData = [];
|
|
|
+ const maxId = await this._getMaxLid(subProj.id);
|
|
|
+ this.transaction = await this.db.beginTransaction();
|
|
|
+ try {
|
|
|
+ if (updateParent) {
|
|
|
+ const updateData = { id: parent.id };
|
|
|
+ updateData[this.setting.isLeaf] = false;
|
|
|
+ this.clearParentingData(updateData);
|
|
|
+ await this.transaction.update(this.tableName, updateData);
|
|
|
+ }
|
|
|
+ // 从最顶层节点依次查询是否存在,否则添加
|
|
|
+ const addStdData = stdData[stdData.length - 1];
|
|
|
+ const newData = { code: addStdData.code, name: addStdData.name };
|
|
|
+ newData[this.setting.kid] = maxId + 1;
|
|
|
+ newData[this.setting.pid] = parent ? parent[this.setting.kid] : this.rootId;
|
|
|
+ newData[this.setting.level] = parent ? parent[this.setting.level] + 1 : 1;
|
|
|
+ newData[this.setting.fullPath] = parent ? `${parent[this.setting.fullPath]}-${newData[this.setting.kid]}` : `${newData[this.setting.kid]}`;
|
|
|
+ const pre = findPreData(children, newData);
|
|
|
+ newData[this.setting.order] = pre ? pre[this.setting.order] + 1 : 1;
|
|
|
+ if (!pre || children.indexOf(pre) < children.length - 1) {
|
|
|
+ await this._updateChildrenOrder(subProj.id, parent ? parent[this.setting.kid] : this.rootId, pre ? pre[this.setting.order] + 1 : 1);
|
|
|
+ }
|
|
|
+ newData[this.setting.isLeaf] = 1;
|
|
|
+ this._getDefaultData(newData, subProj.id);
|
|
|
+ insertData.push(newData);
|
|
|
+ await this.transaction.insert(this.tableName, insertData);
|
|
|
+ await this.transaction.commit();
|
|
|
+ } catch (err) {
|
|
|
+ await this.transaction.rollback();
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
+ this._cacheMaxLid(subProj.id, maxId + stdData.length);
|
|
|
+
|
|
|
+ // 查询应返回的结果
|
|
|
+ const createData = await this.getDataByFullPath(subProj.id, insertData[0][this.setting.fullPath] + '%');
|
|
|
+ const updateData = await this.getNextsData(subProj.id, targetId, insertData[0][this.setting.order]);
|
|
|
+ if (updateParent) {
|
|
|
+ updateData.push(await this.getDataByKid(subProj.id, targetId));
|
|
|
+ }
|
|
|
+ return { create: createData, update: updateData };
|
|
|
+ }
|
|
|
+
|
|
|
+ async addStdNodeWithParent(subProj, targetId, stdData) {
|
|
|
+ const findPreData = function(list, a) {
|
|
|
+ if (!list || list.length === 0) { return null; }
|
|
|
+ for (let i = 0, iLen = list.length; i < iLen; i++) {
|
|
|
+ if (billsUtils.compareCode(list[i].code, a.code) > 0) {
|
|
|
+ return i > 0 ? list[i - 1] : null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return list[list.length - 1];
|
|
|
+ };
|
|
|
+ let parent = await this.getDataByKid(subProj.id, targetId);
|
|
|
+ if (targetId && !parent) throw '新增节点数据错误,请刷新页面重试';
|
|
|
+ const orgParentId = parent.id;
|
|
|
+ let children = await this.getChildrenByParentId(subProj.id, targetId);
|
|
|
+ const updateParent = children.length === 0;
|
|
|
+
|
|
|
+ const insertData = [];
|
|
|
+ const maxId = await this._getMaxLid(subProj.id);
|
|
|
+ this.transaction = await this.db.beginTransaction();
|
|
|
+ try {
|
|
|
+ if (updateParent) {
|
|
|
+ const updateData = { id: parent.id };
|
|
|
+ updateData[this.setting.isLeaf] = false;
|
|
|
+ this.clearParentingData(updateData);
|
|
|
+ await this.transaction.update(this.tableName, updateData);
|
|
|
+ }
|
|
|
+ // 从最顶层节点依次查询是否存在,否则添加
|
|
|
+ for (let i = 0, len = stdData.length; i < len; i++) {
|
|
|
+ const newData = { code: stdData[i].code, name: stdData[i].name };
|
|
|
+ newData[this.setting.kid] = maxId + i + 1;
|
|
|
+ newData[this.setting.pid] = parent ? parent[this.setting.kid] : this.rootId;
|
|
|
+ newData[this.setting.level] = parent ? parent[this.setting.level] + 1 : 1;
|
|
|
+ newData[this.setting.fullPath] = parent ? `${parent[this.setting.fullPath]}-${newData[this.setting.kid]}` : `${newData[this.setting.kid]}`;
|
|
|
+ const pre = findPreData(children, newData);
|
|
|
+ newData[this.setting.order] = pre ? pre[this.setting.order] + 1 : 1;
|
|
|
+ if (!pre || children.indexOf(pre) < children.length - 1) {
|
|
|
+ await this._updateChildrenOrder(subProj.id, parent ? parent[this.setting.kid] : this.rootId, pre ? pre[this.setting.order] + 1 : 1);
|
|
|
+ }
|
|
|
+ newData[this.setting.isLeaf] = (i === len - 1);
|
|
|
+ this._getDefaultData(newData, subProj.id);
|
|
|
+ insertData.push(newData);
|
|
|
+ parent = newData;
|
|
|
+ children = [];
|
|
|
+ }
|
|
|
+ await this.transaction.insert(this.tableName, insertData);
|
|
|
+ await this.transaction.commit();
|
|
|
+ } catch (err) {
|
|
|
+ await this.transaction.rollback();
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
+ this._cacheMaxLid(subProj.id, maxId + stdData.length);
|
|
|
+
|
|
|
+ // 查询应返回的结果
|
|
|
+ const createData = await this.getDataByFullPath(subProj.id, insertData[0][this.setting.fullPath] + '%');
|
|
|
+ const updateData = await this.getNextsData(subProj.id, targetId, insertData[0][this.setting.order]);
|
|
|
+ if (updateParent) {
|
|
|
+ updateData.push(await this.getDataByKid(subProj.id, targetId));
|
|
|
+ }
|
|
|
+ return { create: createData, update: updateData };
|
|
|
+ }
|
|
|
+
|
|
|
async addChild(spid, select, count) {
|
|
|
const maxId = await this._getMaxLid(spid);
|
|
|
const children = await this.getChildrenByParentId(spid, select[this.setting.kid]);
|