report_memory.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. // build-time: 162-384ms, redis-cache: 0-41ms, mysql + IO: 116-146ms
  32. // 一定程度上算是大Value缓存,数据多了以后:
  33. // 1. 达到redis内存阈值时,数据会swap到磁盘,此时将消耗IO时间
  34. // 2. redis单独服务器
  35. // 3. redis集群
  36. async _getReportMemoryCache(name, tid, sid, time) {
  37. // redis
  38. const cacheKey = name + '-t' + tid + (sid ? '-s' + sid : '') + (time ? '-' + time : '');
  39. const data = await this.cache.get(cacheKey);
  40. if (data) {
  41. return eval(data);
  42. } else {
  43. return null;
  44. }
  45. // mysql + IO
  46. // const rm = await this.getDataByCondition({
  47. // tid: tid, sid: sid, name: name, time: time
  48. // });
  49. // if (rm && rm.file) {
  50. // const file = path.join(this.ctx.app.config.filePath, 'report', 'cache', rm.file);
  51. // if (fs.existsSync(file)) {
  52. // const data = await fs.readFileSync(file, 'utf8');
  53. // return eval(data);
  54. // } else {
  55. // return null;
  56. // }
  57. // }
  58. }
  59. async _setReportMemoryCache(name, tid, sid, time, data) {
  60. // redis
  61. const cacheKey = name + '-t' + tid + (sid ? '-s' + sid : '') + (time ? '-' + time : '');
  62. this.cache.set(cacheKey, JSON.stringify(data), 'EX', this.ctx.app.config.cacheTime);
  63. // mysql + IO
  64. // const file = path.join('report', 'cache', 'rm' + (new Date()).getTime() + '.json');
  65. // await this.ctx.helper.saveBufferFile(JSON.stringify(data), path.join(this.ctx.app.config.filePath, file));
  66. // const rm = await this.getDataByCondition({
  67. // tid: tid, sid: sid, name: name, time: time
  68. // });
  69. // if (rm) {
  70. // await this.db.update(this.tableName, {id: rm.id, file: file});
  71. // } else {
  72. // await this.db.insert(this.tableName, {tid: tid, sid: sid, name: name, time: time, file: file});
  73. // }
  74. }
  75. async _generateStageIm(tid, sid, isTz = true) {
  76. if (isTz && this.ctx.stage.im_type !== imType.tz.value) {
  77. throw '您查看的报表跟设置不符,请查看“总量控制”的报表';
  78. } else if (!isTz && this.ctx.stage.im_type === imType.tz.value) {
  79. throw '您查看的报表跟设置不符,请查看“0号台账”的报表';
  80. }
  81. const stageIm = new StageIm(this.ctx);
  82. await stageIm.buildImData();
  83. this.stageImData.main = stageIm.ImData;
  84. if (isTz) {
  85. this.stageImData.bills = stageIm.ImBillsData;
  86. await this._setReportMemoryCache(stageImTz, tid, sid, this.ctx.stage.cacheTime, this.stageImData.main);
  87. await this._setReportMemoryCache(stageImTzBills, tid, sid, this.ctx.stage.cacheTime, this.stageImData.bills);
  88. } else {
  89. await this._setReportMemoryCache(stageImZl, tid, sid, this.ctx.stage.cacheTime, this.stageImData.main);
  90. }
  91. }
  92. async getStageImTzNoReturn(tid, sid) {
  93. // 备注:单独拎出以下几行代码一个是为了提高效率(跟getStageImTzDataDirectlyByKey方法协作使用)
  94. // 二是如果出现并行查询(台账及台账清单)情况下,会出现干扰(已验证过),导致数据丢失
  95. if (!this.stageImData) {
  96. this.stageImData = {};
  97. }
  98. try {
  99. await this._generateStageIm(tid, sid);
  100. } catch (err) {
  101. this.stageImData.main = [];
  102. this.stageImData.bills = [];
  103. }
  104. }
  105. getStageImTzDataDirectlyByKey(key) {
  106. let rst = [];
  107. if (key === 'mem_stage_im_tz') {
  108. rst = this.stageImData.main;
  109. } else {
  110. rst = this.stageImData.bills;
  111. }
  112. return rst;
  113. }
  114. async getStageImTzData(tid, sid) {
  115. await this.ctx.service.tender.checkTender(tid);
  116. await this.ctx.service.stage.checkStage(sid);
  117. const cache = await this._getReportMemoryCache('mem_stage_im_tz', tid, sid, this.ctx.stage.cacheTime);
  118. if (cache) {
  119. // console.log('cache');
  120. return cache;
  121. }
  122. // console.log('build');
  123. if (!this.stageImData) {
  124. this.stageImData = {};
  125. try {
  126. await this._generateStageIm(tid, sid);
  127. } catch (err) {
  128. if (err.statck) {
  129. this.ctx.logger.error(err);
  130. }
  131. this.stageImData.main = err.statck ? '数据错误' : err;
  132. this.stageImData.bills = this.stageImData.main;
  133. }
  134. }
  135. return this.stageImData.main;
  136. }
  137. async getStageImTzBillsData(tid, sid) {
  138. await this.ctx.service.tender.checkTender(tid);
  139. await this.ctx.service.stage.checkStage(sid);
  140. const cache = await this._getReportMemoryCache('mem_stage_im_tz_bills', tid, sid, this.ctx.stage.cacheTime);
  141. if (cache) return cache;
  142. if (!this.stageImData) {
  143. this.stageImData = {};
  144. try {
  145. await this._generateStageIm(tid, sid);
  146. } catch (err) {
  147. if (err.statck) {
  148. this.ctx.logger.error(err);
  149. }
  150. this.stageImData.main = err.statck ? '数据错误' : err;
  151. this.stageImData.bills = this.stageImData.main;
  152. }
  153. }
  154. return this.stageImData.bills;
  155. }
  156. async getStageImZlData(tid, sid) {
  157. await this.ctx.service.tender.checkTender(tid);
  158. await this.ctx.service.stage.checkStage(sid);
  159. const cache = await this._getReportMemoryCache('mem_stage_im_zl', tid, sid, this.ctx.stage.cacheTime);
  160. if (cache) return cache;
  161. this.stageImData = {};
  162. try {
  163. await this._generateStageIm(tid, sid, false);
  164. } catch (err) {
  165. if (err.statck) {
  166. this.ctx.logger.error(err);
  167. }
  168. this.stageImData.main = err.statck ? '数据错误' : err;
  169. }
  170. return this.stageImData.main;
  171. }
  172. async getMonthProgress(tid) {
  173. const helper = this.ctx.helper;
  174. await this.ctx.service.tender.checkTender(tid);
  175. const tender = this.ctx.tender;
  176. const stages = await this.ctx.service.stage.getValidStages(tender.id);
  177. const lastStage = stages.length > 0 ? stages[0] : null;
  178. if (lastStage) {
  179. await this.ctx.service.stage.checkStageGatherData(lastStage);
  180. tender.gather_tp = helper.add(lastStage.contract_tp, lastStage.qc_tp);
  181. tender.end_contract_tp = helper.add(lastStage.contract_tp, lastStage.pre_contract_tp);
  182. tender.end_qc_tp = helper.add(lastStage.qc_tp, lastStage.pre_qc_tp);
  183. tender.end_gather_tp = helper.add(tender.end_contract_tp, tender.end_qc_tp);
  184. tender.pre_gather_tp = helper.add(lastStage.pre_contract_tp, lastStage.pre_qc_tp);
  185. tender.yf_tp = lastStage.yf_tp;
  186. tender.qc_ratio = helper.mul(helper.div(tender.end_qc_tp, tender.info.deal_param.contractPrice, 2), 100);
  187. tender.sum = helper.add(tender.total_price, tender.end_qc_tp);
  188. tender.pre_ratio = helper.mul(helper.div(tender.pre_gather_tp, tender.sum, 2), 100);
  189. tender.cur_ratio = helper.mul(helper.div(tender.gather_tp, tender.sum, 2), 100);
  190. tender.other_tp = helper.sub(helper.sub(tender.sum, tender.pre_gather_tp), tender.gather_tp);
  191. tender.other_ratio = Math.max(0, 100 - tender.pre_ratio - tender.cur_ratio);
  192. }
  193. const monthProgress = [];
  194. for (const s of stages) {
  195. if (s.s_time) {
  196. let progress = monthProgress.find(function (x) {
  197. return x.month === s.s_time;
  198. });
  199. if (!progress) {
  200. progress = {month: s.s_time};
  201. monthProgress.push(progress);
  202. }
  203. progress.tp = helper.add(helper.add(progress.tp, s.contract_tp), s.qc_tp);
  204. }
  205. }
  206. monthProgress.sort(function (x, y) {
  207. return Date.parse(x.month) - Date.parse(y.month);
  208. });
  209. let sum = 0;
  210. for (const p of monthProgress) {
  211. p.ratio = helper.mul(helper.div(p.tp, tender.sum, 4), 100);
  212. sum = helper.add(sum, p.tp);
  213. p.end_tp = sum;
  214. p.end_ratio = helper.mul(helper.div(p.end_tp, tender.sum, 4), 100);
  215. }
  216. return monthProgress;
  217. }
  218. }
  219. return ReportMemory;
  220. };