using System; using System.Collections.Generic; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; using Znyc.Admin.Commons.Cache; using Znyc.Admin.Commons.Extensions; using Znyc.Admin.Commons.Helpers; using Znyc.Admin.Commons.Json; using Znyc.Admin.Commons.Log; using Znyc.Admin.Commons.Mapping; using Znyc.Admin.Commons.Pages; using Znyc.Admin.Commons.Services; using Znyc.Admin.Security.Dtos; using Znyc.Admin.Security.Entitys; using Znyc.Admin.Security.IRepositories; using Znyc.Admin.Security.IServices; namespace Znyc.Admin.Security.Services { /// /// /// public class LogExService : BaseService, ILogExService { private readonly ILogExRepository _iLogRepository; private readonly IUserRepository _iuserRepository; /// /// /// /// /// public LogExService(ILogExRepository repository, IUserRepository userRepository) : base(repository) { _iLogRepository = repository; _iuserRepository = userRepository; } /// /// 根据条件查询数据库,并返回对象集合(用于分页数据显示) /// /// 查询的条件 /// 指定对象的集合 public async Task> FindWithPagerSearchAsync(SearchExceptionsLogsModel search) { bool order = search.Order == "asc" ? false : true; string where = GetDataPrivilege(false); if (!string.IsNullOrEmpty(search.CreatedTime1)) { @where += " and CreatedTime >='" + search.CreatedTime1.ToDateTime() + "'"; } if (!string.IsNullOrEmpty(search.CreatedTime2)) { @where += " and CreatedTime <='" + search.CreatedTime2.ToDateTime() + "'"; } if (!string.IsNullOrEmpty(search.Filter.IPAddress)) { @where += string.Format(" and IPAddress = '{0}'", search.Filter.IPAddress); }; if (!string.IsNullOrEmpty(search.Filter.Account)) { @where += string.Format(" and Account = '{0}'", search.Filter.Account); }; PagerInfo pagerInfo = new PagerInfo { CurrenetPageIndex = search.CurrenetPageIndex, PageSize = search.PageSize }; //Expression> 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 list = await _iLogRepository.FindWithPagerAsync(where, pagerInfo, search.Sort, order); PageResult pageResult = new PageResult { CurrentPage = pagerInfo.CurrenetPageIndex, Items = list.MapTo(), ItemsPerPage = pagerInfo.PageSize, TotalItems = pagerInfo.RecordCount }; return pageResult; } /// /// 根据相关信息,写入用户的操作日志记录 /// /// 操作表名称 /// 操作类型 /// 操作详细表述 /// public bool OnOperationLog(string tableName, string operationType, string note) { try { //虽然实现了这个事件,但是我们还需要判断该表是否在配置表里面,如果不在,则不记录操作日志。 //var identities = _httpContextAccessor.HttpContext.User.Identities; if (HttpContextHelper.HttpContext == null) { return false; } IEnumerable identities = HttpContextHelper.HttpContext.User.Identities; ClaimsIdentity claimsIdentity = identities.First(); List claimlist = claimsIdentity.Claims as List; string userId = claimlist[0].Value; CacheHelper cacheHelper = new CacheHelper(); AdminCurrentUser AdminCurrentUser = new AdminCurrentUser(); AdminCurrentUser user = cacheHelper.Get("login_user_" + userId).ToJson().ToObject(); 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) { LogEx info = new LogEx(); info.Id = Yitter.IdGenerator.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; long lg = _iLogRepository.Insert(info); if (lg > 0) { return true; } } } } catch (Exception ex) { Log4NetHelper.Error("", ex); return false; } return false; } /// /// 根据相关信息,写入用户的操作日志记录 /// 主要用于写操作模块日志 /// /// 操作模块名称 /// 操作类型 /// 操作详细表述 /// 操作用户 /// public bool OnOperationLog(string module, string operationType, string note, AdminCurrentUser AdminCurrentUser) { //虽然实现了这个事件,但是我们还需要判断该表是否在配置表里面,如果不在,则不记录操作日志。 //OperationLogSettingInfo settingInfo = BLLFactory.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) { LogEx info = new LogEx(); info.Id = Yitter.IdGenerator.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 = _iLogRepository.Insert(info); if (lg > 0) { return true; } } } return false; } } }