using Learun.Application.Base.AuthorizeModule; using Learun.Application.Organization; using Learun.DataBase.Repository; using Learun.Util; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Learun.Application.Base.SystemModule { /// /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园 /// Copyright (c) 2013-2018 北京泉江科技有限公司 /// 创建人:陈彬彬 /// 日 期:2017.03.08 /// 描 述:系统日志数据库服务类 /// public class LogService : RepositoryFactory { #region 获取数据 /// /// 日志列表 /// /// 分页 /// 查询参数 /// 操作用户Id /// public IEnumerable GetPageList(Pagination pagination, string queryJson, string userId) { try { var expression = LinqExtensions.True(); var queryParam = queryJson.ToJObject(); // 日志分类 if (!queryParam["CategoryId"].IsEmpty()) { int categoryId = queryParam["CategoryId"].ToInt(); expression = expression.And(t => t.F_CategoryId == categoryId); } // 操作时间 if (!queryParam["StartTime"].IsEmpty() && !queryParam["EndTime"].IsEmpty()) { DateTime startTime = queryParam["StartTime"].ToDate(); DateTime endTime = queryParam["EndTime"].ToDate(); expression = expression.And(t => t.F_OperateTime >= startTime && t.F_OperateTime <= endTime); } // 操作用户Id if (!queryParam["OperateUserId"].IsEmpty()) { string OperateUserId = queryParam["OperateUserId"].ToString(); expression = expression.And(t => t.F_OperateUserId == OperateUserId); } // 操作用户账户 if (!queryParam["OperateAccount"].IsEmpty()) { string OperateAccount = queryParam["OperateAccount"].ToString(); expression = expression.And(t => t.F_OperateAccount.Contains(OperateAccount)); } // 操作类型 if (!queryParam["OperateType"].IsEmpty()) { string operateType = queryParam["OperateType"].ToString(); expression = expression.And(t => t.F_OperateType == operateType); } // 功能模块 if (!queryParam["Module"].IsEmpty()) { string module = queryParam["Module"].ToString(); expression = expression.And(t => t.F_Module.Contains(module)); } // 关键字 if (!queryParam["keyword"].IsEmpty()) { string keyword = queryParam["keyword"].ToString(); expression = expression.And(t => t.F_Module.Contains(keyword) || t.F_OperateType.Contains(keyword) || t.F_IPAddress.Contains(keyword)); } // 登录用户id if (!string.IsNullOrEmpty(userId)) { expression = expression.And(t => t.F_OperateUserId == userId); } return this.BaseRepository().FindList(expression, pagination); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } /// /// 日志列表 /// /// 查询参数 /// public IEnumerable GetList(string queryJson, bool isYear) { try { var list = new List(); var expression = LinqExtensions.True(); var queryParam = queryJson.ToJObject(); // 操作时间 if (!queryParam["date"].IsEmpty()) { if (isYear) //年统计数据 { var year = queryParam["date"].ToDate().Year; expression = expression.And(t => t.F_OperateTime.Value.Year == year); list = this.BaseRepository().FindList(expression).ToList(); } else { DateTime date1 = queryParam["date"].ToDate(); DateTime date2 = date1.AddDays(1).ToDate(); //expression = expression.And(t => t.F_OperateTime >= date1 && t.F_OperateTime < date2); var strSql = new StringBuilder(); strSql.Append(" select t.*,d.F_FullName as Department "); strSql.Append(" from LR_Base_Log t inner join LR_Base_User u on t.F_OperateUserId = u.F_UserId left join LR_Base_Department d on u.F_DepartmentId = d.F_DepartmentId "); strSql.Append($" where t.F_OperateTime < '{date2}' and t.F_OperateTime >= '{date1}' "); list = this.BaseRepository().FindList(strSql.ToString()).ToList(); } } return list; } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } public IEnumerable GetGroupLog(string userid) { return this.BaseRepository().FindList(@"select [F_Module],count([F_Module]) as MTimes,F_ExecuteResultJson from LR_Base_Log where F_OperateUserId = '" + userid + @"' and F_CategoryId = '2' group by[F_Module], F_ExecuteResultJson order by count([F_Module]) desc"); } /// /// 日志实体 /// /// 主键值 /// public LogEntity GetEntity(string keyValue) { return this.BaseRepository().FindEntity(keyValue); } #endregion #region 提交数据 /// /// 清空日志 /// /// 日志分类Id /// 保留时间段内 public void RemoveLog(int categoryId, string keepTime) { try { DateTime operateTime = DateTime.Now; if (keepTime == "7")//保留近一周 { operateTime = DateTime.Now.AddDays(-7); } else if (keepTime == "1")//保留近一个月 { operateTime = DateTime.Now.AddMonths(-1); } else if (keepTime == "3")//保留近三个月 { operateTime = DateTime.Now.AddMonths(-3); } var expression = LinqExtensions.True(); expression = expression.And(t => t.F_OperateTime <= operateTime); expression = expression.And(t => t.F_CategoryId == categoryId); this.BaseRepository().Delete(expression); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } /// /// 写日志 /// /// 对象 public void WriteLog(LogEntity logEntity) { try { logEntity.F_LogId = Guid.NewGuid().ToString(); logEntity.F_OperateTime = DateTime.Now; logEntity.F_DeleteMark = 0; logEntity.F_EnabledMark = 1; logEntity.F_IPAddress = Net.Ip; logEntity.F_Host = Net.Host; logEntity.F_Browser = Net.Browser; this.BaseRepository().Insert(logEntity); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } #endregion #region 扩展数据 /// /// 获取在线用户人数列表 /// /// public IEnumerable GetOnlineUserList() { try { var strSql = new StringBuilder(); strSql.Append(" select distinct t.F_OperateUserId,t.F_IPAddress,t.F_Description,u.F_UserId,u.F_RealName as RealName,d.F_FullName as Department "); strSql.Append(" from LR_Base_Log t inner join LR_Base_User u on t.F_OperateUserId = u.F_UserId left join LR_Base_Department d on u.F_DepartmentId = d.F_DepartmentId "); strSql.Append(" where t.F_OperateTime <= GETDATE() and t.F_OperateTime >= DATEADD(MINUTE, -10, GETDATE()) "); var logList = this.BaseRepository().FindList(strSql.ToString()); foreach (var logItem in logList) { var userRelationList = this.BaseRepository().FindList(x => x.F_Category == 1 && x.F_UserId == logItem.F_OperateUserId); if (userRelationList.Any()) { var roleList = new List(); foreach (var urItem in userRelationList) { var role = this.BaseRepository().FindEntity(x => x.F_RoleId == urItem.F_ObjectId); if (role != null) { roleList.Add(role.F_FullName); } } logItem.Role = string.Join(",", roleList.ToArray()); } } return logList; } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } #endregion public LogEntity GetUserLogList(string userId) { try { LogEntity logEntity=null; var log = BaseRepository().FindList(m => m.F_OperateUserId == userId); if (log.Count()>0) { logEntity = log.OrderByDescending(m => m.F_OperateTime).First(); } return logEntity; } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } } }