BootstrapAdmin/Bootstrap.DataAccess.MongoDB/Log.cs

106 lines
4.3 KiB
C#
Raw Normal View History

2019-05-01 16:46:24 +08:00
using Longbow.Web.Mvc;
using MongoDB.Driver;
2019-05-01 16:46:24 +08:00
using PetaPoco;
2018-10-30 13:07:29 +08:00
using System;
using System.Collections.Generic;
2019-05-01 16:46:24 +08:00
using System.Linq;
2018-10-30 13:07:29 +08:00
namespace Bootstrap.DataAccess.MongoDB
{
/// <summary>
///
/// </summary>
public class Log : DataAccess.Log
{
2019-05-01 16:46:24 +08:00
/// <summary>
///
/// </summary>
/// <param name="po"></param>
/// <param name="startTime"></param>
/// <param name="endTime"></param>
/// <param name="opType"></param>
2018-10-30 13:07:29 +08:00
/// <returns></returns>
public override Page<DataAccess.Log> RetrievePages(PaginationOption po, DateTime? startTime, DateTime? endTime, string opType)
2019-05-01 16:46:24 +08:00
{
var filterBuilder = Builders<DataAccess.Log>.Filter;
var filter = filterBuilder.Empty;
if (startTime.HasValue) filter = filterBuilder.Gte("LogTime", startTime.Value);
if (endTime.HasValue) filter = filterBuilder.Lt("LogTime", endTime.Value.AddDays(1).AddSeconds(-1));
if (startTime == null && endTime == null) filter = filterBuilder.Gt("LogTime", DateTime.Today.AddMonths(0 - DictHelper.RetrieveAccessLogPeriod()));
if (!string.IsNullOrEmpty(opType)) filter = filterBuilder.Eq("CRUD", opType);
// sort
var sortBuilder = Builders<DataAccess.Log>.Sort;
2019-05-01 16:46:24 +08:00
SortDefinition<DataAccess.Log> sort = null;
switch (po.Sort)
{
2019-05-01 16:46:24 +08:00
case "CRUD":
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.CRUD) : sortBuilder.Descending(t => t.CRUD);
2019-05-01 16:46:24 +08:00
break;
case "UserName":
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.UserName) : sortBuilder.Descending(t => t.UserName);
2019-05-01 16:46:24 +08:00
break;
case "LogTime":
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.LogTime) : sortBuilder.Descending(t => t.LogTime);
2019-05-01 16:46:24 +08:00
break;
case "Ip":
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.Ip) : sortBuilder.Descending(t => t.Ip);
2019-05-01 16:46:24 +08:00
break;
case "RequestUrl":
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.RequestUrl) : sortBuilder.Descending(t => t.RequestUrl);
2019-05-01 16:46:24 +08:00
break;
}
var logs = DbManager.Logs.Find(filter).Sort(sort).ToList();
return new Page<DataAccess.Log>()
{
Context = logs,
CurrentPage = po.PageIndex,
ItemsPerPage = po.Limit,
TotalItems = logs.Count,
TotalPages = (long)Math.Ceiling(logs.Count * 1.0 / po.Limit),
Items = logs.Skip(po.Offset).Take(po.Limit).ToList()
};
2019-05-01 16:46:24 +08:00
}
/// <summary>
///
/// </summary>
/// <param name="startTime"></param>
/// <param name="endTime"></param>
/// <param name="opType"></param>
/// <returns></returns>
public override IEnumerable<DataAccess.Log> RetrieveAll(DateTime? startTime, DateTime? endTime, string opType)
{
var filterBuilder = Builders<DataAccess.Log>.Filter;
var filter = filterBuilder.Empty;
if (startTime.HasValue) filter = filterBuilder.Gte("LogTime", startTime.Value);
if (endTime.HasValue) filter = filterBuilder.Lt("LogTime", endTime.Value.AddDays(1).AddSeconds(-1));
if (!string.IsNullOrEmpty(opType)) filter = filterBuilder.Eq("CRUD", opType);
// sort
var sort = Builders<DataAccess.Log>.Sort.Ascending(t => t.LogTime);
return DbManager.Logs.Find(filter).Sort(sort).ToList();
}
/// <summary>
/// 删除日志信息
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private static void DeleteLogAsync() => System.Threading.Tasks.Task.Run(() => DbManager.Logs.DeleteMany(log => log.LogTime < DateTime.Now.AddDays(-7)));
/// <summary>
///
/// </summary>
/// <param name="log"></param>
/// <returns></returns>
public override bool Save(DataAccess.Log log)
{
DbManager.Logs.InsertOne(log);
DeleteLogAsync();
return true;
}
2018-10-30 13:07:29 +08:00
}
}