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

using Shentun.Peis.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;
namespace Shentun.Peis
{
internal class EntityHelper
{
/// <summary>
/// 创建最大显示顺序
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="repository"></param>
/// <returns></returns>
public static async Task<int> CreateMaxDisplayOrder<TEntity>(IRepository<TEntity> repository)
where TEntity: class, IEntity,IDisplayOrder
{
int? maxDisplayOrder = await repository.MaxAsync(o => (int?)o.DisplayOrder);
return (maxDisplayOrder ?? 0) + 1;
}
/// <summary>
/// 检查同名
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <param name="repository"></param>
/// <param name="name"></param>
/// <param name="updatedEntity"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
public static async Task CheckSameName<TEntity,TKey>(IRepository<TEntity> repository,string name, TEntity updatedEntity = null)
where TEntity : class, IEntity<TKey>, IDisplayName
{
Check.NotNullOrWhiteSpace(name, nameof(name));
TEntity existEntity;
if (updatedEntity == null)
{
existEntity = await repository.FindAsync(o => o.DisplayName == name);
}
else
{
existEntity = await repository.FindAsync(o => o.Id.ToString() != updatedEntity.Id.ToString() && o.DisplayName == name);
}
if (existEntity != null)
{
throw new UserFriendlyException($"名称:'{name}'已存在");
}
}
}
}