using Easy.Admin.Application.Config.Dtos;
using Furion.ViewEngine;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace Easy.Admin.Application.Config;
///
/// 自定义配置业务
///
public class CustomConfigService : BaseService, ITransient
{
private readonly ISqlSugarRepository _customConfigRepository;
private readonly IEasyCachingProvider _easyCachingProvider;
private readonly IViewEngine _viewEngine;
private readonly IWebHostEnvironment _environment;
public CustomConfigService(ISqlSugarRepository customConfigRepository,
IEasyCachingProvider easyCachingProvider,
IViewEngine viewEngine,
IWebHostEnvironment environment) : base(customConfigRepository)
{
_customConfigRepository = customConfigRepository;
_easyCachingProvider = easyCachingProvider;
_viewEngine = viewEngine;
_environment = environment;
}
///
/// 获取强类型配置
///
///
///
[NonAction]
public async Task Get()
{
var type = typeof(T);
bool isList = typeof(IEnumerable).IsAssignableFrom(type);
string code = type.IsGenericType && isList ? type.GenericTypeArguments[0].Name : type.Name;
var value = await _easyCachingProvider.GetAsync($"{CacheConst.ConfigCacheKey}{code}", async () =>
{
var queryable = _customConfigRepository.AsQueryable()
.InnerJoin((config, item) => config.Id == item.ConfigId)
.Where((config, item) => config.Code == code)
.Select((config, item) => item.Json);
string json;
if (isList)
{
List list = await queryable.ToListAsync();
if (!list.Any()) return default(T);
json = $"[{string.Join(",", list)}]";
}
else
{
json = await queryable.FirstAsync();
}
return string.IsNullOrWhiteSpace(json) ? default(T) : JsonConvert.DeserializeObject(json);
}, TimeSpan.FromDays(1));
return value.Value;
}
///
/// 获取自定义配置
///
/// 自定义配置唯一编码
///
[DisplayName("获取自定义配置")]
[HttpGet("getConfig")]
public async Task GetConfig([FromQuery][Required(ErrorMessage = "缺少参数")] string code)
{
var value = await _easyCachingProvider.GetAsync