Пример #1
0
        /// <summary>
        /// 获取数据权限的下拉菜单
        /// </summary>
        /// <param name="KnifeVirgo">wtm context</param>
        /// <param name="filter">filter</param>
        /// <returns>数据权限关联表的下拉菜单</returns>
        public List <ComboSelectListItem> GetItemList(VirgoContext KnifeVirgo, string filter = null, List <string> ids = null)
        {
            var user = KnifeVirgo?.LoginUserInfo;

            Expression <Func <T, bool> > where = null;

            if (ids != null)
            {
                where = ids.GetContainIdExpression <T>();
            }
            else
            {
                if (string.IsNullOrEmpty(filter) == false)
                {
                    ChangePara          cp = new ChangePara();
                    ParameterExpression pe = Expression.Parameter(typeof(T));
                    // var toString = Expression.Call(cp.Change(_displayField.Body, pe), "ToString", new Type[] { });
                    var tolower = Expression.Call(cp.Change(_displayField.Body, pe), "ToLower", new Type[] { });
                    var exp     = Expression.Call(tolower, "Contains", null, Expression.Constant(filter.ToLower()));
                    where = Expression.Lambda <Func <T, bool> >(exp, pe);
                    if (_where != null)
                    {
                        var temp     = cp.Change(_where.Body, pe);
                        var together = Expression.And(where.Body, temp);
                        where = Expression.Lambda <Func <T, bool> >(together, pe);
                    }
                }
                else
                {
                    where = _where;
                }
                if (where == null)
                {
                    where = x => 1 == 1;
                }
            }
            List <ComboSelectListItem> rv = new List <ComboSelectListItem>();

            if (user.Roles?.Where(x => x.RoleCode == "001").FirstOrDefault() == null && user.DataPrivileges?.Where(x => x.RelateId == null).FirstOrDefault() == null)
            {
                rv = KnifeVirgo.DC.Set <T>().Where(x => user.DataPrivileges.Select(y => y.RelateId).Contains(x.ID.ToString())).Where(where).GetSelectListItems(null, _displayField, null, ignorDataPrivilege: true);
            }
            else
            {
                rv = KnifeVirgo.DC.Set <T>().Where(where).GetSelectListItems(null, _displayField, null, ignorDataPrivilege: true);
            }
            return(rv);
        }
