|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
-
- //
-
-
-
-
-
-
-
-
- namespace SafeCampus.System;
-
- /// <summary>
- /// <inheritdoc cref="IOperateLogService"/>
- /// </summary>
- public class OperateLogService : DbRepository<SysLogOperate>, IOperateLogService
- {
- /// <summary>
- /// 操作日志中文名称
- /// </summary>
- private readonly string _nameOperate = "操作日志";
-
- /// <summary>
- /// 异常日志中文名称
- /// </summary>
- private readonly string _nameException = "异常日志";
-
- /// <summary>
- /// 分表最多查近多少年的数据
- /// </summary>
- private readonly int _maxTabs = 100;
-
- /// <inheritdoc />
- public async Task<SqlSugarPagedList<SysLogOperate>> Page(OperateLogPageInput input)
- {
- var query = Context.Queryable<SysLogOperate>().WhereIF(!string.IsNullOrEmpty(input.Account), it => it.OpAccount == input.Account)//根据账号查询
- .WhereIF(!string.IsNullOrEmpty(input.Category), it => it.Category == input.Category)//根据分类查询
- .WhereIF(!string.IsNullOrEmpty(input.SearchKey), it => it.Name.Contains(input.SearchKey) || it.OpIp.Contains(input.SearchKey))//根据关键字查询
- .IgnoreColumns(it => new { it.ParamJson, it.ResultJson }).SplitTable(tabs => tabs.Take(_maxTabs))
- .OrderByIF(!string.IsNullOrEmpty(input.SortField), $"{input.SortField} {input.SortOrder}")//排序
- .OrderBy(it => it.CreateTime, OrderByType.Desc);
- var pageInfo = await query.ToPagedListAsync(input.PageNum, input.PageSize);//分页
- return pageInfo;
- }
-
- /// <inheritdoc />
- public async Task<List<OperateLogDayStatisticsOutput>> StatisticsByDay(int day)
- {
- //取最近七天
- var dayArray = Enumerable.Range(0, day).Select(it => DateTime.Now.Date.AddDays(it * -1)).ToList();
- //生成时间表
- var queryableLeft = Context.Reportable(dayArray).ToQueryable<DateTime>();
- //ReportableDateType.MonthsInLast1yea 表式近一年月份 并且queryable之后还能在where过滤
- var queryableRight = Context.Queryable<SysLogOperate>().SplitTable(tabs => tabs.Take(_maxTabs));//声名表
- //报表查询
- var list = await Context
- .Queryable(queryableLeft, queryableRight, JoinType.Left,
- (x1, x2) => x2.CreateTime.Value.ToString("yyyy-MM-dd") == x1.ColumnName.ToString("yyyy-MM-dd"))
- .GroupBy((x1, x2) => x1.ColumnName)//根据时间分组
- .OrderBy((x1, x2) => x1.ColumnName)//根据时间升序排序
- .Select((x1, x2) => new
- {
- OperateCount = SqlFunc.AggregateSum(SqlFunc.IIF(x2.Category == CateGoryConst.LOG_OPERATE, 1, 0)),//null的数据要为0所以不能用count
- ExceptionCount = SqlFunc.AggregateSum(SqlFunc.IIF(x2.Category == CateGoryConst.LOG_EXCEPTION, 1, 0)),//null的数据要为0所以不能用count
- Date = x1.ColumnName.ToString("yyyy-MM-dd")
- }).ToListAsync();
- //定义返回结果
- var result = new List<OperateLogDayStatisticsOutput>();
- //遍历结果
- list.ForEach(it =>
- {
- result.Add(new OperateLogDayStatisticsOutput { Date = it.Date, Name = _nameOperate, Count = it.OperateCount });//添加访问日志
- result.Add(new OperateLogDayStatisticsOutput { Date = it.Date, Name = _nameException, Count = it.ExceptionCount });//添加异常日志
- });
- return result;
- }
-
- /// <inheritdoc />
- public async Task<List<OperateLogTotalCountOutput>> TotalCount()
- {
- var data = await Context.Queryable<SysLogOperate>().SplitTable(tabs => tabs.Take(_maxTabs)).GroupBy(it => it.Category)//根据分类分组
- .Select(it => new
- {
- it.Category,//分类
- Count = SqlFunc.AggregateCount(it.Category)//数量
- }).ToListAsync();
- //定义结果数组
- var operateLogTotalCounts = new List<OperateLogTotalCountOutput>
- {
- //添加操作日志数据
- new OperateLogTotalCountOutput
- {
- Type = _nameOperate,
- Value = data.Where(it => it.Category == CateGoryConst.LOG_OPERATE).Select(it => it.Count).FirstOrDefault()
- },
- //添加异常日志数据
- new OperateLogTotalCountOutput
- {
- Type = _nameException,
- Value = data.Where(it => it.Category == CateGoryConst.LOG_EXCEPTION).Select(it => it.Count).FirstOrDefault()
- }
- };
- return operateLogTotalCounts;
- }
-
- /// <inheritdoc />
- public async Task Delete(string category)
- {
- await Context.Deleteable<SysLogOperate>().Where(it => it.Category == category).SplitTable(tabs => tabs.Take(_maxTabs))
- .ExecuteCommandAsync();//删除对应分类日志
- }
-
- /// <inheritdoc />
- public async Task<SysLogOperate> Detail(BaseIdInput input)
- {
- return await GetFirstSplitTableAsync(it => it.Id == input.Id, tabs => tabs.Take(_maxTabs));
- }
- }
|