pos.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 !== undefined && 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. console.log(ledgerIds);
  86. await transaction.delete(this.tableName, {tid: tid, id: data.updateData});
  87. for (const lid of ledgerIds) {
  88. await this.ctx.service.ledger.calc(tid, lid, transaction);
  89. }
  90. result.ledger.update = ledgerIds;
  91. } else {
  92. throw '提交数据错误';
  93. }
  94. await transaction.commit();
  95. result.pos = data.updateData;
  96. result.ledger.update = await this.ctx.service.ledger.getDataByIds(result.ledger.update);
  97. return result;
  98. } catch (err) {
  99. await transaction.rollback();
  100. throw err;
  101. }
  102. }
  103. /**
  104. * 复制粘贴 部位明细数据
  105. * @param {Array} data - 复制粘贴的数据
  106. * @param {Number} tid - 标段id
  107. * @returns {Promise<{ledger: {}, pos: null}>}
  108. */
  109. async pastePosData(data, tid) {
  110. if (!(data instanceof Array)) {
  111. throw '提交数据错误';
  112. }
  113. const transaction = await this.db.beginTransaction();
  114. const result = { ledger: {}, pos: null }, updateLid = [];
  115. try {
  116. for (const d of data) {
  117. if (d.quantity) {
  118. const bills = await this.ctx.service.ledger.getDataById(d.lid);
  119. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  120. d.quantity = this._.round(d.quantity, precision.value);
  121. if (updateLid.indexOf(d.lid) === -1) {
  122. updateLid.push(d.lid);
  123. }
  124. }
  125. if (d.id) {
  126. await transaction.update(this.tableName, d);
  127. } else {
  128. this._insertPosData(transaction, d, tid);
  129. }
  130. for (const lid of updateLid) {
  131. await this.ctx.service.ledger.calc(tid, lid, transaction);
  132. }
  133. }
  134. await transaction.commit();
  135. } catch (err) {
  136. await transaction.rollback();
  137. throw err;
  138. }
  139. result.pos = data;
  140. if (updateLid.length > 0) {
  141. result.ledger.update = await this.ctx.service.ledger.getDataByIds(updateLid);
  142. }
  143. return result;
  144. }
  145. /**
  146. * 删除清单下部位明细数据(删除清单时调用)
  147. *
  148. * @param transaction - 事务
  149. * @param tid - 标段id
  150. * @param lid - 清单id
  151. * @returns {Promise<void>}
  152. */
  153. async deletePosData(transaction, tid, lid) {
  154. await transaction.delete(this.tableName, {tid: tid, lid: lid});
  155. }
  156. /**
  157. * 复制整块 拷贝部位明细数据
  158. * @param {Number} orgLid - 拷贝的部位明细所属台账id
  159. * @param {Number} newLid - 新的台账id
  160. * @param transaction - 复制整块事务
  161. * @returns {Promise<void>}
  162. */
  163. async copyBillsPosData(orgLid, newLid, transaction) {
  164. const posData = await this.getAllDataByCondition({ where: { lid: orgLid } });
  165. if (posData.length > 0) {
  166. for (const pd of posData) {
  167. delete pd.id;
  168. pd.lid = newLid;
  169. }
  170. await transaction.insert(this.tableName, posData);
  171. }
  172. }
  173. /**
  174. * 批量插入部位数据 - 仅供批量插入清单部位调用
  175. * @param transaction - 所属事务
  176. * @param {Number} tid - 标段id
  177. * @param {Number} lid - 台账id
  178. * @param {Array} data - 新增数据
  179. * @returns {Promise<void>}
  180. */
  181. async insertLedgerPosData(transaction, tid, bills, data) {
  182. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  183. for (const d of data) {
  184. d.tid = tid;
  185. d.lid = bills.id;
  186. // todo 新增期
  187. d.add_stage = 0;
  188. d.add_times = 0;
  189. d.add_user = this.ctx.session.sessionUser.accountId;
  190. if (d.quantity) {
  191. d.quantity = this._.round(d.quantity, precision.value);
  192. }
  193. }
  194. await transaction.insert(this.tableName, data);
  195. }
  196. }
  197. return Pos;
  198. };