Пример #2
0
        /// <summary>
        /// 查询数据源,并转化成TreeSelectListItem列表
        /// </summary>
        /// <typeparam name="T">数据源类型</typeparam>
        /// <param name="baseQuery">基础查询</param>
        /// <param name="dps">数据权限</param>
        /// <param name="whereCondition">条件语句</param>
        /// <param name="textField">表达式用来获取Text字段对应的值</param>
        /// <param name="valueField">表达式用来获取Value字段对应的值,不指定则默认使用Id字段</param>
        /// <param name="iconField">表达式用来获取icon字段对应的值</param>
        /// <param name="urlField">表达式用来获取Url字段对应的值</param>
        /// <param name="tagField">表达式用来获取Tag字段对应的值</param>
        /// <param name="expandField">表达式用来获取Expanded字段对应的值,指示节点是否展开</param>
        /// <param name="ignorDataPrivilege">忽略数据权限判断</param>
        /// <param name="SortByName">是否根据Text字段排序,默认为是</param>
        /// <returns>SelectListItem列表</returns>
        public static List <TreeSelectListItem> GetTreeSelectListItems <T>(this IQueryable <T> baseQuery
                                                                           , List <DataPrivilege> dps
                                                                           , Expression <Func <T, bool> > whereCondition
                                                                           , Expression <Func <T, string> > textField
                                                                           , Expression <Func <T, string> > valueField = null
                                                                           , Expression <Func <T, string> > iconField  = null
                                                                           , Expression <Func <T, string> > urlField   = null
                                                                           , Expression <Func <T, string> > tagField   = null
                                                                           , Expression <Func <T, bool> > expandField  = null
                                                                           , bool ignorDataPrivilege = false
                                                                           , bool SortByName         = true)
            where T : TopBasePoco, ITreeData <T>
        {
            var query = baseQuery;

            //如果条件不为空,则拼上条件
            if (whereCondition != null)
            {
                query = query.Where(whereCondition);
            }
            //如果没有指定忽略权限,则拼接权限过滤的where条件
            if (ignorDataPrivilege == false)
            {
                query = AppendSelfDPWhere(query, dps);
            }

            //处理后面要使用的expression
            if (valueField == null)
            {
                valueField = x => x.ID.ToString();
            }
            Expression <Func <T, string> > parentField = x => x.ParentId.ToString();

            //定义PE
            ParameterExpression pe = Expression.Parameter(typeof(T));
            ChangePara          cp = new ChangePara();

            //创建新类,形成类似 new SimpleTreeTextAndValue() 的表达式
            NewExpression newItem = Expression.New(typeof(TreeSelectListItem));

            //绑定Text字段,形成类似 Text = textField 的表达式
            var           textMI   = typeof(TreeSelectListItem).GetMember("Text")[0];
            MemberBinding textBind = Expression.Bind(textMI, cp.Change(textField.Body, pe));

            //绑定Value字段,形成类似 Value = valueField 的表达式
            var           valueMI   = typeof(TreeSelectListItem).GetMember("Id")[0];
            MemberBinding valueBind = Expression.Bind(valueMI, cp.Change(valueField.Body, pe));

            //绑定ParentId字段,形成类似 Value = valueField 的表达式
            var           parentMI   = typeof(TreeSelectListItem).GetMember("ParentId")[0];
            MemberBinding parentBind = Expression.Bind(parentMI, cp.Change(parentField.Body, pe));

            //绑定Url字段,形成类似 Value = valueField 的表达式
            MemberBinding urlBind = null;
            var           urlMI   = typeof(TreeSelectListItem).GetMember("Url")[0];

            if (urlField != null)
            {
                urlBind = Expression.Bind(urlMI, cp.Change(urlField.Body, pe));
            }
            else
            {
                urlBind = Expression.Bind(urlMI, Expression.Constant(string.Empty));
            }

            //绑定icon字段,形成类似 ICon = iconField 的表达式
            MemberBinding iconBind = null;
            var           iconMI   = typeof(TreeSelectListItem).GetMember("ICon")[0];

            if (iconField != null)
            {
                iconBind = Expression.Bind(iconMI, cp.Change(iconField.Body, pe));
            }
            else
            {
                iconBind = Expression.Bind(iconMI, Expression.Constant(string.Empty));
            }

            //绑定Tag字段,形成类似 Value = valueField 的表达式
            MemberBinding tagBind = null;
            var           tagMI   = typeof(TreeSelectListItem).GetMember("Tag")[0];

            if (tagField != null)
            {
                tagBind = Expression.Bind(tagMI, cp.Change(tagField.Body, pe));
            }
            else
            {
                tagBind = Expression.Bind(tagMI, Expression.Constant(""));
            }

            //绑定Tag字段,形成类似 Value = valueField 的表达式
            MemberBinding expandBind = null;
            var           expandMI   = typeof(TreeSelectListItem).GetMember("Expended")[0];

            if (expandField != null)
            {
                expandBind = Expression.Bind(expandMI, cp.Change(expandField.Body, pe));
            }
            else
            {
                expandBind = Expression.Bind(expandMI, Expression.Constant(false));
            }

            //合并创建新类和绑定字段的表达式,形成类似 new SimpleTextAndValue{ Text = textField, Value = valueField} 的表达式
            MemberInitExpression init = Expression.MemberInit(newItem, textBind, valueBind, iconBind, parentBind, urlBind, tagBind, expandBind);

            //将最终形成的表达式转化为Lambda,形成类似 x=> new SimpleTextAndValue { Text = x.textField, Value = x.valueField} 的表达式
            var lambda = Expression.Lambda <Func <T, TreeSelectListItem> >(init, pe);

            List <TreeSelectListItem> rv = null;

            //根据Text对下拉菜单数据排序
            if (SortByName == true)
            {
                rv = query.Select(lambda).OrderBy(x => x.Text).ToList();
            }
            else
            {
                rv = query.Select(lambda).ToList();
            }

            rv.ForEach(x =>
            {
                x.Children = rv.Where(y => y.ParentId == x.Id).ToList();
            });
            return(rv.Where(x => string.IsNullOrEmpty(x.ParentId)).ToList());
        }
