using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Web.Mvc; using Abp.Application.Services.Dto; using Abp.Auditing; using Abp.Authorization; using Abp.Domain.Repositories; using Abp.Runtime.Caching; using WePlatform.Authorization; using WePlatform.BaseInfo; using WePlatform.BaseSystem.Functions.Dto; using WePlatform.Configuration; using IwbZero.AppServiceBase; using IwbZero.Auditing; using IwbZero.Authorization.Base.Users; using IwbZero.Runtime.Session; namespace WePlatform.BaseSystem.Functions { [AbpAuthorize, AuditLog("系统功能菜单", "菜单")] public class FunctionsAppService : IwbAsyncCrudAppService, IFunctionsAppService { public FunctionsAppService(ICacheManager cacheManager, IRepository repository) : base(repository, "FunctionNo") { CacheManager = cacheManager; } protected override bool KeyIsAuto { get; set; } = false; protected override string KeyExistMessage => string.Format(L(IwbLanguageMessage.KeyExistMessageFormatter), L("function")); protected override string KeyNotExistMessage => string.Format(L(IwbLanguageMessage.KeyNotExistMessageFormatter), L("function")); #region SelectList /// /// 获取 Function 下拉框选项 /// /// [DisableAuditing] public override async Task> GetSelectList() { var list = await Repository.GetAllListAsync(); var sList = new List(); foreach (var l in list) { sList.Add(new SelectListItem() { Value = l.FunctionNo, Text = l.FunctionName }); } return sList; } /// /// 获取 Function 下拉框选项 /// /// [DisableAuditing] public override async Task GetSelectStr() { var list = await Repository.GetAllListAsync(); string options = ""; foreach (var f in list) { options += ""; } return options; } #endregion #region CURD [DisableAuditing] [AbpAuthorize(PermissionNames.PagesSystemMgFunctionMgQuery)] public override async Task> GetAll(IwbPagedRequestDto input) { var query = CreateFilteredQuery(input).Where(a => a.FunctionType != 0); query = ApplyFilter(query, input); var totalCount = await AsyncQueryableExecuter.CountAsync(query); query = query.OrderBy(a => a.FunctionType).ThenBy(a => a.Sort); //query = ApplyPaging(query, input); var entities = await AsyncQueryableExecuter.ToListAsync(query); return new PagedResultDto(totalCount, entities.Select(MapToEntityDto).ToList()); } [AbpAuthorize(PermissionNames.PagesSystemMgFunctionMgCreate)] public override async Task Create(FunctionCreateDto input) { if (AbpSession.UserType != UsersAndRolesTypeDefinition.Supper) { ThrowError(IwbLanguageMessage.NoPermissionOperation); } input.ParentNo = string.IsNullOrEmpty(input.ParentNo) ? "0" : input.ParentNo; var entity = await Repository.FirstOrDefaultAsync(a => a.FunctionNo == input.ParentNo); if (entity == null) { ThrowError("FunctionHasNoParent"); return; } input.Depth = entity.Depth + 1; //input.FunctionPath = entity.FunctionPath + "," + input.FunctionNo; input.PermissionName = entity.PermissionName + "." + input.FunctionNo; input.NeedAuth = true; await CreateEntity(input); } [AbpAuthorize(PermissionNames.PagesSystemMgFunctionMgUpdate)] public override async Task Update(FunctionUpdateDto input) { input.ParentNo = string.IsNullOrEmpty(input.ParentNo) ? "0" : input.ParentNo; input.NeedAuth = true; var entity = await Repository.FirstOrDefaultAsync(a => a.Id == input.Id); if (entity == null) { CheckErrors(NotExistMessage); return; } if (AbpSession.GetClaimValue(IwbClaimTypes.UserName) != UserBase.AdminUserName) { input.FunctionType = entity.FunctionType; input.PermissionName = entity.PermissionName; input.Action = entity.Action; input.Controller = entity.Controller; //input.FunctionPath = entity.FunctionPath; input.Script = entity.Script; input.Class = entity.Class; input.Url = entity.Url; } MapToEntity(input, entity); await Repository.UpdateAsync(entity); await CacheManager.GetCache(IwbCacheNames.FunctionCache).RemoveAsync(entity.FunctionNo); await CacheManager.GetCache(IwbCacheNames.NavFunctionCache).RemoveAsync(entity.FunctionNo); } [AbpAuthorize(PermissionNames.PagesSystemMgFunctionMgDelete)] public override async Task Delete(EntityDto input) { var entity = await Repository.FirstOrDefaultAsync(a => a.Id == input.Id); if ((await Repository.GetAllListAsync(a => a.ParentNo == entity.FunctionNo)).Any()) { ThrowError("FunctionHasChildren"); } await Repository.DeleteAsync(entity); await CurrentUnitOfWork.SaveChangesAsync(); await CacheManager.GetCache(IwbCacheNames.FunctionCache).RemoveAsync(entity.FunctionNo); await CacheManager.GetCache(IwbCacheNames.NavFunctionCache).RemoveAsync(entity.FunctionNo); } /// /// 上移 /// /// /// [AbpAuthorize(PermissionNames.PagesSystemMgFunctionMgMoveUp), AuditLog("上移菜单")] public async Task MoveUp(MoveUpFunctionDto input) { var fun = await GetEntityById(input.Id); int sort = fun.Sort; var prevFun = await Repository.GetAsync(input.PrevId); int prevSort = prevFun.Sort; fun.Sort = prevSort; prevFun.Sort = sort; await CurrentUnitOfWork.SaveChangesAsync(); } /// /// 下移 /// /// /// [AbpAuthorize(PermissionNames.PagesSystemMgFunctionMgMoveDown), AuditLog("下移菜单")] public async Task MoveDown(MoveDownFunctionDto input) { var fun = await GetEntityById(input.Id); int sort = fun.Sort; var nextFun = await Repository.GetAsync(input.NextId); int nextSort = nextFun.Sort; fun.Sort = nextSort; nextFun.Sort = sort; await CurrentUnitOfWork.SaveChangesAsync(); } /// /// 强制刷新 /// /// [AbpAuthorize(PermissionNames.PagesSystemMgFunctionMgRefresh), AuditLog("强制刷新")] public async Task Refresh() { await CacheManager.GetCache(IwbCacheNames.FunctionCache).ClearAsync(); await CacheManager.GetCache(IwbCacheNames.NavFunctionCache).ClearAsync(); } #endregion /// /// 重写排序方法 /// /// /// /// protected override IQueryable ApplySorting(IQueryable query, IwbPagedRequestDto input) { return query.OrderBy(a => a.FunctionType).ThenBy(a => a.Sort); } protected override SysFunction MapToEntity(FunctionCreateDto input) { return base.MapToEntity(input); } } }