ledger_history.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class LedgerTag extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'ledger_history';
  20. }
  21. /** 获取最新数据
  22. *
  23. * @param {Number}tid - 标段id
  24. * @return {Promise<*>} 最新数据
  25. */
  26. async getLatestHistory(tid) {
  27. const his = await this.db.select(this.tableName, {
  28. where: { tid, valid: 1 },
  29. orders: [['in_time', 'desc']],
  30. limit: 1, offset: 0,
  31. });
  32. return his[0];
  33. }
  34. /**
  35. * 备份
  36. * @param {Object} tender - 标段
  37. * @return {Promise<void>} - 新增备份id
  38. * @private
  39. */
  40. async backupLedgerHistory(tender) {
  41. const now = new Date();
  42. const timestamp = (now).getTime();
  43. const billsHis = `${this.ctx.session.sessionProject.id}/${tender.id}/ledger/bills${timestamp}.json`;
  44. const bills = await this.ctx.service.ledger.getData(tender.id);
  45. await this.ctx.hisOss.put(this.ctx.hisOssPath + billsHis, Buffer.from(JSON.stringify(bills), 'utf8'));
  46. const posHis = `${this.ctx.session.sessionProject.id}/${tender.id}/ledger/pos${timestamp}.json`;
  47. const pos = await this.ctx.service.pos.getPosData({ tid: tender.id });
  48. await this.ctx.hisOss.put(this.ctx.hisOssPath + posHis, Buffer.from(JSON.stringify(pos), 'utf8'));
  49. const result = await this.db.insert(this.tableName, {
  50. pid: this.ctx.session.sessionProject.id, tid: tender.id,
  51. in_time: now,
  52. bills_file: billsHis, pos_file: posHis,
  53. bills_count: bills.length, pos_count: pos.length,
  54. });
  55. return result.insertId;
  56. }
  57. /**
  58. * 备份
  59. * @param {Object} tender - 标段
  60. * @return {Promise<void>} - 新增备份id
  61. * @private
  62. */
  63. async backupStageLedgerHistory(stage) {
  64. const sbCount = await this.ctx.service.ledger.count({ tender_id: this.ctx.stage.tid });
  65. const spCount = await this.ctx.service.pos.count({ tid: this.ctx.stage.tid });
  66. const ledgerHis = await this.ctx.service.ledgerHistory.getLatestHistory(this.ctx.tender.id);
  67. if (sbCount === ledgerHis.bills_count && spCount === ledgerHis.pos_count) return ledgerHis.id;
  68. const now = new Date();
  69. const timestamp = (now).getTime();
  70. const billsHis = `${this.ctx.session.sessionProject.id}/${stage.tid}/ledger/bills${timestamp}-s.json`;
  71. const bills = await this.ctx.service.ledger.getData(stage.tid);
  72. await this.ctx.hisOss.put(this.ctx.hisOssPath + billsHis, Buffer.from(JSON.stringify(bills), 'utf8'));
  73. const posHis = `${this.ctx.session.sessionProject.id}/${stage.tid}/ledger/pos${timestamp}-s.json`;
  74. const pos = await this.ctx.service.pos.getPosData({ tid: stage.tid });
  75. await this.ctx.hisOss.put(this.ctx.hisOssPath + posHis, Buffer.from(JSON.stringify(pos), 'utf8'));
  76. const result = await this.db.insert(this.tableName, {
  77. pid: this.ctx.session.sessionProject.id, tid: stage.tid, sid: stage.id,
  78. in_time: now,
  79. bills_file: billsHis, pos_file: posHis,
  80. bills_count: bills.length, pos_count: pos.length,
  81. });
  82. return result.insertId;
  83. }
  84. /**
  85. * 备份
  86. * @param {Object} revise - 修订
  87. * @return {Promise<void>} - 新增备份id
  88. * @private
  89. */
  90. async backupReviseLedgerHistory(revise) {
  91. const now = new Date();
  92. const timestamp = (now).getTime();
  93. const billsHis = `${this.ctx.session.sessionProject.id}/${revise.tid}/ledger/bills${timestamp}-r.json`;
  94. const bills = await this.ctx.service.reviseBills.getData(revise.tid);
  95. await this.ctx.hisOss.put(this.ctx.hisOssPath + billsHis, Buffer.from(JSON.stringify(bills), 'utf8'));
  96. const posHis = `${this.ctx.session.sessionProject.id}/${revise.tid}/ledger/pos${timestamp}-r.json`;
  97. const pos = await this.ctx.service.revisePos.getData(revise.tid);
  98. await this.ctx.hisOss.put(this.ctx.hisOssPath + posHis, Buffer.from(JSON.stringify(pos), 'utf8'));
  99. const result = await this.db.insert(this.tableName, {
  100. pid: this.ctx.session.sessionProject.id, tid: revise.tid,
  101. rid: revise.id, rorder: revise.corder,
  102. in_time: now,
  103. bills_file: billsHis, pos_file: posHis,
  104. bills_count: bills.length, pos_count: pos.length,
  105. });
  106. return result.insertId;
  107. }
  108. /**
  109. * 备份 (预留功能)
  110. * @param {Object} transaction - 事务
  111. * @param {Object} change - 工程变更
  112. * @param {Array} newBillsNode - 新增项目节节点
  113. * @param {Array} newPosNode - 新增计量单元节点
  114. * @return {Promise<void>} - 新增备份id
  115. * @private
  116. */
  117. async backupChangeHistory(transaction, change, newBillsNodes, newPosNodes) {
  118. if ((newBillsNodes || newBillsNodes === 0) && (newPosNodes || newPosNodes.length === 0)) return;
  119. const now = new Date();
  120. const timestamp = (now).getTime();
  121. const billsHis = `${this.ctx.session.sessionProject.id}/${change.tid}/ledger/bills${timestamp}-c.json`;
  122. const bills = await this.ctx.service.ledger.getData(change.tid);
  123. if (newBillsNodes.length > 0) bills.push(...newBillsNodes);
  124. await this.ctx.hisOss.put(this.ctx.hisOssPath + billsHis, Buffer.from(JSON.stringify(bills), 'utf8'));
  125. const posHis = `${this.ctx.session.sessionProject.id}/${change.tid}/ledger/pos${timestamp}-c.json`;
  126. const pos = await this.ctx.service.pos.getPosData({ tid: change.tid });
  127. if (newPosNodes.length > 0) pos.push(...newPosNodes);
  128. await this.ctx.hisOss.put(this.ctx.hisOssPath + posHis, Buffer.from(JSON.stringify(pos), 'utf8'));
  129. const result = await transaction.insert(this.tableName, {
  130. pid: this.ctx.session.sessionProject.id, tid: change.tid,
  131. cid: change.cid,
  132. in_time: now,
  133. bills_file: billsHis, pos_file: posHis,
  134. bills_count: bills.length, pos_count: pos.length,
  135. });
  136. return result.insertId;
  137. }
  138. }
  139. return LedgerTag;
  140. };