using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Text; using System.Threading.Tasks; using Yi.Framework.Infrastructure.Ddd.Dtos; using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract; using Yi.Framework.Infrastructure.Ddd.Entities; using Yi.Framework.Infrastructure.Ddd.Services.Abstract; using Yi.Framework.Infrastructure.Helper; namespace Yi.Framework.Infrastructure.Ddd.Services { public abstract class CrudAppService : CrudAppService where TEntity : class, IEntity where TEntityDto : IEntityDto { } public abstract class CrudAppService : CrudAppService where TEntity : class, IEntity where TEntityDto : IEntityDto { } public abstract class CrudAppService : CrudAppService where TEntity : class, IEntity where TEntityDto : IEntityDto { } public abstract class CrudAppService : CrudAppService where TEntity : class, IEntity where TEntityDto : IEntityDto { protected override Task MapToGetListOutputDtoAsync(TEntity entity) { return MapToGetOutputDtoAsync(entity); } } public abstract class CrudAppService : ReadOnlyAppService, ICrudAppService where TEntity : class, IEntity where TGetOutputDto : IEntityDto where TGetListOutputDto : IEntityDto { protected virtual Task MapToEntityAsync(TGetListInput getListinput) { return Task.FromResult(_mapper.Map(getListinput)); } protected virtual Task MapToEntityAsync(TCreateInput createInput) { var entity = _mapper.Map(createInput); //这里判断实体的T,给id赋值 //雪花id if (entity is IEntity entityForlongId) { if (entityForlongId.Id is default(long)) { //使用反射,暂时先使用sqlsuga的雪花id提供 //ps: linshi ReflexHelper.SetModelValue(nameof(IEntity.Id), SnowflakeHelper.NextId, entity); } } if (entity is IEntity entityForGuidId) { if (entityForGuidId.Id == Guid.Empty) { ReflexHelper.SetModelValue(nameof(IEntity.Id), new Guid(), entity); } } return Task.FromResult(entity); } protected virtual Task MapToEntityAsync(TUpdateInput updateInput, TEntity entity) { _mapper.Map(updateInput, entity); return Task.CompletedTask; } protected virtual Task MapToEntityAsync(TUpdateInput updateInput) { var entity = _mapper.Map(updateInput); return Task.FromResult(entity); } /// /// 增 /// /// /// public virtual async Task CreateAsync(TCreateInput input) { var entity = await MapToEntityAsync(input); //这里还可以设置租户 await _repository.InsertAsync(entity); return await MapToGetOutputDtoAsync(entity); } /// /// 单、多删 /// /// /// /// public virtual async Task DeleteAsync(string id) { if (id is null) { throw new ArgumentNullException(nameof(id)); } var idsValue = id.Split(','); if (idsValue is null || idsValue.Length == 0) { throw new ArgumentNullException(nameof(id)); } return await _repository.DeleteByIdsAsync(idsValue.Select(x => (object)x!).ToArray()); } ///// ///// 删 ///// ///// ///// ///// //public async Task DeleteAsync(TKey id) //{ // if (id is null) // { // throw new ArgumentNullException(nameof(id)); // } // return await _repository.DeleteByIdAsync(id); //} /// /// 改 /// /// /// /// /// public virtual async Task UpdateAsync(TKey id, TUpdateInput input) { if (id is null) { throw new ArgumentNullException(nameof(id)); } var entity = await _repository.GetByIdAsync(id); await MapToEntityAsync(input, entity); await _repository.UpdateAsync(entity); return await MapToGetOutputDtoAsync(entity); } } }