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 : AbstractKeyCrudAppService where TEntity : class, IEntity, new() { protected AbstractKeyCrudAppService(IRepository repository, IMapper mapper) : base(repository, mapper) { } } public abstract class AbstractKeyCrudAppService : AbstractKeyCrudAppService where TEntity : class, IEntity, new() { protected AbstractKeyCrudAppService(IRepository repository, IMapper mapper) : base(repository, mapper) { } } public abstract class AbstractKeyCrudAppService : AbstractKeyCrudAppService where TEntity : class, IEntity, new() { protected AbstractKeyCrudAppService(IRepository repository, IMapper mapper) : base(repository, mapper) { } protected override Task MapToGetListOutputDtoAsync(TEntity entity) { return MapToGetOutputDtoAsync(entity); } protected override TEntityDto MapToGetListOutputDto(TEntity entity) { return MapToGetOutputDto(entity); } } public abstract class AbstractKeyCrudAppService : AbstractKeyReadOnlyAppService, ICrudAppService where TEntity : class, IEntity, new() { protected AbstractKeyCrudAppService(IRepository repository, IMapper mapper) : base(repository, mapper) { } /// /// 创建 /// /// /// public virtual async Task CreateAsync(TCreateInput input) { var entity = await MapToEntityAsync(input); TryToSetTenantId(entity); //这边需要进行判断,实体是什么guid还是雪花id await Repository.InsertReturnSnowflakeIdAsync(entity); var entitydto = await MapToGetOutputDtoAsync(entity); return entitydto; } /// /// 删除 /// /// /// /// public virtual Task DeleteAsync(IEnumerable ids) { throw new NotImplementedException(); } protected abstract Task DeleteByIdAsync(TKey id); /// /// 更新 /// /// /// /// public virtual async Task UpdateAsync(TKey id, TUpdateInput input) { 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; } /// /// 效验更新 /// /// /// /// protected virtual Task UpdateValidAsync(TEntity idEntity, TUpdateInput dto) { return Task.CompletedTask; } /// /// 将 更新输入dto转化为实体的异步 /// /// /// protected virtual Task MapToEntityAsync(TUpdateInput updateInput, TEntity entity) { MapToEntity(updateInput, entity); return Task.CompletedTask; } /// /// 将 更新输入dto转化为实体的同步方法 /// /// /// protected virtual void MapToEntity(TUpdateInput updateInput, TEntity entity) { ObjectMapper.Map(updateInput, entity); } /// /// 创建dto 给 实体的转换的异步方法 /// /// /// protected virtual Task MapToEntityAsync(TCreateInput createInput) { return Task.FromResult(MapToEntity(createInput)); } /// /// 创建dto 给 实体的转换 /// /// /// protected virtual TEntity MapToEntity(TCreateInput createInput) { var entity = ObjectMapper.Map(createInput); SetIdForGuids(entity); return entity; } /// /// 给主键id赋值上guid /// /// protected virtual void SetIdForGuids(TEntity entity) { if (entity is IEntity entityWithGuidId && entityWithGuidId.Id == Guid.Empty) { //这里给主键赋值为guid,l临时写死属性名 entity.GetType().GetProperty("Id").SetValue(entity, Guid.NewGuid()); //EntityHelper.TrySetId( // entityWithGuidId, // () => GuidGenerator.Create(), // true //); } } /// /// 给租户id赋值 /// /// 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); // } //} } /// /// 判断租户id的属性是否为空 /// /// /// protected virtual bool HasTenantIdProperty(TEntity entity) { return entity.GetType().GetProperty(nameof(IMultiTenant.TenantId)) != null; } } }