73 lines
1.7 KiB
C#
73 lines
1.7 KiB
C#
using LMS.Common.Enums;
|
||
using Newtonsoft.Json;
|
||
using System.ComponentModel.DataAnnotations;
|
||
|
||
namespace LMS.Repository.DB;
|
||
|
||
public class Options
|
||
{
|
||
[Key]
|
||
[Required]
|
||
public required string Key { get; set; } = string.Empty;
|
||
|
||
/// <summary>
|
||
/// Value of the option,这个值是一个json字符串
|
||
/// </summary>
|
||
public string? Value { get; set; } = string.Empty;
|
||
|
||
[Required]
|
||
public OptionTypeEnum Type { get; set; } = OptionTypeEnum.String;
|
||
|
||
[Required]
|
||
public OptionCategory Category { get; set; } = OptionCategory.System;
|
||
|
||
[Required]
|
||
public List<long> RoleIds { get; set; } = [];
|
||
|
||
public DateTime CreatedTime { get; set; }
|
||
|
||
// 写一个字段,映射Value,判断是不是json字符串,是的话就解析成对象
|
||
public T? GetValueObject<T>()
|
||
{
|
||
if (string.IsNullOrEmpty(Value))
|
||
{
|
||
return default;
|
||
}
|
||
|
||
if (Type == OptionTypeEnum.JSON)
|
||
{
|
||
return JsonConvert.DeserializeObject<T>(Value ?? "{}");
|
||
}
|
||
|
||
if (Type == OptionTypeEnum.Number)
|
||
{
|
||
if (double.TryParse(Value, out double result))
|
||
{
|
||
return (T)Convert.ChangeType(result, typeof(T));
|
||
}
|
||
return default;
|
||
}
|
||
|
||
return (T)Convert.ChangeType(Value, typeof(T));
|
||
}
|
||
|
||
// 写一个方法,设置Value的值
|
||
public void SetValueObject<T>(T value)
|
||
{
|
||
if (value == null)
|
||
{
|
||
Value = string.Empty;
|
||
return;
|
||
}
|
||
|
||
if (Type == OptionTypeEnum.JSON)
|
||
{
|
||
Value = JsonConvert.SerializeObject(value);
|
||
}
|
||
else
|
||
{
|
||
Value = value.ToString();
|
||
}
|
||
}
|
||
}
|