using learun.iapplication; using learun.util; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; namespace learun.application { /// /// 版 本 EasyCode EC管理后台 /// Copyright (c) 2019-present EC管理有限公司 /// 创建人:tobin /// 日 期:2019.09.25 /// 描 述:系统日志数据库服务类 /// public class LogService : ServiceBase { #region 获取数据 /// /// 日志列表 /// /// 分页 /// 查询参数 /// 操作用户Id /// public Task> GetPageList(Pagination pagination, string queryJson, string userId) { var strSql = new StringBuilder(); strSql.Append("SELECT * FROM lr_base_log t WHERE 1=1"); var queryParam = queryJson.ToJObject(); int categoryId = 0; DateTime startTime = DateTime.Now; DateTime endTime = DateTime.Now; string operateUserId = null; string operateAccount = null; string operateType = null; string module = null; string keyword = null; // 日志分类 if (!queryParam["CategoryId"].IsEmpty()) { categoryId = queryParam["CategoryId"].ToInt(); strSql.Append(" AND F_CategoryId = @categoryId "); } // 操作时间 if (!queryParam["StartTime"].IsEmpty() && !queryParam["EndTime"].IsEmpty()) { startTime = queryParam["StartTime"].ToDate(); endTime = queryParam["EndTime"].ToDate(); strSql.Append(" AND( F_OperateTime >= @startTime AND F_OperateTime <= @endTime )"); } // 操作用户Id if (!queryParam["OperateUserId"].IsEmpty()) { operateUserId = queryParam["OperateUserId"].ToString(); strSql.Append(" AND F_OperateUserId = @operateUserId "); } // 操作用户账户 if (!queryParam["OperateAccount"].IsEmpty()) { operateAccount = "%" + queryParam["OperateAccount"].ToString() + "%"; strSql.Append(" AND F_OperateAccount like @operateAccount "); } // 操作类型 if (!queryParam["OperateType"].IsEmpty()) { operateType = queryParam["OperateType"].ToString(); strSql.Append(" AND F_OperateType = @operateType "); } // 功能模块 if (!queryParam["Module"].IsEmpty()) { module = queryParam["Module"].ToString(); strSql.Append(" AND F_Module = @module "); } // 登录用户id if (!string.IsNullOrEmpty(userId)) { strSql.Append(" AND F_OperateUserId = @userId "); } // 关键字 if (!queryParam["keyword"].IsEmpty()) { keyword = "%" + queryParam["keyword"] + "%"; strSql.Append(" AND (F_Module like @keyword || F_OperateType like @keyword || F_IPAddress like @keyword )"); } return BaseRepository().FindList(strSql.ToString(), new { categoryId, startTime, endTime, operateUserId, operateAccount, operateType, module, userId, keyword }, pagination); } /// /// 日志实体 /// /// 主键值 /// public Task GetEntity(string keyValue) { return BaseRepository().FindEntity(keyValue); } #endregion 获取数据 #region 提交数据 /// /// 清空日志 /// /// 日志分类Id /// 保留时间段内 public async Task Remove(int categoryId, string keepTime) { 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); } await BaseRepository().ExecuteSql(" Delete From lr_base_log where F_CategoryId = @categoryId AND F_OperateTime <= @operateTime ", new { categoryId, operateTime }); } /// /// 写日志 /// /// 对象 public async Task Write(LogEntity logEntity) { logEntity.F_LogId = Guid.NewGuid().ToString(); logEntity.F_OperateTime = DateTime.Now; logEntity.F_DeleteMark = 0; logEntity.F_EnabledMark = 1; await BaseRepository().Insert(logEntity); } #endregion 提交数据 } }