using Furion.DependencyInjection; using Furion.DynamicApiController; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using Myshipping.Core.Entity; using Myshipping.Core.Service.Dict.Dto; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Myshipping.Core.Service; /// /// 系统缓存服务 /// [ApiDescriptionSettings(Name = "Cache", Order = 100)] public class SysCacheService : ISysCacheService, IDynamicApiController, ISingleton { private readonly ICache _cache; private readonly CacheOptions _cacheOptions; public SysCacheService(IOptions cacheOptions, Func resolveNamed) { _cacheOptions = cacheOptions.Value; _cache = resolveNamed(_cacheOptions.CacheType.ToString(), default) as ICache; } /// /// 获取数据范围缓存(机构Id集合) /// /// /// public async Task> GetDataScope(long userId) { var cacheKey = CommonConst.CACHE_KEY_DATASCOPE + $"{userId}"; return await _cache.GetAsync>(cacheKey); } /// /// 获取数据范围缓存(用户Id集合) /// /// /// public async Task> GetUsersDataScope(long userId) { var cacheKey = CommonConst.CACHE_KEY_USERSDATASCOPE + $"{userId}"; return await _cache.GetAsync>(cacheKey); } /// /// 缓存数据范围(机构Id集合) /// /// /// /// [NonAction] public async Task SetDataScope(long userId, List dataScopes) { var cacheKey = CommonConst.CACHE_KEY_DATASCOPE + $"{userId}"; await _cache.SetAsync(cacheKey, dataScopes); } /// /// 缓存数据范围(用户Id集合) /// /// /// /// [NonAction] public async Task SetUsersDataScope(long userId, List dataScopes) { var cacheKey = CommonConst.CACHE_KEY_USERSDATASCOPE + $"{userId}"; await _cache.SetAsync(cacheKey, dataScopes); } /// /// 获取菜单缓存 /// /// /// /// public async Task> GetMenu(long userId, string appCode) { var cacheKey = CommonConst.CACHE_KEY_MENU + $"{userId}-{appCode}"; return await _cache.GetAsync>(cacheKey); } /// /// 缓存菜单 /// /// /// /// /// [NonAction] public async Task SetMenu(long userId, string appCode, List menus) { var cacheKey = CommonConst.CACHE_KEY_MENU + $"{userId}-{appCode}"; await _cache.SetAsync(cacheKey, menus); } /// /// 获取权限缓存(按钮) /// /// /// public async Task> GetPermission(long userId) { var cacheKey = CommonConst.CACHE_KEY_PERMISSION + $"{userId}"; return await _cache.GetAsync>(cacheKey); } /// /// 缓存权限 /// /// /// /// [NonAction] public async Task SetPermission(long userId, List permissions) { var cacheKey = CommonConst.CACHE_KEY_PERMISSION + $"{userId}"; await _cache.SetAsync(cacheKey, permissions); } /// /// 获取所有缓存关键字 /// /// public List GetAllCacheKeys() { var cacheItems = _cache.GetAllKeys(); if (cacheItems == null) return new List(); return cacheItems.Where(u => !u.ToString().StartsWith("mini-profiler")).Select(u => u).ToList(); } /// /// 获取所有按钮权限 /// /// public async Task> GetAllPermission() { var cacheKey = CommonConst.CACHE_KEY_ALLPERMISSION; return await _cache.GetAsync>(cacheKey); } /// /// 设置所有按钮权限 /// /// /// public async Task SetAllPermission(List permissions) { var cacheKey = CommonConst.CACHE_KEY_ALLPERMISSION; await _cache.SetAsync(cacheKey, permissions); } /// /// 删除指定关键字缓存 /// /// /// [NonAction] public bool Del(string key) { _cache.Del(key); return true; } /// /// 删除指定关键字缓存 /// /// /// public Task DelAsync(string key) { _cache.DelAsync(key); return Task.FromResult(true); } /// /// 删除某特征关键字缓存 /// /// /// public Task DelByPatternAsync(string key) { _cache.DelByPatternAsync(key); return Task.FromResult(true); } /// /// 设置缓存 /// /// /// /// [NonAction] public bool Set(string key, object value) { return _cache.Set(key, value); } /// /// 设置缓存 /// /// /// /// public async Task SetAsync(string key, object value) { return await _cache.SetAsync(key, value); } /// /// 设置缓存,并带有过期时间 /// /// /// /// /// public async Task SetTimeoutAsync(string key, object value, TimeSpan ts) { return await _cache.SetAsync(key, value, ts); } /// /// 获取缓存 /// /// /// [NonAction] public string Get(string key) { return _cache.Get(key); } /// /// 获取缓存 /// /// /// public async Task GetAsync(string key) { return await _cache.GetAsync(key); } /// /// 获取缓存 /// /// /// /// [NonAction] public T Get(string key) { return _cache.Get(key); } /// /// 获取缓存 /// /// /// /// public Task GetAsync(string key) { return _cache.GetAsync(key); } /// /// 检查给定 key 是否存在 /// /// 键 /// [NonAction] public bool Exists(string key) { return _cache.Exists(key); } /// /// 检查给定 key 是否存在 /// /// 键 /// public Task ExistsAsync(string key) { return _cache.ExistsAsync(key); } #region 公共库缓存 /// /// 获取公共库 船公司 /// /// public Task> GetAllCodeCarrier() { return _cache.GetAsync>(CommonConst.CACHE_KEY_COMMON_DB_CARRIER); } /// /// 获取公共库 船名 /// /// public Task> GetAllCodeVessel() { return _cache.GetAsync>(CommonConst.CACHE_KEY_COMMON_DB_VESSEL); } /// /// 获取公共库 船代 /// /// public Task> GetAllCodeForwarder() { return _cache.GetAsync>(CommonConst.CACHE_KEY_COMMON_DB_FORWARDER); } /// /// 获取公共库 场站 /// /// public Task> GetAllCodeYard() { return _cache.GetAsync>(CommonConst.CACHE_KEY_COMMON_DB_YARD); } /// /// 获取公共库 起始港 /// /// public Task> GetAllCodePortLoad() { return _cache.GetAsync>(CommonConst.CACHE_KEY_COMMON_DB_PORTLOAD); } /// /// 获取公共库 目的港 /// /// public Task> GetAllCodePort() { return _cache.GetAsync>(CommonConst.CACHE_KEY_COMMON_DB_PORT); } /// /// 获取公共库 包装 /// /// public Task> GetAllCodePackage() { return _cache.GetAsync>(CommonConst.CACHE_KEY_COMMON_DB_PACKAGE); } /// /// 获取公共库 运输方式 /// /// public Task> GetAllCodeService() { return _cache.GetAsync>(CommonConst.CACHE_KEY_COMMON_DB_SERVICE); } /// /// 获取公共库 箱型 /// /// public Task> GetAllCodeCtn() { return _cache.GetAsync>(CommonConst.CACHE_KEY_COMMON_DB_CTN); } /// /// 获取公共库 付费方式 /// /// public Task> GetAllCodeFrt() { return _cache.GetAsync>(CommonConst.CACHE_KEY_COMMON_DB_FRT); } /// /// 获取公共库 船司映射 /// /// public Task> GetAllMappingCtn() { return _cache.GetAsync>(CommonConst.CACHE_KEY_COMMON_DB_MAPPING_CTN); } /// /// 获取公共库 箱型映射 /// /// public Task> GetAllMappingCarrier() { return _cache.GetAsync>(CommonConst.CACHE_KEY_COMMON_DB_MAPPING_CARRIER); } /// /// 获取公共库 费用方式映射 /// /// public Task> GetAllMappingFrt() { return _cache.GetAsync>(CommonConst.CACHE_KEY_COMMON_DB_MAPPING_FRT); } /// /// 获取公共库 船名映射 /// /// public Task> GetAllMappingVessel() { return _cache.GetAsync>(CommonConst.CACHE_KEY_COMMON_DB_MAPPING_VESSEL); } /// /// 设置公共库 船公司 /// /// public Task SetAllCodeCarrier(List list) { return _cache.SetAsync(CommonConst.CACHE_KEY_COMMON_DB_CARRIER, list, TimeSpan.FromMinutes(5)); } /// /// 设置公共库 船名 /// /// public Task SetAllCodeVessel(List list) { return _cache.SetAsync(CommonConst.CACHE_KEY_COMMON_DB_VESSEL, list, TimeSpan.FromMinutes(5)); } /// /// 设置公共库 船代 /// /// public Task SetAllCodeForwarder(List list) { return _cache.SetAsync(CommonConst.CACHE_KEY_COMMON_DB_FORWARDER, list, TimeSpan.FromMinutes(5)); } /// /// 设置公共库 场站 /// /// public Task SetAllCodeYard(List list) { return _cache.SetAsync(CommonConst.CACHE_KEY_COMMON_DB_YARD, list, TimeSpan.FromMinutes(5)); } /// /// 设置公共库 起始港 /// /// public Task SetAllCodePortLoad(List list) { return _cache.SetAsync(CommonConst.CACHE_KEY_COMMON_DB_PORTLOAD, list, TimeSpan.FromMinutes(5)); } /// /// 设置公共库 目的港 /// /// public Task SetAllCodePort(List list) { return _cache.SetAsync(CommonConst.CACHE_KEY_COMMON_DB_PORT, list, TimeSpan.FromMinutes(5)); } /// /// 设置公共库 包装 /// /// public Task SetAllCodePackage(List list) { return _cache.SetAsync(CommonConst.CACHE_KEY_COMMON_DB_PACKAGE, list, TimeSpan.FromMinutes(5)); } /// /// 设置公共库 运输方式 /// /// public Task SetAllCodeService(List list) { return _cache.SetAsync(CommonConst.CACHE_KEY_COMMON_DB_SERVICE, list, TimeSpan.FromMinutes(5)); } /// /// 设置公共库 箱型 /// /// public Task SetAllCodeCtn(List list) { return _cache.SetAsync(CommonConst.CACHE_KEY_COMMON_DB_CTN, list, TimeSpan.FromMinutes(5)); } /// /// 设置公共库 付费方式 /// /// public Task SetAllCodeFrt(List list) { return _cache.SetAsync(CommonConst.CACHE_KEY_COMMON_DB_FRT, list, TimeSpan.FromMinutes(5)); } /// /// 设置公共库 箱型映射 /// /// public Task SetAllMappingCtn(List list) { return _cache.SetAsync(CommonConst.CACHE_KEY_COMMON_DB_MAPPING_CTN, list, TimeSpan.FromMinutes(5)); } /// /// 设置公共库 船司映射 /// /// public Task SetAllMappingCarrier(List list) { return _cache.SetAsync(CommonConst.CACHE_KEY_COMMON_DB_MAPPING_CARRIER, list, TimeSpan.FromMinutes(5)); } /// /// 设置公共库 费用方式映射 /// /// public Task SetAllMappingFrt(List list) { return _cache.SetAsync(CommonConst.CACHE_KEY_COMMON_DB_MAPPING_FRT, list, TimeSpan.FromMinutes(5)); } /// /// 设置公共库 船名映射 /// /// public Task SetAllMappingVessel(List list) { return _cache.SetAsync(CommonConst.CACHE_KEY_COMMON_DB_MAPPING_VESSEL, list, TimeSpan.FromMinutes(5)); } #endregion #region DJY /// /// 获取租户参数 /// /// public Task> GetAllTenantParam() { return _cache.GetAsync>(CommonConst.CACHE_KEY_COMMON_DB_TENANT_PARAM_VALUE); } /// /// 设置租户参数 /// /// public Task SetAllTenantParam(List list) { return _cache.SetAsync(CommonConst.CACHE_KEY_COMMON_DB_TENANT_PARAM_VALUE, list); } /// /// 获取字典缓存 /// /// public Task> GetAllDictData() { return _cache.GetAsync>(CommonConst.CACHE_KEY_COMMON_DB_DICT_DATA); } /// /// 设置字典参数 /// /// public Task SetAllDictData(List list) { return _cache.SetAsync(CommonConst.CACHE_KEY_COMMON_DB_DICT_DATA, list); } /// /// 获取EDI参数 /// /// public Task> GetAllEdiSetting() { return _cache.GetAsync>(CommonConst.CACHE_KEY_DJY_EDI_SETTING); } /// /// 设置EDI参数 /// /// public Task SetAllEdiSetting(List list) { return _cache.SetAsync(CommonConst.CACHE_KEY_DJY_EDI_SETTING, list); } #endregion }