UpdateNavProvider.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 partial class UpdateNavProvider<Root, T> where T : class, new() where Root : class, new()
  10. {
  11. internal UpdateNavRootOptions _RootOptions { get; set; }
  12. public List<Root> _Roots { get; set; }
  13. public List<object> _ParentList { get; set; }
  14. public List<object> _RootList { get; set; }
  15. public EntityInfo _ParentEntity { get; set; }
  16. public EntityColumnInfo _ParentPkColumn { get; set; }
  17. public SqlSugarProvider _Context { get; set; }
  18. public UpdateNavOptions _Options { get; set; }
  19. public bool IsFirst { get; set; }
  20. public bool IsAsNav { get; set; }
  21. internal NavContext NavContext { get; set; }
  22. public UpdateNavProvider<Root, Root> AsNav()
  23. {
  24. return new UpdateNavProvider<Root, Root>
  25. {
  26. _Context = _Context,
  27. _ParentEntity = null,
  28. _ParentList = null,
  29. _Roots = _Roots,
  30. _ParentPkColumn = this._Context.EntityMaintenance.GetEntityInfo<Root>().Columns.First(it => it.IsPrimarykey)
  31. };
  32. }
  33. public UpdateNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
  34. {
  35. return _ThenInclude(expression);
  36. }
  37. public UpdateNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression) where TChild : class, new()
  38. {
  39. return _ThenInclude(expression);
  40. }
  41. public UpdateNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression,UpdateNavOptions options) where TChild : class, new()
  42. {
  43. _Options= options;
  44. return _ThenInclude(expression);
  45. }
  46. public UpdateNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression, UpdateNavOptions options) where TChild : class, new()
  47. {
  48. _Options = options;
  49. return _ThenInclude(expression);
  50. }
  51. private UpdateNavProvider<Root, TChild> _ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
  52. {
  53. var isRoot = _RootList == null;
  54. IsFirst = isRoot && this._ParentList == null;
  55. InitParentList();
  56. var name = ExpressionTool.GetMemberName(expression);
  57. var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
  58. if (nav.Navigat == null)
  59. {
  60. Check.ExceptionEasy($"{name} no navigate attribute", $"{this._ParentEntity.EntityName}的属性{name}没有导航属性");
  61. }
  62. if (_RootOptions != null && _RootOptions.IsDisableUpdateRoot)
  63. {
  64. //Future
  65. }
  66. else
  67. {
  68. UpdateRoot(isRoot, nav);
  69. }
  70. IsFirst = false;
  71. if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne)
  72. {
  73. UpdateOneToOne<TChild>(name, nav);
  74. }
  75. else if (nav.Navigat.NavigatType == NavigateType.OneToMany)
  76. {
  77. UpdateOneToMany<TChild>(name, nav);
  78. }
  79. else
  80. {
  81. UpdateManyToMany<TChild>(name, nav);
  82. }
  83. AddContextInfo(name,isRoot);
  84. return GetResult<TChild>();
  85. }
  86. private UpdateNavProvider<Root, TChild> _ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression) where TChild : class, new()
  87. {
  88. var isRoot = _RootList == null;
  89. IsFirst = isRoot && this._ParentList == null;
  90. InitParentList();
  91. var name = ExpressionTool.GetMemberName(expression);
  92. var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
  93. if (nav.Navigat == null)
  94. {
  95. Check.ExceptionEasy($"{name} no navigate attribute", $"{this._ParentEntity.EntityName}的属性{name}没有导航属性");
  96. }
  97. UpdateRoot(isRoot, nav);
  98. IsFirst = false;
  99. if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne)
  100. {
  101. UpdateOneToOne<TChild>(name, nav);
  102. }
  103. else if (nav.Navigat.NavigatType == NavigateType.OneToMany)
  104. {
  105. UpdateOneToMany<TChild>(name, nav);
  106. }
  107. else
  108. {
  109. UpdateManyToMany<TChild>(name, nav);
  110. }
  111. AddContextInfo(name, isRoot);
  112. return GetResult<TChild>();
  113. }
  114. private void UpdateRoot(bool isRoot, EntityColumnInfo nav)
  115. {
  116. if (isRoot && nav.Navigat.NavigatType != NavigateType.ManyToMany&&_RootOptions?.IsDisableUpdateRoot!=true)
  117. {
  118. UpdateRoot();
  119. }
  120. else if (isRoot &&_RootOptions?.IsInsertRoot==true&& nav.Navigat.NavigatType == NavigateType.ManyToMany)
  121. {
  122. UpdateRoot();
  123. }
  124. else
  125. {
  126. if (_Options != null && _Options.ManyToManyIsUpdateA)
  127. {
  128. UpdateRoot();
  129. }
  130. }
  131. }
  132. private void UpdateRoot()
  133. {
  134. if (IsAsNav)
  135. {
  136. return;
  137. }
  138. if (_Options != null && _Options.RootFunc != null)
  139. {
  140. var updateable = this._Context.Updateable(_Roots);
  141. var exp = _Options.RootFunc as Expression<Action<IUpdateable<Root>>>;
  142. Check.ExceptionEasy(exp == null, "UpdateOptions.RootFunc is error", "UpdateOptions.RootFunc");
  143. var com = exp.Compile();
  144. com(updateable);
  145. updateable.ExecuteCommand();
  146. }
  147. else if (IsFirst && _RootOptions != null)
  148. {
  149. var isInsert = _RootOptions.IsInsertRoot;
  150. if (isInsert)
  151. {
  152. var newRoots = new List<Root>();
  153. foreach (var item in _Roots)
  154. {
  155. var x = this._Context.Storageable(item).ToStorage();
  156. if (x.InsertList.HasValue())
  157. {
  158. newRoots.Add(x.AsInsertable.IgnoreColumns(_RootOptions.IgnoreInsertColumns).EnableDiffLogEventIF(_RootOptions.IsDiffLogEvent, _RootOptions.DiffLogBizData).ExecuteReturnEntity());
  159. }
  160. else
  161. {
  162. x.AsUpdateable
  163. .EnableDiffLogEventIF(_RootOptions.IsDiffLogEvent, _RootOptions.DiffLogBizData)
  164. .UpdateColumns(_RootOptions.UpdateColumns)
  165. .IgnoreColumns(_RootOptions.IgnoreColumns)
  166. .IgnoreNullColumns(_RootOptions.IsIgnoreAllNullColumns)
  167. .ExecuteCommandWithOptLockIF(_RootOptions?.IsOptLock, _RootOptions?.IsOptLock);
  168. newRoots.Add(item);
  169. }
  170. }
  171. _ParentList = _RootList = newRoots.Cast<object>().ToList();
  172. }
  173. else
  174. {
  175. this._Context.Updateable(_Roots)
  176. .EnableDiffLogEventIF(_RootOptions.IsDiffLogEvent,_RootOptions.DiffLogBizData)
  177. .UpdateColumns(_RootOptions.UpdateColumns)
  178. .IgnoreColumns(_RootOptions.IgnoreColumns)
  179. .IgnoreNullColumns(_RootOptions.IsIgnoreAllNullColumns)
  180. .ExecuteCommandWithOptLockIF(_RootOptions?.IsOptLock, _RootOptions?.IsOptLock);
  181. }
  182. }
  183. else if (_RootOptions != null && _RootOptions?.IsDiffLogEvent == true)
  184. {
  185. this._Context.Updateable(_Roots).EnableDiffLogEvent(_RootOptions.DiffLogBizData).ExecuteCommand();
  186. }
  187. else
  188. {
  189. this._Context.Updateable(_Roots).ExecuteCommand();
  190. }
  191. }
  192. private void AddContextInfo(string name, bool isRoot)
  193. {
  194. if (IsAsNav || isRoot)
  195. {
  196. if (this.NavContext != null && this.NavContext.Items != null)
  197. {
  198. this.NavContext.Items.Add(new NavContextItem() { Level = 0, RootName = name });
  199. }
  200. }
  201. }
  202. private bool NotAny(string name)
  203. {
  204. if (IsFirst) return true;
  205. if (this.NavContext == null) return true;
  206. return this.NavContext?.Items?.Any(it => it.RootName == name) == false;
  207. }
  208. }
  209. }