index.js 7.4 KB

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