parent
822c256035
commit
c74a65372b
10 changed files with 538 additions and 22 deletions
@ -0,0 +1,87 @@ |
|||||||
|
using SparkClient.Model.Entity.Base; |
||||||
|
using System.Reflection; |
||||||
|
using System.Runtime.CompilerServices; |
||||||
|
|
||||||
|
namespace SparkClient.Model.Entity; |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 系统设置实体 |
||||||
|
/// </summary> |
||||||
|
|
||||||
|
public class CutterConfigEntity : BaseEntity |
||||||
|
{ |
||||||
|
public static readonly new string TableName = "CUTTER_CONFIG"; |
||||||
|
[DbField("GUID")] |
||||||
|
public Guid GUID { get; set; } |
||||||
|
/// <summary> |
||||||
|
/// 项目名称 |
||||||
|
/// </summary> |
||||||
|
[DbField("ITEM_NAME")] |
||||||
|
public string ItemName { get; set; } |
||||||
|
/// <summary> |
||||||
|
/// key |
||||||
|
/// </summary> |
||||||
|
[DbField("KEY")] |
||||||
|
public string Key { get; set; } |
||||||
|
/// <summary> |
||||||
|
/// value |
||||||
|
/// </summary> |
||||||
|
[DbField("VALUE")] |
||||||
|
public string Value { get; set; } |
||||||
|
private static Dictionary<string, string> GetFieldMappings() |
||||||
|
{ |
||||||
|
var properties = typeof(CutterConfigEntity).GetProperties(BindingFlags.Public | BindingFlags.Instance); |
||||||
|
return properties |
||||||
|
.Where(p => Attribute.IsDefined(p, typeof(DbFieldAttribute))) |
||||||
|
.ToDictionary( |
||||||
|
p => p.Name, |
||||||
|
p => ((DbFieldAttribute)p.GetCustomAttribute(typeof(DbFieldAttribute))).FieldName |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
// 生成 Insert 语句 |
||||||
|
public string GenerateInsertSQL() |
||||||
|
{ |
||||||
|
var mappings = GetFieldMappings(); |
||||||
|
var columns = string.Join(", ", mappings.Values); |
||||||
|
var values = string.Join(", ", mappings.Keys.Select(k => $"@{k}")); |
||||||
|
return $"INSERT INTO {TableName} ({columns}) VALUES ({values});"; |
||||||
|
} |
||||||
|
|
||||||
|
// 生成 Update 语句 |
||||||
|
public string GenerateUpdateSQL(Dictionary<string, object> fieldsToUpdate = null) |
||||||
|
{ |
||||||
|
if (fieldsToUpdate == null || fieldsToUpdate.Count == 0) |
||||||
|
{ |
||||||
|
// 默认根据 ID 更新所有字段 |
||||||
|
var mappings = GetFieldMappings(); |
||||||
|
var setClause = string.Join(", ", mappings.Values.Select(f => $"{f} = @{f}")); |
||||||
|
return $"UPDATE {TableName} SET {setClause} WHERE GUID = @GUID;"; |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
// 根据传入的字段更新 |
||||||
|
var setClause = string.Join(", ", fieldsToUpdate.Keys.Select(f => $"{f} = @{f}")); |
||||||
|
return $"UPDATE {TableName} SET {setClause} WHERE GUID = @GUID;"; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// 生成 Delete 语句 |
||||||
|
public string GenerateDeleteSQL(Dictionary<string, object> conditions = null) |
||||||
|
{ |
||||||
|
if (conditions == null || conditions.Count == 0) |
||||||
|
{ |
||||||
|
// 默认根据 ID 删除 |
||||||
|
return $"DELETE FROM {TableName} WHERE GUID ='" + GUID.ToString() + "';"; |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
// 根据传入条件删除 |
||||||
|
var whereClause = string.Join(" AND ", conditions.Keys.Select(f => $"{f} = @{f}")); |
||||||
|
return $"DELETE FROM {TableName} WHERE {whereClause};"; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
@ -0,0 +1,83 @@ |
|||||||
|
using SparkClient.Model.Entity.Base; |
||||||
|
using System.Reflection; |
||||||
|
using System.Runtime.CompilerServices; |
||||||
|
|
||||||
|
namespace SparkClient.Model.Entity; |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 系统设置实体 |
||||||
|
/// </summary> |
||||||
|
|
||||||
|
public class MethodConfigEntity : BaseEntity |
||||||
|
{ |
||||||
|
public static readonly new string TableName = "METHOD_CONFIG"; |
||||||
|
[DbField("GUID")] |
||||||
|
public Guid GUID { get; set; } |
||||||
|
/// <summary> |
||||||
|
/// json内容 |
||||||
|
/// </summary> |
||||||
|
[DbField("JSON")] |
||||||
|
public string Json { get; set; } |
||||||
|
/// <summary> |
||||||
|
/// Json排序 |
||||||
|
/// </summary> |
||||||
|
[DbField("JSON_ORDER")] |
||||||
|
public int JsonOrder { get; set; } |
||||||
|
|
||||||
|
private static Dictionary<string, string> GetFieldMappings() |
||||||
|
{ |
||||||
|
var properties = typeof(MethodConfigEntity).GetProperties(BindingFlags.Public | BindingFlags.Instance); |
||||||
|
return properties |
||||||
|
.Where(p => Attribute.IsDefined(p, typeof(DbFieldAttribute))) |
||||||
|
.ToDictionary( |
||||||
|
p => p.Name, |
||||||
|
p => ((DbFieldAttribute)p.GetCustomAttribute(typeof(DbFieldAttribute))).FieldName |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
// 生成 Insert 语句 |
||||||
|
public string GenerateInsertSQL() |
||||||
|
{ |
||||||
|
var mappings = GetFieldMappings(); |
||||||
|
var columns = string.Join(", ", mappings.Values); |
||||||
|
var values = string.Join(", ", mappings.Keys.Select(k => $"@{k}")); |
||||||
|
return $"INSERT INTO {TableName} ({columns}) VALUES ({values});"; |
||||||
|
} |
||||||
|
|
||||||
|
// 生成 Update 语句 |
||||||
|
public string GenerateUpdateSQL(Dictionary<string, object> fieldsToUpdate = null) |
||||||
|
{ |
||||||
|
if (fieldsToUpdate == null || fieldsToUpdate.Count == 0) |
||||||
|
{ |
||||||
|
// 默认根据 ID 更新所有字段 |
||||||
|
var mappings = GetFieldMappings(); |
||||||
|
var setClause = string.Join(", ", mappings.Values.Select(f => $"{f} = @{f}")); |
||||||
|
return $"UPDATE {TableName} SET {setClause} WHERE GUID = " + GUID.ToString() + ";"; |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
// 根据传入的字段更新 |
||||||
|
var setClause = string.Join(", ", fieldsToUpdate.Keys.Select(f => $"{f} = @{f}")); |
||||||
|
return $"UPDATE {TableName} SET {setClause} WHERE GUID = " + GUID.ToString() + ";"; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// 生成 Delete 语句 |
||||||
|
public string GenerateDeleteSQL(Dictionary<string, object> conditions = null) |
||||||
|
{ |
||||||
|
if (conditions == null || conditions.Count == 0) |
||||||
|
{ |
||||||
|
// 默认根据 ID 删除 |
||||||
|
return $"DELETE FROM {TableName} WHERE GUID =" + GUID.ToString() + ";"; |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
// 根据传入条件删除 |
||||||
|
var whereClause = string.Join(" AND ", conditions.Keys.Select(f => $"{f} = @{f}")); |
||||||
|
return $"DELETE FROM {TableName} WHERE {whereClause};"; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
After Width: | Height: | Size: 3.0 KiB |
Loading…
Reference in new issue