schedule_ledger.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 'use strict';
  2. module.exports = app => {
  3. class ScheduleLedger extends app.BaseService {
  4. constructor(ctx) {
  5. super(ctx);
  6. this.tableName = 'schedule_ledger';
  7. }
  8. async saveLedger(datas) {
  9. const transaction = await this.db.beginTransaction();
  10. try {
  11. const oldDatas = await this.getAllDataByCondition({
  12. where: { tid: this.ctx.tender.id },
  13. });
  14. const oldLids = this._.map(oldDatas, 'ledger_id');
  15. const insertDatas = [];
  16. for (const l of datas) {
  17. if (oldLids.indexOf(l) === -1) {
  18. const data = {
  19. tid: this.ctx.tender.id,
  20. ledger_id: l,
  21. };
  22. insertDatas.push(data);
  23. } else {
  24. this._.pull(oldLids, l);
  25. }
  26. }
  27. if (oldLids.length > 0) {
  28. for (const ol of oldLids) {
  29. await transaction.delete(this.tableName, { tid: this.ctx.tender.id, ledger_id: ol });
  30. }
  31. }
  32. if (insertDatas.length > 0) await transaction.insert(this.tableName, insertDatas);
  33. await transaction.commit();
  34. return true;
  35. } catch (err) {
  36. await transaction.rollback();
  37. throw err;
  38. }
  39. }
  40. }
  41. return ScheduleLedger;
  42. };