pos.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty'],
  25. });
  26. }
  27. async getPosDataByIds(ids) {
  28. this.initSqlBuilder();
  29. this.sqlBuilder.setAndWhere('id', {
  30. operate: 'in',
  31. value: ids
  32. });
  33. this.sqlBuilder.columns = ['id', 'tid', 'lid', 'name', 'quantity', 'drawing_code', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty'];
  34. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName)
  35. return await this.db.query(sql, sqlParam);
  36. }
  37. async _insertPosData(transaction, data, tid) {
  38. data.tid = tid;
  39. // todo 新增期
  40. data.add_stage = 0;
  41. data.add_times = 0;
  42. data.add_user = this.ctx.session.sessionUser.accountId;
  43. if (data.quantity) {
  44. const bills = await this.ctx.service.ledger.getDataById(data.lid);
  45. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  46. data.quantity = this.round(data.quantity, precision.value);
  47. }
  48. const addRst = await transaction.insert(this.tableName, data);
  49. data.id = addRst.insertId;
  50. }
  51. /**
  52. * 保存部位明细数据
  53. * @param data
  54. * @param {Number} tid - 标段id
  55. * @returns {Promise<{ledger: {}, pos: null}>}
  56. */
  57. async savePosData(data, tid) {
  58. const result = { ledger: {}, pos: null };
  59. const transaction = await this.db.beginTransaction();
  60. try {
  61. if (data.updateType === 'add') {
  62. const tender = await this.ctx.service.tender.getTender(tid);
  63. if (data.updateData instanceof Array) {
  64. for (const d of data.updateData) {
  65. this._insertPosData(transaction, d, tid);
  66. }
  67. } else {
  68. this._insertPosData(transaction, data.updateData, tid);
  69. }
  70. } else if (data.updateType === 'update') {
  71. const datas = data.updateData instanceof Array ? data.updateData : [data.updateData];
  72. result.ledger.update = [];
  73. const orgPos = await this.getPosData({tid: tid, id: this._.map(datas, 'id')});
  74. for (const d of datas) {
  75. const op = this._.find(orgPos, function (p) { return p.id = d.id; });
  76. if (d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined) {
  77. const bills = await this.ctx.service.ledger.getDataById(op.lid);
  78. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  79. if (d.sgfh_qty !== undefined) {
  80. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  81. } else if (op) {
  82. d.sgfh_qty = op.sgfh_qty;
  83. }
  84. if (d.sjcl_qty !== undefined) {
  85. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  86. } else if (op) {
  87. d.sjcl_qty = op.sjcl_qty;
  88. }
  89. if (d.qtcl_qty) {
  90. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  91. } else if (op) {
  92. d.qtcl_qty = op.qtcl_qty;
  93. }
  94. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  95. }
  96. await transaction.update(this.tableName, d, {tid: tid, id: d.id});
  97. if (d.quantity !== undefined && op && (result.ledger.update.indexOf(op.lid) === -1)) {
  98. result.ledger.update.push(op.lid);
  99. }
  100. }
  101. for (const lid of result.ledger.update) {
  102. await this.ctx.service.ledger.calc(tid, lid, transaction);
  103. }
  104. } else if (data.updateType === 'delete') {
  105. if (!data.updateData || data.updateData.length === 0) {
  106. throw '提交数据错误';
  107. }
  108. const pos = await this.getPosData({tid: tid, id: data.updateData});
  109. const ledgerIds = this._.map(pos, 'lid');
  110. await transaction.delete(this.tableName, {tid: tid, id: data.updateData});
  111. for (const lid of ledgerIds) {
  112. await this.ctx.service.ledger.calc(tid, lid, transaction);
  113. }
  114. result.ledger.update = ledgerIds;
  115. } else {
  116. throw '提交数据错误';
  117. }
  118. await transaction.commit();
  119. } catch (err) {
  120. await transaction.rollback();
  121. throw err;
  122. }
  123. result.pos = data.updateData;
  124. result.ledger.update = await this.ctx.service.ledger.getDataByIds(result.ledger.update);
  125. return result;
  126. }
  127. /**
  128. * 复制粘贴 部位明细数据
  129. * @param {Array} data - 复制粘贴的数据
  130. * @param {Number} tid - 标段id
  131. * @returns {Promise<{ledger: {}, pos: null}>}
  132. */
  133. async pastePosData(data, tid) {
  134. if (!(data instanceof Array)) {
  135. throw '提交数据错误';
  136. }
  137. const transaction = await this.db.beginTransaction();
  138. const result = { ledger: {}, pos: null }, updateLid = [];
  139. const orgPos = await this.getPosData({tid: tid, id: this._.map(data, 'id')});
  140. let bills = null, precision = null;
  141. try {
  142. for (const d of data) {
  143. const op = d.id ? this._.find(orgPos, {id: d.id}) : null;
  144. if (d.sgfh_qty || d.sjcl_qty || d.qtcl_qty) {
  145. if (!bills || bills.id !== d.lid) {
  146. bills = await this.ctx.service.ledger.getDataById(d.lid);
  147. precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  148. updateLid.push(d.lid);
  149. }
  150. if (d.sgfh_qty !== undefined) {
  151. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  152. } else if (op) {
  153. d.sgfh_qty = op.sgfh_qty;
  154. }
  155. if (d.sjcl_qty !== undefined) {
  156. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  157. } else if (op) {
  158. d.sjcl_qty = op.sjcl_qty;
  159. }
  160. if (d.qtcl_qty) {
  161. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  162. } else if (op) {
  163. d.qtcl_qty = op.qtcl_qty;
  164. }
  165. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  166. }
  167. if (d.id) {
  168. await transaction.update(this.tableName, d);
  169. } else {
  170. this._insertPosData(transaction, d, tid);
  171. }
  172. }
  173. for (const lid of updateLid) {
  174. await this.ctx.service.ledger.calc(tid, lid, transaction);
  175. }
  176. await transaction.commit();
  177. } catch (err) {
  178. await transaction.rollback();
  179. throw err;
  180. }
  181. result.pos = data;
  182. if (updateLid.length > 0) {
  183. result.ledger.update = await this.ctx.service.ledger.getDataByIds(updateLid);
  184. }
  185. return result;
  186. }
  187. /**
  188. * 删除清单下部位明细数据(删除清单时调用)
  189. *
  190. * @param transaction - 事务
  191. * @param tid - 标段id
  192. * @param lid - 清单id
  193. * @returns {Promise<void>}
  194. */
  195. async deletePosData(transaction, tid, lid) {
  196. await transaction.delete(this.tableName, {tid: tid, lid: lid});
  197. }
  198. /**
  199. * 复制整块 拷贝部位明细数据
  200. * @param {Number} orgLid - 拷贝的部位明细所属台账id
  201. * @param {Number} newLid - 新的台账id
  202. * @param transaction - 复制整块事务
  203. * @returns {Promise<void>}
  204. */
  205. async copyBillsPosData(orgLid, newLid, transaction) {
  206. const posData = await this.getAllDataByCondition({ where: { lid: orgLid } });
  207. if (posData.length > 0) {
  208. for (const pd of posData) {
  209. delete pd.id;
  210. pd.lid = newLid;
  211. }
  212. await transaction.insert(this.tableName, posData);
  213. }
  214. }
  215. /**
  216. * 批量插入部位数据 - 仅供批量插入清单部位调用
  217. * @param transaction - 所属事务
  218. * @param {Number} tid - 标段id
  219. * @param {Number} lid - 台账id
  220. * @param {Array} data - 新增数据
  221. * @returns {Promise<void>}
  222. */
  223. async insertLedgerPosData(transaction, tid, bills, data) {
  224. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  225. const insertDatas = [];
  226. for (const d of data) {
  227. const inD = {
  228. tid: tid, lid: bills.id,
  229. add_stage: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
  230. name: d.name, drawing_code: d.drawing_code,
  231. };
  232. if (d.quantity) {
  233. inD.sgfh_qty = this.round(d.quantity, precision.value);
  234. inD.quantity = inD.sgfh_qty;
  235. }
  236. insertDatas.push(inD);
  237. }
  238. await transaction.insert(this.tableName, insertDatas);
  239. }
  240. }
  241. return Pos;
  242. };