调整地区数据缓存
This commit is contained in:
@@ -10,21 +10,37 @@ namespace Atomx.Data.CacheServices
|
||||
/// 获取国家数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task<List<Area>> GetCountry(bool? reload = false);
|
||||
Task<List<Country>> GetCountries(bool? reload = false);
|
||||
|
||||
/// <summary>
|
||||
/// 通过ID获取国家名称
|
||||
/// 通过ID获取国家
|
||||
/// </summary>
|
||||
/// <param name="countryId"></param>
|
||||
/// <returns></returns>
|
||||
Task<string> GetCountryName(long countryId);
|
||||
Task<Country> GetCountry(long countryId);
|
||||
|
||||
/// <summary>
|
||||
/// 根据上级ID获取下级数据
|
||||
/// 获取区域数据
|
||||
/// </summary>
|
||||
/// <param name="parentId"></param>
|
||||
/// <returns></returns>
|
||||
Task<List<Area>> GetAreas(long parentId, bool? reload = false);
|
||||
Task<List<StateProvince>> GetStateProvinces(long countryId, bool? reload = false);
|
||||
|
||||
/// <summary>
|
||||
/// 获取州省数据
|
||||
/// </summary>
|
||||
/// <param name="countryId"></param>
|
||||
/// <param name="stateProvinceId"></param>
|
||||
/// <returns></returns>
|
||||
Task<StateProvince> GetStateProvince(long countryId, long stateProvinceId);
|
||||
|
||||
/// <summary>
|
||||
/// 获取地区数据
|
||||
/// </summary>
|
||||
/// <param name="countryId"></param>
|
||||
/// <param name="reload"></param>
|
||||
/// <returns></returns>
|
||||
Task<List<Area>> GetAreas(long countryId, bool? reload = false);
|
||||
|
||||
/// <summary>
|
||||
/// 更新调整缓存数据
|
||||
@@ -33,51 +49,67 @@ namespace Atomx.Data.CacheServices
|
||||
/// <returns></returns>
|
||||
Task ResetArea(Area area);
|
||||
|
||||
/// <summary>
|
||||
/// 更新调整州省缓存数据
|
||||
/// </summary>
|
||||
/// <param name="stateProvince"></param>
|
||||
/// <returns></returns>
|
||||
Task ResetStateProvince(StateProvince stateProvince);
|
||||
|
||||
/// <summary>
|
||||
/// 更新调整国家缓存数据
|
||||
/// </summary>
|
||||
/// <param name="country"></param>
|
||||
/// <returns></returns>
|
||||
Task ResetCountry(Country country);
|
||||
|
||||
/// <summary>
|
||||
/// 获取国家-省份-城市树形数据
|
||||
/// </summary>
|
||||
/// <param name="countryId"></param>
|
||||
/// <returns></returns>
|
||||
List<KeyValueTree> GetAreaTreeAsync(long countryId);
|
||||
|
||||
|
||||
Task<List<KeyValueTree>> GetAreaTree(long countryId, bool? reload = false);
|
||||
}
|
||||
public partial class CacheService : ICacheService
|
||||
{
|
||||
public async Task<List<KeyValueTree>> GetAreaTreeAsync(long countryId, bool? reload = false)
|
||||
public async Task<List<KeyValueTree>> GetAreaTree(long countryId, bool? reload = false)
|
||||
{
|
||||
bool reloadData = reload.HasValue ? reload.Value : false;
|
||||
var cacheData = await GetCacheAsync<List<KeyValueTree>>($"{CacheKeys.CountryTree}{countryId}");
|
||||
if (cacheData == null || reload.HasValue && reload.Value)
|
||||
if (cacheData == null || reloadData)
|
||||
{
|
||||
var state = _dbContext.StateProvinces.Where(p => p.CountryId == countryId).ToList();
|
||||
var cities = _dbContext.Areas.Where(p => p.CountryId == countryId).ToList();
|
||||
cacheData = (from s in state
|
||||
select new KeyValueTree
|
||||
{
|
||||
Key = s.Id.ToString(),
|
||||
Value = s.Name,
|
||||
Children = (from c in cities
|
||||
where c.ParentId == s.Id
|
||||
select new KeyValueTree
|
||||
{
|
||||
Key = c.Id.ToString(),
|
||||
Value = c.Name
|
||||
}).ToList()
|
||||
}).ToList();
|
||||
await SetCacheAsync(CacheKeys.Country, cacheData);
|
||||
var states = await GetStateProvinces(countryId);
|
||||
var cities = await GetAreas(countryId);
|
||||
|
||||
var tree = new List<KeyValueTree>();
|
||||
|
||||
foreach (var state in states)
|
||||
{
|
||||
var item = new KeyValueTree
|
||||
{
|
||||
Key = state.Id.ToString(),
|
||||
Value = state.Name,
|
||||
Children = new List<KeyValueTree>()
|
||||
};
|
||||
|
||||
item.Children = BuildAreaTree(state.Id, cities, new List<KeyValueTree>());
|
||||
tree.Add(item);
|
||||
}
|
||||
|
||||
await SetCacheAsync($"{CacheKeys.CountryTree}{countryId}", tree);
|
||||
}
|
||||
return cacheData;
|
||||
return cacheData;
|
||||
}
|
||||
|
||||
public async Task<List<Area>> GetCountry(bool? reload = false)
|
||||
public async Task<List<Country>> GetCountries(bool? reload = false)
|
||||
{
|
||||
bool reloadData = reload.HasValue ? reload.Value : false;
|
||||
|
||||
var cacheData = await GetCacheAsync<List<Area>>(CacheKeys.Country);
|
||||
var cacheData = await GetCacheAsync<List<Country>>(CacheKeys.Country);
|
||||
if (cacheData == null || reloadData)
|
||||
{
|
||||
var countries = (from p in _dbContext.Areas
|
||||
where p.ParentId == 0
|
||||
var countries = (from p in _dbContext.Countries
|
||||
where p.Enabled
|
||||
select p).ToList();
|
||||
await SetCacheAsync(CacheKeys.Country, countries);
|
||||
return countries;
|
||||
@@ -86,47 +118,144 @@ namespace Atomx.Data.CacheServices
|
||||
return cacheData;
|
||||
}
|
||||
|
||||
public async Task<string> GetCountryName(long countryId)
|
||||
public async Task<Country> GetCountry(long countryId)
|
||||
{
|
||||
var cacheData = await GetCountry();
|
||||
var cacheData = await GetCountries();
|
||||
var data = cacheData.SingleOrDefault(p => p.Id == countryId);
|
||||
if (data == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
return data.Name;
|
||||
return data;
|
||||
}
|
||||
|
||||
public async Task<List<Area>> GetAreas(long parentId, bool? reload = false)
|
||||
/// <summary>
|
||||
/// 获取区域数据
|
||||
/// </summary>
|
||||
/// <param name="countryId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<List<StateProvince>> GetStateProvinces(long countryId, bool? reload = false)
|
||||
{
|
||||
if (parentId == 0)
|
||||
bool reloadData = reload.HasValue ? reload.Value : false;
|
||||
var cacheData = await GetCacheAsync<List<StateProvince>>($"{CacheKeys.StateProvince}.{countryId}");
|
||||
if (cacheData == null || reloadData)
|
||||
{
|
||||
return await GetCountry();
|
||||
var stateProvinces = (from p in _dbContext.StateProvinces
|
||||
where p.CountryId == countryId && p.Enabled
|
||||
select p).ToList();
|
||||
await SetCacheAsync($"{CacheKeys.StateProvince}.{countryId}", stateProvinces);
|
||||
return stateProvinces;
|
||||
|
||||
}
|
||||
var cacheData = await GetCacheAsync<List<Area>>($"{CacheKeys.Area}.{parentId}");
|
||||
return cacheData;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取州省数据
|
||||
/// </summary>
|
||||
/// <param name="stateProvinceId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<StateProvince> GetStateProvince(long countryId, long stateProvinceId)
|
||||
{
|
||||
var cacheData = await GetStateProvinces(countryId);
|
||||
var data = cacheData.SingleOrDefault(p => p.Id == stateProvinceId);
|
||||
return data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取地区数据
|
||||
/// </summary>
|
||||
/// <param name="countryId"></param>
|
||||
/// <param name="reload"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<List<Area>> GetAreas(long countryId, bool? reload = false)
|
||||
{
|
||||
var cacheData = await GetCacheAsync<List<Area>>($"{CacheKeys.City}.{countryId}");
|
||||
bool reloadData = reload.HasValue ? reload.Value : false;
|
||||
if (cacheData == null || reloadData)
|
||||
{
|
||||
var data = (from p in _dbContext.Areas
|
||||
where p.ParentId == 0
|
||||
select p).ToList();
|
||||
await SetCacheAsync(CacheKeys.Country, data);
|
||||
return data;
|
||||
|
||||
List<Area> areas = new();
|
||||
var query = from p in _dbContext.Areas
|
||||
where p.CountryId == countryId && p.Enabled
|
||||
select p;
|
||||
var count = query.Count();
|
||||
int size = 50;
|
||||
var pageCount = (int)Math.Ceiling(count / (double)size) + 1;
|
||||
for (var i = 1; i < pageCount; i++)
|
||||
{
|
||||
var list = query.Skip((i - 1) * size).Take(size).ToList();
|
||||
areas.AddRange(list);
|
||||
}
|
||||
await SetCacheAsync(CacheKeys.Country, areas);
|
||||
return areas;
|
||||
}
|
||||
return cacheData;
|
||||
}
|
||||
|
||||
public async Task ResetArea(Area area)
|
||||
{
|
||||
var cacheData = await GetAreas(area.ParentId);
|
||||
var cacheData = await GetAreas(area.CountryId);
|
||||
var data = cacheData.Where(p => p.Id == area.Id).SingleOrDefault();
|
||||
if (data != null)
|
||||
{
|
||||
cacheData.Remove(data);
|
||||
}
|
||||
cacheData.Add(area);
|
||||
await SetCacheAsync($"{CacheKeys.Area}.{area.ParentId}", cacheData);
|
||||
await SetCacheAsync($"{CacheKeys.City}.{area.CountryId}", cacheData);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 更新调整州省缓存数据
|
||||
/// </summary>
|
||||
/// <param name="stateProvince"></param>
|
||||
/// <returns></returns>
|
||||
public async Task ResetStateProvince(StateProvince stateProvince)
|
||||
{
|
||||
var cacheData = await GetStateProvinces(stateProvince.CountryId);
|
||||
var data = cacheData.Where(p => p.Id == stateProvince.Id).SingleOrDefault();
|
||||
if (data != null)
|
||||
{
|
||||
cacheData.Remove(data);
|
||||
}
|
||||
cacheData.Add(stateProvince);
|
||||
await SetCacheAsync($"{CacheKeys.StateProvince}.{stateProvince.CountryId}", cacheData);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新调整国家缓存数据
|
||||
/// </summary>
|
||||
/// <param name="country"></param>
|
||||
/// <returns></returns>
|
||||
public async Task ResetCountry(Country country)
|
||||
{
|
||||
var cacheData = await GetCountries();
|
||||
var data = cacheData.Where(p => p.Id == country.Id).SingleOrDefault();
|
||||
if (data != null)
|
||||
{
|
||||
cacheData.Remove(data);
|
||||
}
|
||||
cacheData.Add(country);
|
||||
await SetCacheAsync($"{CacheKeys.Country}", cacheData);
|
||||
}
|
||||
|
||||
private List<KeyValueTree> BuildAreaTree(long parentId, List<Area> areas, List<KeyValueTree> result)
|
||||
{
|
||||
var data = areas.Where(p => p.ParentId == parentId).ToList();
|
||||
foreach (var area in areas)
|
||||
{
|
||||
var item = new KeyValueTree
|
||||
{
|
||||
Key = area.Id.ToString(),
|
||||
Value = area.Name,
|
||||
Children = new List<KeyValueTree>()
|
||||
};
|
||||
var childs = areas.Where(p => p.ParentId == area.Id).ToList();
|
||||
if (childs.Count > 0)
|
||||
{
|
||||
var childrenTrees = BuildAreaTree(area.Id, areas, result);
|
||||
item.Children.AddRange(childrenTrees);
|
||||
}
|
||||
result.Add(item);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user