private void DoEditPrepare(bool updateAllFields) { if (typeof(TModel).GetTypeInfo().IsSubclassOf(typeof(BasePoco))) { BasePoco ent = Entity as BasePoco; if (ent.UpdateTime == null) { ent.UpdateTime = DateTime.Now; } if (string.IsNullOrEmpty(ent.UpdateBy)) { ent.UpdateBy = LoginUserInfo?.ITCode; } } var pros = typeof(TModel).GetProperties(); #region 更新子表 foreach (var pro in pros) { //找到类型为List<xxx>的字段 if (pro.PropertyType.GenericTypeArguments.Count() > 0) { //获取xxx的类型 var ftype = pro.PropertyType.GenericTypeArguments.First(); //如果xxx继承自TopBasePoco if (ftype.IsSubclassOf(typeof(TopBasePoco))) { //界面传过来的子表数据 if (pro.GetValue(Entity) is IEnumerable <TopBasePoco> list && list.Count() > 0) { //获取外键字段名称 string fkname = DC.GetFKName <TModel>(pro.Name); PropertyInfo[] itemPros = ftype.GetProperties(); bool found = false; foreach (var newitem in list) { var subtype = newitem.GetType(); if (subtype.IsSubclassOf(typeof(BasePoco))) { BasePoco ent = newitem as BasePoco; if (ent.UpdateTime == null) { ent.UpdateTime = DateTime.Now; } if (string.IsNullOrEmpty(ent.UpdateBy)) { ent.UpdateBy = LoginUserInfo?.ITCode; } } //循环页面传过来的子表数据,将关联到TopBasePoco的字段设为null,并且把外键字段的值设定为主表ID foreach (var itempro in itemPros) { if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco))) { itempro.SetValue(newitem, null); } if (!string.IsNullOrEmpty(fkname)) { if (itempro.Name.ToLower() == fkname.ToLower()) { itempro.SetValue(newitem, Entity.GetID()); found = true; } } } } //如果没有找到相应的外建字段,则可能是多对多的关系,或者做了特殊的设定,这种情况框架无法支持,直接退出本次循环 if (found == false) { continue; } TModel _entity = null; //打开新的数据库联接,获取数据库中的主表和子表数据 using (var ndc = DC.CreateNew()) { _entity = ndc.Set <TModel>().Include(pro.Name).AsNoTracking().CheckID(Entity.GetID()).FirstOrDefault(); } //比较子表原数据和新数据的区别 IEnumerable <TopBasePoco> toadd = null; IEnumerable <TopBasePoco> toremove = null; IEnumerable <TopBasePoco> data = _entity.GetType().GetProperty(pro.Name).GetValue(_entity) as IEnumerable <TopBasePoco>; Utils.CheckDifference(data, list, out toremove, out toadd); //设定子表应该更新的字段 List <string> setnames = new List <string>(); foreach (var field in FC.Keys) { if (field.StartsWith("Entity." + pro.Name + "[0].")) { string name = field.Replace("Entity." + pro.Name + "[0].", ""); setnames.Add(name); } } //前台传过来的数据 foreach (var newitem in list) { //数据库中的数据 foreach (var item in data) { //需要更新的数据 if (newitem.GetID().ToString() == item.GetID().ToString()) { dynamic i = newitem; var newitemType = item.GetType(); foreach (var itempro in itemPros) { if (!itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco)) && (updateAllFields == true || setnames.Contains(itempro.Name))) { var notmapped = itempro.GetCustomAttribute <NotMappedAttribute>(); if (itempro.Name != "ID" && notmapped == null && itempro.PropertyType.IsList() == false) { DC.UpdateProperty(i, itempro.Name); } } } if (item.GetType().IsSubclassOf(typeof(BasePoco))) { DC.UpdateProperty(i, "UpdateTime"); DC.UpdateProperty(i, "UpdateBy"); } } } } //需要删除的数据 foreach (var item in toremove) { //如果是PersistPoco,则把IsValid设为false,并不进行物理删除 if (ftype.IsSubclassOf(typeof(PersistPoco))) { (item as PersistPoco).IsValid = false; (item as PersistPoco).UpdateTime = DateTime.Now; (item as PersistPoco).UpdateBy = LoginUserInfo?.ITCode; dynamic i = item; DC.UpdateEntity(i); } else { foreach (var itempro in itemPros) { if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco))) { itempro.SetValue(item, null); } } dynamic i = item; DC.DeleteEntity(i); } } //需要添加的数据 foreach (var item in toadd) { if (item.GetType().IsSubclassOf(typeof(BasePoco))) { BasePoco ent = item as BasePoco; if (ent.CreateTime == null) { ent.CreateTime = DateTime.Now; } if (string.IsNullOrEmpty(ent.CreateBy)) { ent.CreateBy = LoginUserInfo?.ITCode; } } DC.AddEntity(item); } } else if (FC.Keys.Contains("Entity." + pro.Name + ".DONOTUSECLEAR") || (FC.ContainsKey("Entity." + pro.Name) && pro.GetValue(Entity) is IEnumerable <TopBasePoco> list2 && list2?.Count() == 0)) { PropertyInfo[] itemPros = ftype.GetProperties(); var _entity = DC.Set <TModel>().Include(pro.Name).AsNoTracking().CheckID(Entity.GetID()).FirstOrDefault(); if (_entity != null) { IEnumerable <TopBasePoco> removeData = _entity.GetType().GetProperty(pro.Name).GetValue(_entity) as IEnumerable <TopBasePoco>; //如果是PersistPoco,则把IsValid设为false,并不进行物理删除 if (removeData is IEnumerable <PersistPoco> removePersistPocoData) { foreach (var item in removePersistPocoData) { (item as PersistPoco).IsValid = false; (item as PersistPoco).UpdateTime = DateTime.Now; (item as PersistPoco).UpdateBy = LoginUserInfo?.ITCode; dynamic i = item; DC.UpdateEntity(i); } } else { foreach (var item in removeData) { foreach (var itempro in itemPros) { if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco))) { itempro.SetValue(item, null); } } dynamic i = item; DC.DeleteEntity(i); } } } } } } } #endregion if (updateAllFields == false) { foreach (var field in FC.Keys) { if (field.StartsWith("Entity.") && !field.Contains("[")) { string name = field.Replace("Entity.", ""); try { DC.UpdateProperty(Entity, name); } catch (Exception ea) { } } } if (typeof(TModel).GetTypeInfo().IsSubclassOf(typeof(BasePoco))) { try { DC.UpdateProperty(Entity, "UpdateTime"); DC.UpdateProperty(Entity, "UpdateBy"); } catch (Exception) { } } } else { DC.UpdateEntity(Entity); } }
private void DoAddPrepare() { var pros = typeof(TModel).GetProperties(); //将所有TopBasePoco的属性赋空值,防止添加关联的重复内容 if (typeof(TModel) != typeof(FileAttachment)) { foreach (var pro in pros) { if (pro.PropertyType.GetTypeInfo().IsSubclassOf(typeof(TopBasePoco))) { pro.SetValue(Entity, null); } } } //自动设定添加日期和添加人 if (typeof(TModel).GetTypeInfo().IsSubclassOf(typeof(BasePoco))) { BasePoco ent = Entity as BasePoco; if (ent.CreateTime == null) { ent.CreateTime = DateTime.Now; } if (string.IsNullOrEmpty(ent.CreateBy)) { ent.CreateBy = LoginUserInfo?.ITCode; } } if (typeof(TModel).GetTypeInfo().IsSubclassOf(typeof(PersistPoco))) { (Entity as PersistPoco).IsValid = true; } #region 更新子表 foreach (var pro in pros) { //找到类型为List<xxx>的字段 if (pro.PropertyType.GenericTypeArguments.Count() > 0) { //获取xxx的类型 var ftype = pro.PropertyType.GenericTypeArguments.First(); //如果xxx继承自TopBasePoco if (ftype.IsSubclassOf(typeof(TopBasePoco))) { //界面传过来的子表数据 IEnumerable <TopBasePoco> list = pro.GetValue(Entity) as IEnumerable <BasePoco>; if (list != null && list.Count() > 0) { string fkname = DC.GetFKName <TModel>(pro.Name); PropertyInfo[] itemPros = ftype.GetProperties(); bool found = false; foreach (var newitem in list) { foreach (var itempro in itemPros) { if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco))) { itempro.SetValue(newitem, null); } if (!string.IsNullOrEmpty(fkname)) { if (itempro.Name.ToLower() == fkname.ToLower()) { itempro.SetValue(newitem, Entity.GetID()); found = true; } } } } //如果没有找到相应的外建字段,则可能是多对多的关系,或者做了特殊的设定,这种情况框架无法支持,直接退出本次循环 if (found == false) { continue; } //循环页面传过来的子表数据,自动设定添加日期和添加人 foreach (var newitem in list) { var subtype = newitem.GetType(); BasePoco ent = newitem as BasePoco; if (ent.CreateTime == null) { ent.CreateTime = DateTime.Now; } if (string.IsNullOrEmpty(ent.CreateBy)) { ent.CreateBy = LoginUserInfo?.ITCode; } } } } } } #endregion //添加数据 DC.Set <TModel>().Add(Entity); }
/// <summary> /// 修改,进行默认的修改操作。子类如有自定义操作应重载本函数 /// </summary> public virtual void DoEdit(bool updateAllFields = false) { //自动设定修改日期和修改人 if (typeof(TModel).GetTypeInfo().IsSubclassOf(typeof(BasePoco))) { BasePoco ent = Entity as BasePoco; if (ent.UpdateTime == null) { ent.UpdateTime = DateTime.Now; } if (string.IsNullOrEmpty(ent.UpdateBy)) { ent.UpdateBy = LoginUserInfo?.ITCode; } } var pros = typeof(TModel).GetProperties(); #region 更新子表 foreach (var pro in pros) { //找到类型为List<xxx>的字段 if (pro.PropertyType.GenericTypeArguments.Count() > 0) { //获取xxx的类型 var ftype = pro.PropertyType.GenericTypeArguments.First(); //如果xxx继承自TopBasePoco if (ftype.IsSubclassOf(typeof(TopBasePoco))) { //界面传过来的子表数据 if (pro.GetValue(Entity) is IEnumerable <TopBasePoco> list && list.Count() > 0) { //获取外键字段名称 string fkname = DC.GetFKName <TModel>(pro.Name); PropertyInfo[] itemPros = ftype.GetProperties(); bool found = false; foreach (var newitem in list) { foreach (var itempro in itemPros) { if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco))) { itempro.SetValue(newitem, null); } if (!string.IsNullOrEmpty(fkname)) { if (itempro.Name.ToLower() == fkname.ToLower()) { itempro.SetValue(newitem, Entity.ID); found = true; } } } } //如果没有找到相应的外建字段,则可能是多对多的关系,或者做了特殊的设定,这种情况框架无法支持,直接退出本次循环 if (found == false) { continue; } //循环页面传过来的子表数据,将关联到TopBasePoco的字段设为null,并且把外键字段的值设定为主表ID foreach (var newitem in list) { var subtype = newitem.GetType(); if (subtype.IsSubclassOf(typeof(BasePoco))) { BasePoco ent = newitem as BasePoco; if (ent.UpdateTime == null) { ent.UpdateTime = DateTime.Now; } if (string.IsNullOrEmpty(ent.UpdateBy)) { ent.UpdateBy = LoginUserInfo.ITCode; } } foreach (var itempro in itemPros) { if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco))) { itempro.SetValue(newitem, null); } if (!string.IsNullOrEmpty(fkname)) { if (itempro.Name.ToLower() == fkname.ToLower()) { itempro.SetValue(newitem, Entity.ID); found = true; } } } } TModel _entity = null; //打开新的数据库联接,获取数据库中的主表和子表数据 using (var ndc = DC.CreateNew()) { _entity = ndc.Set <TModel>().Include(pro.Name).AsNoTracking().Where(x => x.ID == Entity.ID).FirstOrDefault(); } //比较子表原数据和新数据的区别 IEnumerable <TopBasePoco> toadd = null; IEnumerable <TopBasePoco> toremove = null; IEnumerable <TopBasePoco> data = _entity.GetType().GetProperty(pro.Name).GetValue(_entity) as IEnumerable <TopBasePoco>; Utils.CheckDifference(data, list, out toremove, out toadd); //设定子表应该更新的字段 List <string> setnames = new List <string>(); foreach (var field in FC.Keys) { if (field.StartsWith("Entity." + pro.Name + "[0].")) { string name = field.Replace("Entity." + pro.Name + "[0].", ""); if (name != "UpdateTime" && name != "UpdateBy") { setnames.Add(name); } } } //前台传过来的数据 foreach (var newitem in list) { //数据库中的数据 foreach (var item in data) { //需要更新的数据 if (newitem.ID == item.ID) { dynamic i = newitem; var newitemType = item.GetType(); foreach (var itempro in itemPros) { if (!itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco)) && setnames.Contains(itempro.Name)) { if (itempro.Name != "ID") { DC.UpdateProperty(i, itempro.Name); } } } DC.UpdateProperty(i, "UpdateTime"); DC.UpdateProperty(i, "UpdateBy"); } } } //需要删除的数据 foreach (var item in toremove) { foreach (var itempro in itemPros) { if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco))) { itempro.SetValue(item, null); } } dynamic i = item; DC.DeleteEntity(i); } //需要添加的数据 foreach (var item in toadd) { if (item.GetType().IsSubclassOf(typeof(BasePoco))) { BasePoco ent = item as BasePoco; if (ent.CreateTime == null) { ent.CreateTime = DateTime.Now; } if (string.IsNullOrEmpty(ent.CreateBy)) { ent.CreateBy = LoginUserInfo?.ITCode; } } DC.AddEntity(item); } } else if (FC.Keys.Contains("Entity." + pro.Name + ".DONOTUSECLEAR")) { PropertyInfo[] itemPros = ftype.GetProperties(); var _entity = DC.Set <TModel>().Include(pro.Name).AsNoTracking().Where(x => x.ID == Entity.ID).FirstOrDefault(); IEnumerable <TopBasePoco> removeData = _entity.GetType().GetProperty(pro.Name).GetValue(_entity) as IEnumerable <TopBasePoco>; foreach (var item in removeData) { foreach (var itempro in itemPros) { if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco))) { itempro.SetValue(item, null); } } dynamic i = item; DC.DeleteEntity(i); } } } } } #endregion if (updateAllFields == false) { foreach (var field in FC.Keys) { if (field.StartsWith("Entity.") && !field.Contains("[")) { string name = field.Replace("Entity.", ""); if (name != "UpdateTime" && name != "UpdateBy") { try { DC.UpdateProperty(Entity, name); } catch (Exception) { } } } } } else { DC.UpdateEntity(Entity); } DC.SaveChanges(); //删除不需要的附件 if (DeletedFileIds != null) { foreach (var item in DeletedFileIds) { FileAttachmentVM ofa = new FileAttachmentVM(); ofa.CopyContext(this); ofa.SetEntityById(item); ofa.DoDelete(); } } }
/// <summary> /// 获取上传的结果值 /// </summary> public virtual void SetEntityList() { EntityList = new List <P>(); //获取Model类的所有属性 var pros = typeof(P).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); var excelPros = typeof(T).GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).Where(x => x.FieldType == typeof(ExcelPropety)).ToList(); if (TemplateData == null) { DoMapList(); } //循环Excel中的数据 foreach (var item in TemplateData) { int rowIndex = 2; bool isMainData = false; //主表信息 Dictionary <string, ExcelPropety> mainInfo = new Dictionary <string, ExcelPropety>(); string mainValString = string.Empty; //子表信息 Dictionary <Type, List <FieldInfo> > subpros = new Dictionary <Type, List <FieldInfo> >(); Dictionary <Type, string> subValString = new Dictionary <Type, string>(); //循环TemplateVM中定义的所有的列,区分出主子表 foreach (var epro in excelPros) { //获取本列的ExcelProperty的值 if (typeof(T).GetField(epro.Name).GetValue(item) is ExcelPropety ep) { //如果是子表的字段 if (ep.SubTableType != null) { //保存子表字段信息稍后处理 if (!subpros.ContainsKey(ep.SubTableType)) { subpros[ep.SubTableType] = new List <FieldInfo>(); } subpros[ep.SubTableType].Add(epro); } else { //PropertyHelper.SetPropertyValue(entity, epro.Name, ep.Value, stringBasedValue: true); //保存子表字段信息稍后处理 mainInfo.Add(ep.FieldName, ep); mainValString += ep.Value; } } } //子表信息是否为空 foreach (var sub in subpros) { string subVal = string.Empty; foreach (var field in sub.Value) { ExcelPropety ep = typeof(T).GetField(field.Name).GetValue(item) as ExcelPropety; subVal += ep.Value; } subValString.Add(sub.Key, subVal); } P entity = null; //说明主表信息为空 if (string.IsNullOrEmpty(mainValString)) { entity = EntityList.LastOrDefault(); } else { //初始化一个新的Entity entity = new P(); isMainData = true; //给主表赋值 foreach (var mep in mainInfo) { SetEntityFieldValue(entity, mep.Value, rowIndex, mep.Key, item); } } //给子表赋值 foreach (var sub in subpros) { //循环Entity的所有属性,找到List<SubTableType>类型的字段 foreach (var pro in pros) { if (pro.PropertyType.IsGenericType) { var gtype = pro.PropertyType.GetGenericArguments()[0]; if (gtype == sub.Key) { //子表 var subList = entity.GetType().GetProperty(pro.Name).GetValue(entity); string fk = DC.GetFKName <P>(pro.Name); //如果子表不为空 if (!string.IsNullOrEmpty(subValString.Where(x => x.Key == sub.Key).FirstOrDefault().Value)) { IList list = null; if (subList == null) { //初始化List<SubTableType> list = typeof(List <>).MakeGenericType(gtype).GetConstructor(Type.EmptyTypes).Invoke(null) as IList; } else { list = subList as IList; } //初始化一个SubTableType var obj = gtype.GetConstructor(System.Type.EmptyTypes).Invoke(null); //给SubTableType中和本ExcelProperty同名的字段赋值 foreach (var field in sub.Value) { ExcelPropety ep = typeof(T).GetField(field.Name).GetValue(item) as ExcelPropety; //PropertyHelper.SetPropertyValue(obj, field.Name, ep.Value, stringBasedValue: true); SetEntityFieldValue(obj, ep, rowIndex, ep.FieldName, item); } if (string.IsNullOrEmpty(fk) == false) { PropertyHelper.SetPropertyValue(obj, fk, entity.GetID()); } //将付好值得SubTableType实例添加到List中 list.Add(obj); PropertyHelper.SetPropertyValue(entity, pro.Name, list); } break; } } } } entity.ExcelIndex = item.ExcelIndex; var cinfo = this.SetDuplicatedCheck(); //if (IsUpdateRecordDuplicated(cinfo, entity) == false) //{ if (isMainData) { EntityList.Add(entity); } //} } isEntityListSet = true; }
/// <summary> /// 添加,进行默认的添加操作。子类如有自定义操作应重载本函数 /// </summary> public virtual void DoAdd() { var pros = typeof(TModel).GetProperties(); //将所有TopBasePoco的属性赋空值,防止添加关联的重复内容 if (typeof(TModel) != typeof(FileAttachment)) { foreach (var pro in pros) { if (pro.PropertyType.GetTypeInfo().IsSubclassOf(typeof(TopBasePoco))) { pro.SetValue(Entity, null); } } } //自动设定添加日期和添加人 if (typeof(TModel).GetTypeInfo().IsSubclassOf(typeof(BasePoco))) { BasePoco ent = Entity as BasePoco; if (ent.CreateTime == null) { ent.CreateTime = DateTime.Now; } if (string.IsNullOrEmpty(ent.CreateBy)) { ent.CreateBy = LoginUserInfo?.ITCode; } } if (typeof(TModel).GetTypeInfo().IsSubclassOf(typeof(PersistPoco))) { (Entity as PersistPoco).IsValid = true; } #region 更新子表 foreach (var pro in pros) { //找到类型为List<xxx>的字段 if (pro.PropertyType.GenericTypeArguments.Count() > 0) { //获取xxx的类型 var ftype = pro.PropertyType.GenericTypeArguments.First(); //如果xxx继承自TopBasePoco if (ftype.IsSubclassOf(typeof(BasePoco))) { //界面传过来的子表数据 IEnumerable <TopBasePoco> list = pro.GetValue(Entity) as IEnumerable <BasePoco>; if (list != null && list.Count() > 0) { string fkname = DC.GetFKName <TModel>(pro.Name); PropertyInfo[] itemPros = ftype.GetProperties(); bool found = false; foreach (var newitem in list) { foreach (var itempro in itemPros) { if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco))) { itempro.SetValue(newitem, null); } if (!string.IsNullOrEmpty(fkname)) { if (itempro.Name.ToLower() == fkname.ToLower()) { itempro.SetValue(newitem, Entity.ID); found = true; } } } } //如果没有找到相应的外建字段,则可能是多对多的关系,或者做了特殊的设定,这种情况框架无法支持,直接退出本次循环 if (found == false) { continue; } //循环页面传过来的子表数据,自动设定添加日期和添加人 foreach (var newitem in list) { var subtype = newitem.GetType(); BasePoco ent = newitem as BasePoco; if (ent.CreateTime == null) { ent.CreateTime = DateTime.Now; } if (string.IsNullOrEmpty(ent.CreateBy)) { ent.CreateBy = LoginUserInfo?.ITCode; } var ft = subtype.GetProperties().Where(x => x.PropertyType == typeof(FileAttachment)).ToList(); foreach (var f in ft) { var fileid = subtype.GetProperty(f.Name + "ID").GetValue(ent); if (fileid != null) { var file = DC.Set <FileAttachment>().Find(fileid); file.IsTemprory = false; } } } } } } } #endregion //添加数据 DC.Set <TModel>().Add(Entity); //如果数据中包括附件,则把附件的是否临时的属性设为false,代表已经有数据用这个附件了 var fa = typeof(TModel).GetProperties().Where(x => x.PropertyType == typeof(FileAttachment)).ToList(); foreach (var f in fa) { var fileid = typeof(TModel).GetProperty(f.Name + "Id").GetValue(Entity); if (fileid != null) { var file = DC.Set <FileAttachment>().Find(fileid); file.IsTemprory = false; } } //删除不需要的附件 if (DeletedFileIds != null) { foreach (var item in DeletedFileIds) { FileAttachmentVM ofa = new FileAttachmentVM(); ofa.CopyContext(this); ofa.DoDelete(); } } DC.SaveChanges(); }