123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- 'use strict';
- const Service = require('../service/baseService');
- class SysLogService extends Service {
- tag() {
- return this.ctx.model.SysLogModel;
- }
- // 分页查询
- async listForPage(data) {
- const { model } = this.ctx;
- const page = data.page;
- const rows = Number.parseInt(data.rows) || this.app.config.defaultPageSize;
- delete data.page;
- delete data.rows;
- const where = {};
- if (data.dept1) {
- where.dept1 = this.app.mongoose.Types.ObjectId(data.dept1);// 省
- }
- if (data.dept2) {
- where.dept2 = this.app.mongoose.Types.ObjectId(data.dept2); // 市
- }
- if (data.dept3) {
- where.dept3 = this.app.mongoose.Types.ObjectId(data.dept3); // 区
- }
- if (data.dept4) {
- where.dept4 = this.app.mongoose.Types.ObjectId(data.dept4); // 乡
- }
- if (data.dept5) {
- where.dept5 = this.app.mongoose.Types.ObjectId(data.dept5); // 社区
- }
- if (data.loginName) {
- where.loginName = { $regex: data.loginName };
- }
- if (data.state) {
- where.state = data.state;
- }
- if (data.startTime && data.endTime) {
- // where.time = { $gte: data.startTime + ' 00:00:00', $lt: data.endTime + ' 23:59:59' };
- // where.time = { $gte: ISODate(data.startTime + ' 00:00:00'), $lte: ISODate(data.endTime + ' 23:59:59') };
- where.time = { $gte: new Date(data.startTime + ' 00:00:00'), $lt: new Date(data.endTime + ' 23:59:59') };
- }
- this.ctx.logger.info('条件', where);
- const pop = [
- {
- path: 'dept1',
- select: 'name',
- },
- {
- path: 'dept2',
- select: 'name',
- },
- {
- path: 'dept3',
- select: 'name',
- },
- {
- path: 'dept4',
- select: 'name',
- },
- {
- path: 'dept5',
- select: 'name',
- },
- ];
- // const nowday = new Date();
- // const finalday = nowday.getFullYear() + '-' + (nowday.getMonth() + 1) + '-' + nowday.getDate();
- // const match = {};
- // match.time = { $gte: data.startTime + ' 00:00:00', $lt: data.endTime + ' 23:59:59' };
- const nowdaytotal = await model.SysLogModel.aggregate([
- // { $project: { time: { $dateToString: { format: '%Y-%m-%d %H:%M:%S', date: '$time' } }, _id: 1 } },
- { $match: where },
- // { $match: { time: { $gte: ISODate('2020-10-30 00:00:00'), $lte: ISODate('2020-10-30 23:59:59') } } },
- // { $group: { _id: '$_id' } },
- { $group: { _id: '$loginName' } },
- { $count: 'total' },
- ]);
- this.ctx.logger.info('使用人次============' + JSON.stringify(nowdaytotal));
- const count = await model.SysLogModel.find(where).populate(pop).countDocuments();
- const result = await model.SysLogModel.find(where).populate(pop).skip((page - 1) * rows)
- .limit(rows)
- .sort({ time: -1 });
- return {
- count,
- list: result,
- nowdaytotal: nowdaytotal[0] ? nowdaytotal[0].total : '0',
- };
- }
- }
- module.exports = SysLogService;
|