schedule.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. module.exports = app => {
  3. class Schedule extends app.BaseService {
  4. constructor(ctx) {
  5. super(ctx);
  6. this.tableName = 'schedule';
  7. }
  8. async saveMode(data) {
  9. const transaction = await this.db.beginTransaction();
  10. try {
  11. const options = {
  12. where: {
  13. tid: this.ctx.tender.id,
  14. },
  15. };
  16. const updateData = {
  17. mode: data.mode,
  18. };
  19. await transaction.update(this.tableName, updateData, options);
  20. if (data.update_under_ledger.length > 0) {
  21. const month_list = [];
  22. const update_options = [];
  23. for (const un of data.update_under_ledger) {
  24. const option = {
  25. row: {
  26. plan_tp: un.plan_tp,
  27. plan_gcl: un.plan_gcl,
  28. },
  29. where: {
  30. tid: this.ctx.tender.id,
  31. lid: un.lid,
  32. yearmonth: un.yearmonth,
  33. },
  34. };
  35. update_options.push(option);
  36. if (!this._.find(month_list, un.yearmonth)) {
  37. month_list.push(un.yearmonth);
  38. }
  39. }
  40. if (update_options.length > 0) {
  41. await transaction.updateRows(this.ctx.service.scheduleLedgerMonth.tableName, update_options);
  42. for (const m of month_list) {
  43. await this.ctx.service.scheduleLedgerMonth.calcMonthPlan(transaction, this.ctx.tender.id, m);
  44. }
  45. await this.ctx.service.scheduleMonth.calcPlan(transaction, this.ctx.tender.id);
  46. }
  47. }
  48. await transaction.commit();
  49. return true;
  50. } catch (err) {
  51. await transaction.rollback();
  52. throw err;
  53. }
  54. }
  55. }
  56. return Schedule;
  57. };