UpdateNavTask.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace SqlSugar
  8. {
  9. public class UpdateNavTaskInit<Root, T> where T : class, new() where Root : class, new()
  10. {
  11. internal SqlSugarProvider Context { get; set; }
  12. internal UpdateNavProvider<Root, Root> UpdateNavProvider { get; set; }
  13. internal NavContext NavContext { get; set; }
  14. public UpdateNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, TChild>> expression) where TChild : class, new()
  15. {
  16. this.Context = UpdateNavProvider._Context;
  17. UpdateNavProvider.NavContext = this.NavContext;
  18. UpdateNavTask<Root, TChild> result = new UpdateNavTask<Root, TChild>();
  19. Func<UpdateNavProvider<Root, TChild>> func = () => UpdateNavProvider.ThenInclude(expression);
  20. result.PreFunc = func;
  21. result.Context = this.Context;
  22. result.NavContext = this.NavContext;
  23. return result;
  24. }
  25. public UpdateNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, List<TChild>>> expression) where TChild : class, new()
  26. {
  27. this.Context = UpdateNavProvider._Context;
  28. UpdateNavProvider.NavContext = this.NavContext;
  29. UpdateNavTask<Root, TChild> result = new UpdateNavTask<Root, TChild>();
  30. Func<UpdateNavProvider<Root, TChild>> func = () => UpdateNavProvider.ThenInclude(expression);
  31. result.PreFunc = func;
  32. result.Context = this.Context;
  33. result.NavContext = UpdateNavProvider.NavContext;
  34. return result;
  35. }
  36. public UpdateNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, TChild>> expression,UpdateNavOptions options) where TChild : class, new()
  37. {
  38. this.Context = UpdateNavProvider._Context;
  39. UpdateNavProvider.NavContext = this.NavContext;
  40. UpdateNavTask<Root, TChild> result = new UpdateNavTask<Root, TChild>();
  41. Func<UpdateNavProvider<Root, TChild>> func = () => UpdateNavProvider.ThenInclude(expression,options);
  42. result.PreFunc = func;
  43. result.Context = this.Context;
  44. result.NavContext = UpdateNavProvider.NavContext;
  45. return result;
  46. }
  47. public UpdateNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, List<TChild>>> expression, UpdateNavOptions options) where TChild : class, new()
  48. {
  49. this.Context = UpdateNavProvider._Context;
  50. UpdateNavProvider.NavContext = this.NavContext;
  51. UpdateNavTask<Root, TChild> result = new UpdateNavTask<Root, TChild>();
  52. Func<UpdateNavProvider<Root, TChild>> func = () => UpdateNavProvider.ThenInclude(expression,options);
  53. result.PreFunc = func;
  54. result.Context = this.Context;
  55. result.NavContext = UpdateNavProvider.NavContext;
  56. return result;
  57. }
  58. public UpdateNavMethodInfo IncludesAllFirstLayer(params string[] ignoreColumns)
  59. {
  60. if (ignoreColumns == null)
  61. {
  62. ignoreColumns = new string[] { };
  63. }
  64. this.Context = UpdateNavProvider._Context;
  65. var navColumns = this.Context.EntityMaintenance.GetEntityInfo<Root>().Columns.Where(it=> !ignoreColumns.Contains(it.PropertyName) || !ignoreColumns.Any(z=>z.EqualCase(it.DbColumnName))).Where(it => it.Navigat != null).ToList();
  66. var updateNavs = this;
  67. UpdateNavMethodInfo methodInfo = updateNavs.IncludeByNameString(navColumns[0].PropertyName);
  68. foreach (var item in navColumns.Skip(1))
  69. {
  70. methodInfo = methodInfo.IncludeByNameString(item.PropertyName);
  71. }
  72. return methodInfo;
  73. }
  74. public UpdateNavMethodInfo IncludeByNameString(string navMemberName, UpdateNavOptions updateNavOptions=null)
  75. {
  76. UpdateNavMethodInfo result = new UpdateNavMethodInfo();
  77. result.Context = UpdateNavProvider._Context;
  78. var entityInfo = result.Context.EntityMaintenance.GetEntityInfo<T>();
  79. Type properyItemType;
  80. bool isList;
  81. Expression exp =UtilMethods.GetIncludeExpression(navMemberName, entityInfo, out properyItemType,out isList);
  82. var method = this.GetType().GetMyMethod("Include", 2,isList)
  83. .MakeGenericMethod(properyItemType);
  84. var obj = method.Invoke(this, new object[] { exp, updateNavOptions });
  85. result.MethodInfos = obj;
  86. return result;
  87. }
  88. }
  89. public class UpdateNavTask<Root, T> where T : class, new() where Root : class, new()
  90. {
  91. public SqlSugarProvider Context { get; set; }
  92. public Func<UpdateNavProvider<Root, T>> PreFunc { get; set; }
  93. internal NavContext NavContext { get; set; }
  94. #region +1
  95. public UpdateNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
  96. {
  97. UpdateNavTask<Root, TChild> result = new UpdateNavTask<Root, TChild>();
  98. Func<UpdateNavProvider<Root, TChild>> func = () => PreFunc().ThenInclude(expression);
  99. result.PreFunc = func;
  100. result.Context = this.Context;
  101. result.NavContext = this.NavContext;
  102. return result;
  103. }
  104. public UpdateNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression) where TChild : class, new()
  105. {
  106. UpdateNavTask<Root, TChild> result = new UpdateNavTask<Root, TChild>();
  107. Func<UpdateNavProvider<Root, TChild>> func = () => PreFunc().ThenInclude(expression);
  108. result.PreFunc = func;
  109. result.Context = this.Context;
  110. result.NavContext = this.NavContext;
  111. return result;
  112. }
  113. public UpdateNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, TChild>> expression) where TChild : class, new()
  114. {
  115. return AsNav().ThenInclude(expression);
  116. }
  117. public UpdateNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, List<TChild>>> expression) where TChild : class, new()
  118. {
  119. return AsNav().ThenInclude(expression);
  120. }
  121. #endregion
  122. #region +2
  123. public UpdateNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression, UpdateNavOptions options) where TChild : class, new()
  124. {
  125. UpdateNavTask<Root, TChild> result = new UpdateNavTask<Root, TChild>();
  126. Func<UpdateNavProvider<Root, TChild>> func = () => {
  127. var nav = PreFunc().ThenInclude(expression, options);
  128. nav.NavContext = this.NavContext;
  129. return nav;
  130. };
  131. result.PreFunc = func;
  132. result.Context = this.Context;
  133. return result;
  134. }
  135. public UpdateNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression, UpdateNavOptions options) where TChild : class, new()
  136. {
  137. UpdateNavTask<Root, TChild> result = new UpdateNavTask<Root, TChild>();
  138. Func<UpdateNavProvider<Root, TChild>> func = () => {
  139. var nav = PreFunc().ThenInclude(expression, options);
  140. result.NavContext = this.NavContext;
  141. return nav;
  142. };
  143. result.PreFunc = func;
  144. result.Context = this.Context;
  145. return result;
  146. }
  147. public UpdateNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, TChild>> expression, UpdateNavOptions options) where TChild : class, new()
  148. {
  149. return AsNav().ThenInclude(expression, options);
  150. }
  151. public UpdateNavTask<Root, TChild> Include<TChild>(Expression<Func<Root, List<TChild>>> expression, UpdateNavOptions options) where TChild : class, new()
  152. {
  153. return AsNav().ThenInclude(expression, options);
  154. }
  155. #endregion
  156. public bool ExecuteCommand()
  157. {
  158. var hasTran = this.Context.Ado.Transaction != null;
  159. if (hasTran)
  160. {
  161. PreFunc();
  162. }
  163. else
  164. {
  165. this.Context.Ado.UseTran(() =>
  166. {
  167. PreFunc();
  168. }, ex => throw ex);
  169. }
  170. return true;
  171. }
  172. public async Task<bool> ExecuteCommandAsync()
  173. {
  174. await Task.Run(async () =>
  175. {
  176. ExecuteCommand();
  177. await Task.Delay(0);
  178. });
  179. return true;
  180. }
  181. private UpdateNavTask<Root, Root> AsNav()
  182. {
  183. UpdateNavTask<Root, Root> result = new UpdateNavTask<Root, Root>();
  184. Func<UpdateNavProvider<Root, Root>> func = () => {
  185. var navres=PreFunc().AsNav();
  186. navres.IsAsNav = true;
  187. navres.NavContext = this.NavContext;
  188. return navres;
  189. };
  190. result.PreFunc = func;
  191. result.Context = this.Context;
  192. result.NavContext = this.NavContext;
  193. return result;
  194. }
  195. }
  196. }