stage_change.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. 'use strict';
  2. /**
  3. * 期 - 变更数据
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const defaultPid = -1; // 非pid
  10. module.exports = app => {
  11. class StageChange extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'stage_change';
  21. }
  22. /**
  23. * 查询 调用的变更令
  24. * @param {Number} tid - 标段id
  25. * @param {Number} sid - 期id
  26. * @param {Number} lid - 台账节点id
  27. * @param {Number} pid - 部位明细id
  28. * @returns {Promise<*>}
  29. */
  30. async getLastestStageData(tid, sid, lid, pid) {
  31. const sql = 'SELECT c.* FROM ' + this.tableName + ' As c ' +
  32. ' INNER JOIN ( ' +
  33. ' SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid` From ' + this.tableName +
  34. ' WHERE tid = ? And sid = ? And lid = ? And pid = ?' +
  35. ' ) As m ' +
  36. ' ON c.stimes = m.stimes And c.sorder = m.sorder And c.lid = m.lid And c.pid = m.pid';
  37. const sqlParam = [tid, sid, lid, pid ? pid : -1];
  38. return await this.db.query(sql, sqlParam);
  39. }
  40. /**
  41. * 台账,调用变更令
  42. *
  43. * @param {Object} bills - 台账节点数据
  44. * @param {Array} changes - 调用的变更令
  45. * @returns {Promise<void>}
  46. */
  47. async billsChange(bills, changes) {
  48. const self = this;
  49. function getNewChange(cid, cbid, times, order, qty) {
  50. return {
  51. tid: self.ctx.tender.id,
  52. sid: self.ctx.stage.id,
  53. lid: bills.id,
  54. pid: -1,
  55. cid: cid,
  56. cbid: cbid,
  57. stimes: times,
  58. sorder: order,
  59. qty: qty
  60. }
  61. }
  62. const ledgerBills = await this.ctx.service.ledger.getDataById(bills.id);
  63. if (!ledgerBills || ledgerBills.tender_id !== this.ctx.tender.id) {
  64. throw '提交数据错误';
  65. }
  66. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
  67. // 获取原变更令
  68. const oldChanges = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, bills.id, -1);
  69. const order = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
  70. // 获取更新数据
  71. const updateChanges = [], newChanges = [];
  72. let billsQty = 0;
  73. for (const oc of oldChanges) {
  74. const nc = this._.find(changes, {cid: oc.cid, cbid: oc.cbid});
  75. if (!nc || nc.qty !== oc.qty) {
  76. const qty = nc ? this.round(nc.qty, precision.value) : null;
  77. const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.times, order, qty);
  78. billsQty = this.ctx.helper.plus(billsQty, change.qty);
  79. if (oc.stimes === this.ctx.stage.times && oc.sorder === order) {
  80. change.id = oc.id;
  81. updateChanges.push(change);
  82. } else {
  83. newChanges.push(change);
  84. }
  85. } else {
  86. billsQty = this.ctx.helper.plus(billsQty, oc.qty);
  87. }
  88. }
  89. for (const c of changes) {
  90. const nc = this._.find(oldChanges, {cid: c.cid, cbid: c.cbid});
  91. if (!nc) {
  92. const change = getNewChange(c.cid, c.cbid, this.ctx.stage.times, order, this.round(c.qty, precision.value));
  93. billsQty = this.ctx.helper.plus(billsQty, change.qty);
  94. newChanges.push(change);
  95. }
  96. }
  97. // 更新数据
  98. const transaction = await this.db.beginTransaction();
  99. try {
  100. if (newChanges.length > 0) {
  101. await transaction.insert(this.tableName, newChanges);
  102. }
  103. for (const c of updateChanges) {
  104. await transaction.update(this.tableName, c);
  105. }
  106. const stageBills = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, bills.id);
  107. await this.ctx.service.stageBills.updateStageBillsQty(transaction, ledgerBills, stageBills, { qc_qty: billsQty });
  108. await transaction.commit();
  109. } catch (err) {
  110. await transaction.rollback();
  111. throw err;
  112. }
  113. const result = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [bills.id]);
  114. return { bills: result };
  115. }
  116. /**
  117. * 部位明细,调用变更令
  118. *
  119. * @param {Object} pos - 部位明细数据
  120. * @param {Array} changes - 调用的变更令
  121. * @returns {Promise<{}>}
  122. */
  123. async posChange(pos, changes) {
  124. const self = this;
  125. function getNewChange(cid, cbid, times, order, qty) {
  126. return {
  127. tid: self.ctx.tender.id,
  128. sid: self.ctx.stage.id,
  129. lid: pos.lid,
  130. pid: pos.id,
  131. cid: cid,
  132. cbid: cbid,
  133. stimes: times,
  134. sorder: order,
  135. qty: qty
  136. }
  137. }
  138. const ledgerBills = await this.ctx.service.ledger.getDataById(pos.lid);
  139. if (!ledgerBills || ledgerBills.tender_id !== this.ctx.tender.id) {
  140. throw '提交数据错误';
  141. }
  142. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
  143. // 获取原变更令
  144. const oldChanges = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, pos.lid, pos.id);
  145. const updateChanges = [], newChanges = [];
  146. let posQty = 0;
  147. for (const oc of oldChanges) {
  148. const nc = this._.find(changes, {cid: oc.cid, cbid: oc.cbid});
  149. if (!nc || nc.qty !== oc.qty) {
  150. const qty = nc ? this.round(nc.qty, precision.value) : null;
  151. const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.times, this.ctx.stage.flowOrder, qty);
  152. posQty = this.ctx.helper.plus(posQty, change.qty);
  153. if (oc.stimes === this.ctx.stage.times && oc.sorder === this.ctx.stage.flowOrder) {
  154. change.id = oc.id;
  155. updateChanges.push(change);
  156. } else {
  157. newChanges.push(change);
  158. }
  159. } else {
  160. posQty = this.ctx.helper.plus(posQty, oc.qty);
  161. }
  162. }
  163. for (const c of changes) {
  164. const nc = this._.find(oldChanges, {cid: c.cid, cbid: c.cbid});
  165. if (!nc) {
  166. const change = getNewChange(c.cid, c.cbid, this.ctx.stage.times, this.ctx.stage.flowOrder, this.round(c.qty, precision.value));
  167. posQty = this.ctx.helper.plus(posQty, change.qty);
  168. newChanges.push(change);
  169. }
  170. }
  171. // 更新数据
  172. const transaction = await this.db.beginTransaction();
  173. try {
  174. if (newChanges.length > 0) {
  175. await transaction.insert(this.tableName, newChanges);
  176. }
  177. for (const c of updateChanges) {
  178. await transaction.update(this.tableName, c);
  179. }
  180. await this.ctx.service.stagePos.updateChangeQuantity(transaction, pos, posQty);
  181. await transaction.commit();
  182. } catch (err) {
  183. await transaction.rollback();
  184. throw err;
  185. }
  186. // 获取返回数据
  187. try {
  188. const data = {};
  189. data.bills = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [pos.lid]);
  190. data.pos = await this.ctx.service.stagePos.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [pos.id])
  191. return data;
  192. } catch(err) {
  193. throw '获取数据错误,请刷新页面';
  194. }
  195. }
  196. }
  197. return StageChange;
  198. };