using System.Collections.Generic; using System.Threading.Tasks; using Znyc.Admin.Commons.Dtos; using Znyc.Admin.Commons.Mapping; using Znyc.Admin.Commons.Pages; using Znyc.Admin.Commons.Services; using Znyc.Admin.Security.Dtos; using Znyc.Admin.Security.Entitys; using Znyc.Admin.Security.IRepositories; using Znyc.Admin.Security.IServices; namespace Znyc.Admin.Security.Services { public class RoleService : BaseService, IRoleService { private readonly IRoleRepository _repository; private readonly IOrganizeService _organizeService; public RoleService(IRoleRepository repository, IOrganizeService organizeService) : base(repository) { _repository = repository; _organizeService = organizeService; } /// /// 根据角色编码获取角色 /// /// /// public Role GetRole(string enCode) { string where = string.Format("EnCode='{0}'", enCode); return _repository.GetWhere(where); } /// /// 根据用户角色ID获取角色编码 /// /// 角色ID字符串,用“,”分格 /// public string GetRoleEnCode(string ids) { string roleIDsStr = string.Format("'{0}'", ids.Replace(",", "','")); string sqlWhere = string.Format("Id in({0})", roleIDsStr); IEnumerable listRoles = _repository.GetListWhere(sqlWhere); string strEnCode = string.Empty; foreach (Role item in listRoles) { strEnCode += item.EnCode + ","; } return strEnCode; } /// /// 根据用户角色ID获取角色编码 /// /// 角色ID字符串,用“,”分格 /// public string GetRoleNameStr(string ids) { string roleIDsStr = string.Format("'{0}'", ids.Replace(",", "','")); string sqlWhere = string.Format("Id in({0})", roleIDsStr); IEnumerable listRoles = _repository.GetListWhere(sqlWhere); string strEnCode = string.Empty; foreach (Role item in listRoles) { strEnCode += item.FullName + ","; } return strEnCode; } /// /// 根据条件查询数据库,并返回对象集合(用于分页数据显示) /// /// 查询的条件 /// 指定对象的集合 public override async Task> FindWithPagerAsync(SearchInputDto search) { bool order = search.Order == "asc" ? false : true; string where = GetDataPrivilege(false); if (!string.IsNullOrEmpty(search.Keywords)) { @where += string.Format(" and (FullName like '%{0}%' or EnCode like '%{0}%')", search.Keywords); }; where += " and Category=1"; PagerInfo pagerInfo = new PagerInfo { CurrenetPageIndex = search.CurrenetPageIndex, PageSize = search.PageSize }; List list = await repository.FindWithPagerAsync(where, pagerInfo, search.Sort, order); List resultList = list.MapTo(); List listResult = new List(); foreach (RoleOutputDto item in resultList) { if (!string.IsNullOrEmpty(item.OrganizeId.ToString())) { item.OrganizeName = _organizeService.Get(item.OrganizeId).FullName; } listResult.Add(item); } PageResult pageResult = new PageResult { CurrentPage = pagerInfo.CurrenetPageIndex, Items = listResult.MapTo(), ItemsPerPage = pagerInfo.PageSize, TotalItems = pagerInfo.RecordCount }; return pageResult; } } }