0
0

OprationLogService.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System.Threading.Tasks;
  2. using Microsoft.AspNetCore.Http;
  3. using ZhonTai.Common.Helpers;
  4. using ZhonTai.Common.Domain.Dto;
  5. using ZhonTai.Plate.Admin.Service.OprationLog.Input;
  6. using ZhonTai.Plate.Admin.Service.OprationLog.Output;
  7. using ZhonTai.Plate.Admin.Domain.OprationLog;
  8. namespace ZhonTai.Plate.Admin.Service.OprationLog
  9. {
  10. public class OprationLogService : BaseService, IOprationLogService
  11. {
  12. private readonly IHttpContextAccessor _context;
  13. private readonly IOprationLogRepository _oprationLogRepository;
  14. public OprationLogService(
  15. IHttpContextAccessor context,
  16. IOprationLogRepository oprationLogRepository
  17. )
  18. {
  19. _context = context;
  20. _oprationLogRepository = oprationLogRepository;
  21. }
  22. public async Task<IResponseOutput> PageAsync(PageInput<OprationLogEntity> input)
  23. {
  24. var userName = input.Filter?.CreatedUserName;
  25. var list = await _oprationLogRepository.Select
  26. .WhereIf(userName.NotNull(), a => a.CreatedUserName.Contains(userName))
  27. .Count(out var total)
  28. .OrderByDescending(true, c => c.Id)
  29. .Page(input.CurrentPage, input.PageSize)
  30. .ToListAsync<OprationLogListOutput>();
  31. var data = new PageOutput<OprationLogListOutput>()
  32. {
  33. List = list,
  34. Total = total
  35. };
  36. return ResponseOutput.Ok(data);
  37. }
  38. public async Task<IResponseOutput> AddAsync(OprationLogAddInput input)
  39. {
  40. string ua = _context.HttpContext.Request.Headers["User-Agent"];
  41. var client = UAParser.Parser.GetDefault().Parse(ua);
  42. var device = client.Device.Family;
  43. device = device.ToLower() == "other" ? "" : device;
  44. input.Browser = client.UA.Family;
  45. input.Os = client.OS.Family;
  46. input.Device = device;
  47. input.BrowserInfo = ua;
  48. input.NickName = User.NickName;
  49. input.IP = IPHelper.GetIP(_context?.HttpContext?.Request);
  50. var entity = Mapper.Map<OprationLogEntity>(input);
  51. var id = (await _oprationLogRepository.InsertAsync(entity)).Id;
  52. return ResponseOutput.Result(id > 0);
  53. }
  54. }
  55. }