VberTechAsyncCrudAppService.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Dynamic.Core;
  5. using System.Threading.Tasks;
  6. using Abp.Application.Services.Dto;
  7. using Abp.Auditing;
  8. using Abp.Domain.Entities;
  9. using Abp.Domain.Repositories;
  10. using Abp.Extensions;
  11. using Abp.Linq;
  12. using Abp.ObjectMapping;
  13. using VberTech.Lambda;
  14. using IwbZero;
  15. using IwbZero.AppServiceBase;
  16. using IwbZero.Authorization.Permissions;
  17. using IwbZero.Authorization.Users;
  18. using IwbZero.IdentityFramework;
  19. using Microsoft.AspNet.Identity;
  20. using Abp.Authorization;
  21. using VberTech.Authorization.Permissions;
  22. namespace VberTech
  23. {
  24. public abstract class VberTechAsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TUpdateInput, TGetInput, TDeleteInput>
  25. : IwbCrudAppServiceBase<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TUpdateInput>
  26. where TEntity : class, IEntity<TPrimaryKey>
  27. where TEntityDto : IEntityDto<TPrimaryKey>
  28. where TUpdateInput : IEntityDto<TPrimaryKey>
  29. where TGetInput : IEntityDto<TPrimaryKey>
  30. where TDeleteInput : IEntityDto<TPrimaryKey>
  31. where TGetAllInput : IPagedRequest
  32. {
  33. public IAsyncQueryableExecuter AsyncQueryableExecuter { get; set; }
  34. protected string KeyFiledName { get; set; }
  35. protected string DataPermissionName { get; set; }
  36. protected virtual bool KeyIsAuto { get; set; } = true;
  37. protected virtual string ExistMessage { get; set; } = "编号已存在,请检查后再操作!";
  38. protected virtual string NotExistMessage { get; set; } = "编号不存在,请检查后再操作!";
  39. public new VberTechPermissionChecker PermissionChecker { protected get; set; }
  40. protected VberTechAsyncCrudAppService(IRepository<TEntity, TPrimaryKey> repository, string keyFiledName = null,string dataPermissionName = null)
  41. : base(repository)
  42. {
  43. AsyncQueryableExecuter = NullAsyncQueryableExecuter.Instance;
  44. LocalizationSourceName = IwbZeroConsts.IwbZeroLocalizationSourceName;
  45. ObjectMapper = NullObjectMapper.Instance;
  46. KeyFiledName = keyFiledName;
  47. DataPermissionName = dataPermissionName;
  48. }
  49. [DisableAuditing]
  50. public virtual async Task<TEntityDto> Get(TGetInput input)
  51. {
  52. CheckGetPermission();
  53. if (!KeyFiledName.IsNullOrEmpty() && input.Id == null)
  54. {
  55. return await GetDtoByNo(input.GetFiledValue<string>(KeyFiledName));
  56. }
  57. var entity = await GetEntityById(input.Id);
  58. return MapToEntityDto(entity);
  59. }
  60. [DisableAuditing]
  61. public virtual async Task<PagedResultDto<TEntityDto>> GetAll(TGetAllInput input)
  62. {
  63. CheckGetAllPermission();
  64. var query = CreateFilteredQuery(input);
  65. query = ApplyFilter(query, input);
  66. var totalCount = await AsyncQueryableExecuter.CountAsync(query);
  67. query = ApplySorting(query, input);
  68. query = ApplyPaging(query, input);
  69. var entities = await AsyncQueryableExecuter.ToListAsync(query);
  70. var dtoList = new PagedResultDto<TEntityDto>(
  71. totalCount,
  72. entities.Select(MapToEntityDto).ToList()
  73. );
  74. return dtoList;
  75. }
  76. public virtual async Task<TEntityDto> Create(TCreateInput input)
  77. {
  78. CheckCreatePermission();
  79. return await CreateEntity(input);
  80. }
  81. public virtual async Task<TEntityDto> Update(TUpdateInput input)
  82. {
  83. CheckCreatePermission();
  84. return await UpdateEntity(input);
  85. }
  86. public virtual Task Delete(TDeleteInput input)
  87. {
  88. CheckDeletePermission();
  89. return Repository.DeleteAsync(input.Id);
  90. }
  91. protected virtual IQueryable<T> ApplyFilterEx<T>(IQueryable<T> query, TGetAllInput input)
  92. {
  93. if (!input.KeyWords.IsNullOrEmpty())
  94. {
  95. object keyWords = input.KeyWords;
  96. LambdaObject obj = new LambdaObject()
  97. {
  98. FieldType = (LambdaFieldType)input.FieldType,
  99. FieldName = input.KeyField,
  100. FieldValue = keyWords,
  101. ExpType = (LambdaExpType)input.ExpType
  102. };
  103. var exp = obj.GetExp<T>();
  104. query = query.Where(exp);
  105. }
  106. if (input.SearchList != null && input.SearchList.Count > 0)
  107. {
  108. List<LambdaObject> objList = new List<LambdaObject>();
  109. foreach (var o in input.SearchList)
  110. {
  111. if (o.KeyWords.IsNullOrEmpty())
  112. continue;
  113. object keyWords = o.KeyWords;
  114. objList.Add(new LambdaObject
  115. {
  116. FieldType = (LambdaFieldType)o.FieldType,
  117. FieldName = o.KeyField,
  118. FieldValue = keyWords,
  119. ExpType = (LambdaExpType)o.ExpType
  120. });
  121. }
  122. var exp = objList.GetExp<T>();
  123. query = query.Where(exp);
  124. }
  125. return query;
  126. }
  127. protected virtual IQueryable<TEntity> ApplyFilter(IQueryable<TEntity> query, TGetAllInput input)
  128. {
  129. if (!input.KeyWords.IsNullOrEmpty())
  130. {
  131. object keyWords = input.KeyWords;
  132. LambdaObject obj = new LambdaObject()
  133. {
  134. FieldType = (LambdaFieldType)input.FieldType,
  135. FieldName = input.KeyField,
  136. FieldValue = keyWords,
  137. ExpType = (LambdaExpType)input.ExpType
  138. };
  139. var exp = obj.GetExp<TEntity>();
  140. query = query.Where(exp);
  141. }
  142. if (input.SearchList != null && input.SearchList.Count > 0)
  143. {
  144. List<LambdaObject> objList = new List<LambdaObject>();
  145. foreach (var o in input.SearchList)
  146. {
  147. if (o.KeyWords.IsNullOrEmpty())
  148. continue;
  149. object keyWords = o.KeyWords;
  150. objList.Add(new LambdaObject
  151. {
  152. FieldType = (LambdaFieldType)o.FieldType,
  153. FieldName = o.KeyField,
  154. FieldValue = keyWords,
  155. ExpType = (LambdaExpType)o.ExpType
  156. });
  157. }
  158. var exp = objList.GetExp<TEntity>();
  159. query = query.Where(exp);
  160. }
  161. return query;
  162. }
  163. protected virtual IQueryable<T> ApplySortingEx<T>(IQueryable<T> query, TGetAllInput input)
  164. {
  165. //Try to sort query if available
  166. // ReSharper disable once SuspiciousTypeConversion.Global
  167. if (input is ISortedResultRequest sortInput)
  168. {
  169. if (!sortInput.Sorting.IsNullOrWhiteSpace())
  170. {
  171. return query.OrderBy(sortInput.Sorting);
  172. }
  173. }
  174. //IQueryable.Task requires sorting, so we should sort if Take will be used.
  175. // ReSharper disable once IsExpressionAlwaysTrue
  176. if (input is ILimitedResultRequest)
  177. {
  178. if (!input.Sorting.IsNullOrWhiteSpace())
  179. {
  180. return query.OrderBy(input.Sorting);
  181. }
  182. //return query.OrderByDescending(e => e.Id);
  183. }
  184. //No sorting
  185. return query;
  186. }
  187. /// <summary>
  188. /// Should apply sorting if needed.
  189. /// </summary>
  190. /// <param name="query">The query.</param>
  191. /// <param name="input">The input.</param>
  192. protected new virtual IQueryable<TEntity> ApplySorting(IQueryable<TEntity> query, TGetAllInput input)
  193. {
  194. //Try to sort query if available
  195. // ReSharper disable once SuspiciousTypeConversion.Global
  196. if (input is ISortedResultRequest sortInput)
  197. {
  198. if (!sortInput.Sorting.IsNullOrWhiteSpace())
  199. {
  200. return query.OrderBy(sortInput.Sorting);
  201. }
  202. }
  203. //IQueryable.Task requires sorting, so we should sort if Take will be used.
  204. // ReSharper disable once IsExpressionAlwaysTrue
  205. if (input is ILimitedResultRequest)
  206. {
  207. if (!input.Sorting.IsNullOrWhiteSpace())
  208. {
  209. return query.OrderBy(input.Sorting);
  210. }
  211. return query.OrderByDescending(e => e.Id);
  212. }
  213. //No sorting
  214. return query;
  215. }
  216. /// <summary>
  217. /// Should apply paging if needed.
  218. /// </summary>
  219. /// <param name="query">The query.</param>
  220. /// <param name="input">The input.</param>
  221. protected new virtual IQueryable<TEntity> ApplyPaging(IQueryable<TEntity> query, TGetAllInput input)
  222. {
  223. //Try to use paging if available
  224. if (input is IPagedResultRequest pagedInput)
  225. {
  226. return query.Skip(pagedInput.SkipCount).Take(pagedInput.MaxResultCount);
  227. }
  228. //No paging
  229. return query;
  230. }
  231. public virtual Task<TEntity> GetEntityById(TPrimaryKey id)
  232. {
  233. return Repository.GetAsync(id);
  234. }
  235. public virtual Task<TEntity> GetEntityByNo(string no)
  236. {
  237. if (KeyFiledName.IsNullOrEmpty())
  238. {
  239. CheckErrors(IwbIdentityResult.Failed("编码/编号字段不明确,请检查后再操作!"));
  240. }
  241. LambdaObject obj = new LambdaObject()
  242. {
  243. FieldType = LambdaFieldType.S,
  244. FieldName = KeyFiledName,
  245. FieldValue = no,
  246. ExpType = LambdaExpType.Equal
  247. };
  248. var exp = obj.GetExp<TEntity>();
  249. return Repository.FirstOrDefaultAsync(exp);
  250. }
  251. public virtual async Task<TEntityDto> GetDtoById(TPrimaryKey id)
  252. {
  253. var entity = await GetEntityById(id);
  254. return MapToEntityDto(entity);
  255. }
  256. public virtual async Task<TEntityDto> GetDtoByNo(string no)
  257. {
  258. var entity = await GetEntityByNo(no);
  259. return MapToEntityDto(entity);
  260. }
  261. protected async Task<TEntityDto> CreateEntity(TCreateInput input)
  262. {
  263. if (!KeyFiledName.IsNullOrEmpty())
  264. {
  265. if (KeyIsAuto)
  266. {
  267. input.SetFiledValue(KeyFiledName, Guid.NewGuid().ToString("N"));
  268. }
  269. else
  270. {
  271. LambdaObject obj = new LambdaObject()
  272. {
  273. FieldType = LambdaFieldType.S,
  274. FieldName = KeyFiledName,
  275. FieldValue = input.GetFiledValue<string>(KeyFiledName),
  276. ExpType = LambdaExpType.Equal
  277. };
  278. var exp = obj.GetExp<TEntity>();
  279. if (await Repository.FirstOrDefaultAsync(exp) != null)
  280. {
  281. CheckErrors(IwbIdentityResult.Failed(ExistMessage));
  282. }
  283. }
  284. }
  285. var entity = MapToEntity(input);
  286. await Repository.InsertAsync(entity);
  287. await CurrentUnitOfWork.SaveChangesAsync();
  288. return MapToEntityDto(entity);
  289. }
  290. protected async Task<TEntityDto> UpdateEntity(TUpdateInput input)
  291. {
  292. if (!KeyIsAuto && !KeyFiledName.IsNullOrEmpty())
  293. {
  294. LambdaObject obj = new LambdaObject()
  295. {
  296. FieldType = LambdaFieldType.S,
  297. FieldName = KeyFiledName,
  298. FieldValue = input.GetFiledValue<string>(KeyFiledName),
  299. ExpType = LambdaExpType.Equal
  300. };
  301. var exp = obj.GetExp<TEntity>();
  302. if (await Repository.FirstOrDefaultAsync(exp) == null)
  303. {
  304. CheckErrors(IwbIdentityResult.Failed(NotExistMessage));
  305. }
  306. }
  307. var entity = await GetEntityById(input.Id);
  308. MapToEntity(input, entity);
  309. await CurrentUnitOfWork.SaveChangesAsync();
  310. return MapToEntityDto(entity);
  311. }
  312. protected virtual void CheckErrors(params string[] error)
  313. {
  314. var identityResult = IwbIdentityResult.Failed(error);
  315. identityResult.CheckErrors(LocalizationManager);
  316. }
  317. protected virtual void CheckErrors(IdentityResult identityResult)
  318. {
  319. identityResult.CheckErrors(LocalizationManager);
  320. }
  321. protected virtual int CheckGuid(int? guid)
  322. {
  323. if (guid == null || guid == 0)
  324. {
  325. CheckErrors(IwbIdentityResult.Failed("获取Guid编号失败!"));
  326. return 0;
  327. }
  328. return (int)guid;
  329. }
  330. protected virtual void CheckDataPermission(string permissionName, string key, int operType)
  331. {
  332. if (AbpSession.UserName != UserBase.AdminUserName && AbpSession.UserName != UserBase.SystemUserName && !string.IsNullOrEmpty(permissionName))
  333. PermissionChecker.Authorize(permissionName, key, operType);
  334. }
  335. protected virtual void CheckDataQueryPermission(string permissionName, string key)
  336. {
  337. CheckDataPermission(permissionName, key, (int)OperType.Query);
  338. }
  339. protected virtual void CheckDataUpdatePermission(string permissionName, string key)
  340. {
  341. CheckDataPermission(permissionName, key, (int)OperType.Update);
  342. }
  343. protected virtual void CheckDataDeletePermission(string permissionName, string key)
  344. {
  345. CheckDataPermission(permissionName, key, (int)OperType.Delete);
  346. }
  347. }
  348. #region AppService
  349. public abstract class VberTechAsyncCrudAppService<TEntity, TEntityDto>
  350. : VberTechAsyncCrudAppService<TEntity, TEntityDto, int>
  351. where TEntity : class, IEntity<int>
  352. where TEntityDto : IEntityDto<int>
  353. {
  354. protected VberTechAsyncCrudAppService(IRepository<TEntity, int> repository, string keyFiledName = null, string dataPermissionName = null)
  355. : base(repository)
  356. {
  357. KeyFiledName = keyFiledName;
  358. DataPermissionName = dataPermissionName;
  359. }
  360. }
  361. public abstract class VberTechAsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey>
  362. : VberTechAsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, IPagedRequest>
  363. where TEntity : class, IEntity<TPrimaryKey>
  364. where TEntityDto : IEntityDto<TPrimaryKey>
  365. {
  366. protected VberTechAsyncCrudAppService(IRepository<TEntity, TPrimaryKey> repository, string keyFiledName = null, string dataPermissionName = null)
  367. : base(repository)
  368. {
  369. KeyFiledName = keyFiledName;
  370. DataPermissionName = dataPermissionName;
  371. }
  372. }
  373. public abstract class VberTechAsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput>
  374. : VberTechAsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TEntityDto, TEntityDto>
  375. where TEntity : class, IEntity<TPrimaryKey>
  376. where TEntityDto : IEntityDto<TPrimaryKey>
  377. where TGetAllInput : IPagedRequest
  378. {
  379. protected VberTechAsyncCrudAppService(IRepository<TEntity, TPrimaryKey> repository, string keyFiledName = null, string dataPermissionName = null)
  380. : base(repository)
  381. {
  382. KeyFiledName = keyFiledName;
  383. DataPermissionName = dataPermissionName;
  384. }
  385. }
  386. public abstract class VberTechAsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput>
  387. : VberTechAsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TCreateInput>
  388. where TGetAllInput : IPagedRequest
  389. where TEntity : class, IEntity<TPrimaryKey>
  390. where TEntityDto : IEntityDto<TPrimaryKey>
  391. where TCreateInput : IEntityDto<TPrimaryKey>
  392. {
  393. protected VberTechAsyncCrudAppService(IRepository<TEntity, TPrimaryKey> repository, string keyFiledName = null, string dataPermissionName = null)
  394. : base(repository)
  395. {
  396. KeyFiledName = keyFiledName;
  397. DataPermissionName = dataPermissionName;
  398. }
  399. }
  400. public abstract class VberTechAsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TUpdateInput>
  401. : VberTechAsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TUpdateInput, EntityDto<TPrimaryKey>>
  402. where TEntity : class, IEntity<TPrimaryKey>
  403. where TEntityDto : IEntityDto<TPrimaryKey>
  404. where TUpdateInput : IEntityDto<TPrimaryKey>
  405. where TGetAllInput : IPagedRequest
  406. {
  407. protected VberTechAsyncCrudAppService(IRepository<TEntity, TPrimaryKey> repository, string keyFiledName = null, string dataPermissionName = null)
  408. : base(repository)
  409. {
  410. KeyFiledName = keyFiledName;
  411. DataPermissionName = dataPermissionName;
  412. }
  413. }
  414. public abstract class VberTechAsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TUpdateInput, TGetInput>
  415. : VberTechAsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TUpdateInput, TGetInput, EntityDto<TPrimaryKey>>
  416. where TEntity : class, IEntity<TPrimaryKey>
  417. where TEntityDto : IEntityDto<TPrimaryKey>
  418. where TUpdateInput : IEntityDto<TPrimaryKey>
  419. where TGetInput : IEntityDto<TPrimaryKey>
  420. where TGetAllInput : IPagedRequest
  421. {
  422. protected VberTechAsyncCrudAppService(IRepository<TEntity, TPrimaryKey> repository, string keyFiledName = null, string dataPermissionName = null)
  423. : base(repository)
  424. {
  425. DataPermissionName = dataPermissionName;
  426. KeyFiledName = keyFiledName;
  427. }
  428. }
  429. #endregion
  430. }