using System.Net; using System.Reflection; using System.Text.Json; using XFEExtension.NetCore.AutoConfig; using XFEExtension.NetCore.Exceptions; using XFEExtension.NetCore.ServerInteractive.Interfaces; using XFEExtension.NetCore.ServerInteractive.Models; using XFEExtension.NetCore.ServerInteractive.Models.ServerModels; using XFEExtension.NetCore.ServerInteractive.Models.UserModels; using XFEExtension.NetCore.ServerInteractive.Utilities.Helpers; using XFEExtension.NetCore.StringExtension; using XFEExtension.NetCore.XFETransform.JsonConverter; namespace XFEExtension.NetCore.ServerInteractive.Utilities.DataTable; /// /// XFE数据字典表格 /// /// 字典中值的类型 public class XFEDataDictionaryTable : IXFEDataTable where TValue : IIdModel { /// public Func> GetEncryptedUserLoginModelFunction { get; set; } = () => []; /// public Func> GetUsersFunction { get; set; } = () => []; /// /// 获取字典的方法 /// public Func> GetTableFunction { get; set; } = () => []; /// /// 向字典中添加元素的方法 /// public Action AddToTableFunction { get; set; } = (_, _) => { }; /// /// 从字典中移除元素的方法 /// public Action RemoveFromTableFunction { get; set; } = _ => { }; /// /// 更改字典中元素的方法 /// public Action ChangeItemTableFunction { get; set; } = _ => { }; /// public string TableShowName { get; set; } = typeof(TValue).Name; /// public string TableName { get; set; } = typeof(TValue).Name; /// public string TableNameInRequest { get; set; } /// public int GetPermissionLevel { get; set; } /// public int RemovePermissionLevel { get; set; } /// public int ChangePermissionLevel { get; set; } /// public int AddPermissionLevel { get; set; } /// public JsonSerializerOptions? JsonSerializerOptions { get; set; } /// public JsonSerializerOptions? UserJsonSerializerOptions { get; set; } /// /// 创建字典表格并手动设置Get Set方法等 /// public XFEDataDictionaryTable() => TableNameInRequest = $"{TableName[0]}".ToLower() + TableName[1..]; /// /// 根据指定type自动设置Get Set方法 /// /// 继承于XFEProfile的配置文件类型 /// /// 对于自动设置Get Set方法的Table,请确保配置文件使用XFEProfile创建ProfileDictionary,属性名称为XXXTable /// public XFEDataDictionaryTable(Type type) { TableNameInRequest = $"{TableName[0]}".ToLower() + TableName[1..]; var property = type.GetProperty($"{typeof(TValue).Name}Table", BindingFlags.Public | BindingFlags.Static); var profileDictionaryType = typeof(ProfileDictionary); var addMethod = profileDictionaryType.GetMethod("Add", [typeof(string), typeof(TValue)]); var removeMethod = profileDictionaryType.GetMethod("Remove", [typeof(string)]); var saveMethod = type.GetMethod("SaveProfile", BindingFlags.Public | BindingFlags.Static); if (property is null || addMethod is null || removeMethod is null || saveMethod is null) return; GetTableFunction = () => property.GetValue(null) as ProfileDictionary ?? []; AddToTableFunction = (key, value) => addMethod.Invoke(GetTableFunction(), [key, value]); RemoveFromTableFunction = key => removeMethod.Invoke(GetTableFunction(), [key]); ChangeItemTableFunction = item => { var table = GetTableFunction(); if (!table.ContainsKey(item.Id)) return; table[item.Id] = item; saveMethod.Invoke(null, []); }; } /// /// 获取字典中所有值 /// /// public IEnumerable GetValues() => GetTableFunction().Values; /// /// 添加一个元素 /// /// /// public void Add(string key, TValue value) => AddToTableFunction(key, value); /// /// 移除一个元素 /// /// public void Remove(string key) => RemoveFromTableFunction(key); /// /// 更改一个元素 /// /// public void Change(TValue value) => ChangeItemTableFunction(value); /// public async Task Execute(string execute, QueryableJsonNode requestJsonNode, ServerCoreReturnArgs r) { var statusCode = HttpStatusCode.OK; try { switch (execute) { case "get": Console.Write($"获取{TableShowName}列表请求"); UserHelper.ValidatePermission(requestJsonNode["session"], requestJsonNode["deviceInfo"], r.ClientIP, GetPermissionLevel, GetEncryptedUserLoginModelFunction(), GetUsersFunction(), r); List valueList = [.. GetTableFunction().Values]; var pageCount = requestJsonNode["pageCount"]?.GetValue() ?? -1; if (pageCount == -1) { await r.Args.ReplyAndClose(JsonSerializer.Serialize(new { totalCount = valueList.Count, lastPage = -1, dataList = valueList }, JsonSerializerOptions)); } else { var page = requestJsonNode["page"]?.GetValue() ?? -1; await r.Args.ReplyAndClose(JsonSerializer.Serialize(new { totalCount = valueList.Count, lastPage = (int)Math.Ceiling((double)valueList.Count / pageCount), dataList = valueList[(page * pageCount)..((page + 1) * pageCount)] }, JsonSerializerOptions)); } break; case "add": Console.Write($"添加{TableShowName}请求"); var item = JsonSerializer.Deserialize(Convert.FromBase64String(requestJsonNode["data"]?.ToString() ?? string.Empty), JsonSerializerOptions); if (item is null) { statusCode = HttpStatusCode.BadRequest; throw new StopAction(() => { }, $"\n无法使用Json转换目标{TableShowName}信息"); } Console.Write($":{item.Id}"); UserHelper.ValidatePermission(requestJsonNode["session"], requestJsonNode["deviceInfo"], r.ClientIP, AddPermissionLevel, GetEncryptedUserLoginModelFunction(), GetUsersFunction(), r); if (item.Id.IsNullOrWhiteSpace()) item.Id = Guid.NewGuid().ToString(); var addTable = GetTableFunction(); while (addTable.ContainsKey(item.Id)) item.Id = Guid.NewGuid().ToString(); Add(item.Id, item); r.Args.Close(); break; case "remove": Console.Write($"删除{TableShowName}请求"); var id = requestJsonNode["id"]?.ToString(); Console.Write($":{id}"); UserHelper.ValidatePermission(requestJsonNode["session"], requestJsonNode["deviceInfo"], r.ClientIP, RemovePermissionLevel, GetEncryptedUserLoginModelFunction(), GetUsersFunction(), r); if (id.IsNullOrWhiteSpace()) { statusCode = HttpStatusCode.BadRequest; throw new StopAction(() => { }, $"{TableShowName}ID不能为空"); } Remove(id); r.Args.Close(); break; case "change": Console.Write($"更改{TableShowName}请求"); item = JsonSerializer.Deserialize(Convert.FromBase64String(requestJsonNode["data"]?.ToString() ?? string.Empty), JsonSerializerOptions); if (item is null) { statusCode = HttpStatusCode.BadRequest; throw new StopAction(() => { }, $"\n无法使用Json转换目标{TableShowName}信息"); } Console.Write($":{item.Id}"); if (item.Id.IsNullOrWhiteSpace()) { statusCode = HttpStatusCode.BadRequest; throw new StopAction(() => { }, $"\n{TableShowName}ID不能为空"); } UserHelper.ValidatePermission(requestJsonNode["session"], requestJsonNode["deviceInfo"], r.ClientIP, ChangePermissionLevel, GetEncryptedUserLoginModelFunction(), GetUsersFunction(), r); Change(item); r.Args.Close(); break; default: Console.WriteLine($"[ERROR] 试图查询不存在的表格:{execute}"); await r.Args.ReplyAndClose($"试图查询不存在的表格:{execute}", HttpStatusCode.BadRequest); break; } } catch (ServerCoreReturnArgs returnArgs) { Console.WriteLine($"[WARN]({r.ServerCore.ServerCoreName}){returnArgs.ReturnMessage}"); await returnArgs.Args.ReplyAndClose(returnArgs.ReturnMessage, returnArgs.StatusCode); } catch (Exception ex) { Console.WriteLine($"[WARN]({r.ServerCore.ServerCoreName}){ExceptionHelper.GetExceptionMessage(ex)}"); Console.WriteLine($"[TRACE]{ex.StackTrace}"); await r.Args.ReplyAndClose(ex.Message, statusCode); } return statusCode; } }