|
|
@ -12,78 +12,162 @@ namespace SqlSugar |
|
|
|
internal EntityInfo Entity { get; set; } |
|
|
|
internal Dictionary<string, object> SubList { get; set; } |
|
|
|
internal SqlSugarProvider Context { get; set; } |
|
|
|
internal T InsertObject { get; set; } |
|
|
|
internal T [] InsertObjects { get; set; } |
|
|
|
internal InsertBuilder InsertBuilder { get; set; } |
|
|
|
internal string Pk { get; set; } |
|
|
|
|
|
|
|
public ISubInsertable<T> AddSubList(Expression<Func<T, object>> items) |
|
|
|
{ |
|
|
|
string subMemberName; |
|
|
|
object sublist; |
|
|
|
GetList(new T[] { InsertObject }, items,out subMemberName,out sublist); |
|
|
|
if (!this.SubList.ContainsKey(subMemberName)) |
|
|
|
if (InsertObjects != null&&InsertObjects.Count() > 0) |
|
|
|
{ |
|
|
|
this.SubList.Add(subMemberName, sublist); |
|
|
|
string subMemberName; |
|
|
|
object sublist; |
|
|
|
GetList(InsertObjects, items, out subMemberName, out sublist); |
|
|
|
if (!this.SubList.ContainsKey(subMemberName)) |
|
|
|
{ |
|
|
|
this.SubList.Add(subMemberName, sublist); |
|
|
|
} |
|
|
|
} |
|
|
|
return this; |
|
|
|
} |
|
|
|
|
|
|
|
public object ExecuteReturnPrimaryKey() |
|
|
|
{ |
|
|
|
List<ConditionalModel> conModel = new List<ConditionalModel>(); |
|
|
|
int id = this.Context.Insertable(InsertObject).ExecuteReturnIdentity(); |
|
|
|
int count = 1; |
|
|
|
object pkValue = null; |
|
|
|
var qureyable = this.Context.Queryable<T>(); |
|
|
|
if (id.ObjToInt() == 0) |
|
|
|
|
|
|
|
if (InsertObjects != null && InsertObjects.Count()>0) |
|
|
|
{ |
|
|
|
var primaryProperty = this.Entity.Columns.FirstOrDefault(it => |
|
|
|
it.PropertyName.Equals(this.Pk, StringComparison.CurrentCultureIgnoreCase) || |
|
|
|
it.DbColumnName.Equals(this.Pk, StringComparison.CurrentCultureIgnoreCase) |
|
|
|
); |
|
|
|
pkValue = primaryProperty.PropertyInfo.GetValue(InsertObject); |
|
|
|
qureyable.In(pkValue); |
|
|
|
int count = 1; |
|
|
|
foreach (var InsertObject in InsertObjects) |
|
|
|
{ |
|
|
|
List<ConditionalModel> conModel = new List<ConditionalModel>(); |
|
|
|
int id = this.Context.Insertable(InsertObject).ExecuteReturnIdentity(); |
|
|
|
object pkValue = null; |
|
|
|
var qureyable = this.Context.Queryable<T>(); |
|
|
|
if (id.ObjToInt() == 0) |
|
|
|
{ |
|
|
|
var primaryProperty = this.Entity.Columns.FirstOrDefault(it => |
|
|
|
it.PropertyName.Equals(this.Pk, StringComparison.CurrentCultureIgnoreCase) || |
|
|
|
it.DbColumnName.Equals(this.Pk, StringComparison.CurrentCultureIgnoreCase) |
|
|
|
); |
|
|
|
pkValue = primaryProperty.PropertyInfo.GetValue(InsertObject); |
|
|
|
qureyable.In(pkValue); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
qureyable.In(id); |
|
|
|
pkValue = id; |
|
|
|
} |
|
|
|
var data = qureyable.First(); |
|
|
|
foreach (var item in this.SubList) |
|
|
|
{ |
|
|
|
|
|
|
|
Dictionary<string, object> insertDictionary = new Dictionary<string, object>(); |
|
|
|
if (item.Value == null) |
|
|
|
{ |
|
|
|
continue; |
|
|
|
} |
|
|
|
EntityInfo subEntity = null; |
|
|
|
if (item.Value is IEnumerable<object>) |
|
|
|
{ |
|
|
|
var list = item.Value as IEnumerable<object>; |
|
|
|
if (list.Count() == 0) |
|
|
|
{ |
|
|
|
continue; |
|
|
|
} |
|
|
|
var type = list.First().GetType(); |
|
|
|
this.Context.InitMappingInfo(type); |
|
|
|
subEntity = this.Context.EntityMaintenance.GetEntityInfo(type); |
|
|
|
foreach (var sbItem in list) |
|
|
|
{ |
|
|
|
SetItems(insertDictionary, sbItem, subEntity, item.Key, pkValue); |
|
|
|
} |
|
|
|
} |
|
|
|
else if (item.Value.GetType().IsClass()) |
|
|
|
{ |
|
|
|
var type = item.Value.GetType(); |
|
|
|
this.Context.InitMappingInfo(type); |
|
|
|
subEntity = this.Context.EntityMaintenance.GetEntityInfo(type); |
|
|
|
SetItems(insertDictionary, item.Value, subEntity, item.Key, pkValue); |
|
|
|
} |
|
|
|
count += this.Context.Insertable(insertDictionary).AS(subEntity.DbTableName).ExecuteCommand(); |
|
|
|
} |
|
|
|
} |
|
|
|
return count; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
qureyable.In(id); |
|
|
|
pkValue = id; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
var data = qureyable.First(); |
|
|
|
foreach (var item in this.SubList) |
|
|
|
{ |
|
|
|
|
|
|
|
} |
|
|
|
public async Task<object> ExecuteReturnPrimaryKeyAsync() |
|
|
|
{ |
|
|
|
|
|
|
|
Dictionary<string, object> insertDictionary = new Dictionary<string, object>(); |
|
|
|
if (item.Value == null) |
|
|
|
{ |
|
|
|
continue; |
|
|
|
} |
|
|
|
EntityInfo subEntity = null; |
|
|
|
if (item.Value is IEnumerable<object>) |
|
|
|
if (InsertObjects != null && InsertObjects.Count() > 0) |
|
|
|
{ |
|
|
|
int count = 1; |
|
|
|
foreach (var InsertObject in InsertObjects) |
|
|
|
{ |
|
|
|
var list=item.Value as IEnumerable<object>; |
|
|
|
if (list.Count() == 0) |
|
|
|
List<ConditionalModel> conModel = new List<ConditionalModel>(); |
|
|
|
int id = await this.Context.Insertable(InsertObject).ExecuteReturnIdentityAsync(); |
|
|
|
object pkValue = null; |
|
|
|
var qureyable = this.Context.Queryable<T>(); |
|
|
|
if (id.ObjToInt() == 0) |
|
|
|
{ |
|
|
|
continue; |
|
|
|
var primaryProperty = this.Entity.Columns.FirstOrDefault(it => |
|
|
|
it.PropertyName.Equals(this.Pk, StringComparison.CurrentCultureIgnoreCase) || |
|
|
|
it.DbColumnName.Equals(this.Pk, StringComparison.CurrentCultureIgnoreCase) |
|
|
|
); |
|
|
|
pkValue = primaryProperty.PropertyInfo.GetValue(InsertObject); |
|
|
|
qureyable.In(pkValue); |
|
|
|
} |
|
|
|
var type= list.First().GetType(); |
|
|
|
this.Context.InitMappingInfo(type); |
|
|
|
subEntity=this.Context.EntityMaintenance.GetEntityInfo(type); |
|
|
|
foreach (var sbItem in list) |
|
|
|
else |
|
|
|
{ |
|
|
|
SetItems(insertDictionary, sbItem, subEntity,item.Key,pkValue); |
|
|
|
qureyable.In(id); |
|
|
|
pkValue = id; |
|
|
|
} |
|
|
|
var data =await qureyable.FirstAsync(); |
|
|
|
foreach (var item in this.SubList) |
|
|
|
{ |
|
|
|
|
|
|
|
Dictionary<string, object> insertDictionary = new Dictionary<string, object>(); |
|
|
|
if (item.Value == null) |
|
|
|
{ |
|
|
|
continue; |
|
|
|
} |
|
|
|
EntityInfo subEntity = null; |
|
|
|
if (item.Value is IEnumerable<object>) |
|
|
|
{ |
|
|
|
var list = item.Value as IEnumerable<object>; |
|
|
|
if (list.Count() == 0) |
|
|
|
{ |
|
|
|
continue; |
|
|
|
} |
|
|
|
var type = list.First().GetType(); |
|
|
|
this.Context.InitMappingInfo(type); |
|
|
|
subEntity = this.Context.EntityMaintenance.GetEntityInfo(type); |
|
|
|
foreach (var sbItem in list) |
|
|
|
{ |
|
|
|
SetItems(insertDictionary, sbItem, subEntity, item.Key, pkValue); |
|
|
|
} |
|
|
|
} |
|
|
|
else if (item.Value.GetType().IsClass()) |
|
|
|
{ |
|
|
|
var type = item.Value.GetType(); |
|
|
|
this.Context.InitMappingInfo(type); |
|
|
|
subEntity = this.Context.EntityMaintenance.GetEntityInfo(type); |
|
|
|
SetItems(insertDictionary, item.Value, subEntity, item.Key, pkValue); |
|
|
|
} |
|
|
|
count +=await this.Context.Insertable(insertDictionary).AS(subEntity.DbTableName).ExecuteCommandAsync(); |
|
|
|
} |
|
|
|
} |
|
|
|
else if (item.Value.GetType().IsClass()) |
|
|
|
{ |
|
|
|
var type = item.Value.GetType(); |
|
|
|
this.Context.InitMappingInfo(type); |
|
|
|
subEntity = this.Context.EntityMaintenance.GetEntityInfo(type); |
|
|
|
SetItems(insertDictionary, item.Value, subEntity,item.Key,pkValue); |
|
|
|
} |
|
|
|
count+=this.Context.Insertable(insertDictionary).AS(subEntity.DbTableName).ExecuteCommand(); |
|
|
|
return count; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
return 0; |
|
|
|
} |
|
|
|
return count; |
|
|
|
|
|
|
|
} |
|
|
|
public void GetList(T[] inserts,Expression<Func<T, object>> items, out string subMemberName, out object sublist) |
|
|
|
{ |
|
|
|