lq1405 a37c40a2ef V1.0.6
添加重置用户每月的免费换绑次数
优化项目结构
2025-03-24 16:53:32 +08:00

64 lines
1.6 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]
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字符串是的话就解析成对象
// 写一个字段映射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();
}
}
}