VehicleAppService.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using Microsoft.Extensions.Caching.Memory;
  2. using System;
  3. using System.Threading.Tasks;
  4. using Znaf.Application.Communication;
  5. using Znaf.Application.Interfaces;
  6. using Znaf.Domain.Interfaces;
  7. using Znaf.Domain.Models;
  8. using Znaf.Domain.Models.Queries;
  9. using Znaf.Infra.CrossCutting.Bus;
  10. namespace Znaf.Application.Services
  11. {
  12. /// <summary>
  13. /// 车辆服务
  14. /// </summary>
  15. public class VehicleAppService : IVehicleAppService
  16. {
  17. private readonly IUnitOfWork _unitOfWork;
  18. private readonly IMemoryCache _cache;
  19. private readonly IVehicleRepository _vehicleRepository;
  20. public VehicleAppService(IUnitOfWork unitOfWork, IMemoryCache cache, IVehicleRepository vehicleRepository)
  21. {
  22. _unitOfWork = unitOfWork;
  23. _cache = cache;
  24. _vehicleRepository = vehicleRepository;
  25. }
  26. public async Task<QueryResult<Vehicle>> ListVehicles(VehicleQuery query)
  27. {
  28. var cacheKey = GetCacheKeyForVehicleQuery(query);
  29. var vehicles = await _cache.GetOrCreateAsync(cacheKey, entry =>
  30. {
  31. entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1);
  32. return _vehicleRepository.GetByPagination(query);
  33. });
  34. return vehicles;
  35. }
  36. public async Task<VehicleResponse> AddAsync(Vehicle vehicle)
  37. {
  38. try
  39. {
  40. var existVehicle = await _vehicleRepository.FindByGuidAsync(vehicle.Guid);
  41. if (existVehicle != null)
  42. {
  43. return new VehicleResponse("已存在");
  44. }
  45. await _vehicleRepository.AddAsync(vehicle);
  46. await _unitOfWork.CompleteAsync();
  47. return new VehicleResponse(vehicle);
  48. }
  49. catch (Exception e)
  50. {
  51. return new VehicleResponse($"新增时出现错误:{e.Message}");
  52. }
  53. }
  54. public async Task<VehicleResponse> UpdateAsync(string guid, Vehicle vehicle)
  55. {
  56. var existVehicle = await _vehicleRepository.FindByGuidAsync(guid);
  57. if (existVehicle == null)
  58. {
  59. return new VehicleResponse("未找到车辆信息");
  60. }
  61. //todo 更新数据
  62. try
  63. {
  64. _vehicleRepository.Update(existVehicle);
  65. await _unitOfWork.CompleteAsync();
  66. return new VehicleResponse(existVehicle);
  67. }
  68. catch (Exception e)
  69. {
  70. return new VehicleResponse($"更新出现问题:{e.Message}");
  71. }
  72. }
  73. public async Task<VehicleResponse> DeleteAsync(string guid)
  74. {
  75. var existVehicle = await _vehicleRepository.FindByGuidAsync(guid);
  76. if (existVehicle == null)
  77. {
  78. return new VehicleResponse("未找到车辆信息");
  79. }
  80. try
  81. {
  82. _vehicleRepository.Remove(existVehicle);
  83. await _unitOfWork.CompleteAsync();
  84. return new VehicleResponse(existVehicle);
  85. }
  86. catch (Exception e)
  87. {
  88. return new VehicleResponse($"删除出现问题:{e.Message}");
  89. }
  90. }
  91. private static string GetCacheKeyForVehicleQuery(VehicleQuery query)
  92. {
  93. var key = CacheKeys.VehiclesList.ToString();
  94. key = string.Concat(key, "_", query.Page, "_", query.ItemsPerPage);
  95. return key;
  96. }
  97. }
  98. }