report_memory.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. // console.log('this.ctx.stage.im_type: ' + this.ctx.stage.im_type);
  82. // console.log('imType.tz.value: ' + imType.tz.value);
  83. // console.log('isTz: ' + isTz);
  84. if (isTz && this.ctx.stage.im_type !== imType.tz.value) {
  85. // console.log('您查看的报表跟设置不符,请查看“总量控制”的报表');
  86. throw '您查看的报表跟设置不符,请查看“总量控制”的报表';
  87. } else if (!isTz && this.ctx.stage.im_type === imType.tz.value) {
  88. throw '您查看的报表跟设置不符,请查看“0号台账”的报表';
  89. }
  90. const stageIm = new StageIm(this.ctx);
  91. await stageIm.buildImData();
  92. this.stageImData.main = stageIm.ImData;
  93. if (isTz) {
  94. this.stageImData.bills = stageIm.ImBillsData;
  95. }
  96. }
  97. async getStageImTzData(tid, sid) {
  98. if (!this.stageImData) {
  99. this.stageImData = {};
  100. try {
  101. await this._generateStageIm(tid, sid);
  102. } catch(err) {
  103. // console.log('getStageImTzData exception');
  104. if (err.statck) {
  105. this.ctx.logger.error(error);
  106. }
  107. this.stageImData.main = err.statck ? '数据错误' : err;
  108. this.stageImData.bills = this.stageImData.main;
  109. }
  110. }
  111. return this.stageImData.main;
  112. }
  113. async getStageImTzBillsData(tid, sid) {
  114. if (!this.stageImData) {
  115. this.stageImData = {};
  116. try {
  117. await this._generateStageIm(tid, sid);
  118. } catch (err) {
  119. // console.log(err);
  120. // console.log('getStageImTzBillsData exception');
  121. if (err.statck) {
  122. this.ctx.logger.error(error);
  123. }
  124. this.stageImData.main = err.statck ? '数据错误' : err;
  125. this.stageImData.bills = this.stageImData.main;
  126. }
  127. }
  128. return this.stageImData.bills;
  129. }
  130. async getStageImZlData(tid, sid) {
  131. this.stageImData = {};
  132. try {
  133. await this._generateStageIm(tid, sid, false);
  134. } catch(err) {
  135. // console.log('getStageImZlData exception');
  136. if (err.statck) {
  137. this.ctx.logger.error(error);
  138. }
  139. this.stageImData.main = err.statck ? '数据错误' : err;
  140. }
  141. return this.stageImData.main;
  142. }
  143. }
  144. return ReportMemory;
  145. };