using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using WalkingTec.Mvvm.Core.Extensions;
namespace WalkingTec.Mvvm.Core
{
///
/// 数据权限信息接口
///
public interface IDataPrivilege
{
//数据权限关联的表名
string ModelName { get; set; }
//数据权限名称
string PrivillegeName { get; set; }
Type ModelType { get; set; }
//获取数据权限的下拉菜单
List GetItemList (WTMContext wtmcontext, string filter = null, List ids = null);
List GetTreeParentIds(WTMContext wtmcontext, List dps);
List GetTreeSubIds(WTMContext wtmcontext, List pids);
}
///
/// 数据权限信息
///
/// 关联表类
public class DataPrivilegeInfo : IDataPrivilege where T : TopBasePoco
{
//数据权限关联的表名
public string ModelName { get; set; }
//数据权限名称
public string PrivillegeName { get; set; }
//显示字段
private Expression> _displayField;
//where过滤条件
private Expression> _where;
public Type ModelType { get; set; }
public DataPrivilegeInfo(string name, Expression> displayField, Expression> where = null)
{
ModelType = typeof(T);
ModelName = ModelType.Name;
PrivillegeName = name;
_displayField = displayField;
_where = where;
}
///
/// 获取数据权限的下拉菜单
///
/// wtm context
/// filter
/// ids
/// 数据权限关联表的下拉菜单
public List GetItemList(WTMContext wtmcontext, string filter = null,List ids= null)
{
var user = wtmcontext?.LoginUserInfo;
Expression> where = null;
if (ids != null)
{
where = ids.GetContainIdExpression();
}
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), "Cast", new Type[] { typeof(string)});
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>(exp, pe);
if (_where != null)
{
var temp = cp.Change(_where.Body, pe);
var together = Expression.And(where.Body, temp);
where = Expression.Lambda>(together, pe);
}
}
else
{
where = _where;
}
if (where == null)
{
where = x => 1 == 1;
}
}
List rv = new List();
if (user.Roles?.Where(x => x.RoleCode == "001").FirstOrDefault() == null && user.DataPrivileges?.Where(x => x.RelateId == null).FirstOrDefault() == null)
{
rv = wtmcontext.DC.Set().CheckIDs(user.DataPrivileges.Select(y => y.RelateId).ToList()).Where(where).GetSelectListItems(wtmcontext, _displayField, null, ignorDataPrivilege: true);
}
else
{
rv = wtmcontext.DC.Set().Where(where).GetSelectListItems(wtmcontext, _displayField, null, ignorDataPrivilege: true);
}
return rv;
}
public List GetTreeParentIds(WTMContext wtmcontext, List dps)
{
var ids = dps.Where(x => x.TableName == this.ModelName).Select(x => x.RelateId).ToList();
var idscheck = ids.GetContainIdExpression();
var modified = wtmcontext.DC.Set().Where(idscheck).CheckNotNull("ParentId").DynamicSelect("ParentId");
var skipids = modified.ToList();
return skipids;
}
public List GetTreeSubIds(WTMContext wtmcontext, List pids)
{
ParameterExpression pe = Expression.Parameter(typeof(T));
Expression parentid = Expression.Property(pe, typeof(T).GetSingleProperty("ParentId"));
return wtmcontext.DC.Set().Where(pids.GetContainIdExpression(parentid)).DynamicSelect("ID").ToList();
}
}
}