schedule_stage.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. 'use strict';
  2. module.exports = app => {
  3. class ScheduleStage extends app.BaseService {
  4. constructor(ctx) {
  5. super(ctx);
  6. this.tableName = 'schedule_stage';
  7. }
  8. async getLastPlanMonth() {
  9. const sql = 'SELECT `yearmonth` FROM ?? WHERE `tid` = ? ORDER BY `yearmonth` DESC Limit 0,1';
  10. const sqlParam = [this.tableName, this.ctx.tender.id];
  11. return await this.db.query(sql, sqlParam);
  12. }
  13. async add(data) {
  14. const transaction = await this.db.beginTransaction();
  15. try {
  16. const insertData = {
  17. tid: this.ctx.tender.id,
  18. yearmonth: data.yearmonth,
  19. order: data.order,
  20. };
  21. // 更新schedule_month stage_tp_used为1
  22. const updateData = {
  23. stage_tp_used: 1,
  24. };
  25. const option = {
  26. where: {
  27. tid: this.ctx.tender.id,
  28. yearmonth: data.yearmonth,
  29. },
  30. };
  31. await transaction.update(this.ctx.service.scheduleMonth.tableName, updateData, option);
  32. await transaction.insert(this.tableName, insertData);
  33. await transaction.commit();
  34. return true;
  35. } catch (err) {
  36. await transaction.rollback();
  37. throw err;
  38. }
  39. }
  40. async del(data) {
  41. const transaction = await this.db.beginTransaction();
  42. try {
  43. const info = await this.getDataById(data.id);
  44. await transaction.delete(this.tableName, { id: data.id });
  45. // 更新schedule_month stage_tp_used为0
  46. const updateData = {
  47. stage_tp_used: 0,
  48. };
  49. const option = {
  50. where: {
  51. tid: this.ctx.tender.id,
  52. yearmonth: info.yearmonth,
  53. },
  54. };
  55. await transaction.update(this.ctx.service.scheduleMonth.tableName, updateData, option);
  56. // 重算计量总金额
  57. await this.calcStageSjTp(transaction, this.ctx.tender.id);
  58. await transaction.commit();
  59. return true;
  60. } catch (err) {
  61. await transaction.rollback();
  62. throw err;
  63. }
  64. }
  65. async changeOrder(data) {
  66. const transaction = await this.db.beginTransaction();
  67. try {
  68. const updateData = {
  69. id: data.id,
  70. order: data.order,
  71. };
  72. await transaction.update(this.tableName, updateData);
  73. await transaction.commit();
  74. return true;
  75. } catch (err) {
  76. await transaction.rollback();
  77. throw err;
  78. }
  79. }
  80. async updateOneTp(data) {
  81. const transaction = await this.db.beginTransaction();
  82. try {
  83. const updateData = {
  84. tp: data.tp,
  85. };
  86. const option = {
  87. where: {
  88. tid: this.ctx.tender.id,
  89. order: data.order,
  90. },
  91. };
  92. await transaction.update(this.tableName, updateData, option);
  93. await this.updateStageSjTp(transaction, this.ctx.tender.id, data.stage_sj_tp);
  94. await transaction.commit();
  95. return true;
  96. } catch (err) {
  97. await transaction.rollback();
  98. throw err;
  99. }
  100. }
  101. async calcStageSjTp(transaction, tid) {
  102. const sql = 'SELECT SUM(`tp`) as stage_sj_tp FROM ?? WHERE tid = ?';
  103. const sqlParam = [this.tableName, tid];
  104. const result = await transaction.queryOne(sql, sqlParam);
  105. const updateData = {
  106. stage_sj_tp: result.stage_sj_tp,
  107. };
  108. const option = {
  109. where: {
  110. tid,
  111. },
  112. };
  113. return await transaction.update(this.ctx.service.schedule.tableName, updateData, option);
  114. }
  115. async updateStageSjTp(transaction, tid, sj_tp) {
  116. // 更新计量总金额
  117. const stageData = {
  118. stage_sj_tp: sj_tp,
  119. };
  120. const stageOption = {
  121. where: {
  122. tid,
  123. },
  124. };
  125. await transaction.update(this.ctx.service.schedule.tableName, stageData, stageOption);
  126. }
  127. }
  128. return ScheduleStage;
  129. };