'use strict'; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const { ObjectId } = require('mongoose').Types; const _ = require('lodash'); const assert = require('assert'); // 首页-数据动态统计 class IndexService extends CrudService { constructor(ctx) { super(ctx, 'index'); this.redis = this.app.redis; this.patentModel = this.ctx.model.Patent; this.code = this.ctx.model.Code; this.productModel = this.ctx.model.Product; this.expertModel = this.ctx.model.Expert; // this.userModel = this.ctx.model.User; this.personalModel = this.ctx.model.Personal; this.organizationModel = this.ctx.model.Organization; this.surveyModel = this.ctx.model.Survey; this.dockUser = this.ctx.model.DockUser; this.tranModel = this.ctx.model.Transaction; } /** * 首页专利统计 */ async patent() { const res = await this.patentModel.aggregate([ { $group: { _id: '$apply_personal', value: { $sum: 1 }, }, }, ]); let arr = []; const other = { name: '其他', value: 0 }; for (const i of res) { const { _id, value } = i; const unitList = this.patentUnitList(); const unit = unitList.find( f => (_id && _id.includes(f.name)) || f.name.includes(_id) ); if (unit) { // 说明是需要单拎出来的数据,现查arr中是否有该单位:有=>数字合并;没有=>创建条目 const { name } = unit; const arrItem = arr.find(f => f.name === name); if (arrItem) { const index = arr.findIndex(f => f.name === name); arr[index] = { name, value: (arrItem.value || 0) + value }; } else { arr.push({ name, value }); } } else { other.value += value; } } arr = _.orderBy(arr, [ 'value' ], [ 'desc' ]); arr.push(other); return arr; } /** * 首页成果统计 */ async product() { const categroy = await this.code.find({ category: '01' }); const condition = { type: '1', status: '2' }; const arr = []; for (const c of categroy) { const { name } = c; const value = await this.productModel.count({ ...condition, field: name, }); arr.push({ value, name }); } return arr; } /** * 首页统计专家 */ async expert() { const res = await this.expertModel.aggregate([ { $group: { _id: '$company', value: { $sum: 1 }, }, }, ]); let arr = []; const other = { name: '其他', value: 0 }; for (const i of res) { const { _id, value } = i; const unitList = this.expertList(); const unit = unitList.find( f => (_id && _id.includes(f.name)) || f.name.includes(_id) ); if (unit) { // 说明是需要单拎出来的数据,现查arr中是否有该单位:有=>数字合并;没有=>创建条目 const { name, sort } = unit; const arrItem = arr.find(f => f.name === name); if (arrItem) { const index = arr.findIndex(f => f.name === name); arr[index] = { ...arr[index], value: (arrItem.value || 0) + value }; } else { arr.push({ name, value, sort }); } } else { other.value += value; } } arr = _.orderBy(arr, [ 'sort' ], [ 'asc' ]); arr.push(other); // 换名 arr.map(i => { const r = this.expertList().find(f => f.name === i.name); if (r && r.alias) i.name = r.alias; return i; }); return arr; } /** * 首页数据统计 */ async circle() { const arr = []; const organization = await this.organizationModel.count({ status: '1' }); arr.push({ name: '企业注册数量', value: organization, }); // TODO:如果需要专家和个人用户区分开,则is_expert:false, 混查则不需要is_expert条件 const user = await this.personalModel.count({ status: '1', is_expert: false }); arr.push({ name: '个人注册数量', value: user, }); const exports = await this.expertModel.count(); arr.push({ name: '专家注册数量', value: exports, }); const products = await this.productModel.count({ status: '2' }); arr.push({ name: '产品发布数量', value: products, }); const surveys = await this.surveyModel.count(); arr.push({ name: '交流互动', value: surveys, }); const trans = await this.tranModel.aggregate([ { $match: { status: { $in: [ '0', '1', '3' ] } } }, { $group: { _id: '$status', value: { $sum: 1 }, }, }, ]); arr.push({ name: '正在洽谈', value: _.get(trans.find(f => f._id === '0'), 'value', 0), }); arr.push({ name: '达成意向', value: _.get(trans.find(f => f._id === '1'), 'value', 0), }); arr.push({ name: '对接完成', value: _.get(trans.find(f => f._id === '3'), 'value', 0), }); return arr; } /** * 专家列表 */ expertList() { return [ { name: '中科院长春光学精密机械与物理研究所', alias: '光机所', sort: 1 }, { name: '中国科学院长春应用化学研究所', alias: '应化所', sort: 2 }, { name: '中国科学院东北地理与农业生态研究所', alias: '地理所', sort: 3 }, { name: '吉林大学', sort: 4 }, { name: '长春工业大学', sort: 5 }, ]; } /** * 专利单位列表 */ patentUnitList() { return [ { name: '吉林大学' }, { name: '长春理工大学' }, { name: '东北电力大学' }, { name: '吉林工程技术师范学院' }, { name: '长春工业大学' }, { name: '北华大学' }, { name: '吉林农业大学' }, { name: '吉林师范大学' }, { name: '长春工程学院' }, { name: '吉林建筑大学' }, { name: '通化师范学院' }, { name: '长春大学' }, { name: '东北师范大学' }, { name: '吉林农业科技学院' }, ]; } // 展会首页统计 async dockIndex({ dock_id }) { // 同时在线人数(伪) const tszx = await this.redis.get('login_number'); // 特邀嘉宾 const tyjb = await this.personalModel.count({ is_expert: true }); // 洽谈合作 达成意向 交易完成 const trans = await this.tranModel.aggregate([ { $match: { status: { $in: [ '0', '1', '3' ] } } }, { $group: { _id: '$status', value: { $sum: 1 }, }, }, ]); const qthz = _.get(trans.find(f => f._id === '0'), 'value', 0); const dcyx = _.get(trans.find(f => f._id === '1'), 'value', 0); const jywc = _.get(trans.find(f => f._id === '3'), 'value', 0); // 参展项目 const res = await this.dockUser.aggregate() .match({ dock_id: ObjectId(dock_id), 'goodsList.type': '1', 'goodsList.dockStatus': '1' }) .unwind('$goodsList') .group({ _id: '$dock_id', count: { $sum: 1 }, }); const czxm = _.get(res[0], 'count'); const arr = [ { name: '同时在线', num: tszx, unit: '人' }, { name: '特邀嘉宾', num: tyjb, unit: '人' }, { name: '洽谈合作', num: qthz, unit: '项' }, { name: '达成意向', num: dcyx, unit: '项' }, { name: '交易完成', num: jywc, unit: '项' }, { name: '参展项目', num: czxm, unit: '项' }, ]; return arr; } } module.exports = IndexService;