schedule_ledger.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. }
  32. if (insertDatas.length > 0) await transaction.insert(this.tableName, insertDatas);
  33. // 更新所有为null
  34. await transaction.update(this.tableName, { gcl: null, tp: null }, { where: { tid: this.ctx.tender.id } });
  35. const updateOptions = [];
  36. let total_tp = 0;
  37. // 更新最底层和总设计值
  38. for (const u of datas.under_ledger) {
  39. updateOptions.push({
  40. row: {
  41. gcl: u.gcl,
  42. tp: u.tp,
  43. },
  44. where: {
  45. ledger_id: u.ledger_id,
  46. tid: this.ctx.tender.id,
  47. },
  48. });
  49. total_tp = this.ctx.helper.add(total_tp, u.tp);
  50. }
  51. if (updateOptions.length > 0) await transaction.updateRows(this.tableName, updateOptions);
  52. // 判断是否已创建了形象进度表
  53. const scheduleInfo = await this.ctx.service.schedule.getDataByCondition({ tid: this.ctx.tender.id });
  54. if (!scheduleInfo) {
  55. const newSchedule = {
  56. tid: this.ctx.tender.id,
  57. total_tp,
  58. };
  59. await transaction.insert(this.ctx.service.schedule.tableName, newSchedule);
  60. } else {
  61. await transaction.update(this.ctx.service.schedule.tableName, { id: scheduleInfo.id, total_tp });
  62. }
  63. await transaction.commit();
  64. return true;
  65. } catch (err) {
  66. await transaction.rollback();
  67. throw err;
  68. }
  69. }
  70. }
  71. return ScheduleLedger;
  72. };