Files
EmailBill/Service/ConfigService.cs

78 lines
2.3 KiB
C#
Raw Normal View History

namespace Service;
public interface IConfigService
{
/// <summary>
/// 根据Key获取配置值
/// </summary>
Task<T?> GetConfigByKeyAsync<T>(string key);
/// <summary>
/// 设置配置值
/// </summary>
Task<bool> SetConfigByKeyAsync<T>(string key, T value);
}
public class ConfigService(IConfigRepository configRepository) : IConfigService
{
public async Task<T?> GetConfigByKeyAsync<T>(string key)
{
var config = await configRepository.GetByKeyAsync(key);
if (config == null || string.IsNullOrEmpty(config.Value))
{
return default;
}
return config.Type switch
{
ConfigType.Boolean => (T)(object)bool.Parse(config.Value),
ConfigType.String => (T)(object)config.Value,
ConfigType.Number => (T)Convert.ChangeType(config.Value, typeof(T)),
ConfigType.Json => JsonSerializer.Deserialize<T>(config.Value) ?? default,
_ => default
};
}
public async Task<bool> SetConfigByKeyAsync<T>(string key, T value)
{
if (value == null)
{
return false;
}
var config = await configRepository.GetByKeyAsync(key);
var type = typeof(T) switch
{
2026-01-18 22:25:59 +08:00
{ } t when t == typeof(bool) => ConfigType.Boolean,
{ } t when t == typeof(int)
|| t == typeof(double)
|| t == typeof(float)
|| t == typeof(decimal) => ConfigType.Number,
{ } t when t == typeof(string) => ConfigType.String,
_ => ConfigType.Json
};
var valueStr = type switch
{
ConfigType.Boolean => value.ToString()!.ToLower(),
ConfigType.Number => value.ToString()!,
ConfigType.String => value as string ?? string.Empty,
ConfigType.Json => JsonSerializer.Serialize(value),
_ => throw new InvalidOperationException("Unsupported config type")
};
if (config == null)
{
config = new ConfigEntity
{
Key = key,
Type = type,
};
return await configRepository.AddAsync(config);
}
config.Value = valueStr;
config.Type = type;
return await configRepository.UpdateAsync(config);
}
}