index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.patentModel = this.ctx.model.Patent;
  12. this.code = this.ctx.model.Code;
  13. this.productModel = this.ctx.model.Product;
  14. this.expertModel = this.ctx.model.Expert;
  15. // this.userModel = this.ctx.model.User;
  16. this.personalModel = this.ctx.model.Personal;
  17. this.organizationModel = this.ctx.model.Organization;
  18. this.surveyModel = this.ctx.model.Survey;
  19. this.tranModel = this.ctx.model.Transaction;
  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: '1' };
  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 expert() {
  80. const res = await this.expertModel.aggregate([
  81. {
  82. $group: {
  83. _id: '$company',
  84. value: { $sum: 1 },
  85. },
  86. },
  87. ]);
  88. let arr = [];
  89. const other = { name: '其他', value: 0 };
  90. for (const i of res) {
  91. const { _id, value } = i;
  92. const unitList = this.expertList();
  93. const unit = unitList.find(
  94. f => (_id && _id.includes(f.name)) || f.name.includes(_id)
  95. );
  96. if (unit) {
  97. // 说明是需要单拎出来的数据,现查arr中是否有该单位:有=>数字合并;没有=>创建条目
  98. const { name, sort } = unit;
  99. const arrItem = arr.find(f => f.name === name);
  100. if (arrItem) {
  101. const index = arr.findIndex(f => f.name === name);
  102. arr[index] = { ...arr[index], value: (arrItem.value || 0) + value };
  103. } else {
  104. arr.push({ name, value, sort });
  105. }
  106. } else {
  107. other.value += value;
  108. }
  109. }
  110. arr = _.orderBy(arr, [ 'sort' ], [ 'asc' ]);
  111. arr.push(other);
  112. // 换名
  113. arr.map(i => {
  114. const r = this.expertList().find(f => f.name === i.name);
  115. if (r && r.alias) i.name = r.alias;
  116. return i;
  117. });
  118. return arr;
  119. }
  120. /**
  121. * 首页数据统计
  122. */
  123. async circle() {
  124. const arr = [];
  125. const organization = await this.organizationModel.count({ status: '1' });
  126. arr.push({
  127. name: '企业注册数量',
  128. value: organization,
  129. });
  130. // TODO:如果需要专家和个人用户区分开,则is_expert:false, 混查则不需要is_expert条件
  131. const user = await this.personalModel.count({ status: '1', is_expert: false });
  132. arr.push({
  133. name: '个人注册数量',
  134. value: user,
  135. });
  136. const exports = await this.expertModel.count();
  137. arr.push({
  138. name: '专家注册数量',
  139. value: exports,
  140. });
  141. const products = await this.productModel.count({ status: '2' });
  142. arr.push({
  143. name: '产品发布数量',
  144. value: products,
  145. });
  146. const surveys = await this.surveyModel.count();
  147. arr.push({
  148. name: '交流互动',
  149. value: surveys,
  150. });
  151. const trans = await this.tranModel.aggregate([
  152. { $match: { status: { $in: [ '0', '1', '2' ] } } },
  153. {
  154. $group: {
  155. _id: '$status',
  156. value: { $sum: 1 },
  157. },
  158. },
  159. ]);
  160. arr.push({
  161. name: '正在洽谈',
  162. value: _.get(trans.find(f => f._id === '0'), 'value', 0),
  163. });
  164. arr.push({
  165. name: '达成意向',
  166. value: _.get(trans.find(f => f._id === '1'), 'value', 0),
  167. });
  168. arr.push({
  169. name: '对接完成',
  170. value: _.get(trans.find(f => f._id === '2'), 'value', 0),
  171. });
  172. return arr;
  173. }
  174. /**
  175. * 专家列表
  176. */
  177. expertList() {
  178. return [
  179. { name: '中科院长春光学精密机械与物理研究所', alias: '光机所', sort: 1 },
  180. { name: '中国科学院长春应用化学研究所', alias: '应化所', sort: 2 },
  181. { name: '中国科学院东北地理与农业生态研究所', alias: '地理所', sort: 3 },
  182. { name: '吉林大学', sort: 4 },
  183. { name: '长春工业大学', sort: 5 },
  184. ];
  185. }
  186. /**
  187. * 专利单位列表
  188. */
  189. patentUnitList() {
  190. return [
  191. { name: '长春理工大学' },
  192. { name: '长春工业大学' },
  193. { name: '吉林工程技术师范学院' },
  194. { name: '吉林农业大学' },
  195. { name: '吉林师范大学' },
  196. { name: '长春工程学院' },
  197. { name: '吉林建筑大学' },
  198. { name: '通化师范学院' },
  199. { name: '长春大学' },
  200. { name: '东北师范大学' },
  201. ];
  202. }
  203. }
  204. module.exports = IndexService;