schedule_ledger.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. // 判断是否已创建了形象进度表
  34. const scheduleInfo = await this.ctx.service.schedule.getDataByCondition({ tid: this.ctx.tender.id });
  35. if (!scheduleInfo) {
  36. const newSchedule = {
  37. tid: this.ctx.tender.id,
  38. };
  39. await transaction.insert(this.ctx.service.schedule.tableName, newSchedule);
  40. }
  41. await transaction.commit();
  42. return true;
  43. } catch (err) {
  44. await transaction.rollback();
  45. throw err;
  46. }
  47. }
  48. }
  49. return ScheduleLedger;
  50. };