| 
					
				 | 
			
			
				@@ -0,0 +1,69 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+'use strict'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+/** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * stage_bills_final 期-清单-最终数据(截止本期) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * @author Mai 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * @date 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * @version 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    class StageBillsFinal extends app.BaseService { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+         * 构造函数 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+         * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+         * @param {Object} ctx - egg全局变量 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+         * @return {void} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+         */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        constructor(ctx) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            super(ctx); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            this.tableName = 'stage_bills_final'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+         * 生成本期最终数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+         * @param transaction - 所属事务 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+         * @param {Number} tender - 标段 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+         * @param {Number}stage - 本期 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+         * @returns {Promise<void>} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+         */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        async generateFinalData(transaction, tender, stage) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (!transaction || !tender || !stage) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                throw '数据错误'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            let preStage; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (stage.order > 1) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                preStage = await this.ctx.stage.getDataByCondition({tid: stage.tid, order: stage.order - 1}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (preStage) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const sql = 'Insert Into ??(tid, sid, lid, sorder, contract_qty, contract_tp, qc_qty, qc_tp, postil)' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '  SELECT b.tid, b.sid, b.lid, ? As `sorder`, b.contract_qty, b.contract_tp, b.qc_qty, b.qc_tp, b.postil' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '  FROM ' + this.ctx.service.stageBills.tableName + ' AS b' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '  INNER JOIN(' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '    SELECT Max(`times`) As `times`, MAX(`order`) As `order`, `lid` FROM ' + this.ctx.service.stageBills.tableName + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '    WHERE `sid` = ?' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '    GROUP By `lid`) As MF' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '  ON b.times = MF.times AND b.order = MF.order AND b.lid = MF.lid' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '  WHERE b.sid = ?'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const sqlParam = [this.tableName, stage.order, stage.id, stage.id]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                await transaction.query(sql, sqlParam); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const sql = 'Insert Into ??(tid, sid, lid, sorder, contract_qty, contract_tp, qc_qty, qc_tp, postil)' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '  SELECT b.tid, b.sid, b.lid, ? As `sorder`, b.contract_qty, b.contract_tp, b.qc_qty, b.qc_tp, b.postil' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '  FROM ' + this.ctx.service.stageBills.tableName + ' AS b' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '  INNER JOIN(' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '    SELECT Max(`times`) As `times`, MAX(`order`) As `order`, `lid` FROM ' + this.ctx.service.stageBills.tableName + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '    WHERE `sid` = ?' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '    GROUP By `lid`) As MF' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '  ON b.times = MF.times AND b.order = MF.order AND b.lid = MF.lid' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '  WHERE b.sid = ?'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const sqlParam = [this.tableName, stage.order, stage.id, stage.id]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                await transaction.query(sql, sqlParam); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    return StageBillsFinal; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 |