ledger_history.js 6.4 KB

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