ancillary_gcl.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.name) nd.name = d.name || '';
  34. if (d.unit) nd.unit = d.unit || '';
  35. if (d.quantity) nd.quantity = this.ctx.helper.round(d.quantity || 0, qtyDecimal);
  36. if (d.expr) nd.expr = d.expr || '';
  37. if (d.memo) nd.memo = d.memo || '';
  38. insertData.push(nd);
  39. }
  40. await this.db.insert(this.tableName, insertData);
  41. return await this.getAllDataByCondition({
  42. where: { id: this.ctx.helper._.map(insertData, 'id') }
  43. });
  44. }
  45. async _delDatas (data) {
  46. if (!data || data.length === 0) throw '提交数据错误';
  47. const orgDatas = await this.getAllDataByCondition({ where: { id: data } });
  48. if (!orgDatas || orgDatas.length === 0) throw '删除的辅助工程量不存在';
  49. const bills = await this.ctx.service.ledger.getDataById(orgDatas[0].lid);
  50. let billsAnc = await this.getAllDataByCondition({ where: { tid: bills.tid, lid: bills.id } });
  51. billsAnc = billsAnc.filter(ba => {
  52. return data.indexOf(ba.id) < 0;
  53. });
  54. billsAnc.sort((x, y) => { return x.g_order - y.g_order; });
  55. const updateData = [];
  56. billsAnc.forEach((x, i) => {
  57. if (x.g_order !== i + 1) updateData.push({ id: x.id, g_order: i + 1});
  58. });
  59. const conn = await this.db.beginTransaction();
  60. try {
  61. await conn.delete(this.tableName, { id: data });
  62. if (updateData.length > 0) await conn.updateRows(this.tableName, updateData);
  63. await conn.commit();
  64. return [data, updateData];
  65. } catch (err) {
  66. await conn.rollback();
  67. throw err;
  68. }
  69. }
  70. async _updateDatas (data) {
  71. if (!data || data.length === 0) throw '提交数据错误';
  72. const qtyDecimal = this.ctx.tender.info.decimal.qty;
  73. const user_id = this.ctx.session.sessionUser.accountId;
  74. const datas = data instanceof Array ? data : [data];
  75. const orgDatas = await this.getAllDataByCondition({
  76. where: { id: this.ctx.helper._.map(datas, 'id') }
  77. });
  78. if (!orgDatas || orgDatas.length === 0) throw '修改的辅助工程量不存在';
  79. const uDatas = [];
  80. for (const d of datas) {
  81. const od = orgDatas.find(x => { return x.id === d.id; });
  82. if (!od) continue;
  83. const nd = { id: od.id, update_user_id: user_id };
  84. if (d.name !== undefined) nd.name = d.name;
  85. if (d.g_order !== undefined) nd.g_order = d.g_order;
  86. if (d.unit !== undefined) nd.unit = d.unit;
  87. if (d.quantity !== undefined) nd.quantity = this.ctx.helper.round(d.quantity, qtyDecimal);
  88. if (d.expr !== undefined) nd.expr = d.expr || '';
  89. if (d.memo !== undefined) nd.memo = d.memo || '';
  90. uDatas.push(nd);
  91. }
  92. if (uDatas.length > 0) {
  93. await this.db.updateRows(this.tableName, uDatas);
  94. return uDatas;
  95. } else {
  96. return [];
  97. }
  98. }
  99. async updateDatas(data) {
  100. const result = {add: [], del: [], update: []};
  101. try {
  102. if (data.add) {
  103. result.add = await this._addDatas(data.add);
  104. }
  105. if (data.update) {
  106. result.update = await this._updateDatas(data.update);
  107. }
  108. if (data.del) {
  109. [result.del, result.update] = await this._delDatas(data.del);
  110. }
  111. return result;
  112. } catch (err) {
  113. if (err.stack) {
  114. throw err;
  115. } else {
  116. result.err = err.toString();
  117. return result;
  118. }
  119. }
  120. }
  121. }
  122. return AncillaryGcl;
  123. };