GFSFRLineAddressService.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using Microsoft.Extensions.Caching.Distributed;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using XYY.Data.Standard.OrderAddressCheck;
  7. using XYY.Model.Standard.OrderAddressCheck;
  8. namespace XYY.Service.Standard.AddressCheck
  9. {
  10. public class GFSFRLineAddressService : IAdderssService
  11. {
  12. private IFRSLAreasRepository _frSLAreasRepository;
  13. private const string CacheKey = "FRSLAreas";
  14. private IDistributedCache _cache;
  15. public int ChannelId { get => 99; }
  16. public string CountryCode { get => "FR"; }
  17. public GFSFRLineAddressService(IFRSLAreasRepository frSLAreasRepository, IDistributedCache cache)
  18. {
  19. _frSLAreasRepository = frSLAreasRepository;
  20. _cache = cache;
  21. }
  22. public async Task<AddressGroup> Verify(OrderAddress address)
  23. {
  24. throw new Exception("暂未实现");
  25. //非紧急,暂不实现
  26. //return AddressGroup.Normal;
  27. }
  28. public async Task<RecommendedAddress> GetRecommendedAddresses(OrderAddress address)
  29. {
  30. var all = await GetCacheData();
  31. var RecommendedAddress = new RecommendedAddress
  32. {
  33. CityRecommendedAddress = new List<RecommendedAddressItem>(),
  34. ZipCodeRecommendedAddress = new List<RecommendedAddressItem>()
  35. };
  36. address.City = address.City.ToUpper();
  37. RecommendedAddress.CityRecommendedAddress = all.Where(x => x.AreaName.Contains(address.City))
  38. .Select(x => ConvertAddress(x)).ToList();
  39. RecommendedAddress.ZipCodeRecommendedAddress = all.Where(x => x.ZipCode.Equals(address.ZipCode, StringComparison.InvariantCultureIgnoreCase))
  40. .Select(x => ConvertAddress(x)).ToList();
  41. return RecommendedAddress;
  42. }
  43. private RecommendedAddressItem ConvertAddress(FRSLAreas area)
  44. {
  45. return new RecommendedAddressItem { City = area.AreaName, ZipCode = area.ZipCode };
  46. }
  47. private async Task<List<FRSLAreas>> GetCacheData()
  48. {
  49. var areas = await _cache.GetStringAsync(CacheKey);
  50. if (areas == null || string.IsNullOrEmpty(areas))
  51. {
  52. var list = await _frSLAreasRepository.GetAll();
  53. areas = Newtonsoft.Json.JsonConvert.SerializeObject(list);
  54. await _cache.SetStringAsync(CacheKey, areas);
  55. }
  56. var data = Newtonsoft.Json.JsonConvert.DeserializeObject<List<FRSLAreas>>(areas);
  57. return data;
  58. }
  59. }
  60. }