schedule_ledger.js 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. 'use strict';
  2. module.exports = app => {
  3. class ScheduleLedger extends app.BaseService {
  4. constructor(ctx) {
  5. super(ctx);
  6. this.tableName = 'schedule_ledger';
  7. }
  8. async saveLedger(datas) {
  9. const transaction = await this.db.beginTransaction();
  10. try {
  11. const oldDatas = await this.getAllDataByCondition({
  12. where: { tid: this.ctx.tender.id },
  13. });
  14. const oldLids = this._.map(oldDatas, 'ledger_id');
  15. const insertDatas = [];
  16. for (const l of datas.select_ledger) {
  17. if (oldLids.indexOf(l) === -1) {
  18. const data = {
  19. tid: this.ctx.tender.id,
  20. ledger_id: l,
  21. };
  22. insertDatas.push(data);
  23. } else {
  24. this._.pull(oldLids, l);
  25. }
  26. }
  27. if (oldLids.length > 0) {
  28. for (const ol of oldLids) {
  29. await transaction.delete(this.tableName, { tid: this.ctx.tender.id, ledger_id: ol });
  30. // 更新已选标段值
  31. await transaction.delete(this.ctx.service.scheduleLedgerMonth.tableName, { tid: this.ctx.tender.id, lid: ol });
  32. }
  33. }
  34. if (insertDatas.length > 0) await transaction.insert(this.tableName, insertDatas);
  35. // 更新所有为null
  36. await transaction.update(this.tableName, { gcl: null, tp: null }, { where: { tid: this.ctx.tender.id } });
  37. const updateOptions = [];
  38. let total_tp = 0;
  39. // 更新最底层和总设计值
  40. for (const u of datas.under_ledger) {
  41. updateOptions.push({
  42. row: {
  43. gcl: u.gcl,
  44. tp: u.tp,
  45. },
  46. where: {
  47. ledger_id: u.ledger_id,
  48. tid: this.ctx.tender.id,
  49. },
  50. });
  51. total_tp = this.ctx.helper.add(total_tp, u.tp);
  52. }
  53. if (updateOptions.length > 0) await transaction.updateRows(this.tableName, updateOptions);
  54. // 判断是否已创建了形象进度表
  55. const scheduleInfo = await this.ctx.service.schedule.getDataByCondition({ tid: this.ctx.tender.id });
  56. if (!scheduleInfo) {
  57. const newSchedule = {
  58. tid: this.ctx.tender.id,
  59. total_tp,
  60. };
  61. await transaction.insert(this.ctx.service.schedule.tableName, newSchedule);
  62. } else {
  63. await transaction.update(this.ctx.service.schedule.tableName, { id: scheduleInfo.id, total_tp });
  64. }
  65. // 判断是否已存在计划月,并更新计划月统计数据,再更新总的统计数据
  66. const smList = await this.ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  67. if (smList.length > 0) {
  68. for (const sm of smList) {
  69. await this.ctx.service.scheduleLedgerMonth.calcMonthPlan(transaction, this.ctx.tender.id, sm.yearmonth);
  70. await this.ctx.service.scheduleLedgerMonth.calcMonthSj(transaction, this.ctx.tender.id, sm.yearmonth);
  71. }
  72. await this.ctx.service.scheduleMonth.calcPlan(transaction, this.ctx.tender.id);
  73. await this.ctx.service.scheduleMonth.calcSj(transaction, this.ctx.tender.id);
  74. }
  75. await transaction.commit();
  76. return true;
  77. } catch (err) {
  78. await transaction.rollback();
  79. throw err;
  80. }
  81. }
  82. }
  83. return ScheduleLedger;
  84. };