ancillary_gcl.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. 'use strict';
  2. /**
  3. * 附属工程量(ancillary gcl)
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class AncillaryGcl extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'ancillary_gcl';
  20. }
  21. async _addDatas(data) {
  22. const qtyDecimal = this.ctx.tender.info.decimal.qty;
  23. const user_id = this.ctx.session.sessionUser.accountId;
  24. const datas = data instanceof Array ? data : [data];
  25. const insertData = [];
  26. for (const d of datas) {
  27. if (!d.lid || !d.g_order) throw '新增其他数据,提交的数据错误';
  28. const nd = {
  29. id: this.uuid.v4(), tid: this.ctx.tender.id,
  30. add_user_id: user_id, update_user_id: user_id,
  31. lid: d.lid, g_order: d.g_order,
  32. };
  33. if (d.is_aux !== undefined) nd.is_aux = d.is_aux;
  34. if (d.name) nd.name = d.name || '';
  35. if (d.unit) nd.unit = d.unit || '';
  36. if (d.quantity) nd.quantity = this.ctx.helper.round(d.quantity || 0, qtyDecimal);
  37. if (d.expr) nd.expr = d.expr || '';
  38. if (d.drawing_code !== undefined) nd.drawing_code = d.drawing_code;
  39. if (d.memo) nd.memo = d.memo || '';
  40. insertData.push(nd);
  41. }
  42. await this.db.insert(this.tableName, insertData);
  43. return await this.getAllDataByCondition({
  44. where: { id: this.ctx.helper._.map(insertData, 'id') }
  45. });
  46. }
  47. async _delDatas (data) {
  48. if (!data || data.length === 0) throw '提交数据错误';
  49. const orgDatas = await this.getAllDataByCondition({ where: { id: data } });
  50. if (!orgDatas || orgDatas.length === 0) throw '删除的辅助工程量不存在';
  51. const bills = await this.ctx.service.ledger.getDataById(orgDatas[0].lid);
  52. let billsAnc = await this.getAllDataByCondition({ where: { tid: bills.tid, lid: bills.id } });
  53. billsAnc = billsAnc.filter(ba => {
  54. return data.indexOf(ba.id) < 0;
  55. });
  56. billsAnc.sort((x, y) => { return x.g_order - y.g_order; });
  57. const updateData = [];
  58. billsAnc.forEach((x, i) => {
  59. if (x.g_order !== i + 1) updateData.push({ id: x.id, g_order: i + 1});
  60. });
  61. const conn = await this.db.beginTransaction();
  62. try {
  63. await conn.delete(this.tableName, { id: data });
  64. if (updateData.length > 0) await conn.updateRows(this.tableName, updateData);
  65. await conn.commit();
  66. return [data, updateData];
  67. } catch (err) {
  68. await conn.rollback();
  69. throw err;
  70. }
  71. }
  72. async _updateDatas (data) {
  73. if (!data || data.length === 0) throw '提交数据错误';
  74. const qtyDecimal = this.ctx.tender.info.decimal.qty;
  75. const user_id = this.ctx.session.sessionUser.accountId;
  76. const datas = data instanceof Array ? data : [data];
  77. const orgDatas = await this.getAllDataByCondition({
  78. where: { id: this.ctx.helper._.map(datas, 'id') }
  79. });
  80. if (!orgDatas || orgDatas.length === 0) throw '修改的辅助工程量不存在';
  81. const uDatas = [];
  82. for (const d of datas) {
  83. const od = orgDatas.find(x => { return x.id === d.id; });
  84. if (!od) continue;
  85. const nd = { id: od.id, update_user_id: user_id };
  86. if (d.is_aux !== undefined) nd.is_aux = d.is_aux;
  87. if (d.name !== undefined) nd.name = d.name;
  88. if (d.g_order !== undefined) nd.g_order = d.g_order;
  89. if (d.unit !== undefined) nd.unit = d.unit;
  90. if (d.quantity !== undefined) nd.quantity = this.ctx.helper.round(d.quantity, qtyDecimal);
  91. if (d.expr !== undefined) nd.expr = d.expr || '';
  92. if (d.drawing_code !== undefined) nd.drawing_code = d.drawing_code;
  93. if (d.memo !== undefined) nd.memo = d.memo || '';
  94. uDatas.push(nd);
  95. }
  96. if (uDatas.length > 0) {
  97. await this.db.updateRows(this.tableName, uDatas);
  98. return uDatas;
  99. } else {
  100. return [];
  101. }
  102. }
  103. async updateDatas(data) {
  104. const result = {add: [], del: [], update: []};
  105. try {
  106. if (data.add) {
  107. result.add = await this._addDatas(data.add);
  108. }
  109. if (data.update) {
  110. result.update = await this._updateDatas(data.update);
  111. }
  112. if (data.del) {
  113. [result.del, result.update] = await this._delDatas(data.del);
  114. }
  115. return result;
  116. } catch (err) {
  117. if (err.stack) {
  118. throw err;
  119. } else {
  120. result.err = err.toString();
  121. return result;
  122. }
  123. }
  124. }
  125. async deletePartData(transaction, tid, lid) {
  126. await transaction.delete(this.tableName, { tid: tid, lid: lid });
  127. }
  128. }
  129. return AncillaryGcl;
  130. };