You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
10 KiB

using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Znyc.Cloudcar.Admin.Commons.Core.Dtos;
using Znyc.Cloudcar.Admin.Commons.Entitys;
using Znyc.Cloudcar.Admin.Commons.Extensions;
using Znyc.Cloudcar.Admin.Commons.Mapping;
using Znyc.Cloudcar.Admin.Commons.Services;
using Znyc.Cloudcar.Admin.Security.Dtos;
using Znyc.Cloudcar.Admin.Security.Entitys;
using Znyc.Cloudcar.Admin.Security.IRepositories;
using Znyc.Cloudcar.Admin.Security.IServices;
namespace Znyc.Cloudcar.Admin.Security.Services
{
/// <summary>
/// �˵�
/// </summary>
public class MenuService : BaseService<MenuEntity, MenuOutputDto, long>, IMenuService
{
private readonly IAdminUserRepository _adminUserRepository;
private readonly IMenuRepository _MenuRepository;
private readonly IRoleAuthorizeRepository roleAuthorizeRepository;
private readonly ISystemTypeRepository systemTypeRepository;
/// <summary>
/// </summary>
/// <param name="repository"></param>
/// <param name="_userRepository"></param>
/// <param name="_roleAuthorizeRepository"></param>
/// <param name="_systemTypeRepository"></param>
/// <param name="logService"></param>
public MenuService(IMenuRepository repository, IAdminUserRepository adminUserRepository,
IRoleAuthorizeRepository _roleAuthorizeRepository, ISystemTypeRepository _systemTypeRepository
) : base(repository)
{
_MenuRepository = repository;
_adminUserRepository = adminUserRepository;
roleAuthorizeRepository = _roleAuthorizeRepository;
systemTypeRepository = _systemTypeRepository;
}
/// <summary>
/// �����û���ȡ���ܲ˵�
/// </summary>
/// <param name="userId">�û�ID</param>
/// <returns></returns>
public List<MenuEntity> GetMenuByUser(long userId)
{
List<MenuEntity> result = new List<MenuEntity>();
List<MenuEntity> allMenuls = new List<MenuEntity>();
List<MenuEntity> subMenuls = new List<MenuEntity>();
string where = "Layers=1";
IEnumerable<MenuEntity> allMenus = _MenuRepository.GetAllByIsNotDeleteAndEnabledMark();
allMenuls = allMenus.ToList();
if (userId.ToString() == string.Empty) //��������Ա
{
return allMenuls;
}
AdminUserEntity user = _adminUserRepository.Get(userId);
if (user == null)
{
return result;
}
long userRoles = user.RoleId;
where = string.Format("ItemType = 1 and ObjectType = 1 and ObjectId='{0}'", userRoles);
IEnumerable<RoleAuthorizeEntity> Menus = roleAuthorizeRepository.GetListWhere(where);
foreach (RoleAuthorizeEntity item in Menus)
{
MenuEntity MenuEntity = allMenuls.Find(t => t.Id == item.ItemId);
if (MenuEntity != null)
{
result.Add(MenuEntity);
}
}
return result.OrderBy(t => t.SortCode).ToList();
}
/// <summary>
/// ��ȡ���ܲ˵�������Vue �����б�
/// </summary>
/// <param name="systemTypeId">��ϵͳId</param>
/// <returns></returns>
public async Task<List<MenuTreeTableOutputDto>> GetAllMenuTreeTable(string systemTypeId)
{
string where = "1=1";
List<MenuTreeTableOutputDto> reslist = new List<MenuTreeTableOutputDto>();
if (!string.IsNullOrEmpty(systemTypeId))
{
IEnumerable<MenuEntity> elist = await _MenuRepository.GetListWhereAsync("SystemTypeId='" + systemTypeId + "'");
List<MenuEntity> list = elist.OrderBy(t => t.SortCode).ToList();
List<MenuEntity> oneMenuList = list.FindAll(t => t.ParentId == 0);
foreach (MenuEntity item in oneMenuList)
{
MenuTreeTableOutputDto menuTreeTableOutputDto = new MenuTreeTableOutputDto();
menuTreeTableOutputDto = item.MapTo<MenuTreeTableOutputDto>();
menuTreeTableOutputDto.Children = GetSubMenus(list, item.Id).ToList();
reslist.Add(menuTreeTableOutputDto);
}
}
else
{
IEnumerable<SystemTypeEntity> listSystemType = await systemTypeRepository.GetListWhereAsync(where);
foreach (SystemTypeEntity systemType in listSystemType)
{
MenuTreeTableOutputDto menuTreeTableOutputDto = new MenuTreeTableOutputDto
{
Id = systemType.Id,
FullName = systemType.FullName,
EnCode = systemType.EnCode,
UrlAddress = systemType.Url,
IsEnabled = systemType.IsEnabled,
SystemTag = true
};
IEnumerable<MenuEntity> elist = await _MenuRepository.GetListWhereAsync("SystemTypeId='" + systemType.Id + "'");
if (elist.Count() > 0)
{
List<MenuEntity> list = elist.OrderBy(t => t.SortCode).ToList();
menuTreeTableOutputDto.Children = GetSubMenus(list, 0).ToList();
}
reslist.Add(menuTreeTableOutputDto);
}
}
return reslist;
}
/// <summary>
/// ���ݽ�ɫID�ַ��������ŷֿ�)��ϵͳ����ID����ȡ��Ӧ�IJ��������б�
/// </summary>
/// <param name="roleIds">��ɫID</param>
/// <param name="typeID">ϵͳ����ID</param>
/// <param name="isMenu">�Ƿ��Dz˵�</param>
/// <returns></returns>
public List<MenuEntity> GetFunctions(string roleIds, string typeID, bool isMenu = false)
{
return _MenuRepository.GetFunctions(roleIds, typeID, isMenu).ToList();
}
/// <summary>
/// ����ϵͳ����ID����ȡ��Ӧ�IJ��������б�
/// </summary>
/// <param name="typeID">ϵͳ����ID</param>
/// <returns></returns>
public List<MenuEntity> GetFunctions(string typeID)
{
return _MenuRepository.GetFunctions(typeID).ToList();
}
/// <summary>
/// ���ݸ������ܱ�����ѯ�����Ӽ����ܣ���Ҫ����ҳ��������ťȨ��
/// </summary>
/// <param name="enCode">�˵����ܱ���</param>
/// <returns></returns>
public async Task<IEnumerable<MenuOutputDto>> GetListByParentEnCode(string enCode)
{
string where = string.Format("EnCode='{0}'", enCode);
MenuEntity function = await repository.GetWhereAsync(where);
where = string.Format("ParentId='{0}'", function.ParentId);
IEnumerable<MenuEntity> list = await repository.GetAllByIsNotEnabledMarkAsync(where);
return list.MapTo<MenuOutputDto>().ToList();
}
/// <summary>
/// ����������ɾ��
/// </summary>
/// <param name="idsInfo">����Id����</param>
/// <param name="trans">��������</param>
/// <returns></returns>
public CommonResult DeleteBatchWhere(DeletesInputDto idsInfo, IDbTransaction trans = null)
{
CommonResult result = new CommonResult();
string where = string.Empty;
for (long i = 0; i < idsInfo.Ids.Length; i++)
{
if (idsInfo.Ids[0] != null)
{
@where = string.Format("ParentId='{0}'", idsInfo.Ids[0]);
IEnumerable<MenuEntity> list = _MenuRepository.GetListWhere(@where);
if (list.Count() > 0)
{
result.ErrMsg = "���ܴ����Ӽ����ݣ�����ɾ��";
return result;
}
}
}
where = "id in ('" + idsInfo.Ids.Join(",").Trim(',').Replace(",", "','") + "')";
bool bl = repository.DeleteBatchWhere(where);
if (bl)
{
result.ErrCode = "0";
}
return result;
}
/// <summary>
/// ����������ɾ��
/// </summary>
/// <param name="idsInfo">����Id����</param>
/// <param name="trans">��������</param>
/// <returns></returns>
public async Task<CommonResult> DeleteBatchWhereAsync(DeletesInputDto idsInfo, IDbTransaction trans = null)
{
CommonResult result = new CommonResult();
string where = string.Empty;
for (long i = 0; i < idsInfo.Ids.Length; i++)
{
if (idsInfo.Ids[0].ToString().Length > 0)
{
@where = string.Format("ParentId='{0}'", idsInfo.Ids[0]);
IEnumerable<MenuEntity> list = _MenuRepository.GetListWhere(@where);
if (list.Count() > 0)
{
result.ErrMsg = "���ܴ����Ӽ����ݣ�����ɾ��";
return result;
}
}
}
where = "id in ('" + idsInfo.Ids.Join(",").Trim(',').Replace(",", "','") + "')";
bool bl = await repository.DeleteBatchWhereAsync(where);
if (bl)
{
result.ErrCode = "0";
}
return result;
}
/// <summary>
/// ��ȡ�Ӳ˵����ݹ�����
/// </summary>
/// <param name="data"></param>
/// <param name="ParentId">����Id</param>
/// <returns></returns>
private List<MenuTreeTableOutputDto> GetSubMenus(List<MenuEntity> data, long ParentId)
{
List<MenuTreeTableOutputDto> list = new List<MenuTreeTableOutputDto>();
MenuTreeTableOutputDto menuTreeTableOutputDto = new MenuTreeTableOutputDto();
List<MenuEntity> ChilList = data.FindAll(t => t.ParentId == ParentId);
foreach (MenuEntity entity in ChilList)
{
menuTreeTableOutputDto = entity.MapTo<MenuTreeTableOutputDto>();
menuTreeTableOutputDto.Children = GetSubMenus(data, entity.Id).OrderBy(t => t.SortCode)
.MapTo<MenuTreeTableOutputDto>();
list.Add(menuTreeTableOutputDto);
}
return list;
}
}
}