Exemple #1
0
        /// <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);
        }
Exemple #2
0
        /// <summary>
        /// 批量删除,默认对Ids中包含的主键的数据进行删除。子类如果有特殊判断应重载本函数
        /// </summary>
        /// <returns>true代表成功,false代表失败</returns>
        public virtual bool DoBatchDelete()
        {
            bool rv = true;
            //循环所有数据Id
            List <Guid> idsData = new List <Guid>(Ids);

            for (int i = 0; i < idsData.Count; i++)
            {
                string checkErro = null;
                //检查是否可以删除,如不能删除则直接跳过
                if (CheckIfCanDelete(idsData[i], out checkErro) == false)
                {
                    ErrorMessage.Add(idsData[i], checkErro);
                    rv = false;
                    break;
                }
                //进行删除
                try
                {
                    var ctor = typeof(TModel).GetConstructor(Type.EmptyTypes);
                    if (typeof(TModel).IsSubclassOf(typeof(PersistPoco)))
                    {
                        var pp = DC.Set <TModel>().Find(idsData[i]);
                        (pp as PersistPoco).IsValid    = false;
                        (pp as PersistPoco).UpdateTime = DateTime.Now;
                        (pp as PersistPoco).UpdateBy   = LoginUserInfo.ITCode;
                        DC.UpdateProperty(pp, "IsValid");
                        DC.UpdateProperty(pp, "UpdateTime");
                        DC.UpdateProperty(pp, "UpdateBy");
                    }
                    else
                    {
                        TModel m = ctor.Invoke(null) as TModel;
                        m.ID = idsData[i];
                        DC.Set <TModel>().Attach(m);
                        DC.DeleteEntity(m);
                    }
                }
                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();
                if (ListVM != null)
                {
                    foreach (var item in ListVM?.GetEntityList())
                    {
                        item.BatchError = ErrorMessage.Where(x => x.Key == item.ID).Select(x => x.Value).FirstOrDefault();
                    }
                }
                MSD.AddModelError("", "数据已被使用,无法删除");
            }
            return(rv);
        }