using Longbow.Web.Mvc; using PetaPoco; using System; using System.Collections.Generic; namespace Bootstrap.DataAccess { /// /// /// [TableName("Traces")] public class Trace { /// /// 获得/设置 操作日志主键ID /// public string Id { get; set; } /// /// 获得/设置 用户名称 /// public string UserName { get; set; } /// /// 获得/设置 操作时间 /// public DateTime LogTime { get; set; } /// /// 获得/设置 客户端IP /// public string Ip { get; set; } /// /// /// public string City { get; set; } /// /// /// public string Browser { get; set; } /// /// /// public string OS { get; set; } /// /// 获取/设置 请求网址 /// public string RequestUrl { get; set; } /// /// /// public string UserAgent { get; set; } /// /// 保存访问历史记录 /// /// public virtual bool Save(Trace p) { if (p == null) throw new ArgumentNullException(nameof(p)); DbManager.Create().Save(p); ClearTraces(); return true; } /// /// /// /// /// /// /// /// public virtual Page RetrievePages(PaginationOption po, DateTime? startTime, DateTime? endTime, string ip) { var sql = new Sql("select UserName, LogTime, IP, Browser, OS, City, RequestUrl from Traces"); if (startTime.HasValue) sql.Where("LogTime > @0", startTime.Value); if (endTime.HasValue) sql.Where("LogTime < @0", endTime.Value.AddDays(1).AddSeconds(-1)); if (startTime == null && endTime == null) sql.Where("LogTime > @0", DateTime.Today.AddMonths(0 - DictHelper.RetrieveAccessLogPeriod())); if (!string.IsNullOrEmpty(ip)) sql.Where("IP = @0", ip); sql.OrderBy($"{po.Sort} {po.Order}"); return DbManager.Create().Page(po.PageIndex, po.Limit, sql); } /// /// /// /// /// /// /// public virtual IEnumerable RetrieveAll(DateTime? startTime, DateTime? endTime, string ip) { var sql = new Sql("select UserName, LogTime, IP, Browser, OS, City, RequestUrl from Traces"); if (startTime.HasValue) sql.Where("LogTime > @0", startTime.Value); if (endTime.HasValue) sql.Where("LogTime < @0", endTime.Value.AddDays(1).AddSeconds(-1)); if (!string.IsNullOrEmpty(ip)) sql.Where("IP = @0", ip); sql.OrderBy("LogTime"); return DbManager.Create().Fetch(sql); } private static void ClearTraces() => System.Threading.Tasks.Task.Run(() => { DbManager.Create().Execute("delete from Traces where LogTime < @0", DateTime.Now.AddMonths(0 - DictHelper.RetrieveAccessLogPeriod())); }); } }