|
|
@ -12,27 +12,35 @@ 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) |
|
|
|
{ |
|
|
|
if (InsertObjects != null&&InsertObjects.Count() > 0) |
|
|
|
{ |
|
|
|
string subMemberName; |
|
|
|
object sublist; |
|
|
|
GetList(new T[] { InsertObject }, items,out subMemberName,out sublist); |
|
|
|
GetList(InsertObjects, items, out subMemberName, out sublist); |
|
|
|
if (!this.SubList.ContainsKey(subMemberName)) |
|
|
|
{ |
|
|
|
this.SubList.Add(subMemberName, sublist); |
|
|
|
} |
|
|
|
} |
|
|
|
return this; |
|
|
|
} |
|
|
|
|
|
|
|
public object ExecuteReturnPrimaryKey() |
|
|
|
{ |
|
|
|
|
|
|
|
if (InsertObjects != null && InsertObjects.Count()>0) |
|
|
|
{ |
|
|
|
int count = 1; |
|
|
|
foreach (var InsertObject in InsertObjects) |
|
|
|
{ |
|
|
|
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) |
|
|
@ -61,17 +69,86 @@ namespace SqlSugar |
|
|
|
EntityInfo subEntity = null; |
|
|
|
if (item.Value is IEnumerable<object>) |
|
|
|
{ |
|
|
|
var list=item.Value as 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 |
|
|
|
{ |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
public async Task<object> ExecuteReturnPrimaryKeyAsync() |
|
|
|
{ |
|
|
|
|
|
|
|
if (InsertObjects != null && InsertObjects.Count() > 0) |
|
|
|
{ |
|
|
|
int count = 1; |
|
|
|
foreach (var InsertObject in InsertObjects) |
|
|
|
{ |
|
|
|
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) |
|
|
|
{ |
|
|
|
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 =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(); |
|
|
|
var type = list.First().GetType(); |
|
|
|
this.Context.InitMappingInfo(type); |
|
|
|
subEntity=this.Context.EntityMaintenance.GetEntityInfo(type); |
|
|
|
subEntity = this.Context.EntityMaintenance.GetEntityInfo(type); |
|
|
|
foreach (var sbItem in list) |
|
|
|
{ |
|
|
|
SetItems(insertDictionary, sbItem, subEntity,item.Key,pkValue); |
|
|
|
SetItems(insertDictionary, sbItem, subEntity, item.Key, pkValue); |
|
|
|
} |
|
|
|
} |
|
|
|
else if (item.Value.GetType().IsClass()) |
|
|
@ -79,12 +156,19 @@ namespace SqlSugar |
|
|
|
var type = item.Value.GetType(); |
|
|
|
this.Context.InitMappingInfo(type); |
|
|
|
subEntity = this.Context.EntityMaintenance.GetEntityInfo(type); |
|
|
|
SetItems(insertDictionary, item.Value, subEntity,item.Key,pkValue); |
|
|
|
SetItems(insertDictionary, item.Value, subEntity, item.Key, pkValue); |
|
|
|
} |
|
|
|
count +=await this.Context.Insertable(insertDictionary).AS(subEntity.DbTableName).ExecuteCommandAsync(); |
|
|
|
} |
|
|
|
count+=this.Context.Insertable(insertDictionary).AS(subEntity.DbTableName).ExecuteCommand(); |
|
|
|
} |
|
|
|
return count; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
public void GetList(T[] inserts,Expression<Func<T, object>> items, out string subMemberName, out object sublist) |
|
|
|
{ |
|
|
|
var lambdaExpression = (items as LambdaExpression).Body; |
|
|
|