Пример #3
0
        /// <summary>
        /// 查询数据源,并转化成SelectListItem列表
        /// </summary>
        /// <typeparam name="T">数据源类型</typeparam>
        /// <param name="baseQuery">基础查询</param>
        /// <param name="dps">数据权限</param>
        /// <param name="whereCondition">条件语句</param>
        /// <param name="textField">SelectListItem中Text字段对应的值</param>
        /// <param name="valueField">SelectListItem中Value字段对应的值,默认为Id列</param>
        /// <param name="ignorDataPrivilege">忽略数据权限判断</param>
        /// <param name="SortByName">是否根据Text字段排序,默认为是</param>
        /// <returns>SelectListItem列表</returns>
        public static List <ComboSelectListItem> GetSelectListItems <T>(this IQueryable <T> baseQuery
                                                                        , List <DataPrivilege> dps
                                                                        , Expression <Func <T, bool> > whereCondition
                                                                        , Expression <Func <T, string> > textField
                                                                        , Expression <Func <T, string> > valueField = null
                                                                        , bool ignorDataPrivilege = false
                                                                        , bool SortByName         = true)
            where T : TopBasePoco
        {
            var query = baseQuery;

            //如果条件不为空,则拼上条件
            if (whereCondition != null)
            {
                query = query.Where(whereCondition);
            }
            //如果value字段为空,则默认使用Id字段作为value值
            if (valueField == null)
            {
                valueField = x => x.GetID().ToString().ToLower();
            }

            //如果没有指定忽略权限,则拼接权限过滤的where条件
            if (ignorDataPrivilege == false || dps != null)
            {
                query = AppendSelfDPWhere(query, dps);
            }

            if (typeof(T).IsSubclassOf(typeof(PersistPoco)))
            {
                var mod    = new IsValidModifier();
                var newExp = mod.Modify(query.Expression);
                query = query.Provider.CreateQuery <T>(newExp) as IOrderedQueryable <T>;
            }


            //定义PE
            ParameterExpression pe = Expression.Parameter(typeof(T));
            ChangePara          cp = new ChangePara();
            //创建新类,形成类似 new SimpleTextAndValue() 的表达式
            NewExpression newItem = Expression.New(typeof(ComboSelectListItem));

            //绑定Text字段,形成类似 Text = textField 的表达式
            var           textMI   = typeof(ComboSelectListItem).GetMember("Text")[0];
            MemberBinding textBind = Expression.Bind(textMI, cp.Change(textField.Body, pe));


            //绑定Value字段,形成类似 Value = valueField 的表达式
            var           valueMI   = typeof(ComboSelectListItem).GetMember("Value")[0];
            MemberBinding valueBind = Expression.Bind(valueMI, cp.Change(valueField.Body, pe));

            //如果是树形结构,给ParentId赋值
            MemberBinding parentBind = null;
            var           parentMI   = typeof(ComboSelectListItem).GetMember("ParentId")[0];

            if (typeof(ITreeData <T>).IsAssignableFrom(typeof(T)))
            {
                var parentMember = Expression.MakeMemberAccess(pe, typeof(ITreeData).GetProperty("ParentId"));
                var p            = Expression.Call(parentMember, "ToString", new Type[] { });
                var p1           = Expression.Call(p, "ToLower", new Type[] { });
                parentBind = Expression.Bind(parentMI, p1);
            }
            else
            {
                parentBind = Expression.Bind(parentMI, Expression.Constant(string.Empty));
            }

            //合并创建新类和绑定字段的表达式,形成类似 new SimpleTextAndValue{ Text = textField, Value = valueField} 的表达式
            MemberInitExpression init = Expression.MemberInit(newItem, textBind, valueBind, parentBind);

            //将最终形成的表达式转化为Lambda,形成类似 x=> new SimpleTextAndValue { Text = x.textField, Value = x.valueField} 的表达式
            var lambda = Expression.Lambda <Func <T, ComboSelectListItem> >(init, pe);


            List <ComboSelectListItem> rv = new List <ComboSelectListItem>();

            //根据Text对下拉菜单数据排序
            if (SortByName == true)
            {
                rv = query.Select(lambda).OrderBy(x => x.Text).ToList();
            }
            else
            {
                rv = query.Select(lambda).ToList();
            }

            return(rv);
        }
