You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

228 lines
10 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Yitter.IdGenerator;
using Znyc.Cloudcar.Admin.Commons.Cache;
using Znyc.Cloudcar.Admin.Commons.Extensions;
using Znyc.Cloudcar.Admin.Commons.Helpers;
using Znyc.Cloudcar.Admin.Commons.Json;
using Znyc.Cloudcar.Admin.Commons.Log;
using Znyc.Cloudcar.Admin.Commons.Mapping;
using Znyc.Cloudcar.Admin.Commons.Pages;
using Znyc.Cloudcar.Admin.Commons.Services;
using Znyc.Cloudcar.Admin.Security.Dtos;
using Znyc.Cloudcar.Admin.Security.Entitys;
using Znyc.Cloudcar.Admin.Security.IRepositories;
using Znyc.Cloudcar.Admin.Security.IServices;
namespace Znyc.Cloudcar.Admin.Security.Services
{
/// <summary>
/// 操作日志服务
/// </summary>
public class OperationLogsService : BaseService<OperationLogsEntity, OperationLogsOutputDto, long>,
IOperationLogsService
{
private readonly IOperationLogsRepository _operationLogsRepository;
private readonly IUserRepository _userRepository;
/// <summary>
/// </summary>
/// <param name="repository"></param>
/// <param name="userRepository"></param>
public OperationLogsService(IOperationLogsRepository repository,
IUserRepository userRepository
) : base(repository)
{
_operationLogsRepository = repository;
_userRepository = userRepository;
}
/// <summary>
/// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
/// </summary>
/// <param name="search">查询的条件</param>
/// <returns>指定对象的集合</returns>
public async Task<PageResult<OperationLogsOutputDto>> FindWithPagerSearchAsync(SearchOperationLogsModel search)
{
bool order = search.Order == "asc" ? false : true;
string where = GetDataPrivilege(false);
if (!string.IsNullOrEmpty(search.StartTime))
{
where += " and CreatedTime >='" + search.StartTime.ToDateTime() + "'";
}
if (!string.IsNullOrEmpty(search.EndTime))
{
where += " and CreatedTime <='" + search.EndTime.ToDateTime() + "'";
}
if (search.Filter != null)
{
if (!string.IsNullOrEmpty(search.Filter.IP))
{
where += string.Format(" and IP = '{0}'", search.Filter.IP);
};
if (!string.IsNullOrEmpty(search.Filter.UserName))
{
where += string.Format(" and UserName = '{0}'", search.Filter.UserName);
};
}
PagerInfo pagerInfo = new PagerInfo
{
CurrenetPageIndex = search.CurrenetPageIndex,
PageSize = search.PageSize
};
//Expression<Func<Log, bool>> filter = log => true;
//if (!string.IsNullOrEmpty(search.Keywords))
//{
// filter = filter.And(log => log.Account.StartsWith(search.Keywords) || log.ModuleName.StartsWith(search.Keywords) || log.IPAddress.StartsWith(search.Keywords)
// || log.IPAddressName.StartsWith(search.Keywords) || log.Description.StartsWith(search.Keywords));
//}
//if (!string.IsNullOrEmpty(search.EnCode))
//{
// filter = filter.And(log=>search.EnCode.Contains(log.Type));
//}
List<OperationLogsEntity> list = await _operationLogsRepository.FindWithPagerAsync(where, pagerInfo, search.Sort, order);
PageResult<OperationLogsOutputDto> pageResult = new PageResult<OperationLogsOutputDto>
{
CurrentPage = pagerInfo.CurrenetPageIndex,
Items = list.MapTo<OperationLogsOutputDto>(),
ItemsPerPage = pagerInfo.PageSize,
TotalItems = pagerInfo.RecordCount
};
return pageResult;
}
/// <summary>
/// 根据相关信息,写入用户的操作日志记录
/// </summary>
/// <param name="tableName">操作表名称</param>
/// <param name="operationType">操作类型</param>
/// <param name="note">操作详细表述</param>
/// <returns></returns>
public bool OnOperationLog(string tableName, string operationType, string note)
{
try
{
//虽然实现了这个事件,但是我们还需要判断该表是否在配置表里面,如果不在,则不记录操作日志。
//var identities = _httpContextAccessor.HttpContext.User.Identities;
if (HttpContextHelper.HttpContext == null)
{
return false;
}
IEnumerable<ClaimsIdentity> identities = HttpContextHelper.HttpContext.User.Identities;
ClaimsIdentity claimsIdentity = identities.First();
List<Claim> claimlist = claimsIdentity.Claims as List<Claim>;
string userId = claimlist[0].Value;
CacheHelper cacheHelper = new CacheHelper();
AdminCurrentUser AdminCurrentUser = new AdminCurrentUser();
AdminCurrentUser user = cacheHelper.Get("login_user_" + userId).ToJson().ToObject<AdminCurrentUser>();
if (user != null)
{
AdminCurrentUser = user;
bool insert = operationType == DbLogType.Create.ToString();
; //&& settingInfo.InsertLog;
bool update = operationType == DbLogType.Update.ToString(); // && settingInfo.UpdateLog;
bool delete = operationType == DbLogType.Delete.ToString(); // && settingInfo.DeleteLog;
bool deletesoft = operationType == DbLogType.DeleteSoft.ToString(); // && settingInfo.DeleteLog;
bool exception = operationType == DbLogType.Exception.ToString(); // && settingInfo.DeleteLog;
bool sql = operationType == DbLogType.SQL.ToString(); // && settingInfo.DeleteLog;
if (insert || update || delete || deletesoft || exception || sql)
{
OperationLogsEntity info = new OperationLogsEntity
{
Id = YitIdHelper.NextId(),
//info.ModuleName = tableName;
//info.Type = operationType;
//info.Description = note;
//info.Date = info.CreatedTime = DateTime.Now;
//info.CreatedUserId = CurrentUser.UserId;
//info.Account = AdminCurrentUser.Account;
//info.UserName = AdminCurrentUser.UserName;
//info.OrganizeId = AdminCurrentUser.OrganizeId;
//info.IPAddress = AdminCurrentUser.CurrentLoginIP;
//info.IPAddressName = AdminCurrentUser.IPAddressName;
//info.Result = true;
CRUD = operationType,
UserName = AdminCurrentUser.UserName
};
info.OperationTime = info.CreatedTime = DateTime.Now;
info.IP = AdminCurrentUser.CurrentLoginIP;
long lg = _operationLogsRepository.Insert(info);
if (lg > 0)
{
return true;
}
}
}
}
catch (Exception ex)
{
Log4NetHelper.Error("", ex);
return false;
}
return false;
}
/// <summary>
/// 根据相关信息,写入用户的操作日志记录
/// 主要用于写操作模块日志
/// </summary>
/// <param name="module">操作模块名称</param>
/// <param name="operationType">操作类型</param>
/// <param name="note">操作详细表述</param>
/// <param name="AdminCurrentUser">操作用户</param>
/// <returns></returns>
public bool OnOperationLog(string module, string operationType, string note, AdminCurrentUser AdminCurrentUser)
{
//虽然实现了这个事件,但是我们还需要判断该表是否在配置表里面,如果不在,则不记录操作日志。
//OperationLogSettingInfo settingInfo = BLLFactory<OperationLogSetting>.Instance.FindByTableName(tableName, trans);
if (AdminCurrentUser != null)
{
bool login = operationType == DbLogType.Login.ToString();
bool visit = operationType == DbLogType.Visit.ToString();
bool exit = operationType == DbLogType.Exit.ToString();
bool other = operationType == DbLogType.Other.ToString();
bool insert = operationType == DbLogType.Create.ToString();
bool update = operationType == DbLogType.Update.ToString();
bool delete = operationType == DbLogType.Delete.ToString();
bool deletesoft = operationType == DbLogType.DeleteSoft.ToString();
bool exception = operationType == DbLogType.Exception.ToString();
if (login || visit || exit || other || insert || update || delete || deletesoft || exception)
{
OperationLogsEntity info = new OperationLogsEntity
{
Id = YitIdHelper.NextId()
};
//info.ModuleName = module;
//info.Type = operationType;
//info.Description = note;
//info.Date = info.CreatedTime = DateTime.Now;
//info.CreatedUserId = CurrentUser.UserId;
//info.Account = AdminCurrentUser.Account;
//info.UserName = AdminCurrentUser.UserName;
//info.OrganizeId = AdminCurrentUser.OrganizeId;
//info.IPAddress = AdminCurrentUser.CurrentLoginIP;
//info.IPAddressName = IpAddressUtil.GetCityByIp(AdminCurrentUser.CurrentLoginIP);
//info.Result = true;
long lg = _operationLogsRepository.Insert(info);
if (lg > 0)
{
return true;
}
}
}
return false;
}
}
}