ledger_history.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. console.log(bills[0]);
  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. const sbCount = await this.ctx.service.ledger.count({ tender_id: tid });
  67. const spCount = await this.ctx.service.pos.count({ tid: tid });
  68. const ledgerHis = await this.ctx.service.ledgerHistory.getLatestHistory(tid);
  69. if (sbCount === ledgerHis.bills_count && spCount === ledgerHis.pos_count) return ledgerHis.id;
  70. const now = new Date();
  71. const timestamp = (now).getTime();
  72. const billsHis = `${this.ctx.session.sessionProject.id}/${tid}/ledger/bills${timestamp}-s.json`;
  73. const bills = await this.ctx.service.ledger.getData(tid);
  74. await this.ctx.hisOss.put(this.ctx.hisOssPath + billsHis, Buffer.from(JSON.stringify(bills), 'utf8'));
  75. const posHis = `${this.ctx.session.sessionProject.id}/${tid}/ledger/pos${timestamp}-s.json`;
  76. const pos = await this.ctx.service.pos.getPosData({ tid: tid });
  77. await this.ctx.hisOss.put(this.ctx.hisOssPath + posHis, Buffer.from(JSON.stringify(pos), 'utf8'));
  78. const result = await this.db.insert(this.tableName, {
  79. pid: this.ctx.session.sessionProject.id, tid, sid, rid,
  80. in_time: now,
  81. bills_file: billsHis, pos_file: posHis,
  82. bills_count: bills.length, pos_count: pos.length,
  83. });
  84. return result.insertId;
  85. }
  86. /**
  87. * 备份
  88. * @param {Object} revise - 修订
  89. * @return {Promise<void>} - 新增备份id
  90. * @private
  91. */
  92. async backupReviseLedgerHistory(revise) {
  93. const now = new Date();
  94. const timestamp = (now).getTime();
  95. const price = await this.ctx.service.revisePrice.getAllDataByCondition({ where: { rid: revise.id } });
  96. let sum = { total_price: 0 };
  97. const billsHis = `${this.ctx.session.sessionProject.id}/${revise.tid}/ledger/bills${timestamp}-r.json`;
  98. const bills = await this.ctx.service.reviseBills.getData(revise.tid);
  99. const posHis = `${this.ctx.session.sessionProject.id}/${revise.tid}/ledger/pos${timestamp}-r.json`;
  100. const pos = await this.ctx.service.revisePos.getData(revise.tid);
  101. if (price.length === 0) {
  102. for (const b of bills) {
  103. if (!b.is_leaf) continue;
  104. sum.total_price = this.ctx.helper.add(sum.total_price, b.total_price);
  105. }
  106. await this.ctx.hisOss.put(this.ctx.hisOssPath + billsHis, Buffer.from(JSON.stringify(bills), 'utf8'));
  107. await this.ctx.hisOss.put(this.ctx.hisOssPath + posHis, Buffer.from(JSON.stringify(pos), 'utf8'));
  108. } else {
  109. const reviseTree = new Ledger.reviseTree(this.ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1 });
  110. reviseTree.loadRevisePrice(price, this.ctx.tender.info.decimal);
  111. reviseTree.loadDatas(bills);
  112. sum = reviseTree.sum();
  113. await this.ctx.hisOss.put(this.ctx.hisOssPath + billsHis, Buffer.from(JSON.stringify(reviseTree.getUpdateReviseData()), 'utf8'));
  114. await this.ctx.hisOss.put(this.ctx.hisOssPath + posHis, Buffer.from(JSON.stringify(pos), 'utf8'));
  115. }
  116. const result = await this.db.insert(this.tableName, {
  117. pid: this.ctx.session.sessionProject.id, tid: revise.tid,
  118. rid: revise.id, rorder: revise.corder,
  119. in_time: now,
  120. bills_file: billsHis, pos_file: posHis,
  121. bills_count: bills.length, pos_count: pos.length,
  122. });
  123. return [result.insertId, sum];
  124. }
  125. }
  126. return LedgerTag;
  127. };