You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
2.0 KiB

3 years ago
3 years ago
3 years ago
3 years ago
  1. using Shentun.Peis.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Volo.Abp;
  8. using Volo.Abp.Domain.Entities;
  9. using Volo.Abp.Domain.Repositories;
  10. namespace Shentun.Peis
  11. {
  12. internal class EntityHelper
  13. {
  14. /// <summary>
  15. /// 创建最大显示顺序
  16. /// </summary>
  17. /// <typeparam name="TEntity"></typeparam>
  18. /// <param name="repository"></param>
  19. /// <returns></returns>
  20. public static async Task<int> CreateMaxDisplayOrder<TEntity>(IRepository<TEntity> repository)
  21. where TEntity: class, IEntity,IDisplayOrder
  22. {
  23. int? maxDisplayOrder = await repository.MaxAsync(o => (int?)o.DisplayOrder);
  24. return (maxDisplayOrder ?? 0) + 1;
  25. }
  26. /// <summary>
  27. /// 检查同名
  28. /// </summary>
  29. /// <typeparam name="TEntity"></typeparam>
  30. /// <typeparam name="TKey"></typeparam>
  31. /// <param name="repository"></param>
  32. /// <param name="name"></param>
  33. /// <param name="updatedEntity"></param>
  34. /// <returns></returns>
  35. /// <exception cref="UserFriendlyException"></exception>
  36. public static async Task CheckSameName<TEntity,TKey>(IRepository<TEntity> repository,string name, TEntity updatedEntity = null)
  37. where TEntity : class, IEntity<TKey>, IDisplayName
  38. {
  39. Check.NotNullOrWhiteSpace(name, nameof(name));
  40. TEntity existEntity;
  41. if (updatedEntity == null)
  42. {
  43. existEntity = await repository.FindAsync(o => o.DisplayName == name);
  44. }
  45. else
  46. {
  47. existEntity = await repository.FindAsync(o => o.Id.ToString() != updatedEntity.Id.ToString() && o.DisplayName == name);
  48. }
  49. if (existEntity != null)
  50. {
  51. throw new UserFriendlyException($"名称:'{name}'已存在");
  52. }
  53. }
  54. }
  55. }