Пример #4
0
        /// <summary>
        /// 查询数据源,并转化成SelectListItem列表
        /// </summary>
        /// <typeparam name="T">数据源类型</typeparam>
        /// <param name="baseQuery">基础查询</param>
        /// <param name="dps">数据权限</param>
        /// <param name="whereCondition">条件语句</param>
        /// <param name="textField">SelectListItem中Text字段对应的值</param>
        /// <param name="valueField">SelectListItem中Value字段对应的值,默认为Id列</param>
        /// <param name="ignorDataPrivilege">忽略数据权限判断</param>
        /// <param name="SortByName">是否根据Text字段排序,默认为是</param>
        /// <returns>SelectListItem列表</returns>
        public static List <ComboSelectListItem> GetSelectListItems <T>(this IQueryable <T> baseQuery
                                                                        , List <DataPrivilege> dps
                                                                        , Expression <Func <T, bool> > whereCondition
                                                                        , Expression <Func <T, string> > textField
                                                                        , Expression <Func <T, string> > valueField = null
                                                                        , bool ignorDataPrivilege = false
                                                                        , bool SortByName         = true)
            where T : TopBasePoco
        {
            var query = baseQuery;

            //如果条件不为空,则拼上条件
            if (whereCondition != null)
            {
                query = query.Where(whereCondition);
            }
            //如果value字段为空,则默认使用Id字段作为value值
            if (valueField == null)
            {
                valueField = x => x.ID.ToString();
            }
            //如果没有指定忽略权限,则拼接权限过滤的where条件
            if (ignorDataPrivilege == false)
            {
                query = AppendSelfDPWhere(query, dps);
            }

            //定义PE
            ParameterExpression pe = Expression.Parameter(typeof(T));
            ChangePara          cp = new ChangePara();
            //创建新类,形成类似 new SimpleTextAndValue() 的表达式
            NewExpression newItem = Expression.New(typeof(ComboSelectListItem));

            //绑定Text字段,形成类似 Text = textField 的表达式
            var           textMI   = typeof(ComboSelectListItem).GetMember("Text")[0];
            MemberBinding textBind = Expression.Bind(textMI, cp.Change(textField.Body, pe));


            //绑定Value字段,形成类似 Value = valueField 的表达式
            var           valueMI   = typeof(ComboSelectListItem).GetMember("Value")[0];
            MemberBinding valueBind = Expression.Bind(valueMI, cp.Change(valueField.Body, pe));


            //合并创建新类和绑定字段的表达式,形成类似 new SimpleTextAndValue{ Text = textField, Value = valueField} 的表达式
            MemberInitExpression init = Expression.MemberInit(newItem, textBind, valueBind);

            //将最终形成的表达式转化为Lambda,形成类似 x=> new SimpleTextAndValue { Text = x.textField, Value = x.valueField} 的表达式
            var lambda = Expression.Lambda <Func <T, ComboSelectListItem> >(init, pe);


            List <ComboSelectListItem> rv = new List <ComboSelectListItem>();

            //根据Text对下拉菜单数据排序
            if (SortByName == true)
            {
                rv = query.Select(lambda).OrderBy(x => x.Text).ToList();
            }
            else
            {
                rv = query.Select(lambda).ToList();
            }

            return(rv);
        }
Пример #5
0
        /// <summary>
        /// 查询数据源,并转化成TreeSelectListItem列表
        /// </summary>
        /// <typeparam name="T">数据源类型</typeparam>
        /// <param name="baseQuery">基础查询</param>
        /// <param name="knifeVirgo">context</param>
        /// <param name="whereCondition">条件语句</param>
        /// <param name="textField">表达式用来获取Text字段对应的值</param>
        /// <param name="valueField">表达式用来获取Value字段对应的值,不指定则默认使用Id字段</param>
        /// <param name="iconField">表达式用来获取icon字段对应的值</param>
        /// <param name="urlField">表达式用来获取Url字段对应的值</param>
        /// <param name="tagField">表达式用来获取Tag字段对应的值</param>
        /// <param name="expandField">表达式用来获取Expanded字段对应的值,指示节点是否展开</param>
        /// <param name="ignorDataPrivilege">忽略数据权限判断</param>
        /// <param name="SortByName">是否根据Text字段排序,默认为是</param>
        /// <returns>SelectListItem列表</returns>
        public static List <TreeSelectListItem> GetTreeSelectListItems <T>(this IQueryable <T> baseQuery
                                                                           , VirgoContext knifeVirgo
                                                                           , Expression <Func <T, string> > textField
                                                                           , Expression <Func <T, string> > valueField = null
                                                                           , Expression <Func <T, string> > iconField  = null
                                                                           , Expression <Func <T, string> > urlField   = null
                                                                           , Expression <Func <T, string> > tagField   = null
                                                                           , Expression <Func <T, bool> > expandField  = null
                                                                           , bool ignorDataPrivilege = false
                                                                           , bool SortByName         = true)
            where T : TreePoco
        {
            var dps   = knifeVirgo?.LoginUserInfo?.DataPrivileges;
            var query = baseQuery.AsNoTracking();

            //如果没有指定忽略权限,则拼接权限过滤的where条件
            if (ignorDataPrivilege == false)
            {
                query = AppendSelfDPWhere(query, knifeVirgo, dps);
            }
            if (typeof(IPersistPoco).IsAssignableFrom(typeof(T)))
            {
                var mod    = new IsValidModifier();
                var newExp = mod.Modify(query.Expression);
                query = query.Provider.CreateQuery <T>(newExp) as IOrderedQueryable <T>;
            }

            //处理后面要使用的expression
            //if (valueField == null)
            //{
            valueField = x => x.GetID().ToString().ToLower();
            //}
            Expression <Func <T, string> > parentField = x => x.ParentId.ToString().ToLower();

            //定义PE
            ParameterExpression pe = Expression.Parameter(typeof(T));
            ChangePara          cp = new ChangePara();

            //创建新类,形成类似 new SimpleTreeTextAndValue() 的表达式
            NewExpression newItem = Expression.New(typeof(TreeSelectListItem));

            //绑定Text字段,形成类似 Text = textField 的表达式
            var           textMI   = typeof(TreeSelectListItem).GetMember("Text")[0];
            MemberBinding textBind = Expression.Bind(textMI, cp.Change(textField.Body, pe));

            //绑定Value字段,形成类似 Value = valueField 的表达式
            var           valueMI   = typeof(TreeSelectListItem).GetMember("Id")[0];
            MemberBinding valueBind = Expression.Bind(valueMI, cp.Change(valueField.Body, pe));

            //绑定ParentId字段,形成类似 Value = valueField 的表达式
            var           parentMI   = typeof(TreeSelectListItem).GetMember("ParentId")[0];
            MemberBinding parentBind = Expression.Bind(parentMI, cp.Change(parentField.Body, pe));

            //绑定Url字段,形成类似 Value = valueField 的表达式
            MemberBinding urlBind = null;
            var           urlMI   = typeof(TreeSelectListItem).GetMember("Url")[0];

            if (urlField != null)
            {
                urlBind = Expression.Bind(urlMI, cp.Change(urlField.Body, pe));
            }
            else
            {
                urlBind = Expression.Bind(urlMI, Expression.Constant(string.Empty));
            }

            //绑定icon字段,形成类似 ICon = iconField 的表达式
            MemberBinding iconBind = null;
            var           iconMI   = typeof(TreeSelectListItem).GetMember("ICon")[0];

            if (iconField != null)
            {
                iconBind = Expression.Bind(iconMI, cp.Change(iconField.Body, pe));
            }
            else
            {
                iconBind = Expression.Bind(iconMI, Expression.Constant(string.Empty));
            }

            //绑定Tag字段,形成类似 Value = valueField 的表达式
            MemberBinding tagBind = null;
            var           tagMI   = typeof(TreeSelectListItem).GetMember("Tag")[0];

            if (tagField != null)
            {
                tagBind = Expression.Bind(tagMI, cp.Change(tagField.Body, pe));
            }
            else
            {
                tagBind = Expression.Bind(tagMI, Expression.Constant(""));
            }

            //绑定Tag字段,形成类似 Value = valueField 的表达式
            MemberBinding expandBind = null;
            var           expandMI   = typeof(TreeSelectListItem).GetMember("Expended")[0];

            if (expandField != null)
            {
                expandBind = Expression.Bind(expandMI, cp.Change(expandField.Body, pe));
            }
            else
            {
                expandBind = Expression.Bind(expandMI, Expression.Constant(false));
            }

            //合并创建新类和绑定字段的表达式,形成类似 new SimpleTextAndValue{ Text = textField, Value = valueField} 的表达式
            MemberInitExpression init = Expression.MemberInit(newItem, textBind, valueBind, iconBind, parentBind, urlBind, tagBind, expandBind);

            //将最终形成的表达式转化为Lambda,形成类似 x=> new SimpleTextAndValue { Text = x.textField, Value = x.valueField} 的表达式
            var lambda = Expression.Lambda <Func <T, TreeSelectListItem> >(init, pe);

            List <TreeSelectListItem> rv = null;

            //根据Text对下拉菜单数据排序
            if (SortByName == true)
            {
                rv = query.Select(lambda).OrderBy(x => x.Text).ToList();
            }
            else
            {
                rv = query.Select(lambda).ToList();
            }

            rv.ForEach(x =>
            {
                x.Children = rv.Where(y => y.ParentId == x.Id).ToList();
            });
            return(rv.Where(x => string.IsNullOrEmpty(x.ParentId)).ToList());
        }