2019-05-01 16:46:24 +08:00
|
|
|
|
using Longbow.Web.Mvc;
|
2019-05-01 11:36:24 +08:00
|
|
|
|
using MongoDB.Driver;
|
2019-05-01 16:46:24 +08:00
|
|
|
|
using PetaPoco;
|
2018-10-30 13:07:29 +08:00
|
|
|
|
using System;
|
2019-05-25 14:52:14 +08:00
|
|
|
|
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>
|
2019-05-25 14:52:14 +08:00
|
|
|
|
public override Page<DataAccess.Log> RetrievePages(PaginationOption po, DateTime? startTime, DateTime? endTime, string opType)
|
2019-05-01 16:46:24 +08:00
|
|
|
|
{
|
2019-05-01 11:36: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;
|
2019-05-01 11:36:24 +08:00
|
|
|
|
switch (po.Sort)
|
|
|
|
|
{
|
2019-05-01 16:46:24 +08:00
|
|
|
|
case "CRUD":
|
2019-05-01 11:36:24 +08:00
|
|
|
|
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":
|
2019-05-01 11:36:24 +08:00
|
|
|
|
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":
|
2019-05-01 11:36:24 +08:00
|
|
|
|
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":
|
2019-05-01 11:36:24 +08:00
|
|
|
|
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":
|
2019-05-01 11:36:24 +08:00
|
|
|
|
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.RequestUrl) : sortBuilder.Descending(t => t.RequestUrl);
|
2019-05-01 16:46:24 +08:00
|
|
|
|
break;
|
2019-05-01 11:36:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2019-05-25 14:52:14 +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();
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-30 22:11:27 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除日志信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-15 12:52:09 +08:00
|
|
|
|
private static void DeleteLogAsync() => System.Threading.Tasks.Task.Run(() => DbManager.Logs.DeleteMany(log => log.LogTime < DateTime.Now.AddDays(-7)));
|
2019-05-18 21:21:03 +08:00
|
|
|
|
|
2018-10-30 16:30:58 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="log"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override bool Save(DataAccess.Log log)
|
2018-10-30 16:30:58 +08:00
|
|
|
|
{
|
2019-01-15 12:52:09 +08:00
|
|
|
|
DbManager.Logs.InsertOne(log);
|
2019-05-18 21:21:03 +08:00
|
|
|
|
DeleteLogAsync();
|
2018-10-30 16:30:58 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-10-30 13:07:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|