pos.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /**
  28. * 保存部位明细数据
  29. * @param data
  30. * @param {Number} tid - 标段id
  31. * @returns {Promise<{ledger: {}, pos: null}>}
  32. */
  33. async savePosData(data, tid) {
  34. const transaction = await this.db.beginTransaction();
  35. try {
  36. const result = { ledger: {}, pos: null };
  37. if (data.updateType === 'add') {
  38. const tender = await this.ctx.service.tender.getTender(tid);
  39. data.updateData.tid = tid;
  40. // todo 新增期
  41. data.updateData.add_stage = 0;
  42. data.updateData.add_times = 0;
  43. data.updateData.add_user = this.ctx.session.sessionUser.accountId;
  44. if (data.quantity) {
  45. const bills = await this.ctx.service.ledger.getDataById(data.lid);
  46. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  47. data.quantity = this._.round(data.quantity, precision.value);
  48. }
  49. const addRst = await transaction.insert(this.tableName, data.updateData);
  50. data.updateData.id = addRst.insertId;
  51. } else if (data.updateType === 'update') {
  52. const datas = data.updateData instanceof Array ? data.updateData : [data.updateData];
  53. result.ledger.update = [];
  54. const orgPos = await this.getPosData({tid: tid, id: this._.map(datas, 'id')});
  55. for (const d of datas) {
  56. const op = this._.find(orgPos, function (p) { return p.id = d.id; });
  57. if (d.quantity) {
  58. const bills = await this.ctx.service.ledger.getDataById(op.lid);
  59. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  60. d.quantity = this._.round(d.quantity, precision.value);
  61. }
  62. await transaction.update(this.tableName, d, {tid: tid, id: d.id});
  63. if (d.quantity && op && (result.ledger.update.indexOf(op.lid) === -1)) {
  64. result.ledger.update.push(op.lid);
  65. }
  66. }
  67. for (const lid of result.ledger.update) {
  68. await this.ctx.service.ledger.calc(tid, lid, transaction);
  69. }
  70. } else if (data.updateType === 'delete') {
  71. if (!data.updateData || data.updateData.length === 0) {
  72. throw '提交数据错误';
  73. }
  74. const pos = await this.getPosData({tid: tid, id: data.updateData});
  75. const ledgerIds = this._.map(pos, 'lid');
  76. await transaction.delete(this.tableName, {tid: tid, id: data.updateData});
  77. for (const lid of ledgerIds) {
  78. await this.ctx.service.ledger.calc(tid, lid, transaction);
  79. }
  80. result.ledger.update = ledgerIds;
  81. } else {
  82. throw '提交数据错误';
  83. }
  84. await transaction.commit();
  85. result.pos = data.updateData;
  86. result.ledger.update = await this.ctx.service.ledger.getDataByIds(result.ledger.update);
  87. return result;
  88. } catch (err) {
  89. await transaction.rollback();
  90. throw err;
  91. }
  92. }
  93. /**
  94. * 删除清单下部位明细数据(删除清单时调用)
  95. *
  96. * @param transaction - 事务
  97. * @param tid - 标段id
  98. * @param lid - 清单id
  99. * @returns {Promise<void>}
  100. */
  101. async deletePosData(transaction, tid, lid) {
  102. await transaction.delete(this.tableName, {tid: tid, lid: lid});
  103. }
  104. /**
  105. * 复制整块 拷贝部位明细数据
  106. * @param {Number} orgLid - 拷贝的部位明细所属台账id
  107. * @param {Number} newLid - 新的台账id
  108. * @param transaction - 复制整块事务
  109. * @returns {Promise<void>}
  110. */
  111. async copyBillsPosData(orgLid, newLid, transaction) {
  112. const posData = await this.getAllDataByCondition({ where: { lid: orgLid } });
  113. if (posData.length > 0) {
  114. for (const pd of posData) {
  115. delete pd.id;
  116. pd.lid = newLid;
  117. }
  118. await transaction.insert(this.tableName, posData);
  119. }
  120. }
  121. /**
  122. * 批量插入部位数据 - 仅供批量插入清单部位调用
  123. * @param transaction - 所属事务
  124. * @param {Number} tid - 标段id
  125. * @param {Number} lid - 台账id
  126. * @param {Array} data - 新增数据
  127. * @returns {Promise<void>}
  128. */
  129. async insertLedgerPosData(transaction, tid, lid, data) {
  130. const bills = await this.ctx.service.ledger.getDataById(lid);
  131. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  132. for (const d of data) {
  133. d.tid = tid;
  134. d.lid = lid;
  135. // todo 新增期
  136. d.add_stage = 0;
  137. d.add_times = 0;
  138. d.add_user = this.ctx.session.sessionUser.accountId;
  139. if (d.quantity) {
  140. d.quantity = this._.round(d.quantity, precision.value);
  141. }
  142. }
  143. await transaction.insert(this.tableName, data);
  144. }
  145. }
  146. return Pos;
  147. };