ledger_history.js 4.7 KB

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