pos.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. 'use strict';
  2. /**
  3. * 部位明细
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class Pos extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'pos';
  20. }
  21. async getPosData(condition) {
  22. return await this.db.select(this.tableName, {
  23. where: condition,
  24. columns: ['id', 'tid', 'lid', 'name', 'quantity', 'drawing_code'],
  25. });
  26. }
  27. async _insertPosData(transaction, data, tid) {
  28. data.tid = tid;
  29. // todo 新增期
  30. data.add_stage = 0;
  31. data.add_times = 0;
  32. data.add_user = this.ctx.session.sessionUser.accountId;
  33. if (data.quantity) {
  34. const bills = await this.ctx.service.ledger.getDataById(data.lid);
  35. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  36. data.quantity = this._.round(data.quantity, precision.value);
  37. }
  38. const addRst = await transaction.insert(this.tableName, data);
  39. data.id = addRst.insertId;
  40. }
  41. /**
  42. * 保存部位明细数据
  43. * @param data
  44. * @param {Number} tid - 标段id
  45. * @returns {Promise<{ledger: {}, pos: null}>}
  46. */
  47. async savePosData(data, tid) {
  48. const transaction = await this.db.beginTransaction();
  49. try {
  50. const result = { ledger: {}, pos: null };
  51. if (data.updateType === 'add') {
  52. const tender = await this.ctx.service.tender.getTender(tid);
  53. if (data.updateData instanceof Array) {
  54. for (const d of data.updateData) {
  55. this._insertPosData(transaction, d, tid);
  56. }
  57. } else {
  58. this._insertPosData(transaction, data.updateData, tid);
  59. }
  60. } else if (data.updateType === 'update') {
  61. const datas = data.updateData instanceof Array ? data.updateData : [data.updateData];
  62. result.ledger.update = [];
  63. const orgPos = await this.getPosData({tid: tid, id: this._.map(datas, 'id')});
  64. for (const d of datas) {
  65. const op = this._.find(orgPos, function (p) { return p.id = d.id; });
  66. if (d.quantity) {
  67. const bills = await this.ctx.service.ledger.getDataById(op.lid);
  68. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  69. d.quantity = this._.round(d.quantity, precision.value);
  70. }
  71. await transaction.update(this.tableName, d, {tid: tid, id: d.id});
  72. if (d.quantity && op && (result.ledger.update.indexOf(op.lid) === -1)) {
  73. result.ledger.update.push(op.lid);
  74. }
  75. }
  76. for (const lid of result.ledger.update) {
  77. await this.ctx.service.ledger.calc(tid, lid, transaction);
  78. }
  79. } else if (data.updateType === 'delete') {
  80. if (!data.updateData || data.updateData.length === 0) {
  81. throw '提交数据错误';
  82. }
  83. const pos = await this.getPosData({tid: tid, id: data.updateData});
  84. const ledgerIds = this._.map(pos, 'lid');
  85. await transaction.delete(this.tableName, {tid: tid, id: data.updateData});
  86. for (const lid of ledgerIds) {
  87. await this.ctx.service.ledger.calc(tid, lid, transaction);
  88. }
  89. result.ledger.update = ledgerIds;
  90. } else {
  91. throw '提交数据错误';
  92. }
  93. await transaction.commit();
  94. result.pos = data.updateData;
  95. result.ledger.update = await this.ctx.service.ledger.getDataByIds(result.ledger.update);
  96. return result;
  97. } catch (err) {
  98. await transaction.rollback();
  99. throw err;
  100. }
  101. }
  102. /**
  103. * 复制粘贴 部位明细数据
  104. * @param {Array} data - 复制粘贴的数据
  105. * @param {Number} tid - 标段id
  106. * @returns {Promise<{ledger: {}, pos: null}>}
  107. */
  108. async pastePosData(data, tid) {
  109. if (!(data instanceof Array)) {
  110. throw '提交数据错误';
  111. }
  112. const transaction = await this.db.beginTransaction();
  113. const result = { ledger: {}, pos: null }, updateLid = [];
  114. try {
  115. for (const d of data) {
  116. if (d.quantity) {
  117. const bills = await this.ctx.service.ledger.getDataById(d.lid);
  118. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  119. d.quantity = this._.round(d.quantity, precision.value);
  120. if (updateLid.indexOf(d.lid) === -1) {
  121. updateLid.push(d.lid);
  122. }
  123. }
  124. if (d.id) {
  125. await transaction.update(this.tableName, d);
  126. } else {
  127. this._insertPosData(transaction, d, tid);
  128. }
  129. for (const lid of updateLid) {
  130. await this.ctx.service.ledger.calc(tid, lid, transaction);
  131. }
  132. }
  133. await transaction.commit();
  134. } catch (err) {
  135. await transaction.rollback();
  136. throw err;
  137. }
  138. result.pos = data;
  139. if (updateLid.length > 0) {
  140. result.ledger.update = await this.ctx.service.ledger.getDataByIds(updateLid);
  141. }
  142. return result;
  143. }
  144. /**
  145. * 删除清单下部位明细数据(删除清单时调用)
  146. *
  147. * @param transaction - 事务
  148. * @param tid - 标段id
  149. * @param lid - 清单id
  150. * @returns {Promise<void>}
  151. */
  152. async deletePosData(transaction, tid, lid) {
  153. await transaction.delete(this.tableName, {tid: tid, lid: lid});
  154. }
  155. /**
  156. * 复制整块 拷贝部位明细数据
  157. * @param {Number} orgLid - 拷贝的部位明细所属台账id
  158. * @param {Number} newLid - 新的台账id
  159. * @param transaction - 复制整块事务
  160. * @returns {Promise<void>}
  161. */
  162. async copyBillsPosData(orgLid, newLid, transaction) {
  163. const posData = await this.getAllDataByCondition({ where: { lid: orgLid } });
  164. if (posData.length > 0) {
  165. for (const pd of posData) {
  166. delete pd.id;
  167. pd.lid = newLid;
  168. }
  169. await transaction.insert(this.tableName, posData);
  170. }
  171. }
  172. /**
  173. * 批量插入部位数据 - 仅供批量插入清单部位调用
  174. * @param transaction - 所属事务
  175. * @param {Number} tid - 标段id
  176. * @param {Number} lid - 台账id
  177. * @param {Array} data - 新增数据
  178. * @returns {Promise<void>}
  179. */
  180. async insertLedgerPosData(transaction, tid, bills, data) {
  181. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  182. for (const d of data) {
  183. d.tid = tid;
  184. d.lid = bills.id;
  185. // todo 新增期
  186. d.add_stage = 0;
  187. d.add_times = 0;
  188. d.add_user = this.ctx.session.sessionUser.accountId;
  189. if (d.quantity) {
  190. d.quantity = this._.round(d.quantity, precision.value);
  191. }
  192. }
  193. await transaction.insert(this.tableName, data);
  194. }
  195. }
  196. return Pos;
  197. };