EntityHelper.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace Fuel.Application
  9. {
  10. public class EntityHelper
  11. {
  12. public readonly IFreeSql _fsql;
  13. public EntityHelper(IFreeSql fsql)
  14. {
  15. _fsql = fsql;
  16. }
  17. /// <summary>
  18. /// 查询
  19. /// </summary>
  20. /// <typeparam name="T"></typeparam>
  21. /// <param name="predicate"></param>
  22. /// <returns></returns>
  23. public async Task<List<T>> GetEntitiesAsync<T>(Expression<Func<T, bool>> predicate) where T : class
  24. {
  25. try
  26. {
  27. // 查询满足条件的实体
  28. return await _fsql.Select<T>().Where(predicate).ToListAsync();
  29. }
  30. catch (Exception ex)
  31. {
  32. }
  33. return new List<T>();
  34. }
  35. /// <summary>
  36. /// 插入
  37. /// </summary>
  38. /// <typeparam name="T"></typeparam>
  39. /// <param name="entity"></param>
  40. /// <returns></returns>
  41. public async Task<T> InsertEntityAsync<T>(T entity) where T : class
  42. {
  43. try
  44. {
  45. // 插入数据
  46. _fsql.Insert(entity).ExecuteAffrows();
  47. // 获取插入后的主键值
  48. object insertedIdObj = _fsql.Ado.ExecuteScalar("SELECT LAST_INSERT_ID();");
  49. int insertedId = Convert.ToInt32(insertedIdObj);
  50. // 查询插入后的数据
  51. return await GetInsertedEntityAsync<T>(insertedId);
  52. }
  53. catch (Exception ex)
  54. {
  55. }
  56. return null;
  57. }
  58. private async Task<T> GetInsertedEntityAsync<T>(int insertedId) where T : class
  59. {
  60. // 获取实体类型
  61. Type entityType = typeof(T);
  62. // 获取 Id 属性
  63. PropertyInfo idProperty = entityType.GetProperty("Id");
  64. if (idProperty == null)
  65. {
  66. throw new InvalidOperationException($"Entity type {entityType.Name} does not have an 'Id' property.");
  67. }
  68. // 构建查询条件
  69. var parameter = Expression.Parameter(entityType, "n");
  70. var propertyAccess = Expression.MakeMemberAccess(parameter, idProperty);
  71. var constantValue = Expression.Constant(insertedId, typeof(int));
  72. var equality = Expression.Equal(propertyAccess, constantValue);
  73. var lambda = Expression.Lambda<Func<T, bool>>(equality, parameter);
  74. // 查询插入后的数据
  75. return await _fsql.Select<T>().Where(lambda).FirstAsync();
  76. }
  77. /// <summary>
  78. /// 更新
  79. /// </summary>
  80. /// <typeparam name="T"></typeparam>
  81. /// <param name="entity"></param>
  82. /// <returns></returns>
  83. public async Task<bool> UpdateAsync<T>(T entity) where T : class
  84. {
  85. try
  86. {
  87. // 更新数据,显式指定类型参数 T
  88. int affectedRows = _fsql.Update<T>(entity).ExecuteAffrows();
  89. // 返回是否成功更新
  90. return affectedRows > 0;
  91. }
  92. catch (Exception ex)
  93. {
  94. // 可以在这里处理异常,例如日志记录
  95. // Console.WriteLine(ex.Message);
  96. }
  97. return false;
  98. }
  99. }
  100. }