pos.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. console.log(updateLid);
  121. if (updateLid.indexOf(d.lid) === -1) {
  122. updateLid.push(d.lid);
  123. }
  124. console.log(updateLid);
  125. }
  126. if (d.id) {
  127. await transaction.update(this.tableName, d);
  128. } else {
  129. this._insertPosData(transaction, d, tid);
  130. }
  131. for (const lid of updateLid) {
  132. await this.ctx.service.ledger.calc(tid, lid, transaction);
  133. }
  134. }
  135. await transaction.commit();
  136. } catch (err) {
  137. await transaction.rollback();
  138. throw err;
  139. }
  140. result.pos = data;
  141. if (updateLid.length > 0) {
  142. result.ledger.update = await this.ctx.service.ledger.getDataByIds(updateLid);
  143. }
  144. return result;
  145. }
  146. /**
  147. * 删除清单下部位明细数据(删除清单时调用)
  148. *
  149. * @param transaction - 事务
  150. * @param tid - 标段id
  151. * @param lid - 清单id
  152. * @returns {Promise<void>}
  153. */
  154. async deletePosData(transaction, tid, lid) {
  155. await transaction.delete(this.tableName, {tid: tid, lid: lid});
  156. }
  157. /**
  158. * 复制整块 拷贝部位明细数据
  159. * @param {Number} orgLid - 拷贝的部位明细所属台账id
  160. * @param {Number} newLid - 新的台账id
  161. * @param transaction - 复制整块事务
  162. * @returns {Promise<void>}
  163. */
  164. async copyBillsPosData(orgLid, newLid, transaction) {
  165. const posData = await this.getAllDataByCondition({ where: { lid: orgLid } });
  166. if (posData.length > 0) {
  167. for (const pd of posData) {
  168. delete pd.id;
  169. pd.lid = newLid;
  170. }
  171. await transaction.insert(this.tableName, posData);
  172. }
  173. }
  174. /**
  175. * 批量插入部位数据 - 仅供批量插入清单部位调用
  176. * @param transaction - 所属事务
  177. * @param {Number} tid - 标段id
  178. * @param {Number} lid - 台账id
  179. * @param {Array} data - 新增数据
  180. * @returns {Promise<void>}
  181. */
  182. async insertLedgerPosData(transaction, tid, bills, data) {
  183. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  184. for (const d of data) {
  185. d.tid = tid;
  186. d.lid = bills.id;
  187. // todo 新增期
  188. d.add_stage = 0;
  189. d.add_times = 0;
  190. d.add_user = this.ctx.session.sessionUser.accountId;
  191. if (d.quantity) {
  192. d.quantity = this._.round(d.quantity, precision.value);
  193. }
  194. }
  195. await transaction.insert(this.tableName, data);
  196. }
  197. }
  198. return Pos;
  199. };