sysLogService.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use strict';
  2. const Service = require('../service/baseService');
  3. class SysLogService extends Service {
  4. tag() {
  5. return this.ctx.model.SysLogModel;
  6. }
  7. // 分页查询
  8. async listForPage(data) {
  9. const { model } = this.ctx;
  10. const page = data.page;
  11. const rows = Number.parseInt(data.rows) || this.app.config.defaultPageSize;
  12. delete data.page;
  13. delete data.rows;
  14. const where = {};
  15. if (data.dept1) {
  16. where.dept1 = this.app.mongoose.Types.ObjectId(data.dept1);// 省
  17. }
  18. if (data.dept2) {
  19. where.dept2 = this.app.mongoose.Types.ObjectId(data.dept2); // 市
  20. }
  21. if (data.dept3) {
  22. where.dept3 = this.app.mongoose.Types.ObjectId(data.dept3); // 区
  23. }
  24. if (data.dept4) {
  25. where.dept4 = this.app.mongoose.Types.ObjectId(data.dept4); // 乡
  26. }
  27. if (data.dept5) {
  28. where.dept5 = this.app.mongoose.Types.ObjectId(data.dept5); // 社区
  29. }
  30. if (data.loginName) {
  31. where.loginName = { $regex: data.loginName };
  32. }
  33. if (data.state) {
  34. where.state = data.state;
  35. }
  36. if (data.startTime && data.endTime) {
  37. // where.time = { $gte: data.startTime + ' 00:00:00', $lt: data.endTime + ' 23:59:59' };
  38. // where.time = { $gte: ISODate(data.startTime + ' 00:00:00'), $lte: ISODate(data.endTime + ' 23:59:59') };
  39. where.time = { $gte: new Date(data.startTime + ' 00:00:00'), $lt: new Date(data.endTime + ' 23:59:59') };
  40. }
  41. this.ctx.logger.info('条件', where);
  42. const pop = [
  43. {
  44. path: 'dept1',
  45. select: 'name',
  46. },
  47. {
  48. path: 'dept2',
  49. select: 'name',
  50. },
  51. {
  52. path: 'dept3',
  53. select: 'name',
  54. },
  55. {
  56. path: 'dept4',
  57. select: 'name',
  58. },
  59. {
  60. path: 'dept5',
  61. select: 'name',
  62. },
  63. ];
  64. // const nowday = new Date();
  65. // const finalday = nowday.getFullYear() + '-' + (nowday.getMonth() + 1) + '-' + nowday.getDate();
  66. // const match = {};
  67. // match.time = { $gte: data.startTime + ' 00:00:00', $lt: data.endTime + ' 23:59:59' };
  68. const nowdaytotal = await model.SysLogModel.aggregate([
  69. // { $project: { time: { $dateToString: { format: '%Y-%m-%d %H:%M:%S', date: '$time' } }, _id: 1 } },
  70. { $match: where },
  71. // { $match: { time: { $gte: ISODate('2020-10-30 00:00:00'), $lte: ISODate('2020-10-30 23:59:59') } } },
  72. // { $group: { _id: '$_id' } },
  73. { $group: { _id: '$loginName' } },
  74. { $count: 'total' },
  75. ]);
  76. this.ctx.logger.info('使用人次============' + JSON.stringify(nowdaytotal));
  77. const count = await model.SysLogModel.find(where).populate(pop).countDocuments();
  78. const result = await model.SysLogModel.find(where).populate(pop).skip((page - 1) * rows)
  79. .limit(rows)
  80. .sort({ time: -1 });
  81. return {
  82. count,
  83. list: result,
  84. nowdaytotal: nowdaytotal[0] ? nowdaytotal[0].total : '0',
  85. };
  86. }
  87. }
  88. module.exports = SysLogService;