lq1405 aaebbb9104 V 1.1.0
新增数据信息 完善数据信息得权限问题
2025-05-21 21:28:18 +08:00

73 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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();
}
}
}