using Bootstrap.Security; using Bootstrap.Security.DataAccess; using PetaPoco; using System; using System.Collections.Generic; using System.Linq; namespace Bootstrap.DataAccess { /// /// /// public class Menu : BootstrapMenu { /// /// 删除菜单信息 /// /// public virtual bool Delete(IEnumerable value) { if (!value.Any()) return true; var ret = false; var db = DbManager.Create(); try { var ids = string.Join(",", value); db.BeginTransaction(); db.Execute($"delete from NavigationRole where NavigationID in ({ids})"); db.Execute($"delete from Navigations where ID in ({ids})"); db.CompleteTransaction(); ret = true; } catch (Exception ex) { db.AbortTransaction(); throw ex; } return ret; } /// /// 保存新建/更新的菜单信息 /// /// /// public virtual bool Save(BootstrapMenu p) { if (string.IsNullOrEmpty(p.Name)) throw new ArgumentNullException(nameof(p.Name)); if (p.Name.Length > 50) p.Name = p.Name.Substring(0, 50); if (p.Icon != null && p.Icon.Length > 50) p.Icon = p.Icon.Substring(0, 50); if (p.Url != null && p.Url.Length > 4000) p.Url = p.Url.Substring(0, 4000); DbManager.Create().Save(p); return true; } /// /// 查询某个角色所配置的菜单 /// /// /// public virtual IEnumerable RetrieveMenusByRoleId(string roleId) { var menus = DbManager.Create().Fetch("select NavigationID as Id from NavigationRole where RoleID = @0", roleId); return menus.Select(m => new { m.Id }); } /// /// 通过角色ID保存当前授权菜单 /// /// /// /// public virtual bool SaveMenusByRoleId(string roleId, IEnumerable menuIds) { bool ret = false; var db = DbManager.Create(); try { db.BeginTransaction(); db.Execute("delete from NavigationRole where RoleID = @0", roleId); db.InsertBatch("NavigationRole", menuIds.Select(g => new { NavigationID = g, RoleID = roleId })); db.CompleteTransaction(); ret = true; } catch (Exception ex) { db.AbortTransaction(); throw ex; } return ret; } /// /// 通过当前用户名获得所有菜单 /// /// 当前登陆的用户名 /// public virtual IEnumerable RetrieveAllMenus(string userName) => DbHelper.RetrieveAllMenus(userName); } }