InsertNavProvider.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 InsertNavProvider<Root,T> where T : class,new() where Root:class,new()
  10. {
  11. public InsertNavRootOptions _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 NavigateType? _NavigateType { get; set; }
  19. public bool IsFirst { get; set; }
  20. public InsertNavOptions _navOptions { get; set; }
  21. public bool IsNav { get; internal set; }
  22. internal NavContext NavContext { get; set; }
  23. public InsertNavProvider<Root, Root> AsNav()
  24. {
  25. return new InsertNavProvider<Root, Root> {
  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 InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression,InsertNavOptions options) where TChild : class, new()
  34. {
  35. _navOptions = options;
  36. return _ThenInclude(expression);
  37. }
  38. public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression, InsertNavOptions options) where TChild : class, new()
  39. {
  40. _navOptions = options;
  41. return _ThenInclude(expression);
  42. }
  43. public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
  44. {
  45. return _ThenInclude(expression);
  46. }
  47. public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression) where TChild : class, new()
  48. {
  49. return _ThenInclude(expression);
  50. }
  51. private InsertNavProvider<Root, TChild> _ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
  52. {
  53. var name = ExpressionTool.GetMemberName(expression);
  54. var isRoot = false;
  55. if (this._ParentEntity == null)
  56. {
  57. this._ParentEntity = this._Context.EntityMaintenance.GetEntityInfo<Root>();
  58. this.IsFirst = true;
  59. isRoot = true;
  60. }
  61. var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
  62. if (nav.Navigat == null)
  63. {
  64. Check.ExceptionEasy($"{name} no navigate attribute", $"{this._ParentEntity.EntityName}的属性{name}没有导航属性");
  65. }
  66. if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne)
  67. {
  68. InitParentList();
  69. InsertOneToOne<TChild>(name, nav);
  70. }
  71. else if (nav.Navigat.NavigatType == NavigateType.OneToMany)
  72. {
  73. _NavigateType = NavigateType.OneToMany;
  74. InitParentList();
  75. InsertOneToMany<TChild>(name, nav);
  76. }
  77. else
  78. {
  79. InitParentList();
  80. InsertManyToMany<TChild>(name, nav);
  81. }
  82. AddContextInfo(name,isRoot);
  83. return GetResult<TChild>();
  84. }
  85. private InsertNavProvider<Root, TChild> _ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression) where TChild : class, new()
  86. {
  87. var name = ExpressionTool.GetMemberName(expression);
  88. var isRoot = false;
  89. if (this._ParentEntity == null)
  90. {
  91. this._ParentEntity = this._Context.EntityMaintenance.GetEntityInfo<Root>();
  92. IsFirst = true;
  93. isRoot = true;
  94. }
  95. var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
  96. if (nav.Navigat == null)
  97. {
  98. Check.ExceptionEasy($"{name} no navigate attribute", $"{this._ParentEntity.EntityName}的属性{name}没有导航属性");
  99. }
  100. if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne)
  101. {
  102. InitParentList();
  103. InsertOneToOne<TChild>(name, nav);
  104. }
  105. else if (nav.Navigat.NavigatType == NavigateType.OneToMany)
  106. {
  107. _NavigateType = NavigateType.OneToMany;
  108. InitParentList();
  109. InsertOneToMany<TChild>(name, nav);
  110. }
  111. else
  112. {
  113. InitParentList();
  114. InsertManyToMany<TChild>(name, nav);
  115. }
  116. AddContextInfo(name,isRoot);
  117. return GetResult<TChild>();
  118. }
  119. private void AddContextInfo(string name,bool isRoot)
  120. {
  121. if (IsNav || isRoot)
  122. {
  123. if (this.NavContext != null && this.NavContext.Items != null)
  124. {
  125. this.NavContext.Items.Add(new NavContextItem() { Level = 0, RootName = name });
  126. }
  127. }
  128. }
  129. private bool NotAny(string name)
  130. {
  131. if (IsFirst) return true;
  132. if (this.NavContext == null) return true;
  133. return this.NavContext?.Items?.Any(it => it.RootName == name) == false;
  134. }
  135. }
  136. }