namespace MiaoYu.Api.Admin.ApplicationServices.Systems; /// /// 操作日服务 /// public class SysOperationLogService : ApplicationService> { private readonly IRepository _sysUserRepository; public SysOperationLogService(IRepository defaultRepository, IRepository sysUserRepository) : base(defaultRepository) { _sysUserRepository = sysUserRepository; } /// /// 获取列表数据 /// /// /// public async Task FindListAsync(PagingSearchInput pagingSearchInput) { var query = (from log in _defaultRepository.Select.OrderByDescending(w => w.CreationTime) from use in _sysUserRepository.Select.Where(w => w.Id == log.UserId).DefaultIfEmpty() select new { log, use }) .WhereIf(!string.IsNullOrWhiteSpace(pagingSearchInput.Search.Api), w => w.log.Api.Contains(pagingSearchInput.Search.Api)) .WhereIf(!string.IsNullOrWhiteSpace(pagingSearchInput.Search.Browser), w => w.log.Browser.Contains(pagingSearchInput.Search.Browser)) .WhereIf(!string.IsNullOrWhiteSpace(pagingSearchInput.Search.Ip), w => w.log.Ip.Contains(pagingSearchInput.Search.Ip)) .WhereIf(!string.IsNullOrWhiteSpace(pagingSearchInput.Search.OS), w => w.log.OS.Contains(pagingSearchInput.Search.OS)) .WhereIf(pagingSearchInput.Search.StartTime != null, w => w.log.CreationTime.Date >= pagingSearchInput.Search.StartTime.Value) .WhereIf(pagingSearchInput.Search.EndTime != null, w => w.log.CreationTime.Date <= pagingSearchInput.Search.EndTime.Value) .Select(w => new { w.log.Api, w.log.Browser, w.log.Ip, w.log.OS, w.log.TakeUpTime, UserName = w.use.Name, w.use.LoginName, w.log.ControllerDisplayName, w.log.ActionDisplayName, w.log.CreationTime, w.log.Id, }) ; var result = await _defaultRepository.AsPagingViewAsync(query, pagingSearchInput); //覆盖值 result .FormatValue(query, w => w.CreationTime, (oldValue) => oldValue.ToString("yyyy-MM-dd HH:mm:ss")) ; return result; } /// /// 删除所有数据 /// /// public async Task DeletedAllData() { return await _defaultRepository.DeleteAsync(w => 1 == 1) >= 0; } /// /// 查询表单数据 /// /// /// public async Task> FindFormAsync(Guid id) { var res = new Dictionary(); var form = await _defaultRepository.FindByIdAsync(id); form = form.NullSafe(); var use = await _sysUserRepository.FindByIdAsync(form.UserId); use = use.NullSafe(); res[nameof(id)] = id == Guid.Empty ? "" : id; res[nameof(form)] = form; res[nameof(use)] = use; return res; } /// /// 定时清理日志 保留一个月 /// /// [Scheduled("59 59 23 ? * *", Remark = "每天晚上 23:59:59 执行")] public async Task ClearLogAsync() { var now = DateTime.Now; await _defaultRepository.DeleteBulkAsync(w => w.CreationTime < now.AddMonths(-1)); return true; } }