| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- 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 WeApp.Authorization;
- using WeApp.BaseInfo;
- using WeApp.BaseSystem.States.Dto;
- using WeApp.Configuration;
- using IwbZero;
- using IwbZero.AppServiceBase;
- using IwbZero.Auditing;
- using IwbZero.Authorization.Base.Users;
- using IwbZero.ToolCommon.Lambda;
- namespace WeApp.BaseSystem.States
- {
- [AbpAuthorize, AuditLog("系统字典", "字典")]
- public class StatesAppService : IwbAsyncCrudAppService<SysState, StateDto, int, IwbPagedRequestDto, StateCreateDto, StateUpdateDto>, IStatesAppService
- {
- public StatesAppService(ICacheManager cacheManager, IRepository<SysState, int> 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<int> 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<SysState, int>
- {
- public StateAppService(ICacheManager cacheManager, IRepository<SysState, int> repository) : base(repository, cacheManager)
- {
- }
- public async Task Create(StateCreateDto input)
- {
- await CreateEntity<StateDto, StateCreateDto>(input);
- }
- }
- public class IwbAppServiceBase<TEntity, TPrimaryKey> : IwbAppServiceBase
- where TEntity : class, IEntity<TPrimaryKey>
- where TPrimaryKey : struct
- {
- public IwbAppServiceBase(IRepository<TEntity, TPrimaryKey> repository, ICacheManager cacheManager = null) : base(cacheManager)
- {
- Repository = repository;
- }
- protected IRepository<TEntity, TPrimaryKey> Repository { get; }
- /// <summary>
- /// 添加
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- protected async Task<TOut> CreateEntity<TOut, TIn>(TIn input)
- where TIn : IIwbEntityDto<TPrimaryKey>
- {
- var entity = await GetEntityById(input.Id ?? (TPrimaryKey)default);
- if (entity != null)
- {
- CheckErrors("");
- return default;
- }
- entity = ObjectMapper.Map<TEntity>(input);
- await Repository.InsertAsync(entity);
- await CurrentUnitOfWork.SaveChangesAsync();
- return ObjectMapper.Map<TOut>(entity);
- }
- /// <summary>
- /// 查询实体
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- [DisableAuditing]
- public virtual async Task<TEntity> GetEntityById(TPrimaryKey id)
- {
- var exp = LambdaHelper.CreatePrimaryKeyExp<TEntity, TPrimaryKey>(id);
- if (exp == null)
- {
- CheckErrors(string.Format(L("NoEntity"), id));
- }
- return await Repository.FirstOrDefaultAsync(exp);
- }
- }
- }
|