stage_stash.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class StageStash extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'stage_stash';
  20. }
  21. async getValidStash(tid) {
  22. const sql = `SELECT id, sorder, create_time, uid, uname, remark FROM ${this.tableName} WHERE tid = ? and valid and TO_DAYS(NOW()) - TO_DAYS(create_time) <= 30 ORDER BY create_time DESC`;
  23. return await this.db.query(sql, [tid]);
  24. }
  25. async addStash(stage, remark = '') {
  26. const bills = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id);
  27. const pos = await this.ctx.service.stagePos.getLastestStageData2(stage.tid, stage.id);
  28. const change = await this.ctx.service.stageChange.getFinalStageData(stage.tid, stage.id);
  29. const timestamp = (new Date()).getTime();
  30. const filepath = `${this.ctx.session.sessionProject.id}-${stage.tid}-${timestamp}.json`;
  31. await this.ctx.hisOss.put(this.ctx.stashOssPath + filepath, Buffer.from(JSON.stringify({bills, pos, change}), 'utf8'));
  32. const result = await this.db.insert(this.tableName, {
  33. pid: this.ctx.session.sessionProject.id, tid: stage.tid, sid: stage.id, sorder: stage.order,
  34. uid: this.ctx.session.sessionUser.accountId, uname: this.ctx.session.sessionUser.name,
  35. filepath, info: {status: stage.status, flow: stage.curAuditor ? stage.curAuditor.user_id : stage.uid}, remark
  36. });
  37. return result.insertId;
  38. }
  39. async delStash(id) {
  40. const stash = await this.getDataById(id);
  41. if (!stash) throw '暂存计量不存在';
  42. if (stash.tid !== this.ctx.tender.id) throw '非当前标段暂存计量,不可操作';
  43. await this.deleteById(id);
  44. await this.ctx.hisOss.delete(this.ctx.stashOssPath + stash.filepath);
  45. }
  46. async loadLedgerDataFromOss(filepath) {
  47. const File = await this.ctx.hisOss.get(this.ctx.stashOssPath + filepath);
  48. if (File.res.status !== 200) return '获取暂存计量有误';
  49. const result = JSON.parse(File.content);
  50. return result;
  51. }
  52. async loadAndAnalysis(filepath) {
  53. const data = await this.loadLedgerDataFromOss(filepath);
  54. const billsIndex = {};
  55. for (const b of data.bills) {
  56. billsIndex[b.lid] = b;
  57. }
  58. for (const p of data.pos) {
  59. if (!billsIndex[p.lid]) continue;
  60. if (!billsIndex[p.lid].pos) billsIndex[p.lid].pos = [];
  61. billsIndex[p.lid].pos.push(p);
  62. }
  63. for (const c of data.change) {
  64. if (!billsIndex[c.lid]) continue;
  65. if (billsIndex[c.lid].pos) {
  66. const p = billsIndex[c.lid].pos.find(x => { return x.pid === c.pid });
  67. if (!p) continue;
  68. if (!p.change) p.change = [];
  69. p.change.push(c);
  70. } else {
  71. if (!billsIndex[c.lid].change) billsIndex[c.lid].change = [];
  72. billsIndex[c.lid].change.push(c);
  73. }
  74. }
  75. return data.bills;
  76. }
  77. async reCalcStashData(stage, data) {
  78. const decimal = this.ctx.tender.info.decimal;
  79. const insertBillsData = [], insertPosData = [], insertChangeData = [];
  80. const bills = await this.ctx.service.ledger.getAllDataByCondition({ where: { tender_id: stage.tid, is_leaf: true } });
  81. const pos = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: stage.tid } });
  82. const said = this.ctx.session.sessionUser.accountId;
  83. for (const d of data) {
  84. const b = bills.find(x => { return x.id === d.lid });
  85. if (!b) continue;
  86. const nbs = { tid: stage.tid, sid: stage.id, said, lid: b.id, times: 1, order: 0 };
  87. if (d.pos) {
  88. for (const bp of d.pos) {
  89. const p = pos.find(x => { return x.id === bp.pid});
  90. if (!p) continue;
  91. const nps = { tid: stage.tid, sid: stage.id, said, lid: b.id, pid: p.id, times: 1, order: 0 };
  92. nps.contract_qty = this.ctx.helper.round(bp.contract_qty, decimal.qty);
  93. nps.qc_qty = 0;
  94. insertPosData.push(nps);
  95. if (bp.change) {
  96. for (const c of bp.change) {
  97. if (!c.qty) continue;
  98. const ncs = { tid: stage.tid, sid: stage.id, lid: b.id, pid: p.id, stimes: 1, sorder: 0, cid: c.cid, cbid: c.cbid, minus: c.minus };
  99. const validQty = await this.ctx.service.stageChangeFinal.getChangeBillsValidQty(c.cbid);
  100. ncs.qty = validQty >= c.qty ? c.qty : validQty;
  101. insertChangeData.push(ncs);
  102. if (!ncs.minus) nps.qc_qty = await this.ctx.helper.add(nps.qc_qty, ncs.qty);
  103. }
  104. }
  105. nbs.contract_qty = this.ctx.helper.add(nbs.contract_qty, nps.contract_qty);
  106. nbs.qc_qty = this.ctx.helper.add(nbs.qc_qty, nps.qc_qty);
  107. }
  108. nbs.contract_tp = this.ctx.helper.mul(nbs.contract_qty, b.unit_price, decimal.tp);
  109. nbs.qc_tp = this.ctx.helper.mul(nbs.qc_qty, b.unit_price, decimal.tp);
  110. } else {
  111. if (b.is_tp) {
  112. nbs.contract_qty = 0;
  113. nbs.contract_tp = this.ctx.helper.round(d.contract_tp, decimal.tp);
  114. nbs.qc_qty = 0;
  115. nbs.qc_tp = 0;
  116. } else {
  117. nbs.contract_qty = this.ctx.helper.round(d.contract_qty, decimal.qty);
  118. nbs.contract_tp = this.ctx.helper.mul(nbs.contract_qty, b.unit_price, decimal.tp);
  119. nbs.qc_qty = 0;
  120. if (d.change) {
  121. for (const c of d.change) {
  122. if (!c.qty) continue;
  123. const ncs = { tid: stage.tid, sid: stage.id, lid: d.id, pid: -1, stimes: 1, sorder: 0, cid: c.cid, cbid: c.cbid, minus: c.minus };
  124. const validQty = await this.ctx.service.stageChangeFinal.getChangeBillsValidQty(c.cbid);
  125. ncs.qty = validQty >= c.qty ? c.qty : validQty;
  126. insertChangeData.push(ncs);
  127. if (!ncs.minus) nbs.qc_qty = await this.ctx.helper.add(nbs.qc_qty, ncs.qty);
  128. }
  129. }
  130. nbs.qc_tp = this.ctx.helper.mul(nbs.qc_qty, b.unit_price, decimal.tp);
  131. }
  132. }
  133. insertBillsData.push(nbs);
  134. }
  135. return [insertBillsData, insertPosData, insertChangeData]
  136. }
  137. async recover(stage, id) {
  138. const stash = await this.getDataById(id);
  139. if (!stash) throw '暂存计量不存在';
  140. if (stash.tid !== stage.tid) throw '暂存计量不可导入';
  141. const stashData = await this.loadAndAnalysis(stash.filepath);
  142. const [insertBills, insertPos, insertChange] = await this.reCalcStashData(stage, stashData);
  143. const conn = await this.db.beginTransaction();
  144. try {
  145. await conn.delete(this.ctx.service.stageBills.tableName, { sid: stage.id });
  146. await conn.delete(this.ctx.service.stageChange.tableName, { sid: stage.id });
  147. await conn.delete(this.ctx.service.stagePos.tableName, { sid: stage.id });
  148. if (insertBills.length > 0) conn.insert(this.ctx.service.stageBills.tableName, insertBills);
  149. if (insertPos.length > 0) conn.insert(this.ctx.service.stagePos.tableName, insertPos);
  150. if (insertChange.length > 0) conn.insert(this.ctx.service.stageChange.tableName, insertChange);
  151. await conn.commit();
  152. } catch (err) {
  153. await conn.rollback();
  154. throw err;
  155. }
  156. };
  157. }
  158. return StageStash;
  159. };