report_memory.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const StageIm = require('../lib/stage_im');
  10. const imType = require('../const/tender').imType;
  11. const audit = require('../const/audit');
  12. // const path = require('path');
  13. // const fs = require('fs');
  14. const stageImTz = 'mem_stage_im_tz';
  15. const stageImTzBills = 'mem_stage_im_tz_bills';
  16. const stageImZl = 'mem_stage_im_zl';
  17. module.exports = app => {
  18. class ReportMemory extends app.BaseService {
  19. /**
  20. * 构造函数
  21. *
  22. * @param {Object} ctx - egg全局context
  23. * @return {void}
  24. */
  25. constructor(ctx) {
  26. super(ctx);
  27. this.tableName = 'report_memory';
  28. // 需要缓存的数据
  29. this.stageImData = null;
  30. }
  31. async _checkTender(tid) {
  32. if (this.ctx.tender) return;
  33. const tender = await this.ctx.service.tender.getTender(tid);
  34. tender.info = await this.ctx.service.tenderInfo.getTenderInfo(tid);
  35. this.ctx.tender = tender;
  36. }
  37. async _checkStage(sid) {
  38. if (!this.ctx.stage) {
  39. const status = audit.stage.status;
  40. const stage = await this.ctx.service.stage.getDataById(sid);
  41. stage.auditors = await this.ctx.service.stageAudit.getAuditors(stage.id, stage.times);
  42. stage.curAuditor = await this.ctx.service.stageAudit.getCurAuditor(stage.id, stage.times);
  43. const accountId = this.ctx.session.sessionUser.accountId, auditorIds = this._.map(stage.auditors, 'aid'), shareIds = [];
  44. if (accountId === stage.user_id) { // 原报
  45. if (stage.curAuditor) {
  46. stage.readOnly = stage.curAuditor.aid !== accountId;
  47. } else {
  48. stage.readOnly = stage.status !== status.uncheck && stage.status !== status.checkNo;
  49. }
  50. stage.curTimes = stage.times;
  51. if (stage.status === status.uncheck || stage.status === status.checkNo) {
  52. stage.curOrder = 0;
  53. } else if (stage.status === status.checked) {
  54. stage.curOrder = this._.max(this._.map(stage.auditors, 'order'));
  55. } else {
  56. stage.curOrder = stage.curAuditor.aid === accountId ? stage.curAuditor.order : stage.curAuditor.order - 1;
  57. }
  58. } else if (auditorIds.indexOf(accountId) !== -1) { // 审批人
  59. if (stage.status === status.uncheck) {
  60. throw '您无权查看该数据';
  61. }
  62. stage.curTimes = stage.status === status.checkNo ? stage.times - 1 : stage.times;
  63. if (stage.status === status.checked) {
  64. stage.curOrder = this._.max(this._.map(stage.auditors, 'order'));
  65. } else if (stage.status === status.checkNo) {
  66. const audit = await this.service.stageAudit.getDataByCondition({
  67. sid: stage.id, times: stage.times - 1, status: status.checkNo
  68. });
  69. stage.curOrder = audit.order;
  70. } else {
  71. stage.curOrder = accountId === stage.curAuditor.aid ? stage.curAuditor.order : stage.curAuditor.order - 1;
  72. }
  73. } else if (shareIds.indexOf(accountId) !== -1) { // 分享人
  74. if (stage.status === status.uncheck) {
  75. throw '您无权查看该数据';
  76. }
  77. stage.curTimes = stage.status === status.checkNo ? stage.times - 1 : stage.times;
  78. stage.curOrder = stage.status === status.checked ? this._.max(this._.map(stage.auditors, 'order')) : stage.curAuditor.order - 1;
  79. }
  80. this.ctx.stage = stage;
  81. this.ctx.stage.cacheTime = this.ctx.stage.readOnly ? (this.ctx.stage.cache_time_r).getTime(): (this.ctx.stage.cache_time_l).getTime();
  82. }
  83. }
  84. // build-time: 162-384ms, redis-cache: 0-41ms, mysql + IO: 116-146ms
  85. // 一定程度上算是大Value缓存,数据多了以后:
  86. // 1. 达到redis内存阈值时,数据会swap到磁盘,此时将消耗IO时间
  87. // 2. redis单独服务器
  88. // 3. redis集群
  89. async _getReportMemoryCache(name, tid, sid, time) {
  90. // redis
  91. const cacheKey = name + '-t' + tid + (sid ? '-s' + sid : '') + (time ? '-' + time : '');
  92. const data = await this.cache.get(cacheKey);
  93. if (data) {
  94. return eval(data);
  95. } else {
  96. return null;
  97. }
  98. // mysql + IO
  99. // const rm = await this.getDataByCondition({
  100. // tid: tid, sid: sid, name: name, time: time
  101. // });
  102. // if (rm && rm.file) {
  103. // const file = path.join(this.ctx.app.config.filePath, 'report', 'cache', rm.file);
  104. // if (fs.existsSync(file)) {
  105. // const data = await fs.readFileSync(file, 'utf8');
  106. // return eval(data);
  107. // } else {
  108. // return null;
  109. // }
  110. // }
  111. }
  112. async _setReportMemoryCache(name, tid, sid, time, data) {
  113. // redis
  114. const cacheKey = name + '-t' + tid + (sid ? '-s' + sid : '') + (time ? '-' + time : '');
  115. this.cache.set(cacheKey, JSON.stringify(data), 'EX', this.ctx.app.config.cacheTime);
  116. // mysql + IO
  117. // const file = path.join('report', 'cache', 'rm' + (new Date()).getTime() + '.json');
  118. // await this.ctx.helper.saveBufferFile(JSON.stringify(data), path.join(this.ctx.app.config.filePath, file));
  119. // const rm = await this.getDataByCondition({
  120. // tid: tid, sid: sid, name: name, time: time
  121. // });
  122. // if (rm) {
  123. // await this.db.update(this.tableName, {id: rm.id, file: file});
  124. // } else {
  125. // await this.db.insert(this.tableName, {tid: tid, sid: sid, name: name, time: time, file: file});
  126. // }
  127. }
  128. async _generateStageIm(tid, sid, isTz = true) {
  129. if (isTz && this.ctx.stage.im_type !== imType.tz.value) {
  130. throw '您查看的报表跟设置不符,请查看“总量控制”的报表';
  131. } else if (!isTz && this.ctx.stage.im_type === imType.tz.value) {
  132. throw '您查看的报表跟设置不符,请查看“0号台账”的报表';
  133. }
  134. const stageIm = new StageIm(this.ctx);
  135. await stageIm.buildImData();
  136. this.stageImData.main = stageIm.ImData;
  137. if (isTz) {
  138. this.stageImData.bills = stageIm.ImBillsData;
  139. await this._setReportMemoryCache(stageImTz, tid, sid, this.ctx.stage.cacheTime, this.stageImData.main);
  140. await this._setReportMemoryCache(stageImTzBills, tid, sid, this.ctx.stage.cacheTime, this.stageImData.bills);
  141. } else {
  142. await this._setReportMemoryCache(stageImZl, tid, sid, this.ctx.stage.cacheTime, this.stageImData.main);
  143. }
  144. }
  145. async getStageImTzNoReturn(tid, sid) {
  146. // 备注:单独拎出以下几行代码一个是为了提高效率(跟getStageImTzDataDirectlyByKey方法协作使用)
  147. // 二是如果出现并行查询(台账及台账清单)情况下,会出现干扰(已验证过),导致数据丢失
  148. if (!this.stageImData) {
  149. this.stageImData = {};
  150. }
  151. try {
  152. await this._generateStageIm(tid, sid);
  153. } catch (err) {
  154. this.stageImData.main = [];
  155. this.stageImData.bills = [];
  156. }
  157. }
  158. getStageImTzDataDirectlyByKey(key) {
  159. let rst = [];
  160. if (key === 'mem_stage_im_tz') {
  161. rst = this.stageImData.main;
  162. } else {
  163. rst = this.stageImData.bills;
  164. }
  165. return rst;
  166. }
  167. async getStageImTzData(tid, sid) {
  168. await this._checkTender(tid);
  169. await this._checkStage(sid);
  170. const cache = await this._getReportMemoryCache('mem_stage_im_tz', tid, sid, this.ctx.stage.cacheTime);
  171. if (cache) {
  172. //console.log('cache');
  173. return cache;
  174. }
  175. //console.log('build');
  176. if (!this.stageImData) {
  177. this.stageImData = {};
  178. try {
  179. await this._generateStageIm(tid, sid);
  180. } catch (err) {
  181. if (err.statck) {
  182. this.ctx.logger.error(err);
  183. }
  184. this.stageImData.main = err.statck ? '数据错误' : err;
  185. this.stageImData.bills = this.stageImData.main;
  186. }
  187. }
  188. return this.stageImData.main;
  189. }
  190. async getStageImTzBillsData(tid, sid) {
  191. await this._checkTender(tid);
  192. await this._checkStage(sid);
  193. const cache = await this._getReportMemoryCache('mem_stage_im_tz_bills', tid, sid, this.ctx.stage.cacheTime);
  194. if (cache) return cache;
  195. if (!this.stageImData) {
  196. this.stageImData = {};
  197. try {
  198. await this._generateStageIm(tid, sid);
  199. } catch (err) {
  200. if (err.statck) {
  201. this.ctx.logger.error(err);
  202. }
  203. this.stageImData.main = err.statck ? '数据错误' : err;
  204. this.stageImData.bills = this.stageImData.main;
  205. }
  206. }
  207. return this.stageImData.bills;
  208. }
  209. async getStageImZlData(tid, sid) {
  210. await this._checkTender(tid);
  211. await this._checkStage(sid);
  212. const cache = await this._getReportMemoryCache('mem_stage_im_zl', tid, sid, this.ctx.stage.cacheTime);
  213. if (cache) return cache;
  214. this.stageImData = {};
  215. try {
  216. await this._generateStageIm(tid, sid, false);
  217. } catch (err) {
  218. if (err.statck) {
  219. this.ctx.logger.error(err);
  220. }
  221. this.stageImData.main = err.statck ? '数据错误' : err;
  222. }
  223. return this.stageImData.main;
  224. }
  225. }
  226. return ReportMemory;
  227. };