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.
 
 
 
 
 
 

313 lines
12 KiB

  1. using Learun.Application.Base.AuthorizeModule;
  2. using Learun.Application.Organization;
  3. using Learun.DataBase.Repository;
  4. using Learun.Util;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. namespace Learun.Application.Base.SystemModule
  10. {
  11. /// <summary>
  12. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  13. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  14. /// 创建人:陈彬彬
  15. /// 日 期:2017.03.08
  16. /// 描 述:系统日志数据库服务类
  17. /// </summary>
  18. public class LogService : RepositoryFactory
  19. {
  20. #region 获取数据
  21. /// <summary>
  22. /// 日志列表
  23. /// </summary>
  24. /// <param name="pagination">分页</param>
  25. /// <param name="queryJson">查询参数</param>
  26. /// <param name="userId">操作用户Id</param>
  27. /// <returns></returns>
  28. public IEnumerable<LogEntity> GetPageList(Pagination pagination, string queryJson, string userId)
  29. {
  30. try
  31. {
  32. var expression = LinqExtensions.True<LogEntity>();
  33. var queryParam = queryJson.ToJObject();
  34. // 日志分类
  35. if (!queryParam["CategoryId"].IsEmpty())
  36. {
  37. int categoryId = queryParam["CategoryId"].ToInt();
  38. expression = expression.And(t => t.F_CategoryId == categoryId);
  39. }
  40. // 操作时间
  41. if (!queryParam["StartTime"].IsEmpty() && !queryParam["EndTime"].IsEmpty())
  42. {
  43. DateTime startTime = queryParam["StartTime"].ToDate();
  44. DateTime endTime = queryParam["EndTime"].ToDate();
  45. expression = expression.And(t => t.F_OperateTime >= startTime && t.F_OperateTime <= endTime);
  46. }
  47. // 操作用户Id
  48. if (!queryParam["OperateUserId"].IsEmpty())
  49. {
  50. string OperateUserId = queryParam["OperateUserId"].ToString();
  51. expression = expression.And(t => t.F_OperateUserId == OperateUserId);
  52. }
  53. // 操作用户账户
  54. if (!queryParam["OperateAccount"].IsEmpty())
  55. {
  56. string OperateAccount = queryParam["OperateAccount"].ToString();
  57. expression = expression.And(t => t.F_OperateAccount.Contains(OperateAccount));
  58. }
  59. // 操作类型
  60. if (!queryParam["OperateType"].IsEmpty())
  61. {
  62. string operateType = queryParam["OperateType"].ToString();
  63. expression = expression.And(t => t.F_OperateType == operateType);
  64. }
  65. // 功能模块
  66. if (!queryParam["Module"].IsEmpty())
  67. {
  68. string module = queryParam["Module"].ToString();
  69. expression = expression.And(t => t.F_Module.Contains(module));
  70. }
  71. // 关键字
  72. if (!queryParam["keyword"].IsEmpty())
  73. {
  74. string keyword = queryParam["keyword"].ToString();
  75. expression = expression.And(t => t.F_Module.Contains(keyword) || t.F_OperateType.Contains(keyword) || t.F_IPAddress.Contains(keyword));
  76. }
  77. // 登录用户id
  78. if (!string.IsNullOrEmpty(userId))
  79. {
  80. expression = expression.And(t => t.F_OperateUserId == userId);
  81. }
  82. return this.BaseRepository().FindList(expression, pagination);
  83. }
  84. catch (Exception ex)
  85. {
  86. if (ex is ExceptionEx)
  87. {
  88. throw;
  89. }
  90. else
  91. {
  92. throw ExceptionEx.ThrowServiceException(ex);
  93. }
  94. }
  95. }
  96. /// <summary>
  97. /// 日志列表
  98. /// </summary>
  99. /// <param name="queryJson">查询参数</param>
  100. /// <returns></returns>
  101. public IEnumerable<LogEntity> GetList(string queryJson, bool isYear)
  102. {
  103. try
  104. {
  105. var list = new List<LogEntity>();
  106. var expression = LinqExtensions.True<LogEntity>();
  107. var queryParam = queryJson.ToJObject();
  108. // 操作时间
  109. if (!queryParam["date"].IsEmpty())
  110. {
  111. if (isYear) //年统计数据
  112. {
  113. var year = queryParam["date"].ToDate().Year;
  114. expression = expression.And(t => t.F_OperateTime.Value.Year == year);
  115. list = this.BaseRepository().FindList(expression).ToList();
  116. }
  117. else
  118. {
  119. DateTime date1 = queryParam["date"].ToDate();
  120. DateTime date2 = date1.AddDays(1).ToDate();
  121. //expression = expression.And(t => t.F_OperateTime >= date1 && t.F_OperateTime < date2);
  122. var strSql = new StringBuilder();
  123. strSql.Append(" select t.*,d.F_FullName as Department ");
  124. 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 ");
  125. strSql.Append($" where t.F_OperateTime < '{date2}' and t.F_OperateTime >= '{date1}' ");
  126. list = this.BaseRepository().FindList<LogEntity>(strSql.ToString()).ToList();
  127. }
  128. }
  129. return list;
  130. }
  131. catch (Exception ex)
  132. {
  133. if (ex is ExceptionEx)
  134. {
  135. throw;
  136. }
  137. else
  138. {
  139. throw ExceptionEx.ThrowServiceException(ex);
  140. }
  141. }
  142. }
  143. public IEnumerable<LogEntity> GetGroupLog(string userid)
  144. {
  145. return this.BaseRepository().FindList<LogEntity>(@"select [F_Module],count([F_Module]) as MTimes,F_ExecuteResultJson from LR_Base_Log
  146. where F_OperateUserId = '" + userid + @"' and F_CategoryId = '2'
  147. group by[F_Module], F_ExecuteResultJson
  148. order by count([F_Module]) desc");
  149. }
  150. /// <summary>
  151. /// 日志实体
  152. /// </summary>
  153. /// <param name="keyValue">主键值</param>
  154. /// <returns></returns>
  155. public LogEntity GetEntity(string keyValue)
  156. {
  157. return this.BaseRepository().FindEntity<LogEntity>(keyValue);
  158. }
  159. #endregion
  160. #region 提交数据
  161. /// <summary>
  162. /// 清空日志
  163. /// </summary>
  164. /// <param name="categoryId">日志分类Id</param>
  165. /// <param name="keepTime">保留时间段内</param>
  166. public void RemoveLog(int categoryId, string keepTime)
  167. {
  168. try
  169. {
  170. DateTime operateTime = DateTime.Now;
  171. if (keepTime == "7")//保留近一周
  172. {
  173. operateTime = DateTime.Now.AddDays(-7);
  174. }
  175. else if (keepTime == "1")//保留近一个月
  176. {
  177. operateTime = DateTime.Now.AddMonths(-1);
  178. }
  179. else if (keepTime == "3")//保留近三个月
  180. {
  181. operateTime = DateTime.Now.AddMonths(-3);
  182. }
  183. var expression = LinqExtensions.True<LogEntity>();
  184. expression = expression.And(t => t.F_OperateTime <= operateTime);
  185. expression = expression.And(t => t.F_CategoryId == categoryId);
  186. this.BaseRepository().Delete(expression);
  187. }
  188. catch (Exception ex)
  189. {
  190. if (ex is ExceptionEx)
  191. {
  192. throw;
  193. }
  194. else
  195. {
  196. throw ExceptionEx.ThrowServiceException(ex);
  197. }
  198. }
  199. }
  200. /// <summary>
  201. /// 写日志
  202. /// </summary>
  203. /// <param name="logEntity">对象</param>
  204. public void WriteLog(LogEntity logEntity)
  205. {
  206. try
  207. {
  208. logEntity.F_LogId = Guid.NewGuid().ToString();
  209. logEntity.F_OperateTime = DateTime.Now;
  210. logEntity.F_DeleteMark = 0;
  211. logEntity.F_EnabledMark = 1;
  212. logEntity.F_IPAddress = Net.Ip;
  213. logEntity.F_Host = Net.Host;
  214. logEntity.F_Browser = Net.Browser;
  215. this.BaseRepository().Insert(logEntity);
  216. }
  217. catch (Exception ex)
  218. {
  219. if (ex is ExceptionEx)
  220. {
  221. throw;
  222. }
  223. else
  224. {
  225. throw ExceptionEx.ThrowServiceException(ex);
  226. }
  227. }
  228. }
  229. #endregion
  230. #region 扩展数据
  231. /// <summary>
  232. /// 获取在线用户人数列表
  233. /// <summary>
  234. /// <returns></returns>
  235. public IEnumerable<LogEntity> GetOnlineUserList()
  236. {
  237. try
  238. {
  239. var strSql = new StringBuilder();
  240. 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 ");
  241. 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 ");
  242. strSql.Append(" where t.F_OperateTime <= GETDATE() and t.F_OperateTime >= DATEADD(MINUTE, -10, GETDATE()) ");
  243. var logList = this.BaseRepository().FindList<LogEntity>(strSql.ToString());
  244. foreach (var logItem in logList)
  245. {
  246. var userRelationList = this.BaseRepository().FindList<UserRelationEntity>(x => x.F_Category == 1 && x.F_UserId == logItem.F_OperateUserId);
  247. if (userRelationList.Any())
  248. {
  249. var roleList = new List<string>();
  250. foreach (var urItem in userRelationList)
  251. {
  252. var role = this.BaseRepository().FindEntity<RoleEntity>(x => x.F_RoleId == urItem.F_ObjectId);
  253. if (role != null)
  254. {
  255. roleList.Add(role.F_FullName);
  256. }
  257. }
  258. logItem.Role = string.Join(",", roleList.ToArray());
  259. }
  260. }
  261. return logList;
  262. }
  263. catch (Exception ex)
  264. {
  265. if (ex is ExceptionEx)
  266. {
  267. throw;
  268. }
  269. else
  270. {
  271. throw ExceptionEx.ThrowServiceException(ex);
  272. }
  273. }
  274. }
  275. #endregion
  276. public LogEntity GetUserLogList(string userId)
  277. {
  278. try
  279. {
  280. LogEntity logEntity=null;
  281. var log = BaseRepository().FindList<LogEntity>(m => m.F_OperateUserId == userId);
  282. if (log.Count()>0)
  283. {
  284. logEntity = log.OrderByDescending(m => m.F_OperateTime).First();
  285. }
  286. return logEntity;
  287. }
  288. catch (Exception ex)
  289. {
  290. if (ex is ExceptionEx)
  291. {
  292. throw;
  293. }
  294. else
  295. {
  296. throw ExceptionEx.ThrowServiceException(ex);
  297. }
  298. }
  299. }
  300. }
  301. }