pos.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 savePosData(data, tid) {
  28. const transaction = await this.db.beginTransaction();
  29. try {
  30. const result = { ledger: {}, pos: null };
  31. if (data.updateType === 'add') {
  32. const tender = await this.ctx.service.tender.getTender(tid);
  33. data.updateData.tid = tid;
  34. // todo 新增期
  35. data.updateData.add_stage = 0;
  36. data.updateData.add_times = 0;
  37. data.updateData.add_user = this.ctx.session.sessionUser.accountId;
  38. const addRst = await transaction.insert(this.tableName, data.updateData);
  39. data.updateData.id = addRst.insertId;
  40. } else if (data.updateType === 'update') {
  41. const datas = data.updateData instanceof Array ? data.updateData : [data.updateData];
  42. result.ledger.update = [];
  43. const orgPos = await this.getPosData({tid: tid, id: this._.map(datas, 'id')});
  44. console.log(datas);
  45. for (const d of datas) {
  46. const op = this._.find(orgPos, function (p) { return p.id = d.id; });
  47. await transaction.update(this.tableName, d, {tid: tid, id: d.id});
  48. if (d.quantity && op && (result.ledger.update.indexOf(op.lid) === -1)) {
  49. result.ledger.update.push(op.lid);
  50. }
  51. }
  52. console.log(result.ledger.update);
  53. for (const lid of result.ledger.update) {
  54. await this.ctx.service.ledger.calc(tid, lid, transaction);
  55. }
  56. } else if (data.updateType === 'delete') {
  57. if (!data.updateData || data.updateData.length === 0) {
  58. throw '提交数据错误';
  59. }
  60. const pos = await this.getPosData({tid: tid, id: data.updateData});
  61. const ledgerIds = this._.map(pos, 'lid');
  62. await transaction.delete(this.tableName, {tid: tid, id: data.updateData});
  63. for (const lid of ledgerIds) {
  64. await this.ctx.service.ledger.calc(tid, lid, transaction);
  65. }
  66. result.ledger.update = ledgerIds;
  67. } else {
  68. throw '提交数据错误';
  69. }
  70. await transaction.commit();
  71. result.pos = data.updateData;
  72. result.ledger.update = await this.ctx.service.ledger.getDataByIds(result.ledger.update);
  73. return result;
  74. } catch (err) {
  75. await transaction.rollback();
  76. throw err;
  77. }
  78. }
  79. async deletePosData(transaction, tid, lid) {
  80. await transaction.delete(this.tableName, {tid: tid, lid: lid});
  81. }
  82. async copyBillsPosData(orgLid, newLid, transaction) {
  83. const posData = await this.getAllDataByCondition({ where: { lid: orgLid } });
  84. if (posData.length > 0) {
  85. for (const pd of posData) {
  86. delete pd.id;
  87. pd.lid = newLid;
  88. }
  89. await transaction.insert(this.tableName, posData);
  90. }
  91. }
  92. /**
  93. * 批量插入部位数据 - 仅供批量插入清单部位调用
  94. * @param transaction - 所属事务
  95. * @param {Number} tid - 标段id
  96. * @param {Number} lid - 台账id
  97. * @param {Array} data - 新增数据
  98. * @returns {Promise<void>}
  99. */
  100. async insertLedgerPosData(transaction, tid, lid, data) {
  101. for (const d of data) {
  102. d.tid = tid;
  103. d.lid = lid;
  104. // todo 新增期
  105. d.add_stage = 0;
  106. d.add_times = 0;
  107. d.add_user = this.ctx.session.sessionUser.accountId;
  108. }
  109. await transaction.insert(this.tableName, data);
  110. }
  111. }
  112. return Pos;
  113. };