123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- 'use strict';
- const Service = require('egg').Service;
- class IndividualOrDeptRankService extends Service {
- async visitdeptpage(query) {
- const { ctx } = this;
- let deptGroup = '';
- let params = {};
- if (query.time) {
- query.visitYM = query.time;
- delete query.time;
- }
- if (query.dept1) {
- deptGroup = '$dept2';
- params = { fid: query.dept1 };
- }
- if (query.dept2) {
- deptGroup = '$dept3';
- params = { fid: query.dept2 };
- }
- if (query.dept3) {
- deptGroup = '$dept4';
- params = { fid: query.dept3 };
- }
- if (query.dept4) {
- deptGroup = '$dept5';
- params = { fid: query.dept4 };
- }
- if (query.dept5) {
- deptGroup = '$dept5';
- params = { _id: query.dept5 };
- }
- const resultList = await ctx.model.VisitModel.aggregate([
- { $project: {
- _id: 1, dept1: 1, dept2: 1, dept3: 1, dept4: 1, dept5: 1,
- visitYM: { $dateToString: { format: '%Y-%m', date: { $add: [ '$visitTime', 1000 * 60 * 60 * 8 ] } } },
- },
- },
- { $match: ctx.alterDeptId(query) },
- { $group: {
- _id: deptGroup,
- count: { $sum: 1 },
- },
- },
- { $lookup: { from: 'sys_dept', localField: '_id', foreignField: '_id', as: 'deptInfo' } },
- { $unwind: { path: '$deptInfo', preserveNullAndEmptyArrays: true } },
- { $project: {
- _id: 0, name: '$deptInfo.name', count: 1,
- },
- },
- { $sort: { count: -1 } },
- ]);
- const deptInfoByFid = await ctx.model.SysDeptModel.find(params);
- const resultArr = [];
- const allResultArr = [];
- let result = [];
- try {
- resultList.forEach(item =>
- resultArr.push({ name: item.name, num: item.count })
- );
- deptInfoByFid.forEach(item =>
- allResultArr.push({ name: item.name, num: 0 })
- );
- allResultArr.forEach(itemA => {
- resultArr.forEach(item => {
- if (item.name == itemA.name) {
- itemA.num = item.num;
- }
- });
- });
- result = allResultArr.sort(ctx.compareTo('num', 'DESC'));
- } catch (e) {
- ctx.logger.error('catch--------error', e);
- ctx.error(e);
- } finally {
- return result;
- }
- }
- async visitpersonpage(query) {
- const { ctx } = this;
- if (query.time) {
- query.visitYM = query.time;
- delete query.time;
- }
- const resultList = await ctx.model.VisitModel.aggregate([
- { $project: {
- _id: 1, dept1: 1, dept2: 1, dept3: 1, dept4: 1, dept5: 1, userid: 1,
- visitYM: { $dateToString: { format: '%Y-%m', date: { $add: [ '$visitTime', 1000 * 60 * 60 * 8 ] } } },
- },
- },
- { $match: ctx.alterDeptId(query) },
- { $group: {
- _id: '$userid',
- count: { $sum: 1 },
- },
- },
- { $sort: { count: -1 } },
- {
- $skip: 0,
- },
- {
- $limit: 50,
- },
- { $lookup: { from: 'sys_user', localField: '_id', foreignField: '_id', as: 'userInfo' } },
- { $unwind: { path: '$userInfo', preserveNullAndEmptyArrays: true } },
- { $project: {
- _id: 1, name: '$userInfo.userName', count: 1, dept5: '$userInfo.dept5',
- },
- },
- { $lookup: { from: 'sys_dept', localField: 'dept5', foreignField: '_id', as: 'dept5Info' } },
- { $unwind: { path: '$dept5Info', preserveNullAndEmptyArrays: true } },
- { $project: {
- _id: 0, name: 1, count: 1, dept5: '$dept5Info.name',
- },
- },
- ]);
- const resultArr = [];
- try {
- resultList.forEach(item =>
- resultArr.push({ name: item.name, num: item.count, address: item.dept5 })
- );
- } catch (e) {
- ctx.logger.error('catch--------error', e);
- ctx.error(e);
- } finally {
- return resultArr;
- }
- }
- async infodeptpage(query) {
- const { ctx } = this;
- let deptGroup = '';
- let params = {};
- query.status = '3';
- if (query.time) {
- query.infoYM = query.time;
- delete query.time;
- }
- if (query.dept1) {
- deptGroup = '$dept2';
- params = { fid: query.dept1 };
- }
- if (query.dept2) {
- deptGroup = '$dept3';
- params = { fid: query.dept2 };
- }
- if (query.dept3) {
- deptGroup = '$dept4';
- params = { fid: query.dept3 };
- }
- if (query.dept4) {
- deptGroup = '$dept5';
- params = { fid: query.dept4 };
- }
- if (query.dept5) {
- deptGroup = '$dept5';
- params = { _id: query.dept5 };
- }
- const resultList = await ctx.model.InfoModel.aggregate([
- { $project: {
- _id: 1, dept1: 1, dept2: 1, dept3: 1, dept4: 1, dept5: 1, status: 1,
- infoYM: { $dateToString: { format: '%Y-%m', date: { $add: [ '$time', 1000 * 60 * 60 * 8 ] } } },
- },
- },
- { $match: ctx.alterDeptId(query) },
- { $group: {
- _id: deptGroup,
- count: { $sum: 1 },
- },
- },
- { $lookup: { from: 'sys_dept', localField: '_id', foreignField: '_id', as: 'deptInfo' } },
- { $unwind: { path: '$deptInfo', preserveNullAndEmptyArrays: true } },
- { $project: {
- _id: 1, name: '$deptInfo.name', count: 1,
- },
- },
- { $sort: { count: -1 } },
- ]);
- const deptInfoByFid = await ctx.model.SysDeptModel.find(params);
- const resultArr = [];
- const allResultArr = [];
- let result = [];
- try {
- resultList.forEach(item =>
- resultArr.push({ name: item.name, num: item.count })
- );
- deptInfoByFid.forEach(item =>
- allResultArr.push({ name: item.name, num: 0 })
- );
- allResultArr.forEach(itemA => {
- resultArr.forEach(item => {
- if (item.name == itemA.name) {
- itemA.num = item.num;
- }
- });
- });
- result = allResultArr.sort(ctx.compareTo('num', 'DESC'));
- } catch (e) {
- ctx.logger.error('catch--------error', e);
- ctx.error(e);
- } finally {
- return result;
- }
- }
- async infopersonpage(query) {
- const { ctx } = this;
- query.status = '3';
- if (query.time) {
- query.infoYM = query.time;
- delete query.time;
- }
- const resultList = await ctx.model.InfoModel.aggregate([
- { $project: {
- _id: 1, dept1: 1, dept2: 1, dept3: 1, dept4: 1, dept5: 1, status: 1, userid: 1,
- infoYM: { $dateToString: { format: '%Y-%m', date: { $add: [ '$time', 1000 * 60 * 60 * 8 ] } } },
- },
- },
- { $match: ctx.alterDeptId(query) },
- { $group: {
- _id: '$userid',
- count: { $sum: 1 },
- },
- },
- { $sort: { count: -1 } },
- {
- $skip: 0,
- },
- {
- $limit: 50,
- },
- { $lookup: { from: 'sys_user', localField: '_id', foreignField: '_id', as: 'userInfo' } },
- { $unwind: { path: '$userInfo', preserveNullAndEmptyArrays: true } },
- { $project: {
- _id: 1, name: '$userInfo.userName', count: 1, dept5: '$userInfo.dept5',
- },
- },
- { $lookup: { from: 'sys_dept', localField: 'dept5', foreignField: '_id', as: 'dept5Info' } },
- { $unwind: { path: '$dept5Info', preserveNullAndEmptyArrays: true } },
- { $project: {
- _id: 0, name: 1, count: 1, dept5: '$dept5Info.name',
- },
- },
- ]);
- const resultArr = [];
- try {
- resultList.forEach(item =>
- resultArr.push({ name: item.name, num: item.count, address: item.dept5 })
- );
- } catch (e) {
- ctx.logger.error('catch--------error', e);
- ctx.error(e);
- } finally {
- return resultArr;
- }
- }
- async selectDeptByName(query) {
- const { ctx } = this;
- const deptName = query.deptName;
- let resultStr = '';
- const resultList = await ctx.model.SysDeptModel.find({ name: deptName });
- if (resultList.length > 0) {
- if (resultList[0].fid === '0') {
- resultStr = resultList[0]._id;
- } else {
- const resultList2 = await ctx.model.SysDeptModel.find({ _id: this.app.mongoose.Types.ObjectId(resultList[0].fid) });
- if (resultList2[0].fid === '0') {
- resultStr = resultList2[0]._id + '&' + resultList[0]._id;
- } else {
- const resultList3 = await ctx.model.SysDeptModel.find({ _id: this.app.mongoose.Types.ObjectId(resultList2[0].fid) });
- if (resultList3[0].fid === '0') {
- resultStr = resultList3[0]._id + '&' + resultList2[0]._id + '&' + resultList[0]._id;
- }
- }
- }
- }
- return resultStr;
- }
- }
- module.exports = IndividualOrDeptRankService;
|