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

94 line
4.0 KiB

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