You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
3.6 KiB
108 lines
3.6 KiB
using System.Reflection; |
|
using System.Text; |
|
using BrilliantSightClient.Model.GradeLevel.Entity.DatabaseEntity; |
|
using BrilliantSightClient.Model.Helper; |
|
|
|
|
|
namespace BrilliantSightClient.Model.GradeLevel.Helper; |
|
|
|
public static class EntityHelper |
|
{ |
|
public static void GenerateSign(this BaseEntity entity) |
|
{ |
|
Type type = entity.GetType(); |
|
PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance) |
|
.Where(p => p.Name != nameof(BaseEntity.Sign) && p.CanRead) |
|
.ToArray(); |
|
Array.Sort(properties, (p1, p2) => string.Compare(p1.Name, p2.Name, StringComparison.Ordinal)); |
|
StringBuilder sourceBuilder = new StringBuilder(); |
|
foreach (PropertyInfo property in properties) |
|
{ |
|
object value = property.GetValue(entity); |
|
string valueString = value?.ToString() ?? string.Empty; |
|
sourceBuilder.Append($"{property.Name}={valueString}&"); |
|
} |
|
if (sourceBuilder.Length > 0) |
|
{ |
|
sourceBuilder.Length--; |
|
} |
|
string sign = Common.GenerateMd5Hash(sourceBuilder.ToString()); |
|
entity.Sign = sign; |
|
} |
|
|
|
public static string GenerateInsertSql(this BaseEntity entity) |
|
{ |
|
Type type = entity.GetType(); |
|
string tableName = ConvertToUpperSnakeCase(type.Name); |
|
PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance) |
|
.Where(p => p.CanRead && p.GetValue(entity) != null) |
|
.ToArray(); |
|
StringBuilder columnsBuilder = new StringBuilder(); |
|
StringBuilder valuesBuilder = new StringBuilder(); |
|
|
|
foreach (PropertyInfo property in properties) |
|
{ |
|
string columnName = ConvertToUpperCase(property.Name); |
|
object value = property.GetValue(entity); |
|
string valueString = ProcessValue(value); |
|
columnsBuilder.Append($"{columnName},"); |
|
valuesBuilder.Append($"{valueString},"); |
|
} |
|
if (columnsBuilder.Length > 0) |
|
{ |
|
columnsBuilder.Length--; |
|
valuesBuilder.Length--; |
|
} |
|
string insertSql = $"INSERT INTO {tableName} ({columnsBuilder}) VALUES ({valuesBuilder});"; |
|
return insertSql; |
|
} |
|
|
|
// 辅助方法:大驼峰转大写下划线 |
|
private static string ConvertToUpperSnakeCase(string input) |
|
{ |
|
return string.Concat(input.Select((c, i) => |
|
i > 0 && char.IsUpper(c) |
|
? "_" + c |
|
: c.ToString())).ToUpper(); |
|
} |
|
|
|
// 辅助方法:大驼峰+下划线转纯大写 |
|
private static string ConvertToUpperCase(string input) |
|
{ |
|
return input.ToUpper(); |
|
} |
|
|
|
// 辅助方法:处理不同类型的值 |
|
private static string ProcessValue(object value) |
|
{ |
|
if (value == null) |
|
return "NULL"; |
|
|
|
if (value is string str) |
|
return $"'{str.Replace("'", "''")}'"; |
|
|
|
if (value is DateTime dateTime) |
|
return $"'{dateTime:yyyy-MM-dd HH:mm:ss}'"; |
|
|
|
if (value is bool boolean) |
|
return boolean ? "1" : "0"; |
|
|
|
return value.ToString(); |
|
} |
|
public static int GetValue<TEnum>(this TEnum enumValue) |
|
where TEnum : Enum |
|
{ |
|
return Convert.ToInt32(enumValue); |
|
} |
|
|
|
public static int? GetValueFromName<TEnum>(string name, bool ignoreCase = true) |
|
where TEnum : struct, Enum |
|
{ |
|
if (Enum.TryParse<TEnum>(name, ignoreCase, out var result)) |
|
{ |
|
return Convert.ToInt32(result); |
|
} |
|
return null; // 转换失败返回 null |
|
} |
|
|
|
} |