Ejemplo n.º 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 <string> idsData = Ids.ToList();
            //找到对应的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>().CheckID(idsData[i]).FirstOrDefault();
                    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));
                            }
                        }
                    }

                    //调用controller方法验证model
                    try
                    {
                        Controller.GetType().GetMethod("RedoValidation").Invoke(Controller, new object[] { entity });
                    }
                    catch { }
                    //如果有对应的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.UpdateTime == null)
                        {
                            ent.UpdateTime = DateTime.Now;
                        }
                        if (string.IsNullOrEmpty(ent.UpdateBy))
                        {
                            ent.UpdateBy = 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.GetID().ToString()).Select(x => x.Value).FirstOrDefault();
                }
            }
            return(rv);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 验证Value 并生成错误信息(edit by dufei 2014-06-12,修改了当列设置为不验证时候,下拉列表获取不到值的问题)
        /// </summary>
        /// <param name="value"></param>
        /// <param name="errorMessage"></param>
        /// <param name="rowIndex"></param>
        public void ValueValidity(string value, List <ErrorMessage> errorMessage, int rowIndex)
        {
            if (this.IsNullAble && string.IsNullOrEmpty(value))
            {
                this.Value = value;
            }
            else
            {
                ErrorMessage err = null;
                switch (this.DataType)
                {
                case ColumnDataType.Date:
                case ColumnDataType.DateTime:
                    DateTime tryDateTimeResult;
                    if (!DateTime.TryParse(value, out tryDateTimeResult))
                    {
                        err = new ErrorMessage {
                            Index = rowIndex, Message = Program._localizer["{0}formaterror", this.ColumnName]
                        };
                        //errorMessage.Add(new ErrorMessage { ColumnName = this.ColumnName, Index = rowIndex, Message = "日期格式错误" });
                    }
                    this.Value = tryDateTimeResult;
                    break;

                case ColumnDataType.Number:
                    int tryIntResult;
                    if (!int.TryParse(value, out tryIntResult))
                    {
                        err = new ErrorMessage {
                            Index = rowIndex, Message = Program._localizer["{0}formaterror", this.ColumnName]
                        };
                        //errorMessage.Add(new ErrorMessage { ColumnName = this.ColumnName, Index = rowIndex, Message = "日期格式错误" });
                    }
                    this.Value = tryIntResult;
                    break;

                case ColumnDataType.Float:
                    decimal tryDecimalResult;
                    if (!decimal.TryParse(value, out tryDecimalResult))
                    {
                        err = new ErrorMessage {
                            Index = rowIndex, Message = Program._localizer["{0}formaterror", this.ColumnName]
                        };
                        //errorMessage.Add(new ErrorMessage { ColumnName = this.ColumnName, Index = rowIndex, Message = "日期格式错误" });
                    }
                    this.Value = tryDecimalResult;
                    break;

                case ColumnDataType.Bool:
                    if (value == "是")
                    {
                        this.Value = true;
                    }
                    else if (value == "否")
                    {
                        this.Value = false;
                    }
                    else
                    {
                        err = new ErrorMessage {
                            Index = rowIndex, Message = Program._localizer["{0}formaterror", this.ColumnName]
                        };
                    }
                    break;

                case ColumnDataType.Text:
                    this.Value = value;
                    break;

                case ColumnDataType.ComboBox:
                case ColumnDataType.Enum:
                    if (!this.ListItems.Any(x => x.Text == value))
                    {
                        err = new ErrorMessage {
                            Index = rowIndex, Message = Program._localizer["{0}ValueNotExist", this.ColumnName]
                        };
                    }
                    else
                    {
                        this.Value = this.ListItems.Where(x => x.Text == value).FirstOrDefault().Value;
                    }
                    break;

                default:
                    err = new ErrorMessage {
                        Index = rowIndex, Message = Program._localizer["{0}ValueTypeNotAllowed", this.ColumnName]
                    };
                    break;
                }

                if (err != null && this.SubTableType == null)
                {
                    errorMessage.Add(err);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 批量删除,默认对Ids中包含的主键的数据进行删除。子类如果有特殊判断应重载本函数
        /// </summary>
        /// <returns>true代表成功,false代表失败</returns>
        public virtual bool DoBatchDelete()
        {
            bool rv = true;
            //循环所有数据Id
            List <string> idsData = Ids.ToList();

            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>().CheckID(idsData[i]).FirstOrDefault();
                        (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.SetPropertyValue("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.GetID().ToString()).Select(x => x.Value).FirstOrDefault();
                    }
                }
                MSD.AddModelError("", "数据已被使用,无法删除");
            }
            return(rv);
        }
Ejemplo n.º 4
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 = Assembly.GetExecutingAssembly().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;
                    if (vm == null)
                    {
                        entity = DC.Set <TModel>().Find(idsData[i]);
                    }
                    else
                    {
                        vm.SetEntityById(idsData[i]);
                        entity = vm.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);
                        if (proToSet != null && FC["LinkedVM." + pro.Name] != null && FC["LinkedVM." + pro.Name] != StringValues.Empty)
                        {
                            proToSet.SetValue(entity, pro.GetValue(LinkedVM));
                        }
                    }
                    //如果有对应的BaseCRUDVM则使用其进行数据验证
                    if (vm != null)
                    {
                        var errors = (vm as IValidatableObject).Validate(null);
                        if (errors != null && errors.Count() > 0)
                        {
                            ErrorMessage.Add(idsData[i], errors.ToSpratedString(x => x.ErrorMessage));
                            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);
        }