statistics.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const { ObjectId } = require('mongoose').Types;
  5. const _ = require('lodash');
  6. const assert = require('assert');
  7. // 首页-数据动态统计
  8. class IndexService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'index');
  11. this.redis = this.app.redis;
  12. this.patentModel = this.ctx.model.Patent; // 专利
  13. this.code = this.ctx.model.Code; // 字典
  14. this.productModel = this.ctx.model.Product; // 产品
  15. this.expertModel = this.ctx.model.Expert; // 专家
  16. this.mechanismModel = this.ctx.model.Mechanism;// 机构
  17. this.orgModel = this.ctx.model.Organization; // 企业
  18. this.policyModel = this.ctx.model.Policy; // 高企政策
  19. this.ticketModel = this.ctx.model.Ticket; // 创新券
  20. }
  21. /**
  22. * 首页专利统计
  23. */
  24. async patent() {
  25. const res = await this.patentModel.aggregate([
  26. {
  27. $group: {
  28. _id: '$apply_personal',
  29. value: { $sum: 1 },
  30. },
  31. },
  32. ]);
  33. let arr = [];
  34. const other = { name: '其他', value: 0 };
  35. for (const i of res) {
  36. const { _id, value } = i;
  37. const unitList = this.patentUnitList();
  38. const unit = unitList.find(
  39. f => (_id && _id.includes(f.name)) || f.name.includes(_id)
  40. );
  41. if (unit) {
  42. // 说明是需要单拎出来的数据,现查arr中是否有该单位:有=>数字合并;没有=>创建条目
  43. const { name } = unit;
  44. const arrItem = arr.find(f => f.name === name);
  45. if (arrItem) {
  46. const index = arr.findIndex(f => f.name === name);
  47. arr[index] = { name, value: (arrItem.value || 0) + value };
  48. } else {
  49. arr.push({ name, value });
  50. }
  51. } else {
  52. other.value += value;
  53. }
  54. }
  55. arr = _.orderBy(arr, [ 'value' ], [ 'desc' ]);
  56. arr.push(other);
  57. return arr;
  58. }
  59. /**
  60. * 首页成果统计
  61. */
  62. async product() {
  63. const categroy = await this.code.find({ category: '01' });
  64. const condition = { type: '1', status: '2' };
  65. const arr = [];
  66. for (const c of categroy) {
  67. const { name } = c;
  68. const value = await this.productModel.count({
  69. ...condition,
  70. field: name,
  71. });
  72. arr.push({ value, name });
  73. }
  74. return arr;
  75. }
  76. /**
  77. * 统计用户(企业,专家,机构)数量
  78. */
  79. async users() {
  80. const org = await this.orgModel.count({ status: '1' });
  81. const expert = await this.expertModel.count({ status: '1' });
  82. const mech = await this.mechanismModel.count({ status: '1' });
  83. return { organization: org, expert, mechanism: mech };
  84. }
  85. /**
  86. * 统计各种数据
  87. */
  88. async data() {
  89. // 高企政策服务通过数量 1
  90. const policy = await this.policyModel.count({ status: '1' });
  91. // 创新券领取成果数量 4
  92. const ticket = await this.ticketModel.count({ status: '4' });
  93. // 科技成果成功的数量 8
  94. const product = await this.productModel.count({ status: '6' });
  95. return { policy, ticket, product };
  96. }
  97. /**
  98. * 专利单位列表
  99. */
  100. patentUnitList() {
  101. return [
  102. { name: '吉林大学' },
  103. { name: '长春理工大学' },
  104. { name: '东北电力大学' },
  105. { name: '吉林工程技术师范学院' },
  106. { name: '长春工业大学' },
  107. { name: '北华大学' },
  108. { name: '吉林农业大学' },
  109. { name: '吉林师范大学' },
  110. { name: '长春工程学院' },
  111. { name: '吉林建筑大学' },
  112. { name: '通化师范学院' },
  113. { name: '长春大学' },
  114. { name: '东北师范大学' },
  115. { name: '吉林农业科技学院' },
  116. ];
  117. }
  118. }
  119. module.exports = IndexService;