平安校园
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.
 
 
 
 
 
 

119 lines
4.9 KiB

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