OprationLogService.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System.Threading.Tasks;
  2. using Microsoft.AspNetCore.Http;
  3. using ZhonTai.Common.Helpers;
  4. using ZhonTai.Admin.Core.Dto;
  5. using ZhonTai.Admin.Services.OprationLog.Dto;
  6. using ZhonTai.Admin.Domain.OprationLog;
  7. using ZhonTai.Admin.Domain;
  8. using ZhonTai.DynamicApi;
  9. using ZhonTai.DynamicApi.Attributes;
  10. using Microsoft.AspNetCore.Mvc;
  11. using ZhonTai.Admin.Core.Consts;
  12. namespace ZhonTai.Admin.Services.OprationLog;
  13. /// <summary>
  14. /// 操作日志服务
  15. /// </summary>
  16. [DynamicApi(Area = AdminConsts.AreaName)]
  17. public class OprationLogService : BaseService, IOprationLogService, IDynamicApi
  18. {
  19. private readonly IHttpContextAccessor _context;
  20. private readonly IOprationLogRepository _oprationLogRepository;
  21. public OprationLogService(
  22. IHttpContextAccessor context,
  23. IOprationLogRepository oprationLogRepository
  24. )
  25. {
  26. _context = context;
  27. _oprationLogRepository = oprationLogRepository;
  28. }
  29. /// <summary>
  30. /// 查询分页
  31. /// </summary>
  32. /// <param name="input"></param>
  33. /// <returns></returns>
  34. [HttpPost]
  35. public async Task<PageOutput<OprationLogListOutput>> GetPageAsync(PageInput<LogGetPageDto> input)
  36. {
  37. var userName = input.Filter?.CreatedUserName;
  38. var list = await _oprationLogRepository.Select
  39. .WhereDynamicFilter(input.DynamicFilter)
  40. .WhereIf(userName.NotNull(), a => a.CreatedUserName.Contains(userName))
  41. .Count(out var total)
  42. .OrderByDescending(true, c => c.Id)
  43. .Page(input.CurrentPage, input.PageSize)
  44. .ToListAsync<OprationLogListOutput>();
  45. var data = new PageOutput<OprationLogListOutput>()
  46. {
  47. List = list,
  48. Total = total
  49. };
  50. return data;
  51. }
  52. /// <summary>
  53. /// 新增
  54. /// </summary>
  55. /// <param name="input"></param>
  56. /// <returns></returns>
  57. public async Task<long> AddAsync(OprationLogAddInput input)
  58. {
  59. string ua = _context.HttpContext.Request.Headers["User-Agent"];
  60. if (ua.NotNull())
  61. {
  62. var client = UAParser.Parser.GetDefault().Parse(ua);
  63. var device = client.Device.Family;
  64. device = device.ToLower() == "other" ? "" : device;
  65. input.Browser = client.UA.Family;
  66. input.Os = client.OS.Family;
  67. input.Device = device;
  68. input.BrowserInfo = ua;
  69. }
  70. input.Name = User.Name;
  71. input.IP = IPHelper.GetIP(_context?.HttpContext?.Request);
  72. var entity = Mapper.Map<OprationLogEntity>(input);
  73. await _oprationLogRepository.InsertAsync(entity);
  74. return entity.Id;
  75. }
  76. }