protected void SetEntityFieldValue(object entity, ExcelPropety ep, int rowIndex, string fieldName, T templateVM) { if (ep.FormatData != null) { ProcessResult processResult = ep.FormatData(ep.Value, templateVM); if (processResult != null) { //未添加任何处理结果 if (processResult.EntityValues.Count == 0) { PropertyHelper.SetPropertyValue(entity, fieldName, ep.Value, stringBasedValue: true); } //字段为一对一 if (processResult.EntityValues.Count == 1) { ep.Value = processResult.EntityValues[0].FieldValue; if (!string.IsNullOrEmpty(processResult.EntityValues[0].ErrorMsg)) { ErrorListVM.EntityList.Add(new ErrorMessage { Message = processResult.EntityValues[0].ErrorMsg, ExcelIndex = rowIndex }); } PropertyHelper.SetPropertyValue(entity, fieldName, ep.Value, stringBasedValue: true); } //字段为一对多 if (processResult.EntityValues.Count > 1) { foreach (var entityValue in processResult.EntityValues) { if (!string.IsNullOrEmpty(entityValue.ErrorMsg)) { ErrorListVM.EntityList.Add(new ErrorMessage { Message = entityValue.ErrorMsg, ExcelIndex = rowIndex }); } PropertyHelper.SetPropertyValue(entity, entityValue.FieldName, entityValue.FieldValue, stringBasedValue: true); } } } } else if (ep.FormatSingleData != null) { ep.FormatSingleData(ep.Value, templateVM, out string singleEntityValue, out string errorMsg); if (!string.IsNullOrEmpty(errorMsg)) { ErrorListVM.EntityList.Add(new ErrorMessage { Message = errorMsg, ExcelIndex = rowIndex }); } PropertyHelper.SetPropertyValue(entity, fieldName, singleEntityValue, stringBasedValue: true); } else { PropertyHelper.SetPropertyValue(entity, fieldName, ep.Value, stringBasedValue: true); } }
/// <summary> /// 保存指定表中的数据 /// </summary> /// <returns>成功返回True,失败返回False</returns> public virtual bool BatchSaveData() { //如果没有赋值,则调用赋值函数 if (isEntityListSet == false) { SetEntityList(); } //如果在复制过程中已经有错误,则直接输出错误 if (ErrorListVM.EntityList.Count > 0) { DoReInit(); return(false); } //进行Model层面的验证 //找到对应的BaseCRUDVM,并初始化 var vms = this.GetType().Assembly.GetExportedTypes().Where(x => x.IsSubclassOf(typeof(BaseCRUDVM <P>))).ToList(); var vmtype = vms.Where(x => x.Name.ToLower() == typeof(P).Name.ToLower() + "vm").FirstOrDefault(); if (vmtype == null) { vmtype = vms.FirstOrDefault(); } IBaseCRUDVM <P> vm = null; DuplicatedInfo <P> dinfo = null; if (vmtype != null) { vm = vmtype.GetConstructor(System.Type.EmptyTypes).Invoke(null) as IBaseCRUDVM <P>; vm.CopyContext(this); dinfo = (vm as dynamic).SetDuplicatedCheck(); } var cinfo = this.SetDuplicatedCheck(); DuplicatedInfo <P> finalInfo = new DuplicatedInfo <P> { Groups = new List <DuplicatedGroup <P> >() }; if (dinfo == null) { dinfo = new DuplicatedInfo <P>(); } if (cinfo == null) { cinfo = new DuplicatedInfo <P>(); } if (dinfo.Groups != null) { foreach (var di in dinfo.Groups) { bool found = false; if (cinfo.Groups != null) { foreach (var ci in cinfo.Groups) { if (di.ToString() == ci.ToString()) { found = true; break; } } } if (found == false) { finalInfo.Groups.Add(di); } } } //调用controller方法验证model var vmethod = Controller?.GetType().GetMethod("RedoValidation"); foreach (var entity in EntityList) { try { vmethod.Invoke(Controller, new object[] { entity }); } catch { } if (vm != null) { vm.SetEntity(entity); vm.ByPassBaseValidation = true; vm.Validate(); var basevm = vm as BaseVM; if (basevm?.MSD?.Count > 0) { foreach (var key in basevm.MSD.Keys) { foreach (var error in basevm.MSD[key]) { ErrorListVM.EntityList.Add(new ErrorMessage { Message = error.ErrorMessage, Index = entity.ExcelIndex }); } } } } (vm as BaseVM)?.MSD.Clear(); //在本地EntityList中验证是否有重复 ValidateDuplicateData(finalInfo, entity); } //如果上述验证已经有错误,则直接输出错误 if (ErrorListVM.EntityList.Count > 0) { (vm as BaseVM)?.MSD.Clear(); DoReInit(); return(false); } //循环数据列表 foreach (var item in EntityList) { //根据唯一性的设定查找数据库中是否有同样的数据 P exist = IsDuplicateData(item, finalInfo); //如果有重复数据,则进行修改 if (exist != null) { //如果是普通字段,则直接赋值 var tempPros = typeof(T).GetFields(); foreach (var pro in tempPros) { var excelProp = Template.GetType().GetField(pro.Name).GetValue(Template) as ExcelPropety; var proToSet = typeof(P).GetProperties().Where(x => x.Name == excelProp.FieldName).FirstOrDefault(); if (proToSet != null) { var val = proToSet.GetValue(item); PropertyHelper.SetPropertyValue(exist, excelProp.FieldName, val, stringBasedValue: true); } } //更新修改时间字段 if (tempPros.Where(x => x.Name == "UpdateTime").SingleOrDefault() == null) { if (typeof(BasePoco).IsAssignableFrom(exist.GetType())) { (exist as BasePoco).UpdateTime = DateTime.Now; } } //更新修改人字段 if (tempPros.Where(x => x.Name == "UpdateBy").SingleOrDefault() == null) { if (typeof(BasePoco).IsAssignableFrom(exist.GetType())) { (exist as BasePoco).UpdateBy = LoginUserInfo.ITCode; } } exist.ExcelIndex = item.ExcelIndex; //更新字段 DC.UpdateEntity(exist); } //如果没有重复数据,则填加 else { if (typeof(BasePoco).IsAssignableFrom(item.GetType())) { (item as BasePoco).CreateTime = DateTime.Now; (item as BasePoco).CreateBy = LoginUserInfo?.ITCode; } if (typeof(PersistPoco).IsAssignableFrom(item.GetType())) { (item as PersistPoco).IsValid = true; } DC.Set <P>().Add(item); } } //如果有错误,则返回 if (ErrorListVM.EntityList.Count > 0) { DoReInit(); return(false); } //如果没有错误,更新数据库 if (EntityList.Count > 0) { try { DC.SaveChanges(); } catch (Exception e) { SetExceptionMessage(e, null); DoReInit(); return(false); } } return(true); }
/// <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; }