using Microsoft.AspNetCore.Mvc; using MiniExcelLibs; using Volo.Abp; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Repositories; namespace Yi.Framework.Ddd.Application { public abstract class YiCrudAppService : YiCrudAppService where TEntity : class, IEntity where TEntityDto : IEntityDto { protected YiCrudAppService(IRepository repository) : base(repository) { } } public abstract class YiCrudAppService : YiCrudAppService where TEntity : class, IEntity where TEntityDto : IEntityDto { protected YiCrudAppService(IRepository repository) : base(repository) { } } public abstract class YiCrudAppService : YiCrudAppService where TEntity : class, IEntity where TEntityDto : IEntityDto { protected YiCrudAppService(IRepository repository) : base(repository) { } } public abstract class YiCrudAppService : YiCrudAppService where TEntity : class, IEntity where TEntityDto : IEntityDto { protected YiCrudAppService(IRepository repository) : base(repository) { } } public abstract class YiCrudAppService : CrudAppService where TEntity : class, IEntity where TGetOutputDto : IEntityDto where TGetListOutputDto : IEntityDto { protected YiCrudAppService(IRepository repository) : base(repository) { } public override async Task UpdateAsync(TKey id, TUpdateInput input) { await CheckUpdatePolicyAsync(); var entity = await GetEntityByIdAsync(id); await CheckUpdateInputDtoAsync(entity,input); await MapToEntityAsync(input, entity); await Repository.UpdateAsync(entity, autoSave: true); return await MapToGetOutputDtoAsync(entity); } protected virtual Task CheckUpdateInputDtoAsync(TEntity entity,TUpdateInput input) { return Task.CompletedTask; } public override async Task CreateAsync(TCreateInput input) { await CheckCreatePolicyAsync(); await CheckCreateInputDtoAsync(input); var entity = await MapToEntityAsync(input); TryToSetTenantId(entity); await Repository.InsertAsync(entity, autoSave: true); return await MapToGetOutputDtoAsync(entity); } protected virtual Task CheckCreateInputDtoAsync(TCreateInput input) { return Task.CompletedTask; } /// /// 多查 /// /// /// public override async Task> GetListAsync(TGetListInput input) { List? entites = null; //区分多查还是批量查 if (input is IPagedResultRequest pagedInput) { entites = await Repository.GetPagedListAsync(pagedInput.SkipCount, pagedInput.MaxResultCount, string.Empty); } else { entites = await Repository.GetListAsync(); } var total = await Repository.GetCountAsync(); var output = await MapToGetListOutputDtosAsync(entites); return new PagedResultDto(total, output); //throw new NotImplementedException($"【{typeof(TEntity)}】实体的CrudAppService,查询为具体业务,通用查询几乎无实际场景,请重写实现!"); } /// /// 多删 /// /// /// [RemoteService(isEnabled: true)] public virtual async Task DeleteAsync(IEnumerable id) { await Repository.DeleteManyAsync(id); } /// /// 偷梁换柱 /// /// /// [RemoteService(isEnabled: false)] public override Task DeleteAsync(TKey id) { return base.DeleteAsync(id); } /// /// 导出excel /// /// /// public virtual async Task GetExportExcelAsync(TGetListInput input) { if (input is IPagedResultRequest paged) { paged.SkipCount = 0; paged.MaxResultCount = LimitedResultRequestDto.MaxMaxResultCount; } var output = await this.GetListAsync(input); var dirPath = $"/wwwroot/temp"; var fileName = $"{typeof(TEntity).Name}_{DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")}_{Guid.NewGuid()}"; var filePath = $"{dirPath}/{fileName}.xlsx"; if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } MiniExcel.SaveAs(filePath, output.Items); return new PhysicalFileResult(filePath, "application/vnd.ms-excel"); } /// /// 导入excle /// /// /// public virtual async Task PostImportExcelAsync(List input) { var entities = input.Select(x => MapToEntity(x)).ToList(); //安全起见,该接口需要自己实现 throw new NotImplementedException(); //await Repository.DeleteManyAsync(entities.Select(x => x.Id)); //await Repository.InsertManyAsync(entities); } } }