StatesAppService.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System.Threading.Tasks;
  2. using Abp.Application.Services.Dto;
  3. using Abp.Auditing;
  4. using Abp.Authorization;
  5. using Abp.Domain.Entities;
  6. using Abp.Domain.Repositories;
  7. using Abp.Runtime.Caching;
  8. using WePlatform.Authorization;
  9. using WePlatform.BaseInfo;
  10. using WePlatform.BaseSystem.States.Dto;
  11. using WePlatform.Configuration;
  12. using IwbZero;
  13. using IwbZero.AppServiceBase;
  14. using IwbZero.Auditing;
  15. using IwbZero.Authorization.Base.Users;
  16. using IwbZero.ToolCommon.Lambda;
  17. namespace WePlatform.BaseSystem.States
  18. {
  19. [AbpAuthorize, AuditLog("系统字典", "字典")]
  20. public class StatesAppService : IwbAsyncCrudAppService<SysState, StateDto, int, IwbPagedRequestDto, StateCreateDto, StateUpdateDto>, IStatesAppService
  21. {
  22. public StatesAppService(ICacheManager cacheManager, IRepository<SysState, int> repository) : base(repository, "StateNo")
  23. {
  24. CacheManager = cacheManager;
  25. }
  26. protected override string KeyExistMessage => string.Format(L(IwbLanguageMessage.KeyExistMessageFormatter), L("state"));
  27. protected override string KeyNotExistMessage => string.Format(L(IwbLanguageMessage.KeyNotExistMessageFormatter), L("state"));
  28. protected override string GetPermissionName { get; set; } = PermissionNames.PagesSystemMgStateMgQuery;
  29. protected override string GetAllPermissionName { get; set; } = PermissionNames.PagesSystemMgStateMgQuery;
  30. protected override string CreatePermissionName { get; set; } = PermissionNames.PagesSystemMgStateMgCreate;
  31. protected override string UpdatePermissionName { get; set; } = PermissionNames.PagesSystemMgStateMgUpdate;
  32. protected override string DeletePermissionName { get; set; } = PermissionNames.PagesSystemMgStateMgDelete;
  33. public override async Task Create(StateCreateDto input)
  34. {
  35. if (AbpSession.UserName != UserBase.HostAdminName && AbpSession.UserName != UserBase.HostSystemName && AbpSession.UserName != UserBase.AdminUserName && AbpSession.UserName != UserBase.SystemUserName)
  36. {
  37. ThrowError(IwbLanguageMessage.NoPermissionOperation);
  38. }
  39. await base.Create(input);
  40. }
  41. public override async Task Update(StateUpdateDto input)
  42. {
  43. if (AbpSession.UserName != UserBase.HostAdminName && AbpSession.UserName != UserBase.HostSystemName && AbpSession.UserName != UserBase.AdminUserName && AbpSession.UserName != UserBase.SystemUserName)
  44. {
  45. ThrowError(IwbLanguageMessage.NoPermissionOperation);
  46. }
  47. await UpdateEntity(input);
  48. await CacheManager.GetCache(IwbZeroConsts.SysStateCache)
  49. .RemoveAsync(input.TableName + "." + input.ColumnName + "." + input.CodeValue);
  50. }
  51. public override async Task Delete(EntityDto<int> input)
  52. {
  53. #if DEBUG
  54. if ( AbpSession.UserName != UserBase.HostAdminName && AbpSession.UserName != UserBase.HostSystemName && AbpSession.UserName != UserBase.AdminUserName && AbpSession.UserName != UserBase.SystemUserName )
  55. {
  56. ThrowError(IwbLanguageMessage.NoPermissionOperation);
  57. }
  58. await base.Delete(input);
  59. #else
  60. ThrowError(IwbLanguageMessage.CanNotDelete);
  61. #endif
  62. }
  63. }
  64. public class StateAppService : IwbAppServiceBase<SysState, int>
  65. {
  66. public StateAppService(ICacheManager cacheManager, IRepository<SysState, int> repository) : base(repository, cacheManager)
  67. {
  68. }
  69. public async Task Create(StateCreateDto input)
  70. {
  71. await CreateEntity<StateDto, StateCreateDto>(input);
  72. }
  73. }
  74. public class IwbAppServiceBase<TEntity, TPrimaryKey> : IwbAppServiceBase
  75. where TEntity : class, IEntity<TPrimaryKey>
  76. where TPrimaryKey : struct
  77. {
  78. public IwbAppServiceBase(IRepository<TEntity, TPrimaryKey> repository, ICacheManager cacheManager = null) : base(cacheManager)
  79. {
  80. Repository = repository;
  81. }
  82. protected IRepository<TEntity, TPrimaryKey> Repository { get; }
  83. /// <summary>
  84. /// 添加
  85. /// </summary>
  86. /// <param name="input"></param>
  87. /// <returns></returns>
  88. protected async Task<TOut> CreateEntity<TOut, TIn>(TIn input)
  89. where TIn : IIwbEntityDto<TPrimaryKey>
  90. {
  91. var entity = await GetEntityById(input.Id ?? (TPrimaryKey)default);
  92. if (entity != null)
  93. {
  94. CheckErrors("");
  95. return default;
  96. }
  97. entity = ObjectMapper.Map<TEntity>(input);
  98. await Repository.InsertAsync(entity);
  99. await CurrentUnitOfWork.SaveChangesAsync();
  100. return ObjectMapper.Map<TOut>(entity);
  101. }
  102. /// <summary>
  103. /// 查询实体
  104. /// </summary>
  105. /// <param name="id"></param>
  106. /// <returns></returns>
  107. [DisableAuditing]
  108. public virtual async Task<TEntity> GetEntityById(TPrimaryKey id)
  109. {
  110. var exp = LambdaHelper.CreatePrimaryKeyExp<TEntity, TPrimaryKey>(id);
  111. if (exp == null)
  112. {
  113. CheckErrors(string.Format(L("NoEntity"), id));
  114. }
  115. return await Repository.FirstOrDefaultAsync(exp);
  116. }
  117. }
  118. }