| 
					
				 | 
			
			
				@@ -31,7 +31,7 @@ const keyPre = 'tender_node_maxId:'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    class Ledger extends app.BaseTreeService { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    class Ledger extends app.BaseBillsService { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				          * 构造函数 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -221,34 +221,6 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             return data; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * 根据 父节点id 获取孙子节点 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param tenderId 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param nodeId 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @return {Promise<void>} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        async getPosterityByParentId(tenderId, nodeId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (tenderId <= 0 || !nodeId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                return undefined; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const node = await this.getDataByNodeId(tenderId, nodeId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            this.initSqlBuilder(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            this.sqlBuilder.setAndWhere('tender_id', { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                value: tenderId, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                operate: '=', 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            this.sqlBuilder.setAndWhere('full_path', { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                value: this.db.escape(node.full_path + '-%'), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                operate: 'like', 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const [sql, sqlParam] = this.sqlBuilder.build(this.tableName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const data = await this.db.query(sql, sqlParam); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            return data; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				          * 获取项目工程量 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -290,269 +262,6 @@ 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 = ['ledger_id', this.tableName]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const queryResult = await this.db.queryOne(sql, sqlParam); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            return queryResult.max_id; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * 根据parentData, data新增数据(新增为parentData的最后一个子项) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param {Number} tenderId - 标段id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param {Object} parentData - 父项数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param {Object} data - 新增节点,初始数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @return {Promise<*>} - 新增结果 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @private 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        async _addChildNodeData(tenderId, parentData, data) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (tenderId <= 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                return undefined; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (!data) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                data = {}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const pid = parentData ? parentData.ledger_id : rootId; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const cacheKey = keyPre + tenderId; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let maxId = parseInt(await this.cache.get(cacheKey)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (!maxId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                maxId = await this._getMaxNodeId(tenderId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            this.cache.set(cacheKey, maxId + 1, 'EX', this.ctx.app.config.cacheTime); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            data.id = this.uuid.v4(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            data.tender_id = tenderId; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            data.ledger_id = maxId + 1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            data.ledger_pid = pid; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (data.order === undefined) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                data.order = 1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            data.level = parentData ? parentData.level + 1 : 1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            data.full_path = parentData ? parentData.full_path + '-' + data.ledger_id : '' + data.ledger_id; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (data.is_leaf === undefined) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                data.is_leaf = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const result = await this.transaction.insert(this.tableName, data); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            return [result, data]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * 根据parentData, data新增数据(自动排序) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param tenderId 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param parentData 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param data 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @return {Promise<void>} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @private 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        async _addChildAutoOrder(tenderId, parentData, data) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const self = this; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const findPreData = function(list, a) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                if (!list || list.length === 0) { return null; } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                for (let i = 0, iLen = list.length; i < iLen; i++) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    if (self.ctx.helper.compareCode(list[i].code, a.code) > 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        return i > 0 ? list[i - 1] : null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                return list[list.length - 1]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const pid = parentData ? parentData.ledger_id : rootId; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const children = await this.getChildrenByParentId(tenderId, pid); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const preData = findPreData(children, data); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (!preData || children.indexOf(preData) < children.length - 1) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                await this._updateChildrenOrder(tenderId, pid, preData ? preData.order + 1 : 1); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            data.order = preData ? preData.order + 1 : 1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const [addResult, node] = await this._addChildNodeData(tenderId, parentData, data); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            return [addResult, node]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * 从标准数据中提取有效数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param {Object} stdData - 从标准库中查询所得 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @returns {name, unit, source, code, b_code} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @private 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        _filterStdData(stdData) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const result = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                name: stdData.name, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                unit: stdData.unit, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                source: stdData.source, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                node_type: stdData.node_type, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            result.code = stdData.code ? stdData.code : ''; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            result.b_code = stdData.b_code ? stdData.b_code : ''; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * 添加节点(来自标准清单) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param {Number} tenderId 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param {Number} selectId 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param {Object} stdData 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @return {Promise<*>} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        async addStdNode(tenderId, selectId, stdData) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const newData = this._filterStdData(stdData); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const result = await this.addNode(tenderId, selectId, newData); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * 新增子节点,并排在所有子节点的末尾 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param {Number} tenderId - 标段Id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param {Number} selectId - 父节点Id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param {Object} data - 新增节点数据(编号名称等) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @returns {Promise<*>} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        async addChild(tenderId, selectId, data) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if ((tenderId <= 0) || (selectId <= 0)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                return []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const selectData = await this.getDataByNodeId(tenderId, selectId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (!selectData) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                throw '新增节点数据错误'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const children = await this.getChildrenByParentId(tenderId, selectId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const cacheKey = keyPre + 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.id = this.uuid.v4(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            data.tender_id = tenderId; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            data.ledger_id = maxId + 1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            data.ledger_pid = selectData.ledger_id; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            data.level = selectData.level + 1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            data.order = children.length + 1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            data.full_path = selectData.full_path + '-' + data.ledger_id; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            data.is_leaf = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            this.transaction = await this.db.beginTransaction(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                const result = await this.transaction.insert(this.tableName, data); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                if (children.length === 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    await this.transaction.update(this.tableName, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        {is_leaf: false, quantity: null, unit_price: null, total_price: null, deal_qty: null, deal_tp: null}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        { where: {tender_id: tenderId, ledger_id: selectData.ledger_id} }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                await this.transaction.commit(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } catch(err) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                this.transaction.rollback(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                throw err; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            this.cache.set(cacheKey, maxId + 1, 'EX', this.ctx.app.config.cacheTime); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            // 查询应返回的结果 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const resultData = {}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            resultData.create = await this.getDataByNodeId(tenderId, data.ledger_id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (children.length === 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                resultData.update = await this.getDataByNodeId(tenderId, selectId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            return resultData; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * 添加标准节点,将选择的标准节点,添加为子节点(排序为末尾) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param {Number} tenderId - 标段Id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param {Number} selectId - 添加目标节点Id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param {Object} stdData - 标准节点数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @returns {Promise<*>} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        async addStdNodeAsChild(tenderId, selectId, stdData) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const newData = this._filterStdData(stdData); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const result = await this.addChild(tenderId, selectId, newData); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * 添加节点,并同步添加父节点 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param {Number} tenderId - 标段id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param {Number} selectId - 选中节点id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param {Object} stdData - 节点数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param {StandardLib} stdLib - 标准库 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @return {Promise<void>} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        async addStdNodeWithParent(tenderId, stdData, stdLib) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            // 查询完整标准清单,并按层次排序 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const fullLevel = await stdLib.getFullLevelDataByFullPath(stdData.list_id, stdData.full_path); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            fullLevel.sort(function(x, y) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                return x.level - y.level; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let isNew = false, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                node, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                firstNew, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                updateParent, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                addResult; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const expandIds = []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            this.transaction = await this.db.beginTransaction(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                // 从最顶层节点依次查询是否存在,否则添加 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                for (let i = 0, len = fullLevel.length; i < len; i++) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    const stdNode = fullLevel[i]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    if (isNew) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        const newData = this._filterStdData(stdNode); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        newData.is_leaf = (i === len - 1); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        [addResult, node] = await this._addChildNodeData(tenderId, node, newData); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        const parent = node; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        node = await this.getDataByCondition({ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                            tender_id: tenderId, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                            ledger_pid: parent ? parent.ledger_id : rootId, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                            code: stdNode.code, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                            name: stdNode.name, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        if (!node) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                            isNew = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                            const newData = this._filterStdData(stdNode); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                            newData.is_leaf = (i === len - 1); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                            [addResult, node] = await this._addChildAutoOrder(tenderId, parent, newData); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                            if (parent && parent.is_leaf) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                                await this.transaction.update(this.tableName, { id: parent.id, is_leaf: false, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                                    unit_price: null, quantity: null, total_price: null, deal_qty: null, deal_tp: null}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                                updateParent = parent; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                            firstNew = node; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                            expandIds.push(node.ledger_id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                await this.transaction.commit(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } catch (err) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                await this.transaction.rollback(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                throw err; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            // 查询应返回的结果 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let createData = [], 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                updateData = []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (firstNew) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                createData = await this.getDataByFullPath(tenderId, firstNew.full_path + '%'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                updateData = await this.getNextsData(tenderId, firstNew.ledger_pid, firstNew.order); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                if (updateParent) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    updateData.push(await this.getDataByCondition({ id: updateParent.id })); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            return { create: createData, update: updateData }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				          * 删除相关数据 用于继承 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				          * @param mid 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				          * @param deleteData 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -669,12 +378,7 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     let datas = await this.getDataByFullPath(tenderId, node.full_path + '%'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     datas = this._.sortBy(datas, 'level'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    const cacheKey = keyPre + this.ctx.tender.id; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    let maxId = parseInt(await this.cache.get(cacheKey)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    if (!maxId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        maxId = await this._getMaxNodeId(this.ctx.tender.id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    this.cache.set(cacheKey, maxId + datas.length, 'EX', this.ctx.app.config.cacheTime); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    const maxId = await this._getMaxLid(this.ctx.tender.id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     const leafBillsId = []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     // 计算粘贴数据中需更新部分 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -713,6 +417,7 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     for (const id of leafBillsId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         await this.ctx.service.pos.copyBillsPosData(id.org, id.new, this.transaction); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    this._cacheMaxLid(tenderId, maxId + datas.length); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 await this.transaction.commit(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } catch (err) { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -720,6 +425,7 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 throw err; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             // 查询应返回的结果 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             const order = []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             for (let i = 1; i <= copyNodes.length; i++) { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -844,12 +550,7 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             const result = [], 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 newIds = []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             let tp = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const cacheKey = keyPre + tenderId; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let maxId = parseInt(await this.cache.get(cacheKey)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (!maxId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                maxId = await this._getMaxNodeId(tenderId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            this.cache.set(cacheKey, maxId + xmj.children.length + 1, 'EX', this.ctx.app.config.cacheTime); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const maxId = await this._getMaxLid(tenderId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             // 添加xmj数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             const parent = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 tender_id: tenderId, 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -924,11 +625,7 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 const order = lastChild ? lastChild.order : 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 // 计算id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                const cacheKey = keyPre + tenderId; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                let maxId = parseInt(await this.cache.get(cacheKey)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                if (!maxId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    maxId = await this._getMaxNodeId(tenderId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const maxId = await this._getMaxLid(tenderId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 // 数据库创建新增节点数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 for (let i = 0, iLen = data.length; i < iLen; i++) { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -954,7 +651,7 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         await this._calcNode(qd, this.transaction); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                this.cache.set(cacheKey, maxId + data.length + 1, 'EX', this.ctx.app.config.cacheTime); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                this._cacheMaxLid(tenderId, maxId + data.length); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 await this.transaction.commit(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } catch (err) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 await this.transaction.rollback(); 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -997,11 +694,7 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 // 选中节点的所有后兄弟节点,order+粘贴节点个数 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 await this._updateChildrenOrder(tenderId, selectData.ledger_pid, selectData.order + 1, data.length); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 // 计算id和order 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                const cacheKey = keyPre + tenderId; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                let maxId = parseInt(await this.cache.get(cacheKey)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                if (!maxId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    maxId = await this._getMaxNodeId(tenderId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const maxId = await this._getMaxLid(tenderId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 const order = selectData.order; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 // 数据库创建新增节点数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 for (let i = 0, iLen = data.length; i < iLen; i++) { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -1027,7 +720,7 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         await this._calcNode(qd, this.transaction); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                this.cache.set(cacheKey, maxId + data.length + 1, 'EX', this.ctx.app.config.cacheTime); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                this._cacheMaxLid(tenderId, maxId + data.length); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 await this.transaction.commit(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } catch (err) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 await this.transaction.rollback(); 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -1081,23 +774,6 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             await this._calcNode(node, transaction); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * 查找定位 --> 废弃 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param tenderId 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @param nodeId 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         * @return {Promise<{expand: *}>} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-         */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        async locateNode(tenderId, nodeId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const node = await this.getDataByNodeId(tenderId, nodeId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (!node) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                throw '查询数据有误'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const expandIds = node.full_path.split('-'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            expandIds.pop(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const expandData = await this.getChildrenByParentId(tenderId, expandIds); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            return { expand: expandData }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         async _importCacheTreeNodes(transaction, nodes) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             const datas = []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             for (const node of nodes) { 
			 |