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

VisitLogService.cs 4.0 KiB

4 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 
  2. //
  3. namespace SafeCampus.System;
  4. /// <summary>
  5. /// <inheritdoc cref="IVisitLogService"/>
  6. /// </summary>
  7. public class VisitLogService : DbRepository<SysLogVisit>, IVisitLogService
  8. {
  9. /// <summary>
  10. /// 分表最多查近多少年的数据
  11. /// </summary>
  12. private readonly int _maxTabs = 100;
  13. /// <inheritdoc />
  14. public async Task<SqlSugarPagedList<SysLogVisit>> Page(VisitLogPageInput input)
  15. {
  16. var query = Context.Queryable<SysLogVisit>().WhereIF(!string.IsNullOrEmpty(input.Account), it => it.OpAccount == input.Account)//根据账号查询
  17. .WhereIF(!string.IsNullOrEmpty(input.Category), it => it.Category == input.Category)//根据分类查询
  18. .WhereIF(!string.IsNullOrEmpty(input.SearchKey), it => it.Name.Contains(input.SearchKey) || it.OpIp.Contains(input.SearchKey))//根据关键字查询
  19. .SplitTable(tabs => tabs.Take(_maxTabs)).OrderByIF(!string.IsNullOrEmpty(input.SortField), $"{input.SortField} {input.SortOrder}")//排序
  20. .OrderBy(it => it.CreateTime, OrderByType.Desc);
  21. var pageInfo = await query.ToPagedListAsync(input.PageNum, input.PageSize);//分页
  22. return pageInfo;
  23. }
  24. /// <inheritdoc />
  25. public async Task<List<VisitLogDayStatisticsOutput>> StatisticsByDay(int day)
  26. {
  27. //取最近七天
  28. var dayArray = Enumerable.Range(0, day).Select(it => DateTime.Now.Date.AddDays(it * -1)).ToList();
  29. //生成时间表
  30. var queryableLeft = Context.Reportable(dayArray).ToQueryable<DateTime>();
  31. //ReportableDateType.MonthsInLast1yea 表式近一年月份 并且queryable之后还能在where过滤
  32. var queryableRight = Context.Queryable<SysLogVisit>().SplitTable(tabs => tabs.Take(_maxTabs));//声名表
  33. //报表查询
  34. var list = await Context
  35. .Queryable(queryableLeft, queryableRight, JoinType.Left,
  36. (x1, x2) => x2.CreateTime.Value.ToString("yyyy-MM-dd") == x1.ColumnName.ToString("yyyy-MM-dd"))
  37. .GroupBy((x1, x2) => x1.ColumnName)//根据时间分组
  38. .OrderBy((x1, x2) => x1.ColumnName)//根据时间升序排序
  39. .Select((x1, x2) => new VisitLogDayStatisticsOutput
  40. {
  41. LoginCount = SqlFunc.AggregateSum(SqlFunc.IIF(x2.Category == CateGoryConst.LOG_LOGIN, 1, 0)),//null的数据要为0所以不能用count
  42. LogoutCount = SqlFunc.AggregateSum(SqlFunc.IIF(x2.Category == CateGoryConst.LOG_LOGOUT, 1, 0)),//null的数据要为0所以不能用count
  43. Date = x1.ColumnName.ToString("yyyy-MM-dd")
  44. }).ToListAsync();
  45. return list;
  46. }
  47. /// <inheritdoc />
  48. public async Task<List<VisitLogTotalCountOutput>> TotalCount()
  49. {
  50. var data = await Context.Queryable<SysLogVisit>().SplitTable(tabs => tabs.Take(_maxTabs)).GroupBy(it => it.Category)//根据分类分组
  51. .Select(it => new
  52. {
  53. it.Category,//分类
  54. Count = SqlFunc.AggregateCount(it.Category)//数量
  55. }).ToListAsync();
  56. //定义结果数组
  57. var visitLogTotalCounts = new List<VisitLogTotalCountOutput>
  58. {
  59. //添加登录数据
  60. new VisitLogTotalCountOutput
  61. {
  62. Type = EventSubscriberConst.LOGIN_B,
  63. Value = data.Where(it => it.Category == CateGoryConst.LOG_LOGIN).Select(it => it.Count).FirstOrDefault()
  64. },
  65. //添加登出数据
  66. new VisitLogTotalCountOutput
  67. {
  68. Type = EventSubscriberConst.LOGIN_OUT_B,
  69. Value = data.Where(it => it.Category == CateGoryConst.LOG_LOGOUT).Select(it => it.Count).FirstOrDefault()
  70. }
  71. };
  72. return visitLogTotalCounts;
  73. }
  74. /// <inheritdoc />
  75. public async Task Delete(string category)
  76. {
  77. await Context.Deleteable<SysLogVisit>().Where(it => it.Category == category).SplitTable(tabs => tabs.Take(_maxTabs))
  78. .ExecuteCommandAsync();//删除对应分类日志
  79. }
  80. }