123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- using CZFW.Core;
- using CZFW.Framework.Interface;
- using CZFW.Framework.Model;
- using CZFW.Framework.Model.Entity;
- using CZFW.Framework.Model.ViewModel;
- using Microsoft.AspNetCore.Mvc;
- using System.Collections.Generic;
- using System.Linq;
- namespace CZFW.Framework.Logic
- {
- public class PrivilegeGroupLogic : LogicBase<PrivilegeGroupEntity>, IPrivilegeGroup
- {
- public ResultModel AddEntity(PrivilegeGroupEntity entity)
- {
- entity.Mark = Utility.GenerateTreeMark(entity.ParentMark);
- return base.AddEntity(entity);
- }
- IPrivilege _privilegeLogic;// = new PrivilegeLogic();
- public PrivilegeGroupLogic(CZDbContext db, IPrivilege privilegeLogic)
- {
- _privilegeLogic = privilegeLogic;
- }
-
- public IList<PrivilegeModel> GetAllPrivilegeInGroup(string mark)
- {
- var res = _privilegeLogic.GetSortedQueryable()
- .Where(x => x.GroupMark.Contains(mark))
- .Select(x => new PrivilegeModel()
- {
- Id = x.Id,
- ActionName = x.ActionName,
- AreaName = x.AreaName,
- ControllerName = x.ControllerName,
- Description = x.Description,
- GroupMark = x.GroupMark,
- Name = x.Name,
- Url = x.Url
- }).ToList();
- return res;
- }
- public IList<PrivilegeGroupModel> GetDescendantsById(int id, bool includeSelf = false)
- {
- var mark = GetMarkById(id);
- return GetDescendantsByMark(mark, includeSelf);
- }
- public IList<PrivilegeGroupModel> GetDescendantsByMark(string mark, bool includeSelf = false)
- {
- var res = GetSortedQueryable().Where(x => x.ParentMark.Contains(mark))
- .Select(x => new PrivilegeGroupModel()
- {
- Mark = x.Mark,
- Name = x.Name,
- SortOrder = x.SortOrder ?? 0,
- ParentMark = x.ParentMark,
- Id = x.Id
- }).ToList();
- if (!includeSelf)
- res = res.Where(x => x.Mark != mark).ToList();
- return res;
- }
- public IList<PrivilegeGroupModel> GetTreeById(int id, bool includeSelf = false)
- {
- var mark = GetMarkById(id);
- var list = GetDescendantsById(id, includeSelf);
- var res = SetTree(list, mark);
- if (includeSelf)
- {
- var self = GetModelByMark(mark);
- self.Children = res;
- res = new List<PrivilegeGroupModel>();
- res.Add(self);
- }
- return res;
- }
- public IList<PrivilegeGroupModel> GetTreeByMark(string mark, bool includeSelf = false)
- {
- var list = GetDescendantsByMark(mark, includeSelf);
- var res = SetTree(list, mark);
- if(includeSelf)
- {
- var self = GetModelByMark(mark);
- self.Children = res;
- res = new List<PrivilegeGroupModel>();
- res.Add(self);
- }
- return res;
- }
- public int GetIdByMark(string mark)
- {
- var res = GetQueryable().Where(x => x.Mark == mark).Select(x => x.Id).SingleOrDefault();
- return res;
- }
- public string GetMarkById(int id)
- {
- var res = GetQueryable().Where(x => x.Id == id).Select(x => x.Mark).SingleOrDefault();
- return res;
- }
- public PrivilegeGroupModel GetModelById(int id)
- {
- var res = GetQueryable().Where(x => x.Id == id).Select(x => new PrivilegeGroupModel()
- {
- Mark = x.Mark,
- Name = x.Name,
- ParentMark = x.ParentMark,
- SortOrder = (int)x.SortOrder,
- Id = x.Id
- }).SingleOrDefault();
- return res;
- }
- public PrivilegeGroupModel GetModelByMark(string mark)
- {
- var queryable = Queryable;
- var res =queryable.Where(x => x.Mark == mark).Select(x => new PrivilegeGroupModel()
- {
- Mark = x.Mark,
- Name = x.Name,
- ParentMark = x.ParentMark,
- SortOrder = x.SortOrder??0,
- Id = x.Id
- }).SingleOrDefault();
- return res;
- }
- public IList<PrivilegeModel> GetPrivilegeInGroup(string mark)
- {
- var res = _privilegeLogic.GetSortedQueryable().Where(x => x.GroupMark == mark).Select(x => new PrivilegeModel()
- {
- ActionName = x.ActionName,
- AreaName = x.AreaName,
- ControllerName = x.ControllerName,
- Description = x.Description,
- GroupMark = x.GroupMark,
- Id = x.Id,
- Name = x.Name,
- Url = x.Url
- }).ToList();
- return res;
- }
- public IList<PrivilegeGroupModel> SetTree(IList<PrivilegeGroupModel> list, string parentMark)
- {
- var result = new List<PrivilegeGroupModel>();
- foreach (var item in list)
- {
- if (item.ParentMark == parentMark)
- {
- result.Add(item);
- item.Children = SetTree(list, item.Mark);
- }
- }
- result = result.OrderByDescending(x => x.SortOrder).ToList();
- return result;
- }
- public ResultModel Add(PrivilegeGroupEntity entity)
- {
- if (string.IsNullOrEmpty(entity.ParentMark))
- entity.ParentMark = "0";
- entity.Mark = Utility.GenerateTreeMark(entity.ParentMark);
- var res = AddEntity(entity);
- return res;
- }
- public IList<PrivilegeGroupModel> GetChildrenById(int id)
- {
- var item = GetModelById(id);
- return GetChildrenByMark(item.Mark);
- }
- public IList<PrivilegeGroupModel> GetChildrenByMark(string mark)
- {
- var item = GetModelByMark(mark);
- var res = GetSortedQueryable().Where(x => x.ParentMark == mark)
- .Select(x => new PrivilegeGroupModel()
- {
- Mark = x.Mark,
- Name = x.Name,
- ParentMark = x.ParentMark,
- SortOrder = x.SortOrder ?? 0,
- ParentName = item.Name,
- Id = x.Id
- }).ToList();
- return res;
- }
- }
- }
|