Files
Yi.Admin/Yi.Framework.Net6/Yi.Framework.Service/Base/Crud/AbstractKeyCrudAppService.cs

249 lines
8.3 KiB
C#
Raw Normal View History

2023-01-01 23:06:11 +08:00
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Interface.Base.Crud;
using Yi.Framework.Model.Base;
using Yi.Framework.Repository;
namespace Yi.Framework.Service.Base.Crud
{
public abstract class AbstractKeyCrudAppService<TEntity, TEntityDto, TKey>
: AbstractKeyCrudAppService<TEntity, TEntityDto, TKey, TEntityDto, TEntityDto>
where TEntity : class, IEntity, new()
{
}
public abstract class AbstractKeyCrudAppService<TEntity, TEntityDto, TKey, TCreateUpdateInput>
: AbstractKeyCrudAppService<TEntity, TEntityDto, TKey, TCreateUpdateInput, TCreateUpdateInput>
where TEntity : class, IEntity, new()
{
}
2023-01-05 19:21:48 +08:00
public abstract class AbstractKeyCrudAppService<TEntity, TEntityDto, TKey, TCreateInputDto, TUpdateInputDto>
: AbstractKeyCrudAppService<TEntity, TEntityDto, TEntityDto, TKey, TCreateInputDto, TUpdateInputDto>
2023-01-01 23:06:11 +08:00
where TEntity : class, IEntity, new()
{
protected override Task<TEntityDto> MapToGetListOutputDtoAsync(TEntity entity)
{
return MapToGetOutputDtoAsync(entity);
}
protected override TEntityDto MapToGetListOutputDto(TEntity entity)
{
return MapToGetOutputDto(entity);
}
}
2023-01-05 19:21:48 +08:00
public abstract class AbstractKeyCrudAppService<TEntity, TGetOutputDto, TListOutputDto, TKey, TCreateInputDto, TUpdateInputDto>
: AbstractKeyReadOnlyAppService<TEntity, TGetOutputDto, TListOutputDto, TKey>,
ICrudAppService<TGetOutputDto, TListOutputDto, TKey, TCreateInputDto, TUpdateInputDto>
2023-01-01 23:06:11 +08:00
where TEntity : class, IEntity, new()
{
/// <summary>
/// 创建
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
2023-01-05 19:21:48 +08:00
public virtual async Task<TGetOutputDto> CreateAsync(TCreateInputDto input)
2023-01-01 23:06:11 +08:00
{
2023-01-05 19:21:48 +08:00
2023-01-01 23:06:11 +08:00
var entity = await MapToEntityAsync(input);
TryToSetTenantId(entity);
2023-01-02 23:32:40 +08:00
//这边需要进行判断实体是什么guid还是雪花id
await Repository.InsertReturnSnowflakeIdAsync(entity);
2023-01-01 23:06:11 +08:00
var entitydto = await MapToGetOutputDtoAsync(entity);
return entitydto;
}
2023-01-04 20:50:10 +08:00
2023-01-05 19:21:48 +08:00
public async virtual Task CreateAsync(IEnumerable<TCreateInputDto> dtos)
2023-01-04 20:50:10 +08:00
{
var entity = await MapToEntitysAsync(dtos);
TryToSetTenantId(entity);
//这边需要进行判断实体是什么guid还是雪花id
await Repository.InsertReturnSnowflakeIdAsync(entity);
}
2023-01-01 23:06:11 +08:00
/// <summary>
/// 删除
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
2023-01-02 23:32:40 +08:00
public virtual Task DeleteAsync(IEnumerable<TKey> ids)
2023-01-01 23:06:11 +08:00
{
throw new NotImplementedException();
}
protected abstract Task DeleteByIdAsync(TKey id);
/// <summary>
/// 更新
/// </summary>
/// <param name="id"></param>
/// <param name="dto"></param>
/// <returns></returns>
2023-01-05 19:21:48 +08:00
public virtual async Task<TGetOutputDto> UpdateAsync(TKey id, TUpdateInputDto input)
2023-01-01 23:06:11 +08:00
{
var entity = await GetEntityByIdAsync(id);
await UpdateValidAsync(entity, input);
//TODO: Check if input has id different than given id and normalize if it's default value, throw ex otherwise
await MapToEntityAsync(input, entity);
await Repository.UpdateAsync(entity);
var entitydto = await MapToGetOutputDtoAsync(entity);
return entitydto;
}
/// <summary>
/// 效验更新
/// </summary>
/// <param name="idEntity"></param>
/// <param name="dto"></param>
/// <returns></returns>
2023-01-05 19:21:48 +08:00
protected virtual Task UpdateValidAsync(TEntity idEntity, TUpdateInputDto dto)
2023-01-01 23:06:11 +08:00
{
2023-01-02 00:09:30 +08:00
return Task.CompletedTask;
2023-01-01 23:06:11 +08:00
}
/// <summary>
2023-01-05 19:21:48 +08:00
/// 将 批量更新输入dto转化为实体的同步方法
2023-01-01 23:06:11 +08:00
/// </summary>
/// <param name="updateInput"></param>
2023-01-05 19:21:48 +08:00
/// <returns></returns>
protected virtual List<TEntity> MapToEntity(IEnumerable<TCreateInputDto> updateInput)
2023-01-01 23:06:11 +08:00
{
2023-01-05 19:21:48 +08:00
return ObjectMapper.Map<List<TEntity>>(updateInput);
2023-01-01 23:06:11 +08:00
}
2023-01-04 20:50:10 +08:00
2023-01-05 19:21:48 +08:00
2023-01-04 20:50:10 +08:00
/// <summary>
/// 将 批量更新输入dto转化为实体的异步
/// </summary>
/// <param name="updateInput"></param>
/// <param name="entity"></param>
2023-01-05 19:21:48 +08:00
protected virtual async Task<List<TEntity>> MapToEntitysAsync(IEnumerable<TCreateInputDto> updateInput)
2023-01-04 20:50:10 +08:00
{
2023-01-05 19:21:48 +08:00
List<TEntity> entitys = MapToEntity(updateInput);
2023-01-04 20:50:10 +08:00
return await Task.FromResult(entitys);
}
2023-01-01 23:06:11 +08:00
/// <summary>
/// 将 更新输入dto转化为实体的同步方法
/// </summary>
/// <param name="updateInput"></param>
/// <param name="entity"></param>
2023-01-05 19:21:48 +08:00
protected virtual void MapToEntity(TUpdateInputDto updateInput, TEntity entity)
2023-01-01 23:06:11 +08:00
{
ObjectMapper.Map(updateInput, entity);
}
2023-01-05 19:21:48 +08:00
2023-01-04 20:50:10 +08:00
/// <summary>
2023-01-05 19:21:48 +08:00
/// 将 更新输入dto转化为实体的异步
2023-01-04 20:50:10 +08:00
/// </summary>
/// <param name="updateInput"></param>
2023-01-05 19:21:48 +08:00
/// <param name="entity"></param>
protected virtual Task MapToEntityAsync(TUpdateInputDto updateInput, TEntity entity)
2023-01-04 20:50:10 +08:00
{
2023-01-05 19:21:48 +08:00
MapToEntity(updateInput, entity);
return Task.CompletedTask;
2023-01-04 20:50:10 +08:00
}
2023-01-01 23:06:11 +08:00
/// <summary>
/// 创建dto 给 实体的转换的异步方法
/// </summary>
/// <param name="createInput"></param>
/// <returns></returns>
2023-01-05 19:21:48 +08:00
protected virtual Task<TEntity> MapToEntityAsync(TCreateInputDto createInput)
2023-01-01 23:06:11 +08:00
{
return Task.FromResult(MapToEntity(createInput));
}
/// <summary>
/// 创建dto 给 实体的转换
/// </summary>
/// <param name="createInput"></param>
/// <returns></returns>
2023-01-05 19:21:48 +08:00
protected virtual TEntity MapToEntity(TCreateInputDto createInput)
2023-01-01 23:06:11 +08:00
{
2023-01-05 19:21:48 +08:00
var entity = ObjectMapper.Map<TCreateInputDto, TEntity>(createInput);
2023-01-01 23:06:11 +08:00
SetIdForGuids(entity);
return entity;
}
/// <summary>
/// 给主键id赋值上guid
/// </summary>
/// <param name="entity"></param>
protected virtual void SetIdForGuids(TEntity entity)
{
if (entity is IEntity<Guid> entityWithGuidId && entityWithGuidId.Id == Guid.Empty)
{
//这里给主键赋值为guid,l临时写死属性名
entity.GetType().GetProperty("Id").SetValue(entity, Guid.NewGuid());
//EntityHelper.TrySetId(
// entityWithGuidId,
// () => GuidGenerator.Create(),
// true
//);
}
}
/// <summary>
/// 给租户id赋值
/// </summary>
/// <param name="entity"></param>
protected virtual void TryToSetTenantId(TEntity entity)
{
//实现多租户接口
//if (entity is IMultiTenant)
//{
// //给属性租户id赋值
// if (ServiceLocator.GetTenantId(out var tid))
// {
// var tenantId = tid;
// var propertyInfo = entity.GetType().GetProperty(nameof(IMultiTenant.TenantId));
// if (propertyInfo == null || propertyInfo.GetSetMethod(true) == null)
// {
// return;
// }
// propertyInfo.SetValue(entity, tenantId);
// }
//}
}
2023-01-04 20:50:10 +08:00
protected virtual void TryToSetTenantId(IEnumerable<TEntity> entitys)
{
foreach (var entity in entitys)
{
TryToSetTenantId(entity);
}
}
2023-01-01 23:06:11 +08:00
/// <summary>
/// 判断租户id的属性是否为空
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
protected virtual bool HasTenantIdProperty(TEntity entity)
{
return entity.GetType().GetProperty(nameof(IMultiTenant.TenantId)) != null;
}
}
}