2019-03-18 17:26:12 +08:00
|
|
|
using Longbow.Web.Mvc;
|
|
|
|
using PetaPoco;
|
|
|
|
using System;
|
2019-05-25 14:52:14 +08:00
|
|
|
using System.Collections.Generic;
|
2019-03-18 17:26:12 +08:00
|
|
|
|
|
|
|
namespace Bootstrap.DataAccess
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
2019-05-10 19:46:36 +08:00
|
|
|
[TableName("Traces")]
|
2019-03-18 17:26:12 +08:00
|
|
|
public class Trace
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// 获得/设置 操作日志主键ID
|
|
|
|
/// </summary>
|
|
|
|
public string Id { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 获得/设置 用户名称
|
|
|
|
/// </summary>
|
|
|
|
public string UserName { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 获得/设置 操作时间
|
|
|
|
/// </summary>
|
|
|
|
public DateTime LogTime { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 获得/设置 客户端IP
|
|
|
|
/// </summary>
|
|
|
|
public string Ip { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
public string City { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
public string Browser { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
public string OS { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 获取/设置 请求网址
|
|
|
|
/// </summary>
|
|
|
|
public string RequestUrl { get; set; }
|
|
|
|
|
2019-05-01 16:46:24 +08:00
|
|
|
/// <summary>
|
|
|
|
///
|
2019-04-27 20:39:24 +08:00
|
|
|
/// </summary>
|
|
|
|
public string UserAgent { get; set; }
|
|
|
|
|
2019-03-18 17:26:12 +08:00
|
|
|
/// <summary>
|
|
|
|
/// 保存访问历史记录
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="p"></param>
|
|
|
|
public virtual bool Save(Trace p)
|
|
|
|
{
|
|
|
|
if (p == null) throw new ArgumentNullException(nameof(p));
|
|
|
|
DbManager.Create().Save(p);
|
|
|
|
ClearTraces();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
2019-05-01 18:05:14 +08:00
|
|
|
/// <param name="po"></param>
|
|
|
|
/// <param name="startTime"></param>
|
|
|
|
/// <param name="endTime"></param>
|
2019-05-04 17:13:29 +08:00
|
|
|
/// <param name="ip"></param>
|
2019-03-18 17:26:12 +08:00
|
|
|
/// <returns></returns>
|
2019-05-25 14:52:14 +08:00
|
|
|
public virtual Page<Trace> RetrievePages(PaginationOption po, DateTime? startTime, DateTime? endTime, string ip)
|
2019-03-18 17:26:12 +08:00
|
|
|
{
|
2019-05-01 18:05:14 +08:00
|
|
|
var sql = new Sql("select UserName, LogTime, IP, Browser, OS, City, RequestUrl from Traces");
|
2019-05-04 17:13:29 +08:00
|
|
|
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}");
|
2019-03-18 17:26:12 +08:00
|
|
|
|
|
|
|
return DbManager.Create().Page<Trace>(po.PageIndex, po.Limit, sql);
|
|
|
|
}
|
|
|
|
|
2019-05-25 14:52:14 +08:00
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="startTime"></param>
|
|
|
|
/// <param name="endTime"></param>
|
|
|
|
/// <param name="ip"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public virtual IEnumerable<Trace> 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<Trace>(sql);
|
|
|
|
}
|
|
|
|
|
2019-03-18 17:26:12 +08:00
|
|
|
private static void ClearTraces() => System.Threading.Tasks.Task.Run(() =>
|
|
|
|
{
|
|
|
|
DbManager.Create().Execute("delete from Traces where LogTime < @0", DateTime.Now.AddMonths(0 - DictHelper.RetrieveAccessLogPeriod()));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|