schedule_ledger_month.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 saveSj(data) {
  45. // 判断是添加,删除,还是修改
  46. const transaction = await this.db.beginTransaction();
  47. try {
  48. const info = await this.getDataByCondition({ tid: this.ctx.tender.id, lid: data.lid, yearmonth: data.yearmonth });
  49. if (info) {
  50. if (data.sj_gcl === null && data.sj_tp === null && info.plan_gcl === null && info.plan_tp === null) {
  51. await transaction.delete(this.tableName, { id: info.id });
  52. } else {
  53. const updateData = {
  54. id: info.id,
  55. sj_gcl: data.sj_gcl,
  56. sj_tp: data.sj_tp,
  57. };
  58. await transaction.update(this.tableName, updateData);
  59. }
  60. } else {
  61. const insertData = {
  62. tid: this.ctx.tender.id,
  63. lid: data.lid,
  64. yearmonth: data.yearmonth,
  65. sj_gcl: data.sj_gcl,
  66. sj_tp: data.sj_tp,
  67. };
  68. await transaction.insert(this.tableName, insertData);
  69. }
  70. // 重新计算本月、总 计划金额和计划工程量
  71. await this.calcMonthSj(transaction, this.ctx.tender.id, data.yearmonth);
  72. await this.ctx.service.scheduleMonth.calcSj(transaction, this.ctx.tender.id);
  73. await transaction.commit();
  74. return true;
  75. } catch (err) {
  76. await transaction.rollback();
  77. throw err;
  78. }
  79. }
  80. async calcMonthSj(transaction, tid, yearmonth) {
  81. const sql = 'SELECT SUM(`sj_gcl`) as total_sj_gcl, SUM(`sj_tp`) as total_sj_tp FROM ?? WHERE tid = ? and yearmonth = ?';
  82. const sqlParam = [this.tableName, tid, yearmonth];
  83. const result = await transaction.queryOne(sql, sqlParam);
  84. const updateData = {
  85. sj_gcl: result.total_sj_gcl,
  86. sj_tp: result.total_sj_tp,
  87. };
  88. const option = {
  89. where: {
  90. tid,
  91. yearmonth,
  92. },
  93. };
  94. return await transaction.update(this.ctx.service.scheduleMonth.tableName, updateData, option);
  95. }
  96. async calcMonthPlan(transaction, tid, yearmonth) {
  97. const sql = 'SELECT SUM(`plan_gcl`) as total_plan_gcl, SUM(`plan_tp`) as total_plan_tp FROM ?? WHERE tid = ? and yearmonth = ?';
  98. const sqlParam = [this.tableName, tid, yearmonth];
  99. const result = await transaction.queryOne(sql, sqlParam);
  100. const updateData = {
  101. plan_gcl: result.total_plan_gcl,
  102. plan_tp: result.total_plan_tp,
  103. };
  104. const option = {
  105. where: {
  106. tid,
  107. yearmonth,
  108. },
  109. };
  110. return await transaction.update(this.ctx.service.scheduleMonth.tableName, updateData, option);
  111. }
  112. async getConllectionList(tid, yearmonthArray) {
  113. 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' +
  114. ' FROM ?? WHERE `tid` = ? AND `yearmonth` in (?) GROUP BY `lid`';
  115. const sqlParam = [this.tableName, tid, yearmonthArray];
  116. const result = await this.db.query(sql, sqlParam);
  117. return result;
  118. }
  119. // async getHadDataList(tid) {
  120. // const sql = 'SELECT lid FROM ?? WHERE tid = ? '
  121. // }
  122. }
  123. return ScheduleLedgerMonth;
  124. };