2024-10-18 12:44:12 +08:00
|
|
|
|
using LMS.Common.Enum;
|
|
|
|
|
|
using Newtonsoft.Json;
|
2024-11-13 14:00:39 +08:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
2024-10-18 12:44:12 +08:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
|
|
|
|
|
|
|
namespace LMS.Repository.DB;
|
|
|
|
|
|
|
|
|
|
|
|
public class Options
|
|
|
|
|
|
{
|
|
|
|
|
|
[Key]
|
|
|
|
|
|
public required string Key { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Value of the option,这个值是一个json字符串
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string? Value { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
public OptionTypeEnum Type { get; set; } = OptionTypeEnum.String;
|
|
|
|
|
|
|
|
|
|
|
|
// 写一个字段,映射Value,判断是不是json字符串,是的话就解析成对象
|
2024-11-13 14:00:39 +08:00
|
|
|
|
// 写一个字段,映射Value,判断是不是json字符串,是的话就解析成对象
|
|
|
|
|
|
public T? GetValueObject<T>()
|
2024-10-18 12:44:12 +08:00
|
|
|
|
{
|
2024-11-13 14:00:39 +08:00
|
|
|
|
if (string.IsNullOrEmpty(Value))
|
2024-10-18 12:44:12 +08:00
|
|
|
|
{
|
2024-11-13 14:00:39 +08:00
|
|
|
|
return default;
|
|
|
|
|
|
}
|
2024-10-18 12:44:12 +08:00
|
|
|
|
|
2024-11-13 14:00:39 +08:00
|
|
|
|
if (Type == OptionTypeEnum.JSON)
|
|
|
|
|
|
{
|
|
|
|
|
|
return JsonConvert.DeserializeObject<T>(Value ?? "{}");
|
|
|
|
|
|
}
|
2024-10-18 12:44:12 +08:00
|
|
|
|
|
2024-11-13 14:00:39 +08:00
|
|
|
|
if (Type == OptionTypeEnum.Number)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (double.TryParse(Value, out double result))
|
2024-10-18 12:44:12 +08:00
|
|
|
|
{
|
2024-11-13 14:00:39 +08:00
|
|
|
|
return (T)Convert.ChangeType(result, typeof(T));
|
2024-10-18 12:44:12 +08:00
|
|
|
|
}
|
2024-11-13 14:00:39 +08:00
|
|
|
|
return default;
|
2024-10-18 12:44:12 +08:00
|
|
|
|
}
|
2024-11-13 14:00:39 +08:00
|
|
|
|
|
|
|
|
|
|
return (T)Convert.ChangeType(Value, typeof(T));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 写一个方法,设置Value的值
|
|
|
|
|
|
public void SetValueObject<T>(T value)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (value == null)
|
2024-10-18 12:44:12 +08:00
|
|
|
|
{
|
2024-11-13 14:00:39 +08:00
|
|
|
|
Value = string.Empty;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-10-18 12:44:12 +08:00
|
|
|
|
|
2024-11-13 14:00:39 +08:00
|
|
|
|
if (Type == OptionTypeEnum.JSON)
|
|
|
|
|
|
{
|
|
|
|
|
|
Value = JsonConvert.SerializeObject(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Value = value.ToString();
|
2024-10-18 12:44:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|