settle_select.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class SettleSelect extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'settle_select';
  20. }
  21. async updateData(data) {
  22. const result = {};
  23. const conn = await this.db.beginTransaction();
  24. try {
  25. if (data.del && data.del.length > 0) {
  26. const lid = [], pid = [];
  27. for (const d of data.del) {
  28. if (d.lid) lid.push(d.lid);
  29. if (d.pid) pid.push(d.pid);
  30. }
  31. const delLibData = lid.length > 0 ? await this.getAllDataByCondition({ where: { settle_id: this.ctx.settle.id, lid } }) : [];
  32. const delPidData = pid.length > 0 ? await this.getAllDataByCondition({ where: { settle_id: this.ctx.settle.id, pid } }) : [];
  33. if (delLibData.length + delPidData.length !== data.del.length) throw '提交数据错误';
  34. const deleteData = [...delLibData, ...delPidData];
  35. await conn.delete(this.tableName, { id: deleteData.map(x => { return x.id }) });
  36. result.del = deleteData;
  37. }
  38. if (data.add && data.add.length > 0) {
  39. const addData = [];
  40. for (const d of data.add) {
  41. addData.push({
  42. tid: this.ctx.settle.tid, settle_id: this.ctx.settle.id, settle_order: this.ctx.settle.settle_order,
  43. lid: d.lid || '', pid: d.pid || '', user_id: this.ctx.session.sessionUser.accountId,
  44. });
  45. }
  46. await conn.insert(this.tableName, addData);
  47. result.add = addData;
  48. }
  49. await conn.commit();
  50. } catch (err) {
  51. await conn.rollback();
  52. throw err;
  53. }
  54. return result;
  55. }
  56. }
  57. return SettleSelect;
  58. };