schedule_ledger.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. let total_gcl = 0;
  38. // 更新最底层和总设计值
  39. for (const u of datas.under_ledger) {
  40. updateOptions.push({
  41. row: {
  42. gcl: u.gcl,
  43. tp: u.tp,
  44. },
  45. where: {
  46. ledger_id: u.ledger_id,
  47. tid: this.ctx.tender.id,
  48. },
  49. });
  50. total_gcl = this.ctx.helper.add(total_gcl, u.gcl);
  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. total_gcl,
  61. };
  62. await transaction.insert(this.ctx.service.schedule.tableName, newSchedule);
  63. } else {
  64. await transaction.update(this.ctx.service.schedule.tableName, { id: scheduleInfo.id, total_gcl, total_tp });
  65. }
  66. await transaction.commit();
  67. return true;
  68. } catch (err) {
  69. await transaction.rollback();
  70. throw err;
  71. }
  72. }
  73. }
  74. return ScheduleLedger;
  75. };