stage_bills.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. 'use strict';
  2. /**
  3. * 期计量 - 部位明细计量
  4. *
  5. * @author Mai
  6. * @date 2018/12/8
  7. * @version
  8. */
  9. const calcFields = ['contract_qty', 'qc_qty'];
  10. const auditConst = require('../const/audit');
  11. module.exports = app => {
  12. class StageBills extends app.BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'stage_bills';
  22. }
  23. /**
  24. * 查询期计量最后审核人数据
  25. * @param {Number} tid - 标段id
  26. * @param {Number} sid - 期id
  27. * @param {Number|Array} lid - 台账节点id(可以为空)
  28. * @returns {Promise<*>}
  29. */
  30. async getLastestStageData(tid, sid, lid) {
  31. const lidSql = lid ? ' And Bills.lid in (?)' : '';
  32. const sql = 'SELECT * FROM ' + this.tableName + ' As Bills ' +
  33. ' INNER JOIN ( ' +
  34. ' SELECT MAX(`times`) As `times`, MAX(`order`) As `order`, `lid` From ' + this.tableName +
  35. ' GROUP BY `lid`' +
  36. ' ) As MaxFilter ' +
  37. ' ON Bills.times = MaxFilter.times And Bills.order = MaxFilter.order And Bills.lid = MaxFilter.lid' +
  38. ' WHERE Bills.tid = ? And Bills.sid = ?' + lidSql;
  39. const sqlParam = [tid, sid];
  40. if (!lid) {
  41. return await this.db.query(sql, sqlParam);
  42. } else if (lid instanceof Array) {
  43. sqlParam.push(lid.join(', '));
  44. return await this.db.query(sql, sqlParam);
  45. } else {
  46. sqlParam.push(lid);
  47. return await this.db.queryOne(sql, sqlParam);
  48. }
  49. }
  50. async getAuditorStageData(tid, sid, times, order, lid) {
  51. const lidSql = lid ? ' And Bills.lid in (?)' : '';
  52. const sql = 'SELECT * FROM ' + this.tableName + ' As Bills ' +
  53. ' INNER JOIN ( ' +
  54. ' SELECT MAX(`times`) As `times`, MAX(`order`) As `order`, `lid` From ' + this.tableName +
  55. ' WHERE `times` <= ? AND `order` <= ?' +
  56. ' GROUP BY `lid`' +
  57. ' ) As MaxFilter ' +
  58. ' ON Bills.times = MaxFilter.times And Bills.order = MaxFilter.order And Bills.lid = MaxFilter.lid' +
  59. ' WHERE Bills.tid = ? And Bills.sid = ?' + lidSql;
  60. const sqlParam = [times, order, tid, sid];
  61. if (!lid) {
  62. return await this.db.query(sql, sqlParam);
  63. } else if (lid instanceof Array) {
  64. sqlParam.push(lid.join(', '));
  65. return await this.db.query(sql, sqlParam);
  66. } else {
  67. sqlParam.push(lid);
  68. return await this.db.queryOne(sql, sqlParam);
  69. }
  70. }
  71. async getStageBills(tid, sid, lid) {
  72. const sql = 'SELECT Stage.*, Ledger.unit_price FROM ?? As Stage, ?? As Ledger ' +
  73. ' Where Stage.tid = ?, Stage.sid = ?, Stage.lid = ?, Stage.lid = Ledger.id ' +
  74. ' Order Stage.time DESC, Stage.order DESC ';
  75. const sqlParam = [this.tableName, this.ctx.service.ledger.tableName];
  76. sqlParam.push(this.db.escape(tid));
  77. sqlParam.push(this.db.escape(sid));
  78. sqlParam.push(this.db.escape(lid));
  79. return await this.db.queryOne(sql, sqlParam);
  80. }
  81. async _insertStageBillsData(transaction, insertData, ledgerData) {
  82. const d = {
  83. tid: this.ctx.tender.id,
  84. lid: ledgerData.id,
  85. sid: this.ctx.stage.id,
  86. times: this.ctx.stage.times,
  87. order: this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0,
  88. said: this.ctx.session.sessionUser.accountId,
  89. };
  90. if (insertData.contract_qty) {
  91. d.contract_qty = insertData.contract_qty;
  92. d.contract_tp = d.contract_qty * ledgerData.unit_price;
  93. }
  94. if (insertData.qc_qty) {
  95. d.qc_qty = insertData.qc_qty;
  96. d.qc_tp = d.qc_qty * ledgerData.unit_price;
  97. }
  98. if (insertData.postil) {
  99. d.postil = insertData.postil;
  100. }
  101. await transaction.insert(this.tableName, d);
  102. }
  103. /**
  104. * 前端提交数据
  105. * @param {Object|Array} data - 提交的数据
  106. * @returns {Promise<void>}
  107. */
  108. async updateStageData(data) {
  109. const datas = data instanceof Array ? data : [data];
  110. console.log(datas);
  111. const transaction = await this.db.beginTransaction();
  112. try {
  113. for (const d of datas) {
  114. const stageBills = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, d.lid);
  115. const ledgerBills = await this.ctx.service.ledger.getDataById(d.lid);
  116. if (d.contract_qty !== undefined) {
  117. d.contract_tp = d.contract_qty * ledgerBills.unit_price;
  118. }
  119. if (d.qc_qty !== undefined) {
  120. d.qc_tp = d.qc_qty * ledgerBills.unit_price;
  121. }
  122. console.log(d);
  123. if (!stageBills) {
  124. d.tid = this.ctx.tender.id;
  125. d.sid = this.ctx.stage.id;
  126. d.said = this.ctx.session.sessionUser.accountId;
  127. d.times = this.ctx.stage.times;
  128. d.order = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
  129. await transaction.insert(this.tableName, d);
  130. } else {
  131. d.id = stageBills.id;
  132. await transaction.update(this.tableName, d);
  133. }
  134. }
  135. await transaction.commit();
  136. } catch (err) {
  137. await transaction.rollback();
  138. throw err;
  139. }
  140. return await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, this._.map(datas, 'lid'));
  141. }
  142. /**
  143. *
  144. * @param {Number} tid - 标段id
  145. * @param {Number} id - 需要计算的节点的id
  146. * @param {Object} transaction - 操作所属事务,没有则创建
  147. * @returns {Promise<void>}
  148. */
  149. async calc(tid, sid, lid, transaction) {
  150. const stageBills = await this.getLastestStageData(tid, sid, lid);
  151. const ledgerBills = await this.ctx.service.ledger.getDataById(lid);
  152. if (!ledgerBills) {
  153. throw '提交数据错误';
  154. }
  155. const posGather = await this.ctx.service.stagePos.getPosGather(tid, sid, lid, transaction);
  156. console.log(posGather);
  157. if (!posGather) { return; }
  158. // 计算
  159. if (posGather.contract_qty !== undefined) {
  160. posGather.contract_tp = posGather.contract_qty * ledgerBills.unit_price;
  161. }
  162. if (posGather.qc_qty !== undefined) {
  163. posGather.qc_tp = posGather.qc_qty * ledgerBills.unit_price;
  164. }
  165. if (stageBills) {
  166. if (stageBills.contract_qty === posGather.contract_qty && stageBills.qc_qty === posGather.qc_qty) {
  167. return;
  168. } else {
  169. const curOrder = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
  170. if (stageBills.times === this.ctx.stage.times && stageBills.order === curOrder) {
  171. posGather.id = stageBills.id;
  172. await transaction.update(this.tableName, posGather);
  173. } else {
  174. await this._insertStageBillsData(transaction, posGather, ledgerBills);
  175. }
  176. }
  177. } else {
  178. await this._insertStageBillsData(transaction, posGather, ledgerBills);
  179. }
  180. }
  181. }
  182. return StageBills;
  183. };