Files

190 lines
6.6 KiB
C#
Raw Permalink Normal View History

2024-01-02 23:26:05 +08:00
using Microsoft.AspNetCore.Mvc;
2024-01-02 18:32:38 +08:00
using MiniExcelLibs;
2023-12-11 09:55:12 +08:00
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
{
2024-09-24 11:16:19 +08:00
public abstract class
YiCrudAppService<TEntity, TEntityDto, TKey> : YiCrudAppService<TEntity, TEntityDto, TKey,
PagedAndSortedResultRequestDto>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
2023-12-11 09:55:12 +08:00
{
protected YiCrudAppService(IRepository<TEntity, TKey> repository) : base(repository)
{
}
}
public abstract class YiCrudAppService<TEntity, TEntityDto, TKey, TGetListInput>
: YiCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TEntityDto>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
protected YiCrudAppService(IRepository<TEntity, TKey> repository) : base(repository)
{
}
}
public abstract class YiCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput>
: YiCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TCreateInput>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
protected YiCrudAppService(IRepository<TEntity, TKey> repository) : base(repository)
{
}
}
public abstract class YiCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
: YiCrudAppService<TEntity, TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
protected YiCrudAppService(IRepository<TEntity, TKey> repository) : base(repository)
{
}
}
2024-09-24 11:16:19 +08:00
public abstract class YiCrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput,
TUpdateInput>
2023-12-11 09:55:12 +08:00
: CrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
2024-09-24 11:16:19 +08:00
where TEntity : class, IEntity<TKey>
where TGetOutputDto : IEntityDto<TKey>
where TGetListOutputDto : IEntityDto<TKey>
2023-12-11 09:55:12 +08:00
{
protected YiCrudAppService(IRepository<TEntity, TKey> repository) : base(repository)
{
}
2024-09-24 11:16:19 +08:00
public override async Task<TGetOutputDto> 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<TGetOutputDto> 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;
}
2024-01-02 23:26:05 +08:00
/// <summary>
/// 多查
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
2023-12-14 14:15:56 +08:00
public override async Task<PagedResultDto<TGetListOutputDto>> GetListAsync(TGetListInput input)
2023-12-11 22:14:13 +08:00
{
2023-12-14 14:15:56 +08:00
List<TEntity>? entites = null;
//区分多查还是批量查
if (input is IPagedResultRequest pagedInput)
{
2024-09-24 11:16:19 +08:00
entites = await Repository.GetPagedListAsync(pagedInput.SkipCount, pagedInput.MaxResultCount,
string.Empty);
2023-12-14 14:15:56 +08:00
}
else
{
entites = await Repository.GetListAsync();
}
2024-09-24 11:16:19 +08:00
var total = await Repository.GetCountAsync();
2023-12-14 14:15:56 +08:00
var output = await MapToGetListOutputDtosAsync(entites);
return new PagedResultDto<TGetListOutputDto>(total, output);
//throw new NotImplementedException($"【{typeof(TEntity)}】实体的CrudAppService查询为具体业务通用查询几乎无实际场景请重写实现");
2023-12-11 22:14:13 +08:00
}
2023-12-11 09:55:12 +08:00
/// <summary>
2024-01-02 23:26:05 +08:00
/// 多删
2023-12-11 09:55:12 +08:00
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[RemoteService(isEnabled: true)]
2024-01-02 18:32:38 +08:00
public virtual async Task DeleteAsync(IEnumerable<TKey> id)
2023-12-11 09:55:12 +08:00
{
await Repository.DeleteManyAsync(id);
}
2024-01-02 23:26:05 +08:00
/// <summary>
/// 偷梁换柱
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
2023-12-11 22:14:13 +08:00
[RemoteService(isEnabled: false)]
2023-12-11 09:55:12 +08:00
public override Task DeleteAsync(TKey id)
{
return base.DeleteAsync(id);
}
2024-01-02 18:32:38 +08:00
2024-01-02 23:26:05 +08:00
/// <summary>
/// 导出excel
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
2024-01-02 18:32:38 +08:00
public virtual async Task<IActionResult> 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";
2024-01-02 23:26:05 +08:00
var fileName = $"{typeof(TEntity).Name}_{DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")}_{Guid.NewGuid()}";
var filePath = $"{dirPath}/{fileName}.xlsx";
2024-01-02 18:32:38 +08:00
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
MiniExcel.SaveAs(filePath, output.Items);
2024-01-02 23:26:05 +08:00
return new PhysicalFileResult(filePath, "application/vnd.ms-excel");
2024-01-02 18:32:38 +08:00
}
2024-01-02 23:26:05 +08:00
/// <summary>
/// 导入excle
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public virtual async Task PostImportExcelAsync(List<TCreateInput> input)
2024-01-02 18:32:38 +08:00
{
2024-01-02 23:26:05 +08:00
var entities = input.Select(x => MapToEntity(x)).ToList();
2024-01-03 12:33:39 +08:00
//安全起见,该接口需要自己实现
throw new NotImplementedException();
//await Repository.DeleteManyAsync(entities.Select(x => x.Id));
//await Repository.InsertManyAsync(entities);
2024-01-02 18:32:38 +08:00
}
2023-12-11 09:55:12 +08:00
}
2024-09-24 11:16:19 +08:00
}