3. BLL内容:
[C#] 纯文本查看 复制代码 /*
* 重要提示: 请不要自行修改本类,如果因为你自己修改后将无法升级到新版本。如果确实有什么问题请到官方网站提建议,我们一定会及时修改
* 编码日期: 2017-12-02
* 编码人: 范范
* 官方网址: [url]http://www.sufeinet.com[/url]
* 版本号: 1.0
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Purview.DAL.Sys;
using Purview.Model.Sys;
namespace Purview.BLL.Sys
{
/// <summary>
/// 菜单信息 实现类
/// </summary>
public class Sys_MenuBLL
{
private Sys_MenuDAL dal = new Sys_MenuDAL();
/// <summary>
/// 获取子菜单列表,根据角色id,与主菜单id
/// </summary>
/// <param name="roleId"></param>
/// <param name="parentId"></param>
/// <returns></returns>
public List<Sys_MenuInfo> GetSonMenuByRoleId(int roleId, int parentId)
{
// 获取角色下的所有菜单信息
var roleMenuList = new Sys_MenuInRoleBLL().GetList("MenuID", string.Format("RoleID = {0}", roleId), "");
if (roleMenuList != null && roleMenuList.Any())
{
// 获取所有子菜单列表
var sonRoleMenuList = roleMenuList.Where(r => r.levelFlag == 0);
if (sonRoleMenuList != null && sonRoleMenuList.Any())
{
// 获取所有子菜单ids
var menuIds = sonRoleMenuList.Select(m => m.MenuID);
// 获取所有主菜单下存在的角色菜单信息
var menuList = GetList("", string.Format("MenuID in ({0}) AND ParentID = {1}", string.Join(",", menuIds), parentId), "");
// 返回信息
if (menuList == null) { return new List<Sys_MenuInfo>(); }
return menuList;
}
}
return new List<Sys_MenuInfo>();
}
/// <summary>
/// 获取分页列表信息
/// </summary>
/// <param name="fields">所需字段</param>
/// <param name="query">查询条件</param>
/// <param name="orderby">排序方式</param>
/// <param name="pageIndex">页码</param>
/// <param name="pageSize">页长</param>
/// <param name="totalCount">总记录</param>
/// <returns></returns>
public List<Sys_MenuInfo> GetListPage(string fields, string query, string orderby, int pageIndex, int pageSize, out int totalCount)
{
// 参数合法性校验
if (pageSize <= 0) { totalCount = 0; return new List<Sys_MenuInfo>(); }
if (pageIndex <= 0) { pageIndex = 1; }
if (string.IsNullOrWhiteSpace(fields)) { fields = "*"; }
if (string.IsNullOrWhiteSpace(query)) { query = "1=1"; }
if (string.IsNullOrWhiteSpace(orderby)) { orderby = "MenuID"; }
return dal.GetList(fields, query, orderby, pageIndex, pageSize, out totalCount);
}
/// <summary>
/// 获取列表信息
/// </summary>
/// <param name="fields">所需字段</param>
/// <param name="query">查询条件</param>
/// <param name="orderBy">排序方式</param>
/// <returns></returns>
public List<Sys_MenuInfo> GetList(string fields, string query, string orderBy)
{
// 参数合法性校验
if (string.IsNullOrWhiteSpace(fields)) { fields = "*"; }
if (string.IsNullOrWhiteSpace(query)) { query = "1=1"; }
if (string.IsNullOrWhiteSpace(orderBy)) { orderBy = "MenuID"; }
return dal.GetList(fields, query, orderBy);
}
/// <summary>
/// 获取实体信息
/// </summary>
/// <param name="fields">所需字段</param>
/// <param name="query">查询条件(如果top1 的话,可以在此处添加排序方式)</param>
/// <returns></returns>
public Sys_MenuInfo GetInfo(string fields, string query)
{
// 参数合法性校验
if (string.IsNullOrWhiteSpace(fields)) { fields = "*"; }
if (string.IsNullOrWhiteSpace(query)) { query = "1=1"; }
return dal.GetInfo(fields, query);
}
/// <summary>
/// 新增
/// </summary>
/// <param name="info">实体信息</param>
/// <returns></returns>
public int Insert(Sys_MenuInfo info)
{
// 得到需要保存的菜单id
var menuId = 100;
if (info.ParentID > 0)
{
// 新增子菜单时处理
// 获取当前主菜单下的最大id值
var maxIdInfo = GetInfo(" top 1 *", string.Format(" ParentID = {0} order by MenuID desc", info.ParentID));
if (maxIdInfo != null && maxIdInfo.MenuID > 0)
{
menuId = maxIdInfo.MenuID + 1;
}
else
{
menuId = info.ParentID + 1;
}
}
else
{
// 新增主菜单时处理
// 获取最大的主菜单id值
var maxIdInfo = GetInfo(" top 1 *", " ParentID = 0 order by MenuID desc");
if (maxIdInfo != null && maxIdInfo.MenuID > 0) { menuId = (maxIdInfo.MenuID / 100 + 1) * 100; }
}
info.MenuID = menuId;
return dal.Insert(info);
}
/// <summary>
/// 修改
/// </summary>
/// <param name="info">实体信息</param>
/// <returns></returns>
public int Update(Sys_MenuInfo info)
{
return dal.Update(info);
}
/// <summary>
/// 删除
/// </summary>
/// <param name="query">条件</param>
/// <returns></returns>
public int Delete(string query)
{
// 如果条件为空的话,不进行删除,直接返回
if (string.IsNullOrWhiteSpace(query)) { return -1; }
return dal.Delete(query);
}
}
}
此处重点说下新增方法,菜单id为主键,但是不是自增列
此新增方法可以看出,菜单id是从100开始的,100/200/300/400.....依次为主菜单id
101/102/103.... 202/203/204..... 均为子菜单id
这样的处理方法,主要为列表显示,排序时,能清晰的看到为主菜单,子菜单顺序排列
其他的方法在此不做重点解释,都能清晰的看懂哈
如果有不懂得,可以留言了解
|