127 lines
3.5 KiB
C#
127 lines
3.5 KiB
C#
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; }
|
|
|
|
/// <summary>
|
|
/// 权限类型描述
|
|
/// </summary>
|
|
[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<FunctionDto> Children { get; set; }
|
|
}
|
|
|
|
public class FunctionHelper
|
|
{
|
|
/// <summary>
|
|
/// 操作权限
|
|
/// </summary>
|
|
public const string OperateType = "Operate";
|
|
|
|
static Dictionary<string, int> _FunctionDic;
|
|
|
|
static FunctionHelper()
|
|
{
|
|
Initialize();
|
|
}
|
|
|
|
public static void Initialize()
|
|
{
|
|
var dic = new Dictionary<string, int>();
|
|
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<FunctionHelper>().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<Function> AppendParent(List<Function> rawFunction, List<Function> 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;
|
|
}
|
|
}
|
|
}
|