| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- 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<SysFunction, FunctionDto, int, IwbPagedRequestDto, FunctionCreateDto, FunctionUpdateDto>, IFunctionsAppService
- {
- public FunctionsAppService(ICacheManager cacheManager, IRepository<SysFunction, int> 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
- /// <summary>
- /// 获取 Function 下拉框选项
- /// </summary>
- /// <returns></returns>
- [DisableAuditing]
- public override async Task<List<SelectListItem>> GetSelectList()
- {
- var list = await Repository.GetAllListAsync();
- var sList = new List<SelectListItem>();
- foreach (var l in list)
- {
- sList.Add(new SelectListItem() { Value = l.FunctionNo, Text = l.FunctionName });
- }
- return sList;
- }
- /// <summary>
- /// 获取 Function 下拉框选项
- /// </summary>
- /// <returns></returns>
- [DisableAuditing]
- public override async Task<string> GetSelectStr()
- {
- var list = await Repository.GetAllListAsync();
- string options = "";
- foreach (var f in list)
- {
- options += "<option value=\"" + f.FunctionNo + "\">" + f.FunctionName + "</option>";
- }
- return options;
- }
- #endregion
- #region CURD
- [DisableAuditing]
- [AbpAuthorize(PermissionNames.PagesSystemMgFunctionMgQuery)]
- public override async Task<PagedResultDto<FunctionDto>> 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<FunctionDto>(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<int> 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);
- }
- /// <summary>
- /// 上移
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- [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();
- }
- /// <summary>
- /// 下移
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- [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();
- }
- /// <summary>
- /// 强制刷新
- /// </summary>
- /// <returns></returns>
- [AbpAuthorize(PermissionNames.PagesSystemMgFunctionMgRefresh), AuditLog("强制刷新")]
- public async Task Refresh()
- {
- await CacheManager.GetCache(IwbCacheNames.FunctionCache).ClearAsync();
- await CacheManager.GetCache(IwbCacheNames.NavFunctionCache).ClearAsync();
- }
- #endregion
- /// <summary>
- /// 重写排序方法
- /// </summary>
- /// <param name="query"></param>
- /// <param name="input"></param>
- /// <returns></returns>
- protected override IQueryable<SysFunction> ApplySorting(IQueryable<SysFunction> query, IwbPagedRequestDto input)
- {
- return query.OrderBy(a => a.FunctionType).ThenBy(a => a.Sort);
- }
- protected override SysFunction MapToEntity(FunctionCreateDto input)
- {
- return base.MapToEntity(input);
- }
- }
- }
|