平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OperateLogService.cs 4.9 KiB

4 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. 
  2. //
  3. namespace SafeCampus.System;
  4. /// <summary>
  5. /// <inheritdoc cref="IOperateLogService"/>
  6. /// </summary>
  7. public class OperateLogService : DbRepository<SysLogOperate>, IOperateLogService
  8. {
  9. /// <summary>
  10. /// 操作日志中文名称
  11. /// </summary>
  12. private readonly string _nameOperate = "操作日志";
  13. /// <summary>
  14. /// 异常日志中文名称
  15. /// </summary>
  16. private readonly string _nameException = "异常日志";
  17. /// <summary>
  18. /// 分表最多查近多少年的数据
  19. /// </summary>
  20. private readonly int _maxTabs = 100;
  21. /// <inheritdoc />
  22. public async Task<SqlSugarPagedList<SysLogOperate>> Page(OperateLogPageInput input)
  23. {
  24. var query = Context.Queryable<SysLogOperate>().WhereIF(!string.IsNullOrEmpty(input.Account), it => it.OpAccount == input.Account)//根据账号查询
  25. .WhereIF(!string.IsNullOrEmpty(input.Category), it => it.Category == input.Category)//根据分类查询
  26. .WhereIF(!string.IsNullOrEmpty(input.SearchKey), it => it.Name.Contains(input.SearchKey) || it.OpIp.Contains(input.SearchKey))//根据关键字查询
  27. .IgnoreColumns(it => new { it.ParamJson, it.ResultJson }).SplitTable(tabs => tabs.Take(_maxTabs))
  28. .OrderByIF(!string.IsNullOrEmpty(input.SortField), $"{input.SortField} {input.SortOrder}")//排序
  29. .OrderBy(it => it.CreateTime, OrderByType.Desc);
  30. var pageInfo = await query.ToPagedListAsync(input.PageNum, input.PageSize);//分页
  31. return pageInfo;
  32. }
  33. /// <inheritdoc />
  34. public async Task<List<OperateLogDayStatisticsOutput>> StatisticsByDay(int day)
  35. {
  36. //取最近七天
  37. var dayArray = Enumerable.Range(0, day).Select(it => DateTime.Now.Date.AddDays(it * -1)).ToList();
  38. //生成时间表
  39. var queryableLeft = Context.Reportable(dayArray).ToQueryable<DateTime>();
  40. //ReportableDateType.MonthsInLast1yea 表式近一年月份 并且queryable之后还能在where过滤
  41. var queryableRight = Context.Queryable<SysLogOperate>().SplitTable(tabs => tabs.Take(_maxTabs));//声名表
  42. //报表查询
  43. var list = await Context
  44. .Queryable(queryableLeft, queryableRight, JoinType.Left,
  45. (x1, x2) => x2.CreateTime.Value.ToString("yyyy-MM-dd") == x1.ColumnName.ToString("yyyy-MM-dd"))
  46. .GroupBy((x1, x2) => x1.ColumnName)//根据时间分组
  47. .OrderBy((x1, x2) => x1.ColumnName)//根据时间升序排序
  48. .Select((x1, x2) => new
  49. {
  50. OperateCount = SqlFunc.AggregateSum(SqlFunc.IIF(x2.Category == CateGoryConst.LOG_OPERATE, 1, 0)),//null的数据要为0所以不能用count
  51. ExceptionCount = SqlFunc.AggregateSum(SqlFunc.IIF(x2.Category == CateGoryConst.LOG_EXCEPTION, 1, 0)),//null的数据要为0所以不能用count
  52. Date = x1.ColumnName.ToString("yyyy-MM-dd")
  53. }).ToListAsync();
  54. //定义返回结果
  55. var result = new List<OperateLogDayStatisticsOutput>();
  56. //遍历结果
  57. list.ForEach(it =>
  58. {
  59. result.Add(new OperateLogDayStatisticsOutput { Date = it.Date, Name = _nameOperate, Count = it.OperateCount });//添加访问日志
  60. result.Add(new OperateLogDayStatisticsOutput { Date = it.Date, Name = _nameException, Count = it.ExceptionCount });//添加异常日志
  61. });
  62. return result;
  63. }
  64. /// <inheritdoc />
  65. public async Task<List<OperateLogTotalCountOutput>> TotalCount()
  66. {
  67. var data = await Context.Queryable<SysLogOperate>().SplitTable(tabs => tabs.Take(_maxTabs)).GroupBy(it => it.Category)//根据分类分组
  68. .Select(it => new
  69. {
  70. it.Category,//分类
  71. Count = SqlFunc.AggregateCount(it.Category)//数量
  72. }).ToListAsync();
  73. //定义结果数组
  74. var operateLogTotalCounts = new List<OperateLogTotalCountOutput>
  75. {
  76. //添加操作日志数据
  77. new OperateLogTotalCountOutput
  78. {
  79. Type = _nameOperate,
  80. Value = data.Where(it => it.Category == CateGoryConst.LOG_OPERATE).Select(it => it.Count).FirstOrDefault()
  81. },
  82. //添加异常日志数据
  83. new OperateLogTotalCountOutput
  84. {
  85. Type = _nameException,
  86. Value = data.Where(it => it.Category == CateGoryConst.LOG_EXCEPTION).Select(it => it.Count).FirstOrDefault()
  87. }
  88. };
  89. return operateLogTotalCounts;
  90. }
  91. /// <inheritdoc />
  92. public async Task Delete(string category)
  93. {
  94. await Context.Deleteable<SysLogOperate>().Where(it => it.Category == category).SplitTable(tabs => tabs.Take(_maxTabs))
  95. .ExecuteCommandAsync();//删除对应分类日志
  96. }
  97. /// <inheritdoc />
  98. public async Task<SysLogOperate> Detail(BaseIdInput input)
  99. {
  100. return await GetFirstSplitTableAsync(it => it.Id == input.Id, tabs => tabs.Take(_maxTabs));
  101. }
  102. }