statistics.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. const arr = [];
  84. arr.push({ name: '企业用户', value: org });
  85. arr.push({ name: '专家用户', value: expert });
  86. arr.push({ name: '机构用户', value: mech });
  87. return arr;
  88. }
  89. /**
  90. * 统计各种数据
  91. */
  92. async data() {
  93. // 高企政策服务通过数量 1
  94. const policy = await this.policyModel.count({ status: '1' });
  95. // 创新券领取成果数量 4
  96. const ticket = await this.ticketModel.count({ status: '4' });
  97. // 科技成果成功的数量 6
  98. const product = await this.productModel.count({ status: '6' });
  99. const arr = [];
  100. arr.push({ name: '高企政策服务', value: policy });
  101. arr.push({ name: '创新券服务', value: ticket });
  102. arr.push({ name: '科技成果评价服务', value: product });
  103. return arr;
  104. }
  105. /**
  106. * 专利单位列表
  107. */
  108. patentUnitList() {
  109. return [
  110. { name: '吉林大学' },
  111. { name: '长春理工大学' },
  112. { name: '东北电力大学' },
  113. { name: '吉林工程技术师范学院' },
  114. { name: '长春工业大学' },
  115. { name: '北华大学' },
  116. { name: '吉林农业大学' },
  117. { name: '吉林师范大学' },
  118. { name: '长春工程学院' },
  119. { name: '吉林建筑大学' },
  120. { name: '通化师范学院' },
  121. { name: '长春大学' },
  122. { name: '东北师范大学' },
  123. { name: '吉林农业科技学院' },
  124. ];
  125. }
  126. /**
  127. * 创新劵统计
  128. */
  129. async ticket() {
  130. const statusList = [
  131. { label: '资质审查中', value: '0' },
  132. { label: '准备订单', value: '1' },
  133. { label: '资质审查失败', value: '-1' },
  134. { label: '待中介审核', value: '2' },
  135. { label: '订单入库', value: '3' },
  136. { label: '信息资料审查失败', value: '-3' },
  137. { label: '高企申报成功', value: '4' },
  138. ];
  139. const res = await this.ctx.model.Ticket.aggregate([
  140. { $group: {
  141. _id: '$status',
  142. value: { $sum: 1 },
  143. } },
  144. ]);
  145. const data = statusList.map(i => {
  146. const { label: name, value } = i;
  147. const r = res.find(f => f._id === value);
  148. const obj = { name, value: 0 };
  149. if (r) obj.value = r.value;
  150. return obj;
  151. });
  152. // 政策服务
  153. // 高企认定(ticket);奖励兑现(policy);研发补贴(policy)
  154. const tcount = await this.ctx.model.Ticket.count();
  155. const jldx = await this.ctx.model.Policy.count({ type: '奖励兑现' });
  156. const yfbt = await this.ctx.model.Policy.count({ type: '研发补贴' });
  157. const ticket = await this.ctx.model.Policy.count({ type: '创新券' });
  158. const arr = [];
  159. arr.push({ name: '高企认定', value: tcount });
  160. arr.push({ name: '奖励兑现', value: jldx });
  161. arr.push({ name: '研发补贴', value: yfbt });
  162. arr.push({ name: '创新券', value: ticket });
  163. return { ticket: data, policy: arr };
  164. }
  165. }
  166. module.exports = IndexService;