index.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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.userModel = this.ctx.model.User;
  17. this.personalModel = this.ctx.model.Personal;
  18. this.organizationModel = this.ctx.model.Organization;
  19. this.surveyModel = this.ctx.model.Survey;
  20. this.dockUser = this.ctx.model.DockUser;
  21. this.tranModel = this.ctx.model.Transaction;
  22. }
  23. /**
  24. * 首页专利统计
  25. */
  26. async patent() {
  27. const res = await this.patentModel.aggregate([
  28. {
  29. $group: {
  30. _id: '$apply_personal',
  31. value: { $sum: 1 },
  32. },
  33. },
  34. ]);
  35. let arr = [];
  36. const other = { name: '其他', value: 0 };
  37. for (const i of res) {
  38. const { _id, value } = i;
  39. const unitList = this.patentUnitList();
  40. const unit = unitList.find(
  41. f => (_id && _id.includes(f.name)) || f.name.includes(_id)
  42. );
  43. if (unit) {
  44. // 说明是需要单拎出来的数据,现查arr中是否有该单位:有=>数字合并;没有=>创建条目
  45. const { name } = unit;
  46. const arrItem = arr.find(f => f.name === name);
  47. if (arrItem) {
  48. const index = arr.findIndex(f => f.name === name);
  49. arr[index] = { name, value: (arrItem.value || 0) + value };
  50. } else {
  51. arr.push({ name, value });
  52. }
  53. } else {
  54. other.value += value;
  55. }
  56. }
  57. arr = _.orderBy(arr, [ 'value' ], [ 'desc' ]);
  58. arr.push(other);
  59. return arr;
  60. }
  61. /**
  62. * 首页成果统计
  63. */
  64. async product() {
  65. const categroy = await this.code.find({ category: '01' });
  66. const condition = { type: '1', status: '2' };
  67. const arr = [];
  68. for (const c of categroy) {
  69. const { name } = c;
  70. const value = await this.productModel.count({
  71. ...condition,
  72. field: name,
  73. });
  74. arr.push({ value, name });
  75. }
  76. return arr;
  77. }
  78. /**
  79. * 首页统计专家
  80. */
  81. async expert() {
  82. const res = await this.expertModel.aggregate([
  83. {
  84. $group: {
  85. _id: '$company',
  86. value: { $sum: 1 },
  87. },
  88. },
  89. ]);
  90. let arr = [];
  91. const other = { name: '其他', value: 0 };
  92. for (const i of res) {
  93. const { _id, value } = i;
  94. const unitList = this.expertList();
  95. const unit = unitList.find(
  96. f => (_id && _id.includes(f.name)) || f.name.includes(_id)
  97. );
  98. if (unit) {
  99. // 说明是需要单拎出来的数据,现查arr中是否有该单位:有=>数字合并;没有=>创建条目
  100. const { name, sort } = unit;
  101. const arrItem = arr.find(f => f.name === name);
  102. if (arrItem) {
  103. const index = arr.findIndex(f => f.name === name);
  104. arr[index] = { ...arr[index], value: (arrItem.value || 0) + value };
  105. } else {
  106. arr.push({ name, value, sort });
  107. }
  108. } else {
  109. other.value += value;
  110. }
  111. }
  112. arr = _.orderBy(arr, [ 'sort' ], [ 'asc' ]);
  113. arr.push(other);
  114. // 换名
  115. arr.map(i => {
  116. const r = this.expertList().find(f => f.name === i.name);
  117. if (r && r.alias) i.name = r.alias;
  118. return i;
  119. });
  120. return arr;
  121. }
  122. /**
  123. * 首页数据统计
  124. */
  125. async circle() {
  126. const arr = [];
  127. const organization = await this.organizationModel.count({ status: '1' });
  128. arr.push({
  129. name: '企业注册数量',
  130. value: organization,
  131. });
  132. // TODO:如果需要专家和个人用户区分开,则is_expert:false, 混查则不需要is_expert条件
  133. const user = await this.personalModel.count({ status: '1', is_expert: false });
  134. arr.push({
  135. name: '个人注册数量',
  136. value: user,
  137. });
  138. const exports = await this.expertModel.count();
  139. arr.push({
  140. name: '专家注册数量',
  141. value: exports,
  142. });
  143. const products = await this.productModel.count({ status: '2' });
  144. arr.push({
  145. name: '产品发布数量',
  146. value: products,
  147. });
  148. const surveys = await this.surveyModel.count();
  149. arr.push({
  150. name: '交流互动',
  151. value: surveys,
  152. });
  153. const trans = await this.tranModel.aggregate([
  154. { $match: { status: { $in: [ '0', '1', '3' ] } } },
  155. {
  156. $group: {
  157. _id: '$status',
  158. value: { $sum: 1 },
  159. },
  160. },
  161. ]);
  162. arr.push({
  163. name: '正在洽谈',
  164. value: _.get(trans.find(f => f._id === '0'), 'value', 0),
  165. });
  166. arr.push({
  167. name: '达成意向',
  168. value: _.get(trans.find(f => f._id === '1'), 'value', 0),
  169. });
  170. arr.push({
  171. name: '对接完成',
  172. value: _.get(trans.find(f => f._id === '3'), 'value', 0),
  173. });
  174. return arr;
  175. }
  176. /**
  177. * 专家列表
  178. */
  179. expertList() {
  180. return [
  181. { name: '中科院长春光学精密机械与物理研究所', alias: '光机所', sort: 1 },
  182. { name: '中国科学院长春应用化学研究所', alias: '应化所', sort: 2 },
  183. { name: '中国科学院东北地理与农业生态研究所', alias: '地理所', sort: 3 },
  184. { name: '吉林大学', sort: 4 },
  185. { name: '长春工业大学', sort: 5 },
  186. ];
  187. }
  188. /**
  189. * 专利单位列表
  190. */
  191. patentUnitList() {
  192. return [
  193. { name: '吉林大学' },
  194. { name: '长春理工大学' },
  195. { name: '东北电力大学' },
  196. { name: '吉林工程技术师范学院' },
  197. { name: '长春工业大学' },
  198. { name: '北华大学' },
  199. { name: '吉林农业大学' },
  200. { name: '吉林师范大学' },
  201. { name: '长春工程学院' },
  202. { name: '吉林建筑大学' },
  203. { name: '通化师范学院' },
  204. { name: '长春大学' },
  205. { name: '东北师范大学' },
  206. { name: '吉林农业科技学院' },
  207. ];
  208. }
  209. // 展会首页统计
  210. async dockIndex({ dock_id }) {
  211. // 同时在线人数(伪)
  212. const tszx = await this.redis.get('login_number');
  213. // 特邀嘉宾
  214. const tyjb = await this.personalModel.count({ is_expert: true });
  215. // 洽谈合作 达成意向 交易完成
  216. const trans = await this.tranModel.aggregate([
  217. { $match: { status: { $in: [ '0', '1', '3' ] } } },
  218. {
  219. $group: {
  220. _id: '$status',
  221. value: { $sum: 1 },
  222. },
  223. },
  224. ]);
  225. const qthz = _.get(trans.find(f => f._id === '0'), 'value', 0);
  226. const dcyx = _.get(trans.find(f => f._id === '1'), 'value', 0);
  227. const jywc = _.get(trans.find(f => f._id === '3'), 'value', 0);
  228. // 参展项目
  229. const res = await this.dockUser.aggregate()
  230. .match({ dock_id: ObjectId(dock_id), 'goodsList.type': '1', 'goodsList.dockStatus': '1' })
  231. .unwind('$goodsList')
  232. .group({
  233. _id: '$dock_id',
  234. count: { $sum: 1 },
  235. });
  236. const czxm = _.get(res[0], 'count');
  237. const arr = [
  238. { name: '同时在线', num: tszx, unit: '人' },
  239. { name: '特邀嘉宾', num: tyjb, unit: '人' },
  240. { name: '洽谈合作', num: qthz, unit: '项' },
  241. { name: '达成意向', num: dcyx, unit: '项' },
  242. { name: '交易完成', num: jywc, unit: '项' },
  243. { name: '参展项目', num: czxm, unit: '项' },
  244. ];
  245. return arr;
  246. }
  247. }
  248. module.exports = IndexService;