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>
/// ����������Ϣ��д���û��IJ�����־��¼
/// </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>
/// ����������Ϣ��д���û��IJ�����־��¼
/// ��Ҫ����д����ģ����־
/// </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;
}
}
}