schedule_ledger_month.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 && info.sj_gcl === null && info.sj_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 saveDatas(datas) {
  45. // 判断是添加,删除,还是修改
  46. const transaction = await this.db.beginTransaction();
  47. try {
  48. const ymArray = [];
  49. for (const data of datas) {
  50. const info = await this.getDataByCondition({ tid: this.ctx.tender.id, lid: data.lid, yearmonth: data.yearmonth });
  51. if (!this._.includes(ymArray, data.yearmonth)) {
  52. ymArray.push(data.yearmonth);
  53. }
  54. if (info) {
  55. if (data.plan_gcl === null && data.plan_tp === null && info.sj_gcl === null && info.sj_tp === null) {
  56. await transaction.delete(this.tableName, { id: info.id });
  57. } else {
  58. const updateData = {
  59. id: info.id,
  60. plan_gcl: data.plan_gcl,
  61. plan_tp: data.plan_tp,
  62. };
  63. await transaction.update(this.tableName, updateData);
  64. }
  65. } else {
  66. const insertData = {
  67. tid: this.ctx.tender.id,
  68. lid: data.lid,
  69. yearmonth: data.yearmonth,
  70. plan_gcl: data.plan_gcl,
  71. plan_tp: data.plan_tp,
  72. };
  73. await transaction.insert(this.tableName, insertData);
  74. }
  75. }
  76. // 重新计算本月、总 计划金额和计划工程量
  77. for (const ym of ymArray) {
  78. await this.calcMonthPlan(transaction, this.ctx.tender.id, ym);
  79. }
  80. await this.ctx.service.scheduleMonth.calcPlan(transaction, this.ctx.tender.id);
  81. await transaction.commit();
  82. return true;
  83. } catch (err) {
  84. await transaction.rollback();
  85. throw err;
  86. }
  87. }
  88. async saveSj(data) {
  89. // 判断是添加,删除,还是修改
  90. const transaction = await this.db.beginTransaction();
  91. try {
  92. const info = await this.getDataByCondition({ tid: this.ctx.tender.id, lid: data.lid, yearmonth: data.yearmonth });
  93. if (info) {
  94. if (data.sj_gcl === null && data.sj_tp === null && info.plan_gcl === null && info.plan_tp === null) {
  95. await transaction.delete(this.tableName, { id: info.id });
  96. } else {
  97. const updateData = {
  98. id: info.id,
  99. sj_gcl: data.sj_gcl,
  100. sj_tp: data.sj_tp,
  101. };
  102. await transaction.update(this.tableName, updateData);
  103. }
  104. } else {
  105. const insertData = {
  106. tid: this.ctx.tender.id,
  107. lid: data.lid,
  108. yearmonth: data.yearmonth,
  109. sj_gcl: data.sj_gcl,
  110. sj_tp: data.sj_tp,
  111. };
  112. await transaction.insert(this.tableName, insertData);
  113. }
  114. // 重新计算本月、总 计划金额和计划工程量
  115. await this.calcMonthSj(transaction, this.ctx.tender.id, data.yearmonth);
  116. await this.ctx.service.scheduleMonth.calcSj(transaction, this.ctx.tender.id);
  117. await transaction.commit();
  118. return true;
  119. } catch (err) {
  120. await transaction.rollback();
  121. throw err;
  122. }
  123. }
  124. async calcMonthSj(transaction, tid, yearmonth) {
  125. const sql = 'SELECT SUM(`sj_gcl`) as total_sj_gcl, SUM(`sj_tp`) as total_sj_tp FROM ?? WHERE tid = ? and yearmonth = ?';
  126. const sqlParam = [this.tableName, tid, yearmonth];
  127. const result = await transaction.queryOne(sql, sqlParam);
  128. const updateData = {
  129. sj_gcl: result.total_sj_gcl,
  130. sj_tp: result.total_sj_tp,
  131. };
  132. const option = {
  133. where: {
  134. tid,
  135. yearmonth,
  136. },
  137. };
  138. return await transaction.update(this.ctx.service.scheduleMonth.tableName, updateData, option);
  139. }
  140. async calcMonthPlan(transaction, tid, yearmonth) {
  141. const sql = 'SELECT SUM(`plan_gcl`) as total_plan_gcl, SUM(`plan_tp`) as total_plan_tp FROM ?? WHERE tid = ? and yearmonth = ?';
  142. const sqlParam = [this.tableName, tid, yearmonth];
  143. const result = await transaction.queryOne(sql, sqlParam);
  144. const updateData = {
  145. plan_gcl: result.total_plan_gcl,
  146. plan_tp: result.total_plan_tp,
  147. };
  148. const option = {
  149. where: {
  150. tid,
  151. yearmonth,
  152. },
  153. };
  154. return await transaction.update(this.ctx.service.scheduleMonth.tableName, updateData, option);
  155. }
  156. async getConllectionList(tid, yearmonthArray) {
  157. const sql = 'SELECT tid, lid, SUM(`plan_gcl`) as plan_gcl, SUM(`plan_tp`) as plan_tp, SUM(`sj_gcl`) as sj_gcl, SUM(`sj_tp`) as sj_tp' +
  158. ' FROM ?? WHERE `tid` = ? AND `yearmonth` in (?) GROUP BY `lid`';
  159. const sqlParam = [this.tableName, tid, yearmonthArray];
  160. const result = await this.db.query(sql, sqlParam);
  161. return result;
  162. }
  163. // async getHadDataList(tid) {
  164. // const sql = 'SELECT lid FROM ?? WHERE tid = ? '
  165. // }
  166. }
  167. return ScheduleLedgerMonth;
  168. };