using System.Threading.Tasks; using Abp.Application.Services.Dto; using Abp.Auditing; using Abp.Authorization; using Abp.Domain.Entities; using Abp.Domain.Repositories; using Abp.Runtime.Caching; using WeOnlineApp.Authorization; using WeOnlineApp.BaseInfo; using WeOnlineApp.BaseSystem.States.Dto; using WeOnlineApp.Configuration; using IwbZero; using IwbZero.AppServiceBase; using IwbZero.Auditing; using IwbZero.Authorization.Base.Users; using IwbZero.ToolCommon.Lambda; namespace WeOnlineApp.BaseSystem.States { [AbpAuthorize, AuditLog("系统字典", "字典")] public class StatesAppService : IwbAsyncCrudAppService, IStatesAppService { public StatesAppService(ICacheManager cacheManager, IRepository repository) : base(repository, "StateNo") { CacheManager = cacheManager; } protected override string KeyExistMessage => string.Format(L(IwbLanguageMessage.KeyExistMessageFormatter), L("state")); protected override string KeyNotExistMessage => string.Format(L(IwbLanguageMessage.KeyNotExistMessageFormatter), L("state")); protected override string GetPermissionName { get; set; } = PermissionNames.PagesSystemMgStateMgQuery; protected override string GetAllPermissionName { get; set; } = PermissionNames.PagesSystemMgStateMgQuery; protected override string CreatePermissionName { get; set; } = PermissionNames.PagesSystemMgStateMgCreate; protected override string UpdatePermissionName { get; set; } = PermissionNames.PagesSystemMgStateMgUpdate; protected override string DeletePermissionName { get; set; } = PermissionNames.PagesSystemMgStateMgDelete; public override async Task Create(StateCreateDto input) { if (AbpSession.UserName != UserBase.HostAdminName && AbpSession.UserName != UserBase.HostSystemName && AbpSession.UserName != UserBase.AdminUserName && AbpSession.UserName != UserBase.SystemUserName) { ThrowError(IwbLanguageMessage.NoPermissionOperation); } await base.Create(input); } public override async Task Update(StateUpdateDto input) { if (AbpSession.UserName != UserBase.HostAdminName && AbpSession.UserName != UserBase.HostSystemName && AbpSession.UserName != UserBase.AdminUserName && AbpSession.UserName != UserBase.SystemUserName) { ThrowError(IwbLanguageMessage.NoPermissionOperation); } await UpdateEntity(input); await CacheManager.GetCache(IwbZeroConsts.SysStateCache) .RemoveAsync(input.TableName + "." + input.ColumnName + "." + input.CodeValue); } public override async Task Delete(EntityDto input) { #if DEBUG if ( AbpSession.UserName != UserBase.HostAdminName && AbpSession.UserName != UserBase.HostSystemName && AbpSession.UserName != UserBase.AdminUserName && AbpSession.UserName != UserBase.SystemUserName ) { ThrowError(IwbLanguageMessage.NoPermissionOperation); } await base.Delete(input); #else ThrowError(IwbLanguageMessage.CanNotDelete); #endif } } public class StateAppService : IwbAppServiceBase { public StateAppService(ICacheManager cacheManager, IRepository repository) : base(repository, cacheManager) { } public async Task Create(StateCreateDto input) { await CreateEntity(input); } } public class IwbAppServiceBase : IwbAppServiceBase where TEntity : class, IEntity where TPrimaryKey : struct { public IwbAppServiceBase(IRepository repository, ICacheManager cacheManager = null) : base(cacheManager) { Repository = repository; } protected IRepository Repository { get; } /// /// 添加 /// /// /// protected async Task CreateEntity(TIn input) where TIn : IIwbEntityDto { var entity = await GetEntityById(input.Id ?? (TPrimaryKey)default); if (entity != null) { CheckErrors(""); return default; } entity = ObjectMapper.Map(input); await Repository.InsertAsync(entity); await CurrentUnitOfWork.SaveChangesAsync(); return ObjectMapper.Map(entity); } /// /// 查询实体 /// /// /// [DisableAuditing] public virtual async Task GetEntityById(TPrimaryKey id) { var exp = LambdaHelper.CreatePrimaryKeyExp(id); if (exp == null) { CheckErrors(string.Format(L("NoEntity"), id)); } return await Repository.FirstOrDefaultAsync(exp); } } }