Mini.Crm/Mini.Services/Bas/BasLeftMenuService.cs

312 lines
13 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Air.Model.AirAdminViewModel;
using Air.Model;
using Mini.Model;
using Mini.Model.Entity;
namespace Mini.Services.Bas
{
public class BasLeftMenuService : IBasLeftMenuService
{
private readonly IAdminRepository<Bas_LeftMemu> _basLeftMenuRepository;
private readonly IAdminRepository<Bas_ModuleMenu> _basModuleMenuRepository;
private readonly IAdminRepository<Bas_Right> _basRightRepository;
private readonly ICacheService _cache;
public BasLeftMenuService(IAdminRepository<Bas_LeftMemu> basLeftMenuRepository, IAdminRepository<Bas_ModuleMenu> basModuleMenuRepository, IAdminRepository<Bas_Right> basRightRepository, ICacheService cache)
{
this._basLeftMenuRepository = basLeftMenuRepository;
this._basModuleMenuRepository = basModuleMenuRepository;
this._basRightRepository = basRightRepository;
this._cache = cache;
}
public void Create(Bas_LeftMemuModel model)
{
var info = new Bas_LeftMemu()
{
ModuleMenuId = model.ModuleMenuId,
MName = model.MName,
Url = model.Url,
IsGroup = model.IsGroup,
IsShow = model.IsShow,
RightId = model.RightId,
ParentId = model.ParentId,
SortId = model.SortId,
CTime = model.CTime,
CreateUser = model.CreateUser
};
_basLeftMenuRepository.Add(info);
}
public void Delete(Bas_LeftMemuModel model)
{
var info = _basLeftMenuRepository.Get(p => p.MenuId == model.MenuId);
if (info != null)
{
_basLeftMenuRepository.Delete(info);
}
}
public Bas_LeftMemuModel Get(int Id)
{
var model = _basLeftMenuRepository.Get(p => p.MenuId == Id);
var info = new Bas_LeftMemuModel() {
MenuId = model.MenuId,
ModuleMenuId = model.ModuleMenuId,
MName = model.MName,
Url = model.Url,
IsGroup = model.IsGroup,
IsShow = model.IsShow,
RightId = model.RightId,
ParentId = model.ParentId,
SortId = model.SortId,
CTime = model.CTime,
CreateUser = model.CreateUser
};
return info;
}
public IList<Bas_LeftMenuTreeViewModel> GetBasLeftMenuByModuleId(int moduleId, string[] rightIds)
{
var list = _basLeftMenuRepository.GetList(p => p.ModuleMenuId == moduleId && p.IsShow == 1 && (p.IsGroup == 1 || rightIds.Contains(p.RightId)))
.OrderBy(p=>p.SortId)
.Select(p => new Bas_LeftMemuModel()
{
MenuId = p.MenuId,
ModuleMenuId = p.ModuleMenuId,
MName = p.MName,
Url = p.Url,
IsGroup = p.IsGroup,
IsShow = p.IsShow,
RightId = p.RightId,
ParentId = p.ParentId,
SortId = p.SortId,
CTime = p.CTime,
CreateUser = p.CreateUser
}).ToList();
return GetALLleftMenu(0, list);
}
public void Sort(string ids, string sortIds)
{
IList<Bas_LeftMemu> list = new List<Bas_LeftMemu>();
var idStr = ids.Split(',');
var sortStr = sortIds.Split(',');
for (int i = 0; i < idStr.Length; i++)
{
var id = Convert.ToInt32(idStr[i]);
var sortId = Convert.ToInt32(sortStr[i]);
var entry = _basLeftMenuRepository.Get(p => p.MenuId == id);
if (entry == null)
continue;
entry.SortId = sortId;
list.Add(entry);
}
_basLeftMenuRepository.Update(list);
}
public void Update(Bas_LeftMemuModel model)
{
var info = _basLeftMenuRepository.Get(p => p.MenuId == model.MenuId);
if (info != null)
{
info.ModuleMenuId = model.ModuleMenuId;
info.MName = model.MName;
info.Url = model.Url;
info.IsGroup = model.IsGroup;
info.IsShow = model.IsShow;
info.RightId = model.RightId;
info.ParentId = model.ParentId;
info.SortId = model.SortId;
info.CTime = model.CTime;
info.CreateUser = model.CreateUser;
_basLeftMenuRepository.Update(info);
}
}
private IList<Bas_LeftMenuTreeViewModel> GetALLleftMenu(int parentId, IList<Bas_LeftMemuModel> menulist)
{
List<Bas_LeftMenuTreeViewModel> obj = null;
//下面子菜单
foreach (var model in menulist.Where(n => n.ParentId == parentId))
{
if (obj == null)
obj = new List<Bas_LeftMenuTreeViewModel>();
obj.Add(new Bas_LeftMenuTreeViewModel
{
id = model.MenuId,
text = model.MName,
value = (model.Url == "#" || model.Url == "") ? "" : model.Url,
showcheck = false,
complete = true,
isexpand = true,
checkstate = 0,
parentid = model.ParentId,
hasChildren = (model.IsGroup == 1),
ChildNodes = GetALLleftMenu(model.MenuId, menulist)
});
}
return obj;
}
public object GetTreeList_leftMenu()
{
var leftMemuList = _basLeftMenuRepository.GetList().OrderBy(m => m.SortId).ToList();
var moduleMenuList = _basModuleMenuRepository.GetList().OrderBy(m => m.SortId).ToList();
var rightList = _basRightRepository.GetList().ToList();//获取所有的权限
Bas_Right right = null;
List<object> obj = new List<object>();
List<object> childObj = null;
//顶部模块部分
foreach (var m in moduleMenuList)
{
foreach (var model in leftMemuList.Where(n => n.ParentId == 0 && n.ModuleMenuId == m.ModuleMenuId))
{
if (childObj == null)
childObj = new List<object>();
right = null;
if (!string.IsNullOrEmpty(model.RightId))
right = rightList.Where(x => x.RightId == model.RightId).FirstOrDefault();
//if (model.ISSHOW != 0)
//{
childObj.Add(new
{
id = model.MenuId.ToString(),
text = model.MName,
moduleMenuId = m.ModuleMenuId.ToString(),
sortId = model.SortId,
title = "地址:" + model.Url + " 权限:" + (right == null || string.IsNullOrEmpty(model.RightId) ? "无" : right.RName),
iconCls = (model.IsShow == 1 ? (model.IsGroup == 1 ? "" : "icon-treenode") : "icon-cant"),
children = GetleftMenuTree(model.MenuId, leftMemuList, rightList)
});
//}
}
right = null;
if (!string.IsNullOrEmpty(m.RightId))
right = rightList.Where(x => x.RightId == m.RightId).FirstOrDefault();
obj.Add(new
{
id = (-m.ModuleMenuId).ToString(),//模块ID为负数
text = m.MName,
iconCls = "icon-group",
moduleMenuId = m.ModuleMenuId.ToString(),
sortId = m.SortId,
title = "图片:" + m.ImageUrl + " 权限:" + (right == null || string.IsNullOrEmpty(m.RightId) ? "无" : right.RName),
children = childObj
});
childObj = null;
}
return obj;
}
private object GetleftMenuTree(decimal parentId, List<Bas_LeftMemu> menulist, List<Bas_Right> rightList)
{
List<object> obj = null;
Bas_Right right = null;
//下面子菜单
foreach (var model in menulist.Where(n => n.ParentId == parentId))
{
if (obj == null)
obj = new List<object>();
right = null;
if (!string.IsNullOrEmpty(model.RightId))
right = rightList.Where(x => x.RightId == model.RightId).FirstOrDefault();
obj.Add(new
{
id = model.MenuId.ToString(),
moduleMenuId = model.ModuleMenuId.ToString(),
text = model.MName,
sortId = model.SortId,
iconCls = (model.IsShow == 1 ? (model.IsGroup == 1 ? "" : "icon-treenode") : "icon-cant"),
title = "地址:" + model.Url + " 权限:" + (right == null || string.IsNullOrEmpty(model.RightId) ? "无" : right.RName),
children = GetleftMenuTree(model.MenuId, menulist, rightList)
});
}
return obj;
}
public object GetTreeList_leftMenu(string[] userRights)
{
var leftMemuList = _cache.GetList_LeftMemu()
.OrderBy(m => m.SortId)
.Where(m => m.IsShow == 1)
.Where(m => userRights.Contains(m.RightId) || string.IsNullOrEmpty(m.RightId) || m.RightId.Trim() == "0")
.Select(p=> new Bas_LeftMemu() { MenuId = p.MenuId, ModuleMenuId = p.ModuleMenuId, MName = p.MName, RightId = p.RightId, ParentId = p.ParentId, Url = p.Url, IsGroup = p.IsGroup, IsShow = p.IsShow, SortId = p.SortId, CTime = p.CTime, CreateUser = p.CreateUser })
.ToList();
var moduleMenuList = _cache.GetList_ModuleMenu().OrderBy(m => m.SortId).Where(m => userRights.Contains(m.RightId) || string.IsNullOrEmpty(m.RightId) || m.RightId.Trim() == "0").ToList();
List<object> obj = new List<object>();
List<object> childObj = null;
//顶部模块部分
foreach (var m in moduleMenuList)
{
foreach (var model in leftMemuList.Where(n => n.ParentId == 0 && n.ModuleMenuId == m.ModuleMenuId))
{
if (childObj == null)
childObj = new List<object>();
childObj.Add(new
{
id = (model.IsGroup == 1 ? "-" : "") + model.MenuId.ToString(),
text = model.MName,
moduleMenuId = m.ModuleMenuId.ToString(),
sortId = model.SortId,
iconCls = (model.IsGroup == 1 ? "" : "icon-treenode"),
children = GetleftMenuTree(model.MenuId, leftMemuList)
});
}
obj.Add(new
{
id = (-m.ModuleMenuId).ToString(),//模块ID为负数
text = m.MName,
iconCls = "icon-group",
moduleMenuId = m.ModuleMenuId.ToString(),
sortId = m.SortId,
children = childObj
});
childObj = null;
}
return obj;
}
private object GetleftMenuTree(decimal parentId, List<Bas_LeftMemu> menulist)
{
List<object> obj = null;
//下面子菜单
foreach (var model in menulist.Where(n => n.ParentId == parentId))
{
if (obj == null)
obj = new List<object>();
if (model.IsGroup == 1)
obj.Add(new
{
id = "-" + model.MenuId.ToString(),
moduleMenuId = model.ModuleMenuId.ToString(),
text = model.MName,
sortId = model.SortId,
iconCls = (model.IsGroup == 1 ? "" : "icon-treenode"),
children = GetleftMenuTree(model.MenuId, menulist)
});
else
obj.Add(new
{
id = model.MenuId.ToString(),
moduleMenuId = model.ModuleMenuId.ToString(),
text = model.MName,
sortId = model.SortId,
iconCls = (model.IsGroup == 1 ? "" : "icon-treenode")
});
}
return obj;
}
}
}