report_memory.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const Service = require('egg').Service;
  10. const StageIm = require('../lib/stage_im');
  11. const imType = require('../const/tender').imType;
  12. const audit = require('../const/audit');
  13. module.exports = app => {
  14. class ReportMemory extends Service {
  15. /**
  16. * 构造函数
  17. *
  18. * @param {Object} ctx - egg全局context
  19. * @return {void}
  20. */
  21. constructor(ctx) {
  22. super(ctx);
  23. this.db = this.app.mysql;
  24. this.cache = this.app.redis;
  25. this._ = this.app._;
  26. // 需要缓存的数据
  27. this.stageImData = null;
  28. }
  29. async _checkTender(tid) {
  30. const tender = await this.ctx.service.tender.getTender(tid);
  31. tender.info = await this.ctx.service.tenderInfo.getTenderInfo(tid);
  32. this.ctx.tender = tender;
  33. }
  34. async _checkStage(sid) {
  35. const status = audit.stage.status;
  36. const stage = await this.ctx.service.stage.getDataById(sid);
  37. stage.auditors = await this.ctx.service.stageAudit.getAuditors(stage.id, stage.times);
  38. stage.curAuditor = await this.ctx.service.stageAudit.getCurAuditor(stage.id, stage.times);
  39. const accountId = this.ctx.session.sessionUser.accountId, auditorIds = this._.map(stage.auditors, 'aid'), shareIds = [];
  40. if (accountId === stage.user_id) { // 原报
  41. if (stage.curAuditor) {
  42. stage.readOnly = stage.curAuditor.aid !== accountId;
  43. } else {
  44. stage.readOnly = stage.status !== status.uncheck && stage.status !== status.checkNo;
  45. }
  46. stage.curTimes = stage.times;
  47. if (stage.status === status.uncheck || stage.status === status.checkNo) {
  48. stage.curOrder = 0;
  49. } else if (stage.status === status.checked) {
  50. stage.curOrder = this._.max(this._.map(stage.auditors, 'order'));
  51. } else {
  52. stage.curOrder = stage.curAuditor.aid === accountId ? stage.curAuditor.order : stage.curAuditor.order - 1;
  53. }
  54. } else if (auditorIds.indexOf(accountId) !== -1) { // 审批人
  55. if (stage.status === status.uncheck) {
  56. throw '您无权查看该数据';
  57. }
  58. stage.curTimes = stage.status === status.checkNo ? stage.times - 1 : stage.times;
  59. if (stage.status === status.checked) {
  60. stage.curOrder = this._.max(this._.map(stage.auditors, 'order'));
  61. } else if (stage.status === status.checkNo) {
  62. const audit = await this.service.stageAudit.getDataByCondition({
  63. sid: stage.id, times: stage.times - 1, status: status.checkNo
  64. });
  65. stage.curOrder = audit.order;
  66. } else {
  67. stage.curOrder = accountId === stage.curAuditor.aid ? stage.curAuditor.order : stage.curAuditor.order - 1;
  68. }
  69. } else if (shareIds.indexOf(accountId) !== -1) { // 分享人
  70. if (stage.status === status.uncheck) {
  71. throw '您无权查看该数据';
  72. }
  73. stage.curTimes = stage.status === status.checkNo ? stage.times - 1 : stage.times;
  74. stage.curOrder = stage.status === status.checked ? this._.max(this._.map(stage.auditors, 'order')) : stage.curAuditor.order - 1;
  75. }
  76. this.ctx.stage = stage;
  77. }
  78. async _generateStageIm(tid, sid, isTz = true) {
  79. await this._checkTender(tid);
  80. await this._checkStage(sid);
  81. if (isTz && this.ctx.stage.im_type !== imType.tz.value) {
  82. throw '您查看的报表跟设置不符,请查看“总量控制”的报表';
  83. } else if (!isTz && this.ctx.stage.im_type === imType.tz.value) {
  84. throw '您查看的报表跟设置不符,请查看“0号台账”的报表';
  85. }
  86. const stageIm = new StageIm(this.ctx);
  87. await stageIm.buildImData();
  88. this.stageImData.main = stageIm.ImData;
  89. if (isTz) {
  90. this.stageImData.bills = stageIm.ImBillsData;
  91. }
  92. }
  93. async getStageImTzData(tid, sid) {
  94. if (!this.stageImData) {
  95. this.stageImData = {};
  96. try {
  97. await this._generateStageIm(tid, sid);
  98. } catch(err) {
  99. if (err.statck) {
  100. this.ctx.logger.error(error);
  101. }
  102. this.stageImData.main = err.statck ? '数据错误' : err;
  103. this.stageImData.bills = this.stageImData.main;
  104. }
  105. }
  106. return this.stageImData.main;
  107. }
  108. async getStageImTzBillsData(tid, sid) {
  109. if (!this.stageImData) {
  110. this.stageImData = {};
  111. try {
  112. await this._generateStageIm(tid, sid);
  113. } catch(err) {
  114. if (err.statck) {
  115. this.ctx.logger.error(error);
  116. }
  117. this.stageImData.main = err.statck ? '数据错误' : err;
  118. this.stageImData.bills = this.stageImTz.main;
  119. }
  120. }
  121. return this.stageImData.bills;
  122. }
  123. async getStageImZlData(tid, sid) {
  124. this.stageImData = {};
  125. try {
  126. await this._generateStageIm(tid, sid, false);
  127. } catch(err) {
  128. if (err.statck) {
  129. this.ctx.logger.error(error);
  130. }
  131. this.stageImData.main = err.statck ? '数据错误' : err;
  132. }
  133. return this.stageImData.main;
  134. }
  135. }
  136. return ReportMemory;
  137. };