using Longbow.Web.Mvc;
using MongoDB.Driver;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess.MongoDB
{
///
///
///
public class LoginUser : DataAccess.LoginUser
{
///
///
///
///
///
public override bool Log(DataAccess.LoginUser user)
{
DbManager.LoginUsers.InsertOne(user);
return true;
}
///
///
///
///
///
///
///
///
public override Page RetrieveByPages(PaginationOption po, DateTime? startTime, DateTime? endTime, string ip)
{
var logs = RetrieveAll(startTime, endTime, ip);
return new Page()
{
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()
};
}
///
/// 获取所有登录数据
///
///
public override IEnumerable RetrieveAll(DateTime? startTime, DateTime? endTime, string ip)
{
var filterBuilder = Builders.Filter;
var filter = filterBuilder.Empty;
if (startTime.HasValue) filter = filterBuilder.Gte(l => l.LoginTime, startTime.Value);
if (endTime.HasValue) filter = filterBuilder.Lt(l => l.LoginTime, endTime.Value.AddDays(1));
if (!string.IsNullOrEmpty(ip)) filter = filterBuilder.Eq(l => l.Ip, ip);
return DbManager.LoginUsers
.Find(filter)
.Sort(Builders.Sort.Ascending(t => t.LoginTime)).ToList();
}
}
}