using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace BaseOUDAL
{
public class Function
{
public int Id { get; set; }
[DisplayName("显示名字")]
public string Title { get; set; }
///
/// 权限类型描述
///
[DisplayName("权限类型")]
public string Type { get; set; }
[DisplayName("说明")]
public string Note { get; set; }
public int Sort { get; set; }
[Required]
[MaxLength(50)]
[DisplayName("父级名称")]
public string ParentName { get; set; }
[Required]
[MaxLength(50)]
[DisplayName("名称")]
public string Name { get; set; }
public Function Clone()
{
return (Function)MemberwiseClone();
}
public override string ToString()
{
return $"{ParentName}-{Name}";
}
}
[NotMapped]
public class FunctionDto : Function
{
public IEnumerable Children { get; set; }
}
public class FunctionHelper
{
///
/// 操作权限
///
public const string OperateType = "Operate";
static Dictionary _FunctionDic;
static FunctionHelper()
{
Initialize();
}
public static void Initialize()
{
var dic = new Dictionary();
using (var db = new ErpBaseContext())
{
var query = (from o in db.Functions.AsNoTracking() select o).ToList();
try
{
foreach (Function f in query)
{
if (f.ParentName != "-")
{
f.Name = string.Format("{0}-{1}", f.ParentName, f.Name);//目前只支持2级权限
}
if (!dic.Keys.Contains(f.Name))
{
dic.Add(f.Name, f.Id);
}
}
}
catch (Exception e)
{
LogFactory.GetLogger().Error("Function Init", e);
throw;
}
}
_FunctionDic = dic;
}
public static bool TryGetFunctionId(string functionName, out int functionId)
{
functionId = 0;
if (string.IsNullOrEmpty(functionName))
{
return false;
}
return _FunctionDic.TryGetValue(functionName, out functionId);
}
public static List AppendParent(List rawFunction, List targetFunction)
{
if (targetFunction != null)
{
for (int i = 0; i < targetFunction.Count; i++)
{
var func = rawFunction.FirstOrDefault(f => f.Name == targetFunction[i].ParentName);
if (func != null)
{
if (!targetFunction.Any(c => c.Name == func.Name))
{
Function copyFunc = func.Clone();
copyFunc.Type = targetFunction[i].Type;
targetFunction.Add(copyFunc);
}
}
}
}
return targetFunction;
}
}
}