From 9ca96199d92168fe221dda9aba56f55520a561d8 Mon Sep 17 00:00:00 2001 From: hutongqing <hutongqing@hnkhzn.com> Date: 星期二, 29 十月 2024 17:30:59 +0800 Subject: [PATCH] 1 --- WIDESEAWCS_Server/WIDESEAWCS_Core/Utilities/EntityProperties.cs | 127 +++++++++++++++++++++++++++++++++++++----- 1 files changed, 111 insertions(+), 16 deletions(-) diff --git a/WIDESEAWCS_Server/WIDESEAWCS_Core/Utilities/EntityProperties.cs b/WIDESEAWCS_Server/WIDESEAWCS_Core/Utilities/EntityProperties.cs index c2e4b8a..4782e2a 100644 --- a/WIDESEAWCS_Server/WIDESEAWCS_Core/Utilities/EntityProperties.cs +++ b/WIDESEAWCS_Server/WIDESEAWCS_Core/Utilities/EntityProperties.cs @@ -1,5 +1,6 @@ 锘縰sing SqlSugar; using System; +using System.Collections; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; @@ -28,7 +29,7 @@ if (propertyInfo != null) { sugarColumn = propertyInfo.GetCustomAttribute<SugarColumn>(); - dbType = propertyInfo != null ? propertyInfo.GetProperWithDbType() : SqlDbTypeName.NVarChar; + dbType = propertyInfo.PropertyType != null ? propertyInfo.GetProperWithDbType() : SqlDbTypeName.NVarChar; } dbType = dbType.ToLower(); string val = value?.ToString(); @@ -85,9 +86,8 @@ } else { - int length = sugarColumn.Length; - if (length == 0) { return (true, null, null); } + if (length == 0) { return (true, null, value); } //鍒ゆ柇鍙屽瓧鑺備笌鍗曞瓧娈� else if (length < 8000 && ((dbType.Substring(0, 1) != "n" @@ -106,17 +106,28 @@ return (reslutMsg == "" ? true : false, reslutMsg, value); } + public static List<(bool, string, object)> ValidationValueForDbType(this PropertyInfo propertyInfo, params object[] values) + { + List<(bool, string, object)> result = new List<(bool, string, object)>(); + foreach (object value in values) + { + result.Add(propertyInfo.ValidationVal(value)); + } + return result; + } + private static readonly Dictionary<Type, string> ProperWithDbType = new Dictionary<Type, string>() { - { typeof(string),SqlDbTypeName.NVarChar }, - { typeof(DateTime),SqlDbTypeName.DateTime}, - {typeof(long),SqlDbTypeName.BigInt }, - {typeof(int),SqlDbTypeName.Int}, - { typeof(decimal),SqlDbTypeName.Decimal }, - { typeof(float),SqlDbTypeName.Float }, - { typeof(double),SqlDbTypeName.Double }, - { typeof(byte),SqlDbTypeName.Int },//绫诲瀷寰呭畬 - { typeof(Guid),SqlDbTypeName.UniqueIdentifier} + { typeof(string), SqlDbTypeName.NVarChar }, + { typeof(DateTime), SqlDbTypeName.DateTime}, + { typeof(long), SqlDbTypeName.BigInt }, + { typeof(int), SqlDbTypeName.Int}, + { typeof(decimal), SqlDbTypeName.Decimal }, + { typeof(float), SqlDbTypeName.Float }, + { typeof(double), SqlDbTypeName.Double }, + { typeof(byte), SqlDbTypeName.Int },//绫诲瀷寰呭畬 + { typeof(Guid), SqlDbTypeName.UniqueIdentifier} }; + public static string GetProperWithDbType(this PropertyInfo propertyInfo) { bool result = ProperWithDbType.TryGetValue(propertyInfo.PropertyType, out string value); @@ -175,12 +186,29 @@ } continue; } - string str = dic[property.Name.FirstLetterToLower()].ToString(); - //灏嗘墍鏈夌┖鍊艰缃负null - if (dic[property.Name.FirstLetterToLower()] != null && str == string.Empty) - dic[property.Name.FirstLetterToLower()] = null; + if(dic[property.Name.FirstLetterToLower()] != null) + { + string str = dic[property.Name.FirstLetterToLower()].ToString(); + //灏嗘墍鏈夌┖鍊艰缃负null + if (str == string.Empty) + dic[property.Name.FirstLetterToLower()] = null; + } + } return string.Empty; + } + + public static string ValidateDicInEntity(this Type typeinfo, List<Dictionary<string, object>> dicList, bool removerKey, string[] ignoreFields = null) + { + PropertyInfo[] propertyInfo = typeinfo.GetProperties(); + string reslutMsg = string.Empty; + foreach (Dictionary<string, object> dic in dicList) + { + reslutMsg = typeinfo.ValidateDicInEntity(dic, removerKey, propertyInfo, ignoreFields); + if (!string.IsNullOrEmpty(reslutMsg)) + return reslutMsg; + } + return reslutMsg; } public static string GetKeyName(this Type typeinfo) @@ -212,5 +240,72 @@ } return null; } + + public static Type GetDetailType(this Type typeinfo) + { + PropertyInfo[] properties = typeinfo.GetProperties(); + foreach (PropertyInfo property in properties) + { + Navigate? navigate = property.GetCustomAttribute<Navigate>(); + if (navigate is not null) + { + if (navigate.GetNavigateType() == NavigateType.OneToOne) + return property.PropertyType; + else + return property.PropertyType.GenericTypeArguments[0]; + } + } + return null; + } + + public static string GetMainIdByDetail(this Type typeinfo) + { + PropertyInfo[] properties = typeinfo.GetProperties(); + foreach (PropertyInfo property in properties) + { + Navigate? navigate = property.GetCustomAttribute<Navigate>(); + if (navigate is not null) + { + return navigate.GetName(); + } + } + return null; + } + + public static void SetDetailId<T>(this Type typeinfo, T enetiy, object id, string name) + { + PropertyInfo property = typeinfo.GetProperty(name); + if (property != null) + { + property.SetValue(enetiy, id); + } + } + + public static PropertyInfo? GetNavigatePro(this Type typeinfo) + { + PropertyInfo[] properties = typeinfo.GetProperties(); + foreach (PropertyInfo property in properties) + { + Navigate? navigate = property.GetCustomAttribute<Navigate>(); + if (navigate is not null) + { + return property; + } + } + return null; + } + + public static object GetPropertyValue<T>(this Type typeinfo, T data, string propertyName) + { + if (typeinfo != typeof(T)) + return null; + + PropertyInfo? property = typeinfo.GetProperty(propertyName); + if (property != null) + { + return property.GetValue(data); + } + return null; + } } } -- Gitblit v1.9.3