YiCrudAppService.cs
8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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
{
/// <summary>
/// CRUD应用服务基类 - 基础版本
/// </summary>
public abstract class YiCrudAppService<TEntity, TEntityDto, TKey>
: YiCrudAppService<TEntity, TEntityDto, TKey, PagedAndSortedResultRequestDto>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
protected YiCrudAppService(IRepository<TEntity, TKey> repository)
: base(repository)
{
}
}
/// <summary>
/// CRUD应用服务基类 - 支持自定义查询输入
/// </summary>
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)
{
}
}
/// <summary>
/// CRUD应用服务基类 - 支持自定义创建输入
/// </summary>
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)
{
}
}
/// <summary>
/// CRUD应用服务基类 - 支持自定义更新输入
/// </summary>
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)
{
}
}
/// <summary>
/// CRUD应用服务基类 - 完整实现
/// </summary>
public abstract class YiCrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
: CrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
where TEntity : class, IEntity<TKey>
where TGetOutputDto : IEntityDto<TKey>
where TGetListOutputDto : IEntityDto<TKey>
{
/// <summary>
/// 临时文件存储路径
/// </summary>
private const string TempFilePath = "/wwwroot/temp";
protected YiCrudAppService(IRepository<TEntity, TKey> repository)
: base(repository)
{
}
/// <summary>
/// 更新实体
/// </summary>
/// <param name="id">实体ID</param>
/// <param name="input">更新输入</param>
/// <returns>更新后的实体DTO</returns>
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);
}
/// <summary>
/// 检查更新输入数据的有效性
/// </summary>
protected virtual Task CheckUpdateInputDtoAsync(TEntity entity, TUpdateInput input)
{
return Task.CompletedTask;
}
/// <summary>
/// 创建实体
/// </summary>
/// <param name="input">创建输入</param>
/// <returns>创建后的实体DTO</returns>
public override async Task<TGetOutputDto> 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);
}
/// <summary>
/// 检查创建输入数据的有效性
/// </summary>
protected virtual Task CheckCreateInputDtoAsync(TCreateInput input)
{
return Task.CompletedTask;
}
/// <summary>
/// 获取实体列表
/// </summary>
/// <param name="input">查询输入</param>
/// <returns>分页结果</returns>
public override async Task<PagedResultDto<TGetListOutputDto>> GetListAsync(TGetListInput input)
{
List<TEntity> 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<TGetListOutputDto>(totalCount, dtos);
}
/// <summary>
/// 获取实体动态下拉框列表,子类重写该方法,通过 keywords 进行筛选
/// </summary>
/// <param name="keywords">查询关键字</param>
/// <returns></returns>
public virtual async Task<List<TGetListOutputDto>> GetSelectDataListAsync(string? keywords = null)
{
List<TEntity> entities = await Repository.GetListAsync();
// 获取总数并映射结果
var dtos = await MapToGetListOutputDtosAsync(entities);
return dtos;
}
/// <summary>
/// 批量删除实体
/// </summary>
/// <param name="ids">实体ID集合</param>
[RemoteService(isEnabled: true)]
public virtual async Task DeleteAsync(IEnumerable<TKey> ids)
{
await Repository.DeleteManyAsync(ids);
}
/// <summary>
/// 单个删除实体(禁用远程访问)
/// </summary>
[RemoteService(isEnabled: false)]
public override Task DeleteAsync(TKey id)
{
return base.DeleteAsync(id);
}
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="input">查询条件</param>
/// <returns>Excel文件</returns>
public virtual async Task<IActionResult> 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");
}
/// <summary>
/// 生成Excel文件名
/// </summary>
private string GenerateExcelFileName()
{
return $"{typeof(TEntity).Name}_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}_{Guid.NewGuid()}.xlsx";
}
/// <summary>
/// 导入Excel(需要实现类重写此方法)
/// </summary>
public virtual Task PostImportExcelAsync(List<TCreateInput> input)
{
throw new NotImplementedException("请在实现类中重写此方法以支持Excel导入");
}
}
}