using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Caching.Distributed; using Shentun.Peis.Enums; using Shentun.Peis.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Volo.Abp.Caching; using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Repositories; using Volo.Abp.Identity; namespace Shentun.Peis { public class CacheService : ISingletonDependency { private readonly IDistributedCache _userCache; private readonly IDistributedCache _customerOrgCache; private readonly IDistributedCache _customerOrgTopNameCache; private readonly IDistributedCache _nationCache; private readonly IDistributedCache _sexCache; private readonly IDistributedCache _maritalStatusCache; private readonly IDistributedCache _forSexCache; private readonly IDistributedCache _deviceTypeCache; private readonly IDistributedCache _medicalTypeCache; private readonly IDistributedCache _personnelTypeCache; private readonly IDistributedCache _asbitemCache; private readonly IDistributedCache _itemTypeCache; private readonly IRepository _userRepository; private readonly IRepository _nationRepository; private readonly IRepository _sexRepository; private readonly IRepository _forSexRepository; private readonly IRepository _deviceTypeRepository; private readonly IRepository _asbitemRepository; private readonly IRepository _itemTypeRepository; private readonly IRepository _customerOrgRepository; private readonly IRepository _maritalStatusRepository; private readonly IRepository _medicalTypeRepository; private readonly IRepository _personnelTypeRepository; public CacheService( IDistributedCache userCache, IDistributedCache customerOrgCache, IDistributedCache nationCache, IRepository userRepository, IDistributedCache sexCache, IDistributedCache asbitemCache, IDistributedCache itemTypeCache, IDistributedCache maritalStatusCache, IDistributedCache medicalTypeCache, IDistributedCache personnelTypeCache, IRepository sexRepository, IDistributedCache deviceTypeCache, IRepository deviceTypeRepository, IDistributedCache forSexCache, IRepository forSexRepository, IRepository asbitemRepository, IRepository itemTypeRepository, IRepository nationRepository, IRepository customerOrgRepository, IRepository maritalStatusRepository, IRepository medicalTypeRepository, IRepository personnelTypeRepository , IDistributedCache customerOrgTopNameCache) { _userCache = userCache; _userRepository = userRepository; _sexCache = sexCache; _sexRepository = sexRepository; _forSexCache = forSexCache; _forSexRepository = forSexRepository; _deviceTypeCache = deviceTypeCache; _deviceTypeRepository = deviceTypeRepository; _asbitemCache = asbitemCache; _asbitemRepository = asbitemRepository; _itemTypeCache = itemTypeCache; _itemTypeRepository = itemTypeRepository; _nationCache = nationCache; _nationRepository = nationRepository; _customerOrgCache = customerOrgCache; _customerOrgRepository = customerOrgRepository; _maritalStatusCache = maritalStatusCache; _maritalStatusRepository = maritalStatusRepository; _medicalTypeCache = medicalTypeCache; _medicalTypeRepository = medicalTypeRepository; _personnelTypeCache = personnelTypeCache; _personnelTypeRepository = personnelTypeRepository; _customerOrgTopNameCache = customerOrgTopNameCache; } private async Task GetUserAsync(Guid id) { var entity = await _userCache.GetOrAddAsync( id, //缓存键 async () => await _userRepository.FirstOrDefaultAsync(m => m.Id == id) ); return entity; } public async Task GetSurnameAsync(Guid? id) { if (id == null || id == default(Guid) || !id.HasValue) { return ""; } var entity = await GetUserAsync((Guid)id); if (entity != null) return entity.Surname; else return ""; } private async Task GetSexAsync(char id) { var entity = await _sexCache.GetOrAddAsync( id, //缓存键 async () => (await _sexRepository.GetQueryableAsync()).Where(o => o.Id == id).Single() ); return entity; } public async Task GetSexNameAsync(char id) { var entity = await GetSexAsync(id); return entity.DisplayName; } private async Task GetForSexAsync(char id) { var entity = await _forSexCache.GetOrAddAsync( id, //缓存键 async () => (await _forSexRepository.GetQueryableAsync()).Where(o => o.Id == id).Single() ); return entity; } public async Task GetForSexNameAsync(char id) { var entity = await GetForSexAsync(id); return entity.DisplayName; } private async Task GetMaritalStatusAsync(char id) { var entity = await _maritalStatusCache.GetOrAddAsync( id, //缓存键 async () => (await _maritalStatusRepository.GetQueryableAsync()).Where(o => o.Id == id).Single() ); return entity; } public async Task GetMaritalStatusNameAsync(char id) { var entity = await GetMaritalStatusAsync(id); return entity.DisplayName; } private async Task GetDeviceTypeAsync(Guid id) { var entity = await _deviceTypeCache.GetOrAddAsync( id, //缓存键 async () => await _deviceTypeRepository.GetAsync(id) ); return entity; } public async Task GetDeviceTypeNameAsync(Guid? id) { if (id == null || id == default(Guid) || !id.HasValue) { return ""; } var entity = await GetDeviceTypeAsync((Guid)id); return entity.DisplayName; } private async Task GetMedicalTypeAsync(Guid id) { var entity = await _medicalTypeCache.GetOrAddAsync( id, //缓存键 async () => await _medicalTypeRepository.GetAsync(id) ); return entity; } public async Task GetMedicalTypeNameAsync(Guid? id) { if (id == null || id == default(Guid) || !id.HasValue) { return ""; } var entity = await GetMedicalTypeAsync((Guid)id); return entity.DisplayName; } private async Task GetPersonnelTypeAsync(Guid id) { var entity = await _personnelTypeCache.GetOrAddAsync( id, //缓存键 async () => await _personnelTypeRepository.GetAsync(id) ); return entity; } public async Task GetPersonnelTypeNameAsync(Guid? id) { if (id == null || id == default(Guid) || !id.HasValue) { return ""; } var entity = await GetPersonnelTypeAsync((Guid)id); return entity.DisplayName; } public async Task GetAsbitemAsync(Guid id) { var entity = await _asbitemCache.GetOrAddAsync( id, //缓存键 async () => await _asbitemRepository.GetAsync(id) ); return entity; } public async Task GetItemTypeAsync(Guid id) { var entity = await _itemTypeCache.GetOrAddAsync( id, //缓存键 async () => await _itemTypeRepository.GetAsync(id) ); return entity; } public async Task GetNationAsync(string id) { var entity = await _nationCache.GetOrAddAsync( id, //缓存键 async () => await _nationRepository.GetAsync(o => o.Id == id) ); return entity; } public async Task GetNationNameAsync(string id) { if (string.IsNullOrWhiteSpace(id)) { return ""; } var entity = await GetNationAsync(id); return entity.DisplayName; } public async Task GetCustomerOrgAsync(Guid id) { var entity = await _customerOrgCache.GetAsync(id); if (entity == null) { entity = await _customerOrgRepository.GetAsync(o => o.Id == id); _customerOrgCache.Set(id, entity); } return entity; } public async Task GetCustomerOrgNameAsync(Guid? id) { if (id == null || id == default(Guid) || !id.HasValue) { return ""; } var entity = await GetCustomerOrgAsync((Guid)id); return entity.DisplayName; } public async Task GetTopCustomerOrgAsync(Guid id) { var entity = await _customerOrgCache.GetAsync(id); if(entity == null) { entity = await _customerOrgRepository.GetAsync(o => o.Id == id); _customerOrgCache.Set(id, entity); } if (entity.ParentId != null && entity.ParentId != Guid.Empty) { entity = await GetTopCustomerOrgAsync((Guid)entity.ParentId); } return entity; } /// /// 缓存取单位名称 /// /// 部门ID /// public async Task GetTopCustomerOrgNameAsync(Guid CustomerOrgId) { string topCustomerOrgName = await _customerOrgTopNameCache.GetOrAddAsync( CustomerOrgId, //缓存键 async () => await GetTopCustomerOrgNameInCustomerOrgIdAsync(CustomerOrgId) ); return topCustomerOrgName; } /// /// 根据部门ID 按PathCode码查询单位名称 /// /// /// private async Task GetTopCustomerOrgNameInCustomerOrgIdAsync(Guid id) { var entity = await _customerOrgCache.GetAsync(id); if (entity == null) { entity = await _customerOrgRepository.GetAsync(o => o.Id == id); _customerOrgCache.Set(id, entity); } if (entity.Id == GuidFlag.PersonCustomerOrgId) { return entity.DisplayName; } if (entity.PathCode.Length == 5) { return entity.DisplayName; } entity = await _customerOrgRepository.GetAsync(o => o.PathCode == entity.PathCode.Substring(0, 5)); return entity.DisplayName; } } }