schedule_ledger_month.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use strict';
  2. module.exports = app => {
  3. class ScheduleLedgerMonth extends app.BaseService {
  4. constructor(ctx) {
  5. super(ctx);
  6. this.tableName = 'schedule_ledger_month';
  7. }
  8. async save(data) {
  9. // 判断是添加,删除,还是修改
  10. const transaction = await this.db.beginTransaction();
  11. try {
  12. const info = await this.getDataByCondition({ tid: this.ctx.tender.id, lid: data.lid, yearmonth: data.yearmonth });
  13. if (info) {
  14. if (data.plan_gcl === null && data.plan_tp === null) {
  15. await transaction.delete(this.tableName, { id: info.id });
  16. } else {
  17. const updateData = {
  18. id: info.id,
  19. plan_gcl: data.plan_gcl,
  20. plan_tp: data.plan_tp,
  21. };
  22. await transaction.update(this.tableName, updateData);
  23. }
  24. } else {
  25. const insertData = {
  26. tid: this.ctx.tender.id,
  27. lid: data.lid,
  28. yearmonth: data.yearmonth,
  29. plan_gcl: data.plan_gcl,
  30. plan_tp: data.plan_tp,
  31. };
  32. await transaction.insert(this.tableName, insertData);
  33. }
  34. // 重新计算本月、总 计划金额和计划工程量
  35. await this.calcMonthPlan(transaction, this.ctx.tender.id, data.yearmonth);
  36. await this.ctx.service.scheduleMonth.calcPlan(transaction, this.ctx.tender.id);
  37. await transaction.commit();
  38. return true;
  39. } catch (err) {
  40. await transaction.rollback();
  41. throw err;
  42. }
  43. }
  44. async calcMonthPlan(transaction, tid, yearmonth) {
  45. const sql = 'SELECT SUM(`plan_gcl`) as total_plan_gcl, SUM(`plan_tp`) as total_plan_tp FROM ?? WHERE tid = ? and yearmonth = ?';
  46. const sqlParam = [this.tableName, tid, yearmonth];
  47. const result = await transaction.queryOne(sql, sqlParam);
  48. const updateData = {
  49. plan_gcl: result.total_plan_gcl,
  50. plan_tp: result.total_plan_tp,
  51. };
  52. const option = {
  53. where: {
  54. tid,
  55. yearmonth,
  56. },
  57. };
  58. return await transaction.update(this.ctx.service.scheduleMonth.tableName, updateData, option);
  59. }
  60. }
  61. return ScheduleLedgerMonth;
  62. };