LogController.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Http;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using DW5S.DTO;
  8. using DW5S.Repostory;
  9. using System.IO;
  10. using DW5S.Entity;
  11. using DW5S.WebApi;
  12. using System.Diagnostics;
  13. using System.Configuration;
  14. using Microsoft.AspNetCore.Mvc;
  15. using Microsoft.Extensions.Logging;
  16. using Microsoft.EntityFrameworkCore;
  17. namespace DW5S.App.Controllers
  18. {
  19. /// <summary>
  20. /// 日志相关接口
  21. /// </summary>
  22. public class LogController : BaseController
  23. {
  24. [Autowired]
  25. private readonly ILogger logger;
  26. [Autowired]
  27. private readonly UnitOfWork unitOfWork;
  28. /// <summary>
  29. /// 将日志写入到平台
  30. /// </summary>
  31. /// <param name="dto">日志信息</param>
  32. /// <returns></returns>
  33. [HttpPost]
  34. public async Task<AjaxResult> Add(LogInfoDto dto)
  35. {
  36. try
  37. {
  38. var repsLog = unitOfWork.Of<LogRes>();
  39. await repsLog.AddOrUpdateAsync(new LogRes()
  40. {
  41. LogTime = dto.LogTime,
  42. Module = dto.Module,
  43. LogType = (Entity.EnumLogType)(int)dto.LogType,
  44. Msg = dto.Msg,
  45. });
  46. await unitOfWork.SaveAsync();
  47. return Success();
  48. }
  49. catch (Exception ex)
  50. {
  51. string msg = "日志信息写入异常";
  52. logger.LogError(ex, msg);
  53. return Error(msg);
  54. }
  55. }
  56. /// <summary>
  57. /// 查询日志信息
  58. /// </summary>
  59. /// <param name="dto">日志查询参数</param>
  60. /// <returns></returns>
  61. [HttpPost]
  62. public async Task<AjaxResult<List<LogInfoDto>>> Query(LogQueryDto dto)
  63. {
  64. try
  65. {
  66. var query = unitOfWork.Of<LogRes>().AsQueryable();
  67. if (!string.IsNullOrWhiteSpace(dto.Module) && dto.Module != "全部")
  68. query = query.Where(p => p.Module == dto.Module);
  69. if (dto.LogTimeBegin != null && dto.LogTimeBegin != DateTime.MinValue)
  70. query = query.Where(p => p.LogTime >= dto.LogTimeBegin);
  71. if (dto.LogTimeEnd != null && dto.LogTimeEnd != DateTime.MinValue)
  72. query = query.Where(p => p.LogTime <= dto.LogTimeEnd);
  73. var res = await query.OrderBy(p => p.Module).ThenByDescending(t => t.LogTime).ToListAsync();
  74. var dtoRes = res.Select(t => new LogInfoDto
  75. {
  76. ID = t.Id,
  77. LogTime = t.LogTime,
  78. LogType = (EnumLogTypeDto)(int)t.LogType,
  79. Module = t.Module,
  80. Msg = t.Msg,
  81. }).ToList();
  82. return Success(dtoRes);
  83. }
  84. catch (Exception ex)
  85. {
  86. string msg = "日志信息查询异常";
  87. logger.LogError(ex, msg);
  88. return Error<List<LogInfoDto>>(msg);
  89. }
  90. }
  91. /// <summary>
  92. /// 获取所有日志模块
  93. /// </summary>
  94. /// <returns></returns>
  95. [HttpPost]
  96. public async Task<AjaxResult<List<LogModulesResDto>>> GetModules()
  97. {
  98. try
  99. {
  100. var repsLog = unitOfWork.Of<LogRes>();
  101. var res = await repsLog.GetDistinctAsync(p => p.Module);
  102. var dtoRes = res.Select(p => new LogModulesResDto()
  103. {
  104. Module = p
  105. }).OrderBy(p => p.Module).ToList();
  106. return Success(dtoRes);
  107. }
  108. catch (Exception ex)
  109. {
  110. string msg = "日志模块查询异常";
  111. logger.LogError(ex, msg);
  112. return Error<List<LogModulesResDto>>(msg);
  113. }
  114. }
  115. /// <summary>
  116. /// 删除日志
  117. /// </summary>
  118. /// <param name="dto"></param>
  119. /// <returns></returns>
  120. [HttpPost]
  121. public async Task<AjaxResult> Delete(IEnumerable<LogDeleteDto> dto)
  122. {
  123. if (dto == null || !dto.Any()) return Success();
  124. try
  125. {
  126. var repsLog = unitOfWork.Of<LogRes>();
  127. var ids = dto.Select(p => p.ID);
  128. await repsLog.DeleteAsync(p => ids.Contains(p.Id));
  129. await unitOfWork.SaveAsync();
  130. return Success();
  131. }
  132. catch (Exception ex)
  133. {
  134. string msg = "日志信息删除异常";
  135. logger.LogError(ex, msg);
  136. return Error(msg);
  137. }
  138. }
  139. }
  140. }