ledger_history.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const Ledger = require('../lib/ledger');
  10. module.exports = app => {
  11. class LedgerTag extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'ledger_history';
  21. }
  22. /** 获取最新数据
  23. *
  24. * @param {Number}tid - 标段id
  25. * @return {Promise<*>} 最新数据
  26. */
  27. async getLatestHistory(tid) {
  28. const his = await this.db.select(this.tableName, {
  29. where: { tid, valid: 1 },
  30. orders: [['in_time', 'desc']],
  31. limit: 1, offset: 0,
  32. });
  33. return his[0];
  34. }
  35. /**
  36. * 备份
  37. * @param {Object} tender - 标段
  38. * @return {Promise<void>} - 新增备份id
  39. * @private
  40. */
  41. async backupLedgerHistory(tender) {
  42. const now = new Date();
  43. const timestamp = (now).getTime();
  44. const billsHis = `${this.ctx.session.sessionProject.id}/${tender.id}/ledger/bills${timestamp}.json`;
  45. const bills = await this.ctx.service.ledger.getData(tender.id);
  46. await this.ctx.hisOss.put(this.ctx.hisOssPath + billsHis, Buffer.from(JSON.stringify(bills), 'utf8'));
  47. const posHis = `${this.ctx.session.sessionProject.id}/${tender.id}/ledger/pos${timestamp}.json`;
  48. const pos = await this.ctx.service.pos.getPosData({ tid: tender.id });
  49. await this.ctx.hisOss.put(this.ctx.hisOssPath + posHis, Buffer.from(JSON.stringify(pos), 'utf8'));
  50. const result = await this.db.insert(this.tableName, {
  51. pid: this.ctx.session.sessionProject.id, tid: tender.id,
  52. in_time: now,
  53. bills_file: billsHis, pos_file: posHis,
  54. bills_count: bills.length, pos_count: pos.length,
  55. });
  56. return result.insertId;
  57. }
  58. /**
  59. * 备份
  60. * @param {Object} tender - 标段
  61. * @return {Promise<void>} - 新增备份id
  62. * @private
  63. */
  64. async checkBackupLedgerHistory(tid, sid = 0, rid = '') {
  65. const sbCount = await this.ctx.service.ledger.count({ tender_id: tid });
  66. const spCount = await this.ctx.service.pos.count({ tid: tid });
  67. const ledgerHis = await this.ctx.service.ledgerHistory.getLatestHistory(tid);
  68. if (sbCount === ledgerHis.bills_count && spCount === ledgerHis.pos_count) return ledgerHis.id;
  69. const now = new Date();
  70. const timestamp = (now).getTime();
  71. const billsHis = `${this.ctx.session.sessionProject.id}/${tid}/ledger/bills${timestamp}-s.json`;
  72. const bills = await this.ctx.service.ledger.getData(tid);
  73. await this.ctx.hisOss.put(this.ctx.hisOssPath + billsHis, Buffer.from(JSON.stringify(bills), 'utf8'));
  74. const posHis = `${this.ctx.session.sessionProject.id}/${tid}/ledger/pos${timestamp}-s.json`;
  75. const pos = await this.ctx.service.pos.getPosData({ tid: tid });
  76. await this.ctx.hisOss.put(this.ctx.hisOssPath + posHis, Buffer.from(JSON.stringify(pos), 'utf8'));
  77. const result = await this.db.insert(this.tableName, {
  78. pid: this.ctx.session.sessionProject.id, tid, sid, rid,
  79. in_time: now,
  80. bills_file: billsHis, pos_file: posHis,
  81. bills_count: bills.length, pos_count: pos.length,
  82. });
  83. return result.insertId;
  84. }
  85. /**
  86. * 备份
  87. * @param {Object} revise - 修订
  88. * @return {Promise<void>} - 新增备份id
  89. * @private
  90. */
  91. async backupReviseLedgerHistory(revise) {
  92. const now = new Date();
  93. const timestamp = (now).getTime();
  94. const price = await this.ctx.service.revisePrice.getAllDataByCondition({ where: { rid: revise.id } });
  95. let sum = { total_price: 0 };
  96. const billsHis = `${this.ctx.session.sessionProject.id}/${revise.tid}/ledger/bills${timestamp}-r.json`;
  97. const bills = await this.ctx.service.reviseBills.getData(revise.tid);
  98. const posHis = `${this.ctx.session.sessionProject.id}/${revise.tid}/ledger/pos${timestamp}-r.json`;
  99. const pos = await this.ctx.service.revisePos.getData(revise.tid);
  100. if (price.length === 0) {
  101. for (const b of bills) {
  102. if (!b.is_leaf) continue;
  103. sum.total_price = this.ctx.helper.add(sum.total_price, b.total_price);
  104. }
  105. await this.ctx.hisOss.put(this.ctx.hisOssPath + billsHis, Buffer.from(JSON.stringify(bills), 'utf8'));
  106. await this.ctx.hisOss.put(this.ctx.hisOssPath + posHis, Buffer.from(JSON.stringify(pos), 'utf8'));
  107. } else {
  108. const reviseTree = new Ledger.reviseTree(this.ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1 });
  109. reviseTree.loadRevisePrice(price, this.ctx.tender.info.decimal);
  110. reviseTree.loadDatas(bills);
  111. sum = reviseTree.sum();
  112. await this.ctx.hisOss.put(this.ctx.hisOssPath + billsHis, Buffer.from(JSON.stringify(reviseTree.getUpdateReviseData()), 'utf8'));
  113. await this.ctx.hisOss.put(this.ctx.hisOssPath + posHis, Buffer.from(JSON.stringify(pos), 'utf8'));
  114. }
  115. const result = await this.db.insert(this.tableName, {
  116. pid: this.ctx.session.sessionProject.id, tid: revise.tid,
  117. rid: revise.id, rorder: revise.corder,
  118. in_time: now,
  119. bills_file: billsHis, pos_file: posHis,
  120. bills_count: bills.length, pos_count: pos.length,
  121. });
  122. return [result.insertId, sum];
  123. }
  124. }
  125. return LedgerTag;
  126. };