ledger_history.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 },
  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. });
  54. return result.insertId;
  55. }
  56. /**
  57. * 备份
  58. * @param {Object} tender - 标段
  59. * @return {Promise<void>} - 新增备份id
  60. * @private
  61. */
  62. async backupStageLedgerHistory(stage) {
  63. const now = new Date();
  64. const timestamp = (now).getTime();
  65. const billsHis = `${this.ctx.session.sessionProject.id}/${stage.tid}/ledger/bills${timestamp}-s.json`;
  66. const bills = await this.ctx.service.ledger.getData(stage.tid);
  67. await this.ctx.hisOss.put(this.ctx.hisOssPath + billsHis, Buffer.from(JSON.stringify(bills), 'utf8'));
  68. const posHis = `${this.ctx.session.sessionProject.id}/${stage.tid}/ledger/pos${timestamp}-s.json`;
  69. const pos = await this.ctx.service.pos.getPosData({ tid: stage.tid });
  70. await this.ctx.hisOss.put(this.ctx.hisOssPath + posHis, Buffer.from(JSON.stringify(pos), 'utf8'));
  71. const result = await this.db.insert(this.tableName, {
  72. pid: this.ctx.session.sessionProject.id, tid: stage.tid, sid: stage.id,
  73. in_time: now,
  74. bills_file: billsHis, pos_file: posHis,
  75. });
  76. return result.insertId;
  77. }
  78. /**
  79. * 备份
  80. * @param {Object} revise - 修订
  81. * @return {Promise<void>} - 新增备份id
  82. * @private
  83. */
  84. async backupReviseLedgerHistory(revise) {
  85. const now = new Date();
  86. const timestamp = (now).getTime();
  87. const billsHis = `${this.ctx.session.sessionProject.id}/${revise.tid}/ledger/bills${timestamp}-r.json`;
  88. const bills = await this.ctx.service.reviseBills.getData(revise.tid);
  89. await this.ctx.hisOss.put(this.ctx.hisOssPath + billsHis, Buffer.from(JSON.stringify(bills), 'utf8'));
  90. const posHis = `${this.ctx.session.sessionProject.id}/${revise.tid}/ledger/pos${timestamp}-r.json`;
  91. const pos = await this.ctx.service.revisePos.getData(revise.tid);
  92. await this.ctx.hisOss.put(this.ctx.hisOssPath + posHis, Buffer.from(JSON.stringify(pos), 'utf8'));
  93. const result = await this.db.insert(this.tableName, {
  94. pid: this.ctx.session.sessionProject.id, tid: revise.tid,
  95. rid: revise.id, rorder: revise.corder,
  96. in_time: now,
  97. bills_file: billsHis, pos_file: posHis,
  98. });
  99. return result.insertId;
  100. }
  101. /**
  102. * 备份 (预留功能)
  103. * @param {Object} transaction - 事务
  104. * @param {Object} change - 工程变更
  105. * @param {Array} newBillsNode - 新增项目节节点
  106. * @param {Array} newPosNode - 新增计量单元节点
  107. * @return {Promise<void>} - 新增备份id
  108. * @private
  109. */
  110. async backupChangeHistory(transaction, change, newBillsNodes, newPosNodes) {
  111. if ((newBillsNodes || newBillsNodes === 0) && (newPosNodes || newPosNodes.length === 0)) return;
  112. const now = new Date();
  113. const timestamp = (now).getTime();
  114. const billsHis = `${this.ctx.session.sessionProject.id}/${change.tid}/ledger/bills${timestamp}-c.json`;
  115. const bills = await this.ctx.service.ledger.getData(change.tid);
  116. if (newBillsNodes.length > 0) bills.push(...newBillsNodes);
  117. await this.ctx.hisOss.put(this.ctx.hisOssPath + billsHis, Buffer.from(JSON.stringify(bills), 'utf8'));
  118. const posHis = `${this.ctx.session.sessionProject.id}/${change.tid}/ledger/pos${timestamp}-c.json`;
  119. const pos = await this.ctx.service.pos.getPosData({ tid: change.tid });
  120. if (newPosNodes.length > 0) pos.push(...newPosNodes);
  121. await this.ctx.hisOss.put(this.ctx.hisOssPath + posHis, Buffer.from(JSON.stringify(pos), 'utf8'));
  122. const result = await transaction.insert(this.tableName, {
  123. pid: this.ctx.session.sessionProject.id, tid: change.tid,
  124. cid: change.cid,
  125. in_time: now,
  126. bills_file: billsHis, pos_file: posHis,
  127. });
  128. return result.insertId;
  129. }
  130. }
  131. return LedgerTag;
  132. };