pos.js 11 KB

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