schedule_ledger.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. 'use strict';
  2. const scheduleConst = require('../const/schedule');
  3. module.exports = app => {
  4. class ScheduleLedger extends app.BaseService {
  5. constructor(ctx) {
  6. super(ctx);
  7. this.tableName = 'schedule_ledger';
  8. }
  9. async saveLedger(datas) {
  10. const transaction = await this.db.beginTransaction();
  11. try {
  12. const oldDatas = await this.getAllDataByCondition({
  13. where: { tid: this.ctx.tender.id },
  14. });
  15. const oldLids = this._.map(oldDatas, 'ledger_id');
  16. const insertDatas = [];
  17. for (const l of datas.select_ledger) {
  18. if (oldLids.indexOf(l) === -1) {
  19. const data = {
  20. tid: this.ctx.tender.id,
  21. ledger_id: l,
  22. };
  23. insertDatas.push(data);
  24. } else {
  25. this._.pull(oldLids, l);
  26. }
  27. }
  28. if (oldLids.length > 0) {
  29. for (const ol of oldLids) {
  30. await transaction.delete(this.tableName, { tid: this.ctx.tender.id, ledger_id: ol });
  31. // 更新已选标段值
  32. await transaction.delete(this.ctx.service.scheduleLedgerMonth.tableName, { tid: this.ctx.tender.id, lid: ol });
  33. }
  34. }
  35. if (insertDatas.length > 0) await transaction.insert(this.tableName, insertDatas);
  36. // 更新所有为null
  37. await transaction.update(this.tableName, { gcl: null, tp: null }, { where: { tid: this.ctx.tender.id } });
  38. const updateOptions = [];
  39. let total_tp = 0;
  40. // 更新最底层和总设计值并更新最底层已填的计划与实际(金额、工程量)
  41. for (const u of datas.under_ledger) {
  42. updateOptions.push({
  43. row: {
  44. gcl: u.gcl,
  45. tp: u.tp,
  46. },
  47. where: {
  48. ledger_id: u.ledger_id,
  49. tid: this.ctx.tender.id,
  50. },
  51. });
  52. total_tp = this.ctx.helper.add(total_tp, u.tp);
  53. }
  54. if (updateOptions.length > 0) await transaction.updateRows(this.tableName, updateOptions);
  55. // 判断是否已创建了投资进度表
  56. const scheduleInfo = await this.ctx.service.schedule.getDataByCondition({ tid: this.ctx.tender.id });
  57. if (!scheduleInfo) {
  58. const newSchedule = {
  59. tid: this.ctx.tender.id,
  60. total_tp,
  61. };
  62. await transaction.insert(this.ctx.service.schedule.tableName, newSchedule);
  63. const ledgerData = await this.ctx.service.ledger.getData(this.ctx.tender.id);
  64. const insertDatas = [];
  65. for (const le of ledgerData) {
  66. insertDatas.push({ tid: this.ctx.tender.id, ledger_id: le.ledger_id });
  67. }
  68. await transaction.insert(this.ctx.service.scheduleLedgerHistory.tableName, insertDatas);
  69. } else {
  70. await transaction.update(this.ctx.service.schedule.tableName, { id: scheduleInfo.id, total_tp, revising: 0 });
  71. }
  72. // 判断是否已存在计划月,并更新计划月统计数据,再更新总的统计数据
  73. const smList = await this.ctx.service.scheduleMonth.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  74. if (scheduleInfo && smList.length > 0) {
  75. // 删除所有父节点已填过的值
  76. for (const p of datas.parent_ledger) {
  77. await transaction.delete(this.ctx.service.scheduleLedgerMonth.tableName, { tid: this.ctx.tender.id, lid: p });
  78. }
  79. if (datas.type === 'xz') {
  80. const slmUpdateOption = [];
  81. const mode = scheduleInfo.mode;
  82. for (const u of datas.under_ledger) {
  83. const dgn_price = this.ctx.helper.round(this.ctx.helper.div(u.tp, u.gcl), 2);
  84. if (dgn_price && dgn_price !== 0) {
  85. const sql = 'SELECT * FROM ?? WHERE tid = ? and lid = ?';
  86. const sqlParam = [this.ctx.service.scheduleLedgerMonth.tableName, this.ctx.tender.id, u.ledger_id];
  87. const lidList = await transaction.query(sql, sqlParam);
  88. if (lidList.length > 0) {
  89. for (const l of lidList) {
  90. if (mode === scheduleConst.plan_mode.tp) {
  91. const new_plan_gcl = l.plan_tp ? this.ctx.helper.round(this.ctx.helper.div(l.plan_tp, dgn_price), 3) : null;
  92. const new_sj_gcl = l.sj_tp ? this.ctx.helper.round(this.ctx.helper.div(l.sj_tp, dgn_price), 3) : null;
  93. slmUpdateOption.push({ id: l.id, plan_gcl: new_plan_gcl, sj_gcl: new_sj_gcl });
  94. } else if (mode === scheduleConst.plan_mode.gcl) {
  95. const new_plan_tp = l.plan_gcl ? this.ctx.helper.round(this.ctx.helper.mul(l.plan_gcl, dgn_price), 0) : null;
  96. const new_sj_tp = l.sj_gcl ? this.ctx.helper.round(this.ctx.helper.mul(l.sj_gcl, dgn_price), 0) : null;
  97. slmUpdateOption.push({ id: l.id, plan_tp: new_plan_tp, sj_tp: new_sj_tp });
  98. }
  99. }
  100. }
  101. }
  102. }
  103. if (slmUpdateOption.length > 0) await transaction.updateRows(this.ctx.service.scheduleLedgerMonth.tableName, slmUpdateOption);
  104. // scheduleLedgerHistory更新到最新版台账
  105. const ledgerData = await this.ctx.service.ledger.getData(this.ctx.tender.id);
  106. const insertDatas = [];
  107. for (const le of ledgerData) {
  108. insertDatas.push({ tid: this.ctx.tender.id, ledger_id: le.ledger_id });
  109. }
  110. await transaction.delete(this.ctx.service.scheduleLedgerHistory.tableName, { tid: this.ctx.tender.id });
  111. await transaction.insert(this.ctx.service.scheduleLedgerHistory.tableName, insertDatas);
  112. }
  113. for (const sm of smList) {
  114. await this.ctx.service.scheduleLedgerMonth.calcMonthPlan(transaction, this.ctx.tender.id, sm.yearmonth);
  115. await this.ctx.service.scheduleLedgerMonth.calcMonthSj(transaction, this.ctx.tender.id, sm.yearmonth);
  116. }
  117. await this.ctx.service.scheduleMonth.calcPlan(transaction, this.ctx.tender.id);
  118. await this.ctx.service.scheduleMonth.calcSj(transaction, this.ctx.tender.id);
  119. }
  120. // 判断是否已存在计量进度,并更新计量统计数据,再更新总的统计数据
  121. if (datas.stageTpList) {
  122. await transaction.updateRows(this.ctx.service.scheduleStage.tableName, datas.stageTpList);
  123. // await this.ctx.service.scheduleStage.calcStageSjTp(transaction, this.ctx.tender.id);
  124. }
  125. await this.ctx.service.scheduleStage.updateStageSjTp(transaction, this.ctx.tender.id, datas.stageSjTp);
  126. await transaction.commit();
  127. return true;
  128. } catch (err) {
  129. await transaction.rollback();
  130. throw err;
  131. }
  132. }
  133. }
  134. return ScheduleLedger;
  135. };