'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;