| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 | 
							- 'use strict';
 
- /**
 
-  *
 
-  *
 
-  * @author Mai
 
-  * @date
 
-  * @version
 
-  */
 
- const Ledger = require('../lib/ledger');
 
- module.exports = app => {
 
-     class LedgerTag extends app.BaseService {
 
-         /**
 
-          * 构造函数
 
-          *
 
-          * @param {Object} ctx - egg全局变量
 
-          * @return {void}
 
-          */
 
-         constructor(ctx) {
 
-             super(ctx);
 
-             this.tableName = 'ledger_history';
 
-         }
 
-         /** 获取最新数据
 
-          *
 
-          * @param {Number}tid - 标段id
 
-          * @return {Promise<*>} 最新数据
 
-          */
 
-         async getLatestHistory(tid) {
 
-             const his = await this.db.select(this.tableName, {
 
-                 where: { tid, valid: 1 },
 
-                 orders: [['in_time', 'desc']],
 
-                 limit: 1, offset: 0,
 
-             });
 
-             return his[0];
 
-         }
 
-         /**
 
-          * 备份
 
-          * @param {Object} tender - 标段
 
-          * @return {Promise<void>} - 新增备份id
 
-          * @private
 
-          */
 
-         async backupLedgerHistory(tender) {
 
-             const now = new Date();
 
-             const timestamp = (now).getTime();
 
-             const billsHis = `${this.ctx.session.sessionProject.id}/${tender.id}/ledger/bills${timestamp}.json`;
 
-             const bills = await this.ctx.service.ledger.getData(tender.id);
 
-             await this.ctx.hisOss.put(this.ctx.hisOssPath + billsHis, Buffer.from(JSON.stringify(bills), 'utf8'));
 
-             const posHis = `${this.ctx.session.sessionProject.id}/${tender.id}/ledger/pos${timestamp}.json`;
 
-             const pos = await this.ctx.service.pos.getPosData({ tid: tender.id });
 
-             await this.ctx.hisOss.put(this.ctx.hisOssPath + posHis, Buffer.from(JSON.stringify(pos), 'utf8'));
 
-             const result = await this.db.insert(this.tableName, {
 
-                 pid: this.ctx.session.sessionProject.id, tid: tender.id,
 
-                 in_time: now,
 
-                 bills_file: billsHis, pos_file: posHis,
 
-                 bills_count: bills.length, pos_count: pos.length,
 
-             });
 
-             return result.insertId;
 
-         }
 
-         /**
 
-          * 备份
 
-          * @param {Object} tender - 标段
 
-          * @return {Promise<void>} - 新增备份id
 
-          * @private
 
-          */
 
-         async checkBackupLedgerHistory(tid, sid = 0, rid = '') {
 
-             const sbCount = await this.ctx.service.ledger.count({ tender_id: tid });
 
-             const spCount = await this.ctx.service.pos.count({ tid: tid });
 
-             const ledgerHis = await this.ctx.service.ledgerHistory.getLatestHistory(tid);
 
-             if (sbCount === ledgerHis.bills_count && spCount === ledgerHis.pos_count) return ledgerHis.id;
 
-             const now = new Date();
 
-             const timestamp = (now).getTime();
 
-             const billsHis = `${this.ctx.session.sessionProject.id}/${tid}/ledger/bills${timestamp}-s.json`;
 
-             const bills = await this.ctx.service.ledger.getData(tid);
 
-             await this.ctx.hisOss.put(this.ctx.hisOssPath + billsHis, Buffer.from(JSON.stringify(bills), 'utf8'));
 
-             const posHis = `${this.ctx.session.sessionProject.id}/${tid}/ledger/pos${timestamp}-s.json`;
 
-             const pos = await this.ctx.service.pos.getPosData({ tid: tid });
 
-             await this.ctx.hisOss.put(this.ctx.hisOssPath + posHis, Buffer.from(JSON.stringify(pos), 'utf8'));
 
-             const result = await this.db.insert(this.tableName, {
 
-                 pid: this.ctx.session.sessionProject.id, tid, sid, rid,
 
-                 in_time: now,
 
-                 bills_file: billsHis, pos_file: posHis,
 
-                 bills_count: bills.length, pos_count: pos.length,
 
-             });
 
-             return result.insertId;
 
-         }
 
-         /**
 
-          * 备份
 
-          * @param {Object} revise - 修订
 
-          * @return {Promise<void>} - 新增备份id
 
-          * @private
 
-          */
 
-         async backupReviseLedgerHistory(revise) {
 
-             const now = new Date();
 
-             const timestamp = (now).getTime();
 
-             const price = await this.ctx.service.revisePrice.getAllDataByCondition({ where: { rid: revise.id } });
 
-             let sum = { total_price: 0 };
 
-             const billsHis = `${this.ctx.session.sessionProject.id}/${revise.tid}/ledger/bills${timestamp}-r.json`;
 
-             const bills = await this.ctx.service.reviseBills.getData(revise.tid);
 
-             const posHis = `${this.ctx.session.sessionProject.id}/${revise.tid}/ledger/pos${timestamp}-r.json`;
 
-             const pos = await this.ctx.service.revisePos.getData(revise.tid);
 
-             if (price.length === 0) {
 
-                 for (const b of bills) {
 
-                     if (!b.is_leaf) continue;
 
-                     sum.total_price = this.ctx.helper.add(sum.total_price, b.total_price);
 
-                 }
 
-                 await this.ctx.hisOss.put(this.ctx.hisOssPath + billsHis, Buffer.from(JSON.stringify(bills), 'utf8'));
 
-                 await this.ctx.hisOss.put(this.ctx.hisOssPath + posHis, Buffer.from(JSON.stringify(pos), 'utf8'));
 
-             } else {
 
-                 const reviseTree = new Ledger.reviseTree(this.ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1 });
 
-                 reviseTree.loadRevisePrice(price, this.ctx.tender.info.decimal);
 
-                 reviseTree.loadDatas(bills);
 
-                 sum = reviseTree.sum();
 
-                 await this.ctx.hisOss.put(this.ctx.hisOssPath + billsHis, Buffer.from(JSON.stringify(reviseTree.getUpdateReviseData()), 'utf8'));
 
-                 await this.ctx.hisOss.put(this.ctx.hisOssPath + posHis, Buffer.from(JSON.stringify(pos), 'utf8'));
 
-             }
 
-             const result = await this.db.insert(this.tableName, {
 
-                 pid: this.ctx.session.sessionProject.id, tid: revise.tid,
 
-                 rid: revise.id, rorder: revise.corder,
 
-                 in_time: now,
 
-                 bills_file: billsHis, pos_file: posHis,
 
-                 bills_count: bills.length, pos_count: pos.length,
 
-             });
 
-             return [result.insertId, sum];
 
-         }
 
-     }
 
-     return LedgerTag;
 
- };
 
 
  |