You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

362 lines
12 KiB

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<IdentityUser, Guid> _userCache;
private readonly IDistributedCache<CustomerOrg, Guid> _customerOrgCache;
private readonly IDistributedCache<string, Guid> _customerOrgTopNameCache;
private readonly IDistributedCache<Nation, string> _nationCache;
private readonly IDistributedCache<Sex, char> _sexCache;
private readonly IDistributedCache<MaritalStatus, char> _maritalStatusCache;
private readonly IDistributedCache<ForSex, char> _forSexCache;
private readonly IDistributedCache<DeviceType, Guid> _deviceTypeCache;
private readonly IDistributedCache<MedicalType, Guid> _medicalTypeCache;
private readonly IDistributedCache<PersonnelType, Guid> _personnelTypeCache;
private readonly IDistributedCache<Asbitem, Guid> _asbitemCache;
private readonly IDistributedCache<ItemType, Guid> _itemTypeCache;
private readonly IRepository<IdentityUser, Guid> _userRepository;
private readonly IRepository<Nation> _nationRepository;
private readonly IRepository<Sex> _sexRepository;
private readonly IRepository<ForSex> _forSexRepository;
private readonly IRepository<DeviceType, Guid> _deviceTypeRepository;
private readonly IRepository<Asbitem, Guid> _asbitemRepository;
private readonly IRepository<ItemType, Guid> _itemTypeRepository;
private readonly IRepository<CustomerOrg, Guid> _customerOrgRepository;
private readonly IRepository<MaritalStatus> _maritalStatusRepository;
private readonly IRepository<MedicalType, Guid> _medicalTypeRepository;
private readonly IRepository<PersonnelType, Guid> _personnelTypeRepository;
public CacheService(
IDistributedCache<IdentityUser, Guid> userCache,
IDistributedCache<CustomerOrg, Guid> customerOrgCache,
IDistributedCache<Nation, string> nationCache,
IRepository<IdentityUser, Guid> userRepository,
IDistributedCache<Sex, char> sexCache,
IDistributedCache<Asbitem, Guid> asbitemCache,
IDistributedCache<ItemType, Guid> itemTypeCache,
IDistributedCache<MaritalStatus, char> maritalStatusCache,
IDistributedCache<MedicalType, Guid> medicalTypeCache,
IDistributedCache<PersonnelType, Guid> personnelTypeCache,
IRepository<Sex> sexRepository,
IDistributedCache<DeviceType, Guid> deviceTypeCache,
IRepository<DeviceType, Guid> deviceTypeRepository,
IDistributedCache<ForSex, char> forSexCache,
IRepository<ForSex> forSexRepository,
IRepository<Asbitem, Guid> asbitemRepository,
IRepository<ItemType, Guid> itemTypeRepository,
IRepository<Nation> nationRepository,
IRepository<CustomerOrg, Guid> customerOrgRepository,
IRepository<MaritalStatus> maritalStatusRepository,
IRepository<MedicalType, Guid> medicalTypeRepository,
IRepository<PersonnelType, Guid> personnelTypeRepository
,
IDistributedCache<string, Guid> 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<IdentityUser> GetUserAsync(Guid id)
{
var entity = await _userCache.GetOrAddAsync(
id, //缓存键
async () => await _userRepository.FirstOrDefaultAsync(m => m.Id == id)
);
return entity;
}
public async Task<string> 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<Sex> GetSexAsync(char id)
{
var entity = await _sexCache.GetOrAddAsync(
id, //缓存键
async () => (await _sexRepository.GetQueryableAsync()).Where(o => o.Id == id).Single()
);
return entity;
}
public async Task<string> GetSexNameAsync(char id)
{
var entity = await GetSexAsync(id);
return entity.DisplayName;
}
private async Task<ForSex> GetForSexAsync(char id)
{
var entity = await _forSexCache.GetOrAddAsync(
id, //缓存键
async () => (await _forSexRepository.GetQueryableAsync()).Where(o => o.Id == id).Single()
);
return entity;
}
public async Task<string> GetForSexNameAsync(char id)
{
var entity = await GetForSexAsync(id);
return entity.DisplayName;
}
private async Task<MaritalStatus> GetMaritalStatusAsync(char id)
{
var entity = await _maritalStatusCache.GetOrAddAsync(
id, //缓存键
async () => (await _maritalStatusRepository.GetQueryableAsync()).Where(o => o.Id == id).Single()
);
return entity;
}
public async Task<string> GetMaritalStatusNameAsync(char id)
{
var entity = await GetMaritalStatusAsync(id);
return entity.DisplayName;
}
private async Task<DeviceType> GetDeviceTypeAsync(Guid id)
{
var entity = await _deviceTypeCache.GetOrAddAsync(
id, //缓存键
async () => await _deviceTypeRepository.GetAsync(id)
);
return entity;
}
public async Task<string> GetDeviceTypeNameAsync(Guid? id)
{
if (id == null || id == default(Guid) || !id.HasValue)
{
return "";
}
var entity = await GetDeviceTypeAsync((Guid)id);
return entity.DisplayName;
}
private async Task<MedicalType> GetMedicalTypeAsync(Guid id)
{
var entity = await _medicalTypeCache.GetOrAddAsync(
id, //缓存键
async () => await _medicalTypeRepository.GetAsync(id)
);
return entity;
}
public async Task<string> GetMedicalTypeNameAsync(Guid? id)
{
if (id == null || id == default(Guid) || !id.HasValue)
{
return "";
}
var entity = await GetMedicalTypeAsync((Guid)id);
return entity.DisplayName;
}
private async Task<PersonnelType> GetPersonnelTypeAsync(Guid id)
{
var entity = await _personnelTypeCache.GetOrAddAsync(
id, //缓存键
async () => await _personnelTypeRepository.GetAsync(id)
);
return entity;
}
public async Task<string> GetPersonnelTypeNameAsync(Guid? id)
{
if (id == null || id == default(Guid) || !id.HasValue)
{
return "";
}
var entity = await GetPersonnelTypeAsync((Guid)id);
return entity.DisplayName;
}
public async Task<Asbitem> GetAsbitemAsync(Guid id)
{
var entity = await _asbitemCache.GetOrAddAsync(
id, //缓存键
async () => await _asbitemRepository.GetAsync(id)
);
return entity;
}
public async Task<ItemType> GetItemTypeAsync(Guid id)
{
var entity = await _itemTypeCache.GetOrAddAsync(
id, //缓存键
async () => await _itemTypeRepository.GetAsync(id)
);
return entity;
}
public async Task<Nation> GetNationAsync(string id)
{
var entity = await _nationCache.GetOrAddAsync(
id, //缓存键
async () => await _nationRepository.GetAsync(o => o.Id == id)
);
return entity;
}
public async Task<string> GetNationNameAsync(string id)
{
if (string.IsNullOrWhiteSpace(id))
{
return "";
}
var entity = await GetNationAsync(id);
return entity.DisplayName;
}
public async Task<CustomerOrg> 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<string> GetCustomerOrgNameAsync(Guid? id)
{
if (id == null || id == default(Guid) || !id.HasValue)
{
return "";
}
var entity = await GetCustomerOrgAsync((Guid)id);
return entity.DisplayName;
}
public async Task<CustomerOrg> 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;
}
/// <summary>
/// 缓存取单位名称
/// </summary>
/// <param name="CustomerOrgId">部门ID</param>
/// <returns></returns>
public async Task<string> GetTopCustomerOrgNameAsync(Guid CustomerOrgId)
{
string topCustomerOrgName = await _customerOrgTopNameCache.GetOrAddAsync(
CustomerOrgId, //缓存键
async () => await GetTopCustomerOrgNameInCustomerOrgIdAsync(CustomerOrgId)
);
return topCustomerOrgName;
}
/// <summary>
/// 根据部门ID 按PathCode码查询单位名称
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private async Task<string> 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;
}
}
}