pos.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. console.log(data);
  49. const transaction = await this.db.beginTransaction();
  50. try {
  51. const result = { ledger: {}, pos: null };
  52. if (data.updateType === 'add') {
  53. const tender = await this.ctx.service.tender.getTender(tid);
  54. if (data.updateData instanceof Array) {
  55. for (const d of data.updateData) {
  56. this._insertPosData(transaction, d, tid);
  57. }
  58. } else {
  59. this._insertPosData(transaction, data.updateData, tid);
  60. }
  61. } else if (data.updateType === 'update') {
  62. const datas = data.updateData instanceof Array ? data.updateData : [data.updateData];
  63. result.ledger.update = [];
  64. const orgPos = await this.getPosData({tid: tid, id: this._.map(datas, 'id')});
  65. for (const d of datas) {
  66. const op = this._.find(orgPos, function (p) { return p.id = d.id; });
  67. if (d.quantity) {
  68. const bills = await this.ctx.service.ledger.getDataById(op.lid);
  69. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  70. d.quantity = this._.round(d.quantity, precision.value);
  71. }
  72. await transaction.update(this.tableName, d, {tid: tid, id: d.id});
  73. if (d.quantity && op && (result.ledger.update.indexOf(op.lid) === -1)) {
  74. result.ledger.update.push(op.lid);
  75. }
  76. }
  77. for (const lid of result.ledger.update) {
  78. await this.ctx.service.ledger.calc(tid, lid, transaction);
  79. }
  80. } else if (data.updateType === 'delete') {
  81. if (!data.updateData || data.updateData.length === 0) {
  82. throw '提交数据错误';
  83. }
  84. const pos = await this.getPosData({tid: tid, id: data.updateData});
  85. const ledgerIds = this._.map(pos, 'lid');
  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. console.log(updateLid);
  122. if (updateLid.indexOf(d.lid) === -1) {
  123. updateLid.push(d.lid);
  124. }
  125. console.log(updateLid);
  126. }
  127. if (d.id) {
  128. await transaction.update(this.tableName, d);
  129. } else {
  130. this._insertPosData(transaction, d, tid);
  131. }
  132. for (const lid of updateLid) {
  133. await this.ctx.service.ledger.calc(tid, lid, transaction);
  134. }
  135. }
  136. await transaction.commit();
  137. } catch (err) {
  138. await transaction.rollback();
  139. throw err;
  140. }
  141. result.pos = data;
  142. if (updateLid.length > 0) {
  143. result.ledger.update = await this.ctx.service.ledger.getDataByIds(updateLid);
  144. }
  145. return result;
  146. }
  147. /**
  148. * 删除清单下部位明细数据(删除清单时调用)
  149. *
  150. * @param transaction - 事务
  151. * @param tid - 标段id
  152. * @param lid - 清单id
  153. * @returns {Promise<void>}
  154. */
  155. async deletePosData(transaction, tid, lid) {
  156. await transaction.delete(this.tableName, {tid: tid, lid: lid});
  157. }
  158. /**
  159. * 复制整块 拷贝部位明细数据
  160. * @param {Number} orgLid - 拷贝的部位明细所属台账id
  161. * @param {Number} newLid - 新的台账id
  162. * @param transaction - 复制整块事务
  163. * @returns {Promise<void>}
  164. */
  165. async copyBillsPosData(orgLid, newLid, transaction) {
  166. const posData = await this.getAllDataByCondition({ where: { lid: orgLid } });
  167. if (posData.length > 0) {
  168. for (const pd of posData) {
  169. delete pd.id;
  170. pd.lid = newLid;
  171. }
  172. await transaction.insert(this.tableName, posData);
  173. }
  174. }
  175. /**
  176. * 批量插入部位数据 - 仅供批量插入清单部位调用
  177. * @param transaction - 所属事务
  178. * @param {Number} tid - 标段id
  179. * @param {Number} lid - 台账id
  180. * @param {Array} data - 新增数据
  181. * @returns {Promise<void>}
  182. */
  183. async insertLedgerPosData(transaction, tid, bills, data) {
  184. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  185. for (const d of data) {
  186. d.tid = tid;
  187. d.lid = bills.id;
  188. // todo 新增期
  189. d.add_stage = 0;
  190. d.add_times = 0;
  191. d.add_user = this.ctx.session.sessionUser.accountId;
  192. if (d.quantity) {
  193. d.quantity = this._.round(d.quantity, precision.value);
  194. }
  195. }
  196. await transaction.insert(this.tableName, data);
  197. }
  198. }
  199. return Pos;
  200. };