stage_change.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. * @param {Object} target - 台账|部位
  43. * @param {Array} changes - 调用的变更令
  44. * @returns {Promise<void>}
  45. */
  46. async billsChange(bills, changes) {
  47. const self = this;
  48. function getNewChange(cid, cbid, times, order, qty) {
  49. return {
  50. tid: self.ctx.tender.id,
  51. sid: self.ctx.stage.id,
  52. lid: bills.id,
  53. pid: -1,
  54. cid: cid,
  55. cbid: cbid,
  56. stimes: times,
  57. sorder: order,
  58. qty: qty
  59. }
  60. }
  61. const ledgerBills = await this.ctx.service.ledger.getDataById(bills.id);
  62. if (!ledgerBills || ledgerBills.tender_id !== this.ctx.tender.id) {
  63. throw '提交数据错误';
  64. }
  65. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
  66. // 获取原变更令
  67. const oldChanges = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, bills.id, -1);
  68. const order = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
  69. // 获取更新数据
  70. const updateChanges = [], newChanges = [];
  71. let billsQty = 0;
  72. for (const oc of oldChanges) {
  73. const nc = this._.find(changes, {cid: oc.cid, cbid: oc.cbid});
  74. if (!nc || nc.qty !== oc.qty) {
  75. const qty = nc ? this.round(nc.qty, precision.value) : null;
  76. const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.times, order, qty);
  77. billsQty = this.ctx.helper.plus(billsQty, change.qty);
  78. if (oc.stimes === this.ctx.stage.times && oc.sorder === order) {
  79. change.id = oc.id;
  80. updateChanges.push(change);
  81. } else {
  82. newChanges.push(change);
  83. }
  84. } else {
  85. billsQty = this.ctx.helper.plus(billsQty, oc.qty);
  86. }
  87. }
  88. for (const c of changes) {
  89. const nc = this._.find(oldChanges, {cid: c.cid, cbid: c.cbid});
  90. if (!nc) {
  91. const change = getNewChange(c.cid, c.cbid, this.ctx.stage.times, order, this.round(c.qty, precision.value));
  92. billsQty = this.ctx.helper.plus(billsQty, change.qty);
  93. newChanges.push(change);
  94. }
  95. }
  96. // 更新数据
  97. const transaction = await this.db.beginTransaction();
  98. try {
  99. if (newChanges.length > 0) {
  100. await transaction.insert(this.tableName, newChanges);
  101. }
  102. for (const c of updateChanges) {
  103. await transaction.update(this.tableName, c);
  104. }
  105. const stageBills = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, bills.id);
  106. await this.ctx.service.stageBills.updateStageBillsQty(transaction, ledgerBills, stageBills, { qc_qty: billsQty });
  107. await transaction.commit();
  108. } catch (err) {
  109. await transaction.rollback();
  110. throw err;
  111. }
  112. const result = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [bills.id]);
  113. return { bills: result };
  114. }
  115. async posChange(pos, changes) {
  116. const self = this;
  117. function getNewChange(cid, cbid, times, order, qty) {
  118. return {
  119. tid: self.ctx.tender.id,
  120. sid: self.ctx.stage.id,
  121. lid: pos.lid,
  122. pid: pos.id,
  123. cid: cid,
  124. cbid: cbid,
  125. stimes: times,
  126. sorder: order,
  127. qty: qty
  128. }
  129. }
  130. const ledgerBills = await this.ctx.service.ledger.getDataById(pos.lid);
  131. if (!ledgerBills || ledgerBills.tender_id !== this.ctx.tender.id) {
  132. throw '提交数据错误';
  133. }
  134. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
  135. // 获取原变更令
  136. const oldChanges = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, pos.lid, pos.id);
  137. const updateChanges = [], newChanges = [];
  138. let posQty = 0;
  139. for (const oc of oldChanges) {
  140. const nc = this._.find(changes, {cid: oc.cid, cbid: oc.cbid});
  141. if (!nc || nc.qty !== oc.qty) {
  142. const qty = nc ? this.round(nc.qty, precision.value) : null;
  143. const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.times, this.ctx.stage.flowOrder, qty);
  144. posQty = this.ctx.helper.plus(posQty, change.qty);
  145. if (oc.stimes === this.ctx.stage.times && oc.sorder === this.ctx.stage.flowOrder) {
  146. change.id = oc.id;
  147. updateChanges.push(change);
  148. } else {
  149. newChanges.push(change);
  150. }
  151. } else {
  152. posQty = this.ctx.helper.plus(posQty, oc.qty);
  153. }
  154. }
  155. for (const c of changes) {
  156. const nc = this._.find(oldChanges, {cid: c.cid, cbid: c.cbid});
  157. if (!nc) {
  158. const change = getNewChange(c.cid, c.cbid, this.ctx.stage.times, this.ctx.stage.flowOrder, this.round(c.qty, precision.value));
  159. posQty = this.ctx.helper.plus(posQty, change.qty);
  160. newChanges.push(change);
  161. }
  162. }
  163. // 更新数据
  164. const transaction = await this.db.beginTransaction();
  165. try {
  166. if (newChanges.length > 0) {
  167. await transaction.insert(this.tableName, newChanges);
  168. }
  169. for (const c of updateChanges) {
  170. await transaction.update(this.tableName, c);
  171. }
  172. await this.ctx.service.stagePos.updateChangeQuantity(transaction, pos, posQty);
  173. await transaction.commit();
  174. } catch (err) {
  175. await transaction.rollback();
  176. throw err;
  177. }
  178. // 获取返回数据
  179. try {
  180. const data = {};
  181. data.bills = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [pos.lid]);
  182. data.pos = await this.ctx.service.stagePos.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [pos.id])
  183. return data;
  184. } catch(err) {
  185. throw '获取数据错误,请刷新页面';
  186. }
  187. }
  188. }
  189. return StageChange;
  190. };