pos.js 11 KB

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