示例#1
0
        /// <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);
        }
示例#2
0
文件: BaseBatchVM.cs 项目: zzti/WTM
        /// <summary>
        /// 批量修改,默认对Ids中包含的数据进行修改,子类如果有特殊判断应重载本函数
        /// </summary>
        /// <returns>true代表成功,false代表失败</returns>
        public virtual bool DoBatchEdit()
        {
            //获取批量修改VM的所有属性
            var         pros    = LinkedVM.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly);
            bool        rv      = true;
            List <Guid> idsData = new List <Guid>(Ids);
            //找到对应的BaseCRUDVM,并初始化
            var vmtype = this.GetType().Assembly.GetExportedTypes().Where(x => x.IsSubclassOf(typeof(BaseCRUDVM <TModel>))).FirstOrDefault();
            IBaseCRUDVM <TModel> vm = null;

            if (vmtype != null)
            {
                vm = vmtype.GetConstructor(System.Type.EmptyTypes).Invoke(null) as IBaseCRUDVM <TModel>;
                vm.CopyContext(this);
            }
            //循环所有数据
            for (int i = 0; i < idsData.Count; i++)
            {
                try
                {
                    //如果找不到对应数据,则输出错误
                    TModel entity = null;
                    entity = DC.Set <TModel>().Find(idsData[i]);
                    if (vm != null)
                    {
                        vm.SetEntity(entity);
                    }
                    if (entity == null)
                    {
                        ErrorMessage.Add(idsData[i], "数据不存在");
                        rv = false;
                        break;
                    }
                    //如果能找到,则循环LinkedVM中的属性,给entity中同名属性赋值
                    foreach (var pro in pros)
                    {
                        var proToSet = entity.GetType().GetProperty(pro.Name);
                        var val      = FC.ContainsKey("LinkedVM." + pro.Name) ? FC["LinkedVM." + pro.Name] : null;
                        if (proToSet != null && val != null)
                        {
                            var hasvalue = false;
                            if (val is StringValues sv && StringValues.IsNullOrEmpty(sv) == false)
                            {
                                hasvalue = true;
                            }
                            if (hasvalue)
                            {
                                proToSet.SetValue(entity, pro.GetValue(LinkedVM));
                            }
                        }
                    }
                    //如果有对应的BaseCRUDVM则使用其进行数据验证
                    if (vm != null)
                    {
                        vm.Validate();
                        var errors = vm.MSD;
                        if (errors != null && errors.Count > 0)
                        {
                            var error = "";
                            foreach (var key in errors.Keys)
                            {
                                if (errors[key].Count > 0)
                                {
                                    error += errors[key].Select(x => x.ErrorMessage).ToSpratedString();
                                }
                            }
                            if (error != "")
                            {
                                ErrorMessage.Add(idsData[i], error);
                                rv = false;
                                break;
                            }
                        }
                    }
                    if (typeof(TModel).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;
                        }
                    }
                    DC.UpdateEntity(entity);
                }
                catch (Exception e)
                {
                    SetExceptionMessage(e, idsData[i]);
                    rv = false;
                }
            }
            //进行数据库的修改操作
            if (rv == true)
            {
                try
                {
                    DC.SaveChanges();
                }
                catch (Exception e)
                {
                    SetExceptionMessage(e, null);
                    rv = false;
                }
            }

            //如果有错误,输出错误信息
            if (rv == false)
            {
                if (ErrorMessage.Count > 0)
                {
                    foreach (var id in idsData)
                    {
                        if (!ErrorMessage.ContainsKey(id))
                        {
                            ErrorMessage.Add(id, "已回滚");
                        }
                    }
                }
                ListVM.DoSearch();
                foreach (var item in ListVM.GetEntityList())
                {
                    item.BatchError = ErrorMessage.Where(x => x.Key == item.ID).Select(x => x.Value).FirstOrDefault();
                }
            }
            return(rv);
        }