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
{
///
/// CRUD应用服务基类 - 基础版本
///
public abstract class YiCrudAppService
: YiCrudAppService
where TEntity : class, IEntity
where TEntityDto : IEntityDto
{
protected YiCrudAppService(IRepository repository)
: base(repository)
{
}
}
///
/// CRUD应用服务基类 - 支持自定义查询输入
///
public abstract class YiCrudAppService
: YiCrudAppService
where TEntity : class, IEntity
where TEntityDto : IEntityDto
{
protected YiCrudAppService(IRepository repository)
: base(repository)
{
}
}
///
/// CRUD应用服务基类 - 支持自定义创建输入
///
public abstract class YiCrudAppService
: YiCrudAppService
where TEntity : class, IEntity
where TEntityDto : IEntityDto
{
protected YiCrudAppService(IRepository repository)
: base(repository)
{
}
}
///
/// CRUD应用服务基类 - 支持自定义更新输入
///
public abstract class YiCrudAppService
: YiCrudAppService
where TEntity : class, IEntity
where TEntityDto : IEntityDto
{
protected YiCrudAppService(IRepository repository)
: base(repository)
{
}
}
///
/// CRUD应用服务基类 - 完整实现
///
public abstract class YiCrudAppService
: CrudAppService
where TEntity : class, IEntity
where TGetOutputDto : IEntityDto
where TGetListOutputDto : IEntityDto
{
///
/// 临时文件存储路径
///
private const string TempFilePath = "/wwwroot/temp";
protected YiCrudAppService(IRepository repository)
: base(repository)
{
}
///
/// 更新实体
///
/// 实体ID
/// 更新输入
/// 更新后的实体DTO
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;
}
///
/// 创建实体
///
/// 创建输入
/// 创建后的实体DTO
public override async Task CreateAsync(TCreateInput input)
{
// 检查创建权限
await CheckCreatePolicyAsync();
// 检查创建输入
await CheckCreateInputDtoAsync(input);
// 映射到实体
var entity = await MapToEntityAsync(input);
// 设置租户ID
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 entities;
// 根据输入类型决定查询方式
if (input is IPagedResultRequest pagedInput)
{
// 分页查询
entities = await Repository.GetPagedListAsync(
pagedInput.SkipCount,
pagedInput.MaxResultCount,
string.Empty
);
}
else
{
// 查询全部
entities = await Repository.GetListAsync();
}
// 获取总数并映射结果
var totalCount = await Repository.GetCountAsync();
var dtos = await MapToGetListOutputDtosAsync(entities);
return new PagedResultDto(totalCount, dtos);
}
///
/// 获取实体动态下拉框列表,子类重写该方法,通过 keywords 进行筛选
///
/// 查询关键字
///
public virtual async Task> GetSelectDataListAsync(string? keywords = null)
{
List entities = await Repository.GetListAsync();
// 获取总数并映射结果
var totalCount = entities.Count;
var dtos = await MapToGetListOutputDtosAsync(entities);
return new PagedResultDto(totalCount, dtos);
}
///
/// 批量删除实体
///
/// 实体ID集合
[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
///
/// 查询条件
/// Excel文件
public virtual async Task GetExportExcelAsync(TGetListInput input)
{
// 重置分页参数以获取全部数据
if (input is IPagedResultRequest paged)
{
paged.SkipCount = 0;
paged.MaxResultCount = LimitedResultRequestDto.MaxMaxResultCount;
}
// 获取数据
var output = await GetListAsync(input);
// 确保临时目录存在
if (!Directory.Exists(TempFilePath))
{
Directory.CreateDirectory(TempFilePath);
}
// 生成文件名和路径
var fileName = GenerateExcelFileName();
var filePath = Path.Combine(TempFilePath, fileName);
// 保存Excel文件
await MiniExcel.SaveAsAsync(filePath, output.Items);
return new PhysicalFileResult(filePath, "application/vnd.ms-excel");
}
///
/// 生成Excel文件名
///
private string GenerateExcelFileName()
{
return $"{typeof(TEntity).Name}_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}_{Guid.NewGuid()}.xlsx";
}
///
/// 导入Excel(需要实现类重写此方法)
///
public virtual Task PostImportExcelAsync(List input)
{
throw new NotImplementedException("请在实现类中重写此方法以支持Excel导入");
}
}
}