/// <summary> /// 获取权限列表 /// </summary> /// <returns></returns> public virtual IEnumerable <string> GetPrivileges() { // 注册查看权限 foreach (var privilege in ViewPrivileges) { yield return(privilege); } // 允许添加或编辑数据时注册编辑权限 if (!string.IsNullOrEmpty(AddUrl) || !string.IsNullOrEmpty(EditUrl)) { foreach (var privilege in EditPrivileges) { yield return(privilege); } } // 允许批量操作数据时注册删除权限 if (!string.IsNullOrEmpty(BatchUrl)) { // 可回收时注册删除权限 if (RecyclableTrait.For <TData>().IsRecyclable) { foreach (var privilege in DeletePrivileges) { yield return(privilege); } } // 注册永久删除权限 foreach (var privilege in DeleteForeverPrivilege) { yield return(privilege); } } }
/// <summary> /// 按回收站状态过滤数据 /// </summary> /// <typeparam name="TData">数据类型</typeparam> /// <param name="query">查询对象</param> /// <param name="request">搜索请求</param> public static IQueryable <TData> FilterByRecycleBin <TData>( this IQueryable <TData> query, AjaxTableSearchRequest request) { var deleted = request.Conditions.GetOrDefault <bool>("Deleted"); var propertyName = RecyclableTrait.For <TData>().PropertyName; return(query.Where( ExpressionUtils.MakeMemberEqualiventExpression <TData>(propertyName, deleted))); }
/// <summary> /// 初始化 /// </summary> public CrudPageBuilder() { IncludeCss = new List <string>(); IncludeJs = new List <string>(); ExtraTemplateArguments = new Dictionary <string, object>(); BatchActions = new Dictionary <string, Func <IActionResult> >(); BatchActions["delete_forever"] = BatchActionForDeleteForever; if (RecyclableTrait.For <TData>().IsRecyclable) { BatchActions["delete"] = BatchActionForDelete; BatchActions["recover"] = BatchActionForRecover; } }
/// <summary> /// 获取选项列表 /// </summary> /// <returns></returns> public IEnumerable <ListItem> GetItems() { var databaseManager = Application.Ioc.Resolve <DatabaseManager>(); var propertyName = RecyclableTrait.For <TData>().PropertyName; var expression = ExpressionUtils.MakeMemberEqualiventExpression <TData>(propertyName, false); using (var context = databaseManager.CreateContext()) { foreach (var data in context.Query <TData>().Where(expression)) { yield return(new ListItem(data.ToString(), EntityTrait.GetPrimaryKey(data).ToString())); } } }
/// <summary> /// 获取指定Id的单个对象,找不到或标记已删除时返回null /// </summary> /// <param name="id">数据Id</param> /// <returns></returns> public virtual TData GetByIdWhereNotDeleted(object id) { // 判断类型是否可回收 var entityTrait = EntityTrait.For <TData>(); var recyclableTrait = RecyclableTrait.For <TData>(); if (!recyclableTrait.IsRecyclable) { throw new ArgumentException(string.Format("entity {0} not recyclable", typeof(TData).Name)); } // 构建表达式 (data => data.Id == id && !data.Deleted) var dataParam = Expression.Parameter(typeof(TData), "data"); var body = Expression.AndAlso( Expression.Equal( Expression.Property(dataParam, entityTrait.PrimaryKey), Expression.Constant(id.ConvertOrDefault(entityTrait.PrimaryKeyType, null))), Expression.Not(Expression.Property(dataParam, recyclableTrait.PropertyName))); return(Get(Expression.Lambda <Func <TData, bool> >(body, dataParam))); }
/// <summary> /// 对增删查改页面使用的搜索栏构建器进行标准的设置 /// 添加以下菜单项 /// - 回收站(如果数据支持回收) /// - 添加菜单(如果添加Url不是空) /// 添加以下按钮 /// - 添加按钮(如果添加Url不是空) /// </summary> /// <typeparam name="TBuilder">后台应用类型</typeparam> /// <param name="searchBar">搜索栏构建器</param> /// <param name="keywordPlaceHolder">关键词的预留文本,传入后会自动翻译,可传入原文</param> public static void StandardSetupForCrudPage <TBuilder>( this AjaxTableSearchBarBuilder searchBar, string keywordPlaceHolder) where TBuilder : class, ICrudPageBuilder, new() { var app = new TBuilder(); searchBar.KeywordPlaceHolder = new T(keywordPlaceHolder); var addDividerOnce = new Lazy <Action>( () => { searchBar.MenuItems.AddDivider(); return(() => { }); }); if (RecyclableTrait.For(app.DataType).IsRecyclable) { addDividerOnce.Value(); searchBar.MenuItems.AddRecycleBin(); } if (!string.IsNullOrEmpty(app.AddUrl)) { addDividerOnce.Value(); searchBar.MenuItems.AddAddActionForCrudPage <TBuilder>(); searchBar.BeforeItems.AddAddActionForCrudPage <TBuilder>(); } }
/// <summary> /// 批量恢复 /// 返回恢复的数量 /// </summary> /// <param name="ids">Id列表</param> /// <returns></returns> public virtual long BatchRecover(IEnumerable <object> ids) { var databaseManager = Application.Ioc.Resolve <DatabaseManager>(); var propertyName = RecyclableTrait.For <TData>().PropertyName; var setter = ReflectionUtils.MakeSetter <TData, bool>(propertyName); var trait = EntityTrait.For <TData>(); long count = 0; foreach (var id in ids) { var data = Context.Get( ExpressionUtils.MakeMemberEqualiventExpression <TData>( trait.PrimaryKey, id.ConvertOrDefault(trait.PrimaryKeyType, null))); if (data != null) { Context.Save(ref data, d => setter(d, false)); ++count; } } return(count); }
/// <summary> /// 添加删除相关的按钮 /// 如果数据类型可以回收,则添加批量删除或批量恢复和永久删除 /// 如果数据类型不可以回收,则添加批量永久删除 /// </summary> /// <param name="column">Id列</param> /// <param name="request">搜索请求</param> /// <param name="dataType">数据类型</param> /// <param name="typeName">类型名称</param> /// <param name="batchUrl">批量操作使用的Url</param> public static void AddDeleteActions( this AjaxTableIdColumn column, AjaxTableSearchRequest request, Type dataType, string typeName, string batchUrl) { // 判断需要添加哪些操作 bool addBatchDelete = false; bool addBatchRecover = false; bool addBatchDeleteForever = false; if (RecyclableTrait.For(dataType).IsRecyclable) { var deleted = request.Conditions.GetOrDefault <bool>("Deleted"); addBatchDelete = !deleted; addBatchRecover = deleted; addBatchDeleteForever = deleted; } else { addBatchDeleteForever = true; } // 添加批量删除 typeName = new T(typeName); var entityTrait = EntityTrait.For(dataType); if (addBatchDelete) { column.AddConfirmActionForMultiChecked( new T("Batch Delete"), "fa fa-recycle", string.Format(new T("Please select {0} to delete"), typeName), new T("Batch Delete"), ScriptStrings.ConfirmMessageTemplateForMultiSelected( string.Format(new T("Sure to delete following {0}?"), typeName), "ToString"), ScriptStrings.PostConfirmedActionForMultiSelected( entityTrait.PrimaryKey, batchUrl + "?action=delete"), new { type = "type-danger" }); } // 添加批量恢复 if (addBatchRecover) { column.AddConfirmActionForMultiChecked( new T("Batch Recover"), "fa fa-history", string.Format(new T("Please select {0} to recover"), typeName), new T("Batch Recover"), ScriptStrings.ConfirmMessageTemplateForMultiSelected( string.Format(new T("Sure to recover following {0}?"), typeName), "ToString"), ScriptStrings.PostConfirmedActionForMultiSelected( entityTrait.PrimaryKey, batchUrl + "?action=recover")); } // 添加批量永久删除 if (addBatchDeleteForever) { column.AddConfirmActionForMultiChecked( new T("Batch Delete Forever"), "fa fa-remove", string.Format(new T("Please select {0} to delete"), typeName), new T("Batch Delete Forever"), ScriptStrings.ConfirmMessageTemplateForMultiSelected( string.Format(new T("Sure to delete following {0} forever?"), typeName), "ToString"), ScriptStrings.PostConfirmedActionForMultiSelected( entityTrait.PrimaryKey, batchUrl + "?action=delete_forever"), new { type = "type-danger" }); } }