您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LuCrudBusiness.cs 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using System;
  2. using System.Collections.Generic;
  3. using Luticate2.Utils.Dbo;
  4. using Luticate2.Utils.Dbo.Basic;
  5. using Luticate2.Utils.Dbo.OrderBy;
  6. using Luticate2.Utils.Dbo.Result;
  7. using Luticate2.Utils.Interfaces;
  8. namespace Luticate2.Utils.Business
  9. {
  10. public abstract class LuCrudBusiness<TDataAccess, TDboCreate, TDboRead, TDboUpdate> : LuBusiness, ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate>
  11. where TDataAccess : ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate>
  12. where TDboCreate : class
  13. where TDboRead : class
  14. where TDboUpdate : class
  15. {
  16. protected readonly TDataAccess DataAccess;
  17. protected LuCrudBusiness(TDataAccess dataAccess)
  18. {
  19. DataAccess = dataAccess;
  20. }
  21. protected virtual LuResult<TDboCreate> CheckAdd(TDboCreate obj)
  22. {
  23. return LuResult<TDboCreate>.Ok(obj);
  24. }
  25. protected virtual LuResult<IEnumerable<TDboCreate>> CheckAdd(IEnumerable<TDboCreate> objs)
  26. {
  27. var list = new List<TDboCreate>();
  28. foreach (var obj in objs)
  29. {
  30. var res = CheckAdd(obj);
  31. if (!res)
  32. {
  33. return res.To<IEnumerable<TDboCreate>>();
  34. }
  35. list.Add(res.Data);
  36. }
  37. return LuResult<IEnumerable<TDboCreate>>.Ok(list);
  38. }
  39. protected virtual LuResult<TDboUpdate> CheckEdit(TDboRead dbo, TDboUpdate update)
  40. {
  41. return LuResult<TDboUpdate>.Ok(update);
  42. }
  43. protected LuResult<TDboUpdate> GetAndCheckEdit(string id, TDboUpdate update)
  44. {
  45. var res = GetSingleById(id);
  46. if (!res)
  47. {
  48. return res.To<TDboUpdate>();
  49. }
  50. return CheckEdit(res.Data, update);
  51. }
  52. protected LuResult<TDboUpdate> GetAndCheckEdit(long id, TDboUpdate update)
  53. {
  54. var res = GetSingleById(id);
  55. if (!res)
  56. {
  57. return res.To<TDboUpdate>();
  58. }
  59. return CheckEdit(res.Data, update);
  60. }
  61. public LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
  62. {
  63. var res = CheckAdd(objs);
  64. if (!res)
  65. {
  66. return res.To<T>();
  67. }
  68. return DataAccess.Add(res.Data, returnFunc);
  69. }
  70. public LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
  71. {
  72. var res = CheckAdd(obj);
  73. if (!res)
  74. {
  75. return res.To<T>();
  76. }
  77. return DataAccess.Add(res.Data, returnFunc);
  78. }
  79. public LuResult<IEnumerable<string>> AddGuid(IEnumerable<TDboCreate> objs)
  80. {
  81. var res = CheckAdd(objs);
  82. if (!res)
  83. {
  84. return res.To<IEnumerable<string>>();
  85. }
  86. return DataAccess.AddGuid(res.Data);
  87. }
  88. public LuResult<string> AddGuid(TDboCreate obj)
  89. {
  90. var res = CheckAdd(obj);
  91. if (!res)
  92. {
  93. return res.To<string>();
  94. }
  95. return DataAccess.AddGuid(res.Data);
  96. }
  97. public LuResult<IEnumerable<long>> AddId(IEnumerable<TDboCreate> obj)
  98. {
  99. var res = CheckAdd(obj);
  100. if (!res)
  101. {
  102. return res.To<IEnumerable<long>>();
  103. }
  104. return DataAccess.AddId(res.Data);
  105. }
  106. public LuResult<long> AddId(TDboCreate obj)
  107. {
  108. var res = CheckAdd(obj);
  109. if (!res)
  110. {
  111. return res.To<long>();
  112. }
  113. return DataAccess.AddId(res.Data);
  114. }
  115. public LuResult<IEnumerable<TDboRead>> AddDbo(IEnumerable<TDboCreate> obj)
  116. {
  117. var res = CheckAdd(obj);
  118. if (!res)
  119. {
  120. return res.To<IEnumerable<TDboRead>>();
  121. }
  122. return DataAccess.AddDbo(res.Data);
  123. }
  124. public LuResult<TDboRead> AddDbo(TDboCreate obj)
  125. {
  126. var res = CheckAdd(obj);
  127. if (!res)
  128. {
  129. return res.To<TDboRead>();
  130. }
  131. return DataAccess.AddDbo(res.Data);
  132. }
  133. public LuResult<TDboRead> GetSingleById(string id)
  134. {
  135. return DataAccess.GetSingleById(id);
  136. }
  137. public LuResult<TDboRead> GetSingleById(long id)
  138. {
  139. return DataAccess.GetSingleById(id);
  140. }
  141. public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, int page = 0, int perPage = int.MaxValue)
  142. {
  143. return DataAccess.GetMultiple(orderBy, 0, perPage);
  144. }
  145. public LuResult<T> EditSingleById<T>(long id, TDboUpdate update, Func<TDboRead, T> returnFunc)
  146. {
  147. var obj = GetAndCheckEdit(id, update);
  148. if (!obj)
  149. {
  150. return obj.To<T>();
  151. }
  152. return DataAccess.EditSingleById(id, obj.Data, returnFunc);
  153. }
  154. public LuResult<long> EditSingleByIdId(long id, TDboUpdate update)
  155. {
  156. var obj = GetAndCheckEdit(id, update);
  157. if (!obj)
  158. {
  159. return obj.To<long>();
  160. }
  161. return DataAccess.EditSingleByIdId(id, obj.Data);
  162. }
  163. public LuResult<TDboRead> EditSingleByIdDbo(long id, TDboUpdate update)
  164. {
  165. var obj = GetAndCheckEdit(id, update);
  166. if (!obj)
  167. {
  168. return obj.To<TDboRead>();
  169. }
  170. return DataAccess.EditSingleByIdDbo(id, obj.Data);
  171. }
  172. public LuResult<T> EditSingleById<T>(string id, TDboUpdate update, Func<TDboRead, T> returnFunc)
  173. {
  174. var obj = GetAndCheckEdit(id, update);
  175. if (!obj)
  176. {
  177. return obj.To<T>();
  178. }
  179. return DataAccess.EditSingleById(id, obj.Data, returnFunc);
  180. }
  181. public LuResult<string> EditSingleByIdGuid(string id, TDboUpdate update)
  182. {
  183. var obj = GetAndCheckEdit(id, update);
  184. if (!obj)
  185. {
  186. return obj.To<string>();
  187. }
  188. return DataAccess.EditSingleByIdGuid(id, obj.Data);
  189. }
  190. public LuResult<TDboRead> EditSingleByIdDbo(string id, TDboUpdate update)
  191. {
  192. var obj = GetAndCheckEdit(id, update);
  193. if (!obj)
  194. {
  195. return obj.To<TDboRead>();
  196. }
  197. return DataAccess.EditSingleByIdDbo(id, obj.Data);
  198. }
  199. public LuResult<T> DeleteSingleById<T>(string id, Func<TDboRead, T> returnFunc)
  200. {
  201. return DataAccess.DeleteSingleById(id, returnFunc);
  202. }
  203. public LuResult<string> DeleteSingleByIdGuid(string id)
  204. {
  205. return DataAccess.DeleteSingleByIdGuid(id);
  206. }
  207. public LuResult<TDboRead> DeleteSingleByIdDbo(string id)
  208. {
  209. return DataAccess.DeleteSingleByIdDbo(id);
  210. }
  211. public LuResult<T> DeleteSingleById<T>(long id, Func<TDboRead, T> returnFunc)
  212. {
  213. return DataAccess.DeleteSingleById(id, returnFunc);
  214. }
  215. public LuResult<long> DeleteSingleByIdId(long id)
  216. {
  217. return DataAccess.DeleteSingleByIdId(id);
  218. }
  219. public LuResult<TDboRead> DeleteSingleByIdDbo(long id)
  220. {
  221. return DataAccess.DeleteSingleByIdDbo(id);
  222. }
  223. }
  224. }