| 
					
				 | 
			
			
				@@ -326,6 +326,75 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 stage.id, copyTimes, copyOrder]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             return await transaction.query(sql, sqlParam); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        _newExprAfterChangeOrder(expr, orderPart1, orderPart2) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (!expr) return [false, expr]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const orderParam = [...expr.matchAll(/f\d+/ig)]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (orderParam.length === 0) return [false, expr]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const exprPart = [], newExprPart = []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            for (const [i, op] of orderParam.entries()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (i === 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    exprPart.push(expr.substring(0, op.index)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    const preOp = orderParam[i-1]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    exprPart.push(expr.substring(preOp.index + preOp[0].length, op.index)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                exprPart.push(op[0]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (i === orderParam.length - 1) exprPart.push(expr.substring(op.index + op[0].length, expr.length)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            let change = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            for (const ep of exprPart) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (ep === orderPart1) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    newExprPart.push(orderPart2); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    change = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } else if (ep === orderPart2) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    newExprPart.push(orderPart1); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    change = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    newExprPart.push(ep); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return [change, newExprPart.join('')]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+         * 交换两个合同支付项的顺序 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+         * @param {Number} id1 - 合同支付项1的id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+         * @param {Number} id2 - 合同支付项1的id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+         * @returns {Promise<void>} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+         */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        async changeOrder(id1, id2) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (!this.ctx.tender || !this.ctx.stage) throw '数据错误'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const pay1 = await this.ctx.service.pay.getDataByCondition({tid: this.ctx.tender.id, id: id1}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const pay2 = await this.ctx.service.pay.getDataByCondition({tid: this.ctx.tender.id, id: id2}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (!pay1 || !pay2) throw '数据错误'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const stagePays = await this.getStagePays(this.ctx.stage); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const conn = await this.db.beginTransaction(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const orderPart1 = `f${pay1.order}`; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const orderPart2 = `f${pay2.order}`; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const updateData = [{id: pay1.id, order: pay2.order}, {id: pay2.id, order: pay1.order}]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const updateStageData = []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                for (const p of stagePays) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    const [change, newExpr] = this._newExprAfterChangeOrder(p.expr, orderPart1, orderPart2); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    if (change) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        updateStageData.push({ condition: { where: { pid: p.pid } }, update: { expr: newExpr } }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                await conn.updateRows(this.ctx.service.pay.tableName, updateData); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                for (const usd of updateStageData) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    await conn.update(this.tableName, usd.update, usd.condition); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                await conn.commit(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                return await this.getStagePays(this.ctx.stage); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } catch (err) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                await conn.rollback(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                throw err; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     return StagePay; 
			 |