123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- 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 },
- 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,
- });
- return result.insertId;
- }
- /**
- * 备份
- * @param {Object} tender - 标段
- * @return {Promise<void>} - 新增备份id
- * @private
- */
- async backupStageLedgerHistory(stage) {
- const now = new Date();
- const timestamp = (now).getTime();
- const billsHis = `${this.ctx.session.sessionProject.id}/${stage.tid}/ledger/bills${timestamp}-s.json`;
- const bills = await this.ctx.service.ledger.getData(stage.tid);
- await this.ctx.hisOss.put(this.ctx.hisOssPath + billsHis, Buffer.from(JSON.stringify(bills), 'utf8'));
- const posHis = `${this.ctx.session.sessionProject.id}/${stage.tid}/ledger/pos${timestamp}-s.json`;
- const pos = await this.ctx.service.pos.getPosData({ tid: stage.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: stage.tid, sid: stage.id,
- in_time: now,
- bills_file: billsHis, pos_file: posHis,
- });
- return result.insertId;
- }
- /**
- * 备份
- * @param {Object} revise - 修订
- * @return {Promise<void>} - 新增备份id
- * @private
- */
- async backupReviseLedgerHistory(revise) {
- const now = new Date();
- const timestamp = (now).getTime();
- const billsHis = `${this.ctx.session.sessionProject.id}/${revise.tid}/ledger/bills${timestamp}-r.json`;
- const bills = await this.ctx.service.reviseBills.getData(revise.tid);
- await this.ctx.hisOss.put(this.ctx.hisOssPath + billsHis, Buffer.from(JSON.stringify(bills), 'utf8'));
- const posHis = `${this.ctx.session.sessionProject.id}/${revise.tid}/ledger/pos${timestamp}-r.json`;
- const pos = await this.ctx.service.revisePos.getData(revise.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: revise.tid,
- rid: revise.id, rorder: revise.corder,
- in_time: now,
- bills_file: billsHis, pos_file: posHis,
- });
- return result.insertId;
- }
- /**
- * 备份 (预留功能)
- * @param {Object} transaction - 事务
- * @param {Object} change - 工程变更
- * @param {Array} newBillsNode - 新增项目节节点
- * @param {Array} newPosNode - 新增计量单元节点
- * @return {Promise<void>} - 新增备份id
- * @private
- */
- async backupChangeHistory(transaction, change, newBillsNodes, newPosNodes) {
- if ((newBillsNodes || newBillsNodes === 0) && (newPosNodes || newPosNodes.length === 0)) return;
- const now = new Date();
- const timestamp = (now).getTime();
- const billsHis = `${this.ctx.session.sessionProject.id}/${change.tid}/ledger/bills${timestamp}-c.json`;
- const bills = await this.ctx.service.ledger.getData(change.tid);
- if (newBillsNodes.length > 0) bills.push(...newBillsNodes);
- await this.ctx.hisOss.put(this.ctx.hisOssPath + billsHis, Buffer.from(JSON.stringify(bills), 'utf8'));
- const posHis = `${this.ctx.session.sessionProject.id}/${change.tid}/ledger/pos${timestamp}-c.json`;
- const pos = await this.ctx.service.pos.getPosData({ tid: change.tid });
- if (newPosNodes.length > 0) pos.push(...newPosNodes);
- await this.ctx.hisOss.put(this.ctx.hisOssPath + posHis, Buffer.from(JSON.stringify(pos), 'utf8'));
- const result = await transaction.insert(this.tableName, {
- pid: this.ctx.session.sessionProject.id, tid: change.tid,
- cid: change.cid,
- in_time: now,
- bills_file: billsHis, pos_file: posHis,
- });
- return result.insertId;
- }
- }
- return LedgerTag;
- };
|