stage_bills.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, updateNode.unit);
  91. if (insertData.contract_qty) {
  92. d.contract_qty = this.round(insertData.contract_qty, precision.value);
  93. d.contract_tp = d.contract_qty * ledgerData.unit_price;
  94. }
  95. if (insertData.qc_qty) {
  96. d.qc_qty = this.round(insertData.qc_qty, precision.value);
  97. d.qc_tp = d.qc_qty * ledgerData.unit_price;
  98. }
  99. if (insertData.postil) {
  100. d.postil = insertData.postil;
  101. }
  102. await transaction.insert(this.tableName, d);
  103. }
  104. /**
  105. * 前端提交数据
  106. * @param {Object|Array} data - 提交的数据
  107. * @returns {Promise<void>}
  108. */
  109. async updateStageData(data) {
  110. const datas = data instanceof Array ? data : [data];
  111. console.log(datas);
  112. const transaction = await this.db.beginTransaction();
  113. try {
  114. for (const d of datas) {
  115. const stageBills = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, d.lid);
  116. const ledgerBills = await this.ctx.service.ledger.getDataById(d.lid);
  117. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
  118. if (d.contract_qty !== undefined) {
  119. d.contract_qty = this.round(d.contract_qty, precision.value);
  120. d.contract_tp = d.contract_qty * ledgerBills.unit_price;
  121. }
  122. if (d.qc_qty !== undefined) {
  123. d.qc_qty = this.round(d.qc_qty, precision.value);
  124. d.qc_tp = d.qc_qty * ledgerBills.unit_price;
  125. }
  126. console.log(d);
  127. if (!stageBills) {
  128. d.tid = this.ctx.tender.id;
  129. d.sid = this.ctx.stage.id;
  130. d.said = this.ctx.session.sessionUser.accountId;
  131. d.times = this.ctx.stage.times;
  132. d.order = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
  133. await transaction.insert(this.tableName, d);
  134. } else {
  135. d.id = stageBills.id;
  136. await transaction.update(this.tableName, d);
  137. }
  138. }
  139. await transaction.commit();
  140. } catch (err) {
  141. await transaction.rollback();
  142. throw err;
  143. }
  144. return await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, this._.map(datas, 'lid'));
  145. }
  146. /**
  147. *
  148. * @param {Number} tid - 标段id
  149. * @param {Number} id - 需要计算的节点的id
  150. * @param {Object} transaction - 操作所属事务,没有则创建
  151. * @returns {Promise<void>}
  152. */
  153. async calc(tid, sid, lid, transaction) {
  154. const stageBills = await this.getLastestStageData(tid, sid, lid);
  155. const ledgerBills = await this.ctx.service.ledger.getDataById(lid);
  156. if (!ledgerBills) {
  157. throw '提交数据错误';
  158. }
  159. const posGather = await this.ctx.service.stagePos.getPosGather(tid, sid, lid, transaction);
  160. console.log(posGather);
  161. if (!posGather) { return; }
  162. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
  163. // 计算
  164. if (posGather.contract_qty !== undefined) {
  165. posGather.contract_qty = this.round(posGather.contract_qty, precision.value);
  166. posGather.contract_tp = posGather.contract_qty * ledgerBills.unit_price;
  167. }
  168. if (posGather.qc_qty !== undefined) {
  169. posGather.qc_qty = this.round(posGather.qc_qty, precision.value);
  170. posGather.qc_tp = posGather.qc_qty * ledgerBills.unit_price;
  171. }
  172. if (stageBills) {
  173. if (stageBills.contract_qty === posGather.contract_qty && stageBills.qc_qty === posGather.qc_qty) {
  174. return;
  175. } else {
  176. const curOrder = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
  177. if (stageBills.times === this.ctx.stage.times && stageBills.order === curOrder) {
  178. posGather.id = stageBills.id;
  179. await transaction.update(this.tableName, posGather);
  180. } else {
  181. await this._insertStageBillsData(transaction, posGather, ledgerBills);
  182. }
  183. }
  184. } else {
  185. await this._insertStageBillsData(transaction, posGather, ledgerBills);
  186. }
  187. }
  188. }
  189. return StageBills;
  190. };