LocationAppService.cs
13.8 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
using System.IO;
using FoodLabeling.Application.Helpers;
using FoodLabeling.Application.Contracts.Dtos.Location;
using FoodLabeling.Application.Contracts.Dtos.Common;
using FoodLabeling.Application.Contracts.IServices;
using FoodLabeling.Application.Options;
using FoodLabeling.Domain.Entities;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using SqlSugar;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace FoodLabeling.Application.Services;
/// <summary>
/// 门店管理服务(美国版)
/// </summary>
public class LocationAppService : ApplicationService, ILocationAppService
{
private readonly ISqlSugarRepository<LocationAggregateRoot, Guid> _locationRepository;
private readonly ISqlSugarDbContext _dbContext;
private readonly IOptionsSnapshot<FoodLabelingBatchImportOptions> _batchImportOptions;
public LocationAppService(
ISqlSugarRepository<LocationAggregateRoot, Guid> locationRepository,
ISqlSugarDbContext dbContext,
IOptionsSnapshot<FoodLabelingBatchImportOptions> batchImportOptions)
{
_locationRepository = locationRepository;
_dbContext = dbContext;
_batchImportOptions = batchImportOptions;
}
/// <inheritdoc />
public async Task<PagedResultWithPageDto<LocationGetListOutputDto>> GetListAsync([FromQuery] LocationGetListInputVo input)
{
RefAsync<int> total = 0;
var query = await BuildFilteredQueryAsync(input);
if (!string.IsNullOrWhiteSpace(input.Sorting))
{
query = query.OrderBy(input.Sorting);
}
else
{
query = query.OrderBy(x => x.CreationTime, OrderByType.Desc);
}
var entities = await query.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
var items = entities.Select(ToListDto).ToList();
var pageSize = input.MaxResultCount <= 0 ? items.Count : input.MaxResultCount;
var pageIndex = pageSize <= 0 ? 1 : PagedQueryConvention.PageIndexFromSkipCount(input.SkipCount);
var totalPages = pageSize <= 0 ? 0 : (int)Math.Ceiling(total.Value / (double)pageSize);
return new PagedResultWithPageDto<LocationGetListOutputDto>
{
PageIndex = pageIndex,
PageSize = pageSize,
TotalCount = total.Value,
TotalPages = totalPages,
Items = items
};
}
/// <inheritdoc />
public async Task<LocationGetListOutputDto> CreateAsync([FromBody] LocationCreateInputVo input)
{
var locationCode = input.LocationCode?.Trim();
if (string.IsNullOrWhiteSpace(locationCode))
{
throw new UserFriendlyException("Location ID不能为空");
}
var locationName = input.LocationName?.Trim();
if (string.IsNullOrWhiteSpace(locationName))
{
throw new UserFriendlyException("Location Name不能为空");
}
var isExist = await _locationRepository.IsAnyAsync(x => x.LocationCode == locationCode);
if (isExist)
{
throw new UserFriendlyException("Location ID已存在");
}
var entity = new LocationAggregateRoot(GuidGenerator.Create())
{
Partner = input.Partner?.Trim(),
GroupName = input.GroupName?.Trim(),
LocationCode = locationCode,
LocationName = locationName,
Street = input.Street?.Trim(),
City = input.City?.Trim(),
StateCode = input.StateCode?.Trim(),
Country = input.Country?.Trim(),
ZipCode = input.ZipCode?.Trim(),
Phone = input.Phone?.Trim(),
Email = input.Email?.Trim(),
Latitude = input.Latitude,
Longitude = input.Longitude,
OperatingHours = input.OperatingHours?.Trim(),
State = input.State
};
await _locationRepository.InsertAsync(entity);
return ToListDto(entity);
}
/// <inheritdoc />
public async Task<LocationGetListOutputDto> UpdateAsync(Guid id, [FromBody] LocationUpdateInputVo input)
{
var entity = await _locationRepository.GetSingleAsync(x => x.Id == id && x.IsDeleted == false);
if (entity is null)
{
throw new UserFriendlyException("门店不存在");
}
var locationName = input.LocationName?.Trim();
if (string.IsNullOrWhiteSpace(locationName))
{
throw new UserFriendlyException("Location Name不能为空");
}
entity.Partner = input.Partner?.Trim();
entity.GroupName = input.GroupName?.Trim();
entity.LocationName = locationName;
entity.Street = input.Street?.Trim();
entity.City = input.City?.Trim();
entity.StateCode = input.StateCode?.Trim();
entity.Country = input.Country?.Trim();
entity.ZipCode = input.ZipCode?.Trim();
entity.Phone = input.Phone?.Trim();
entity.Email = input.Email?.Trim();
entity.Latitude = input.Latitude;
entity.Longitude = input.Longitude;
entity.OperatingHours = input.OperatingHours?.Trim();
entity.State = input.State;
await _locationRepository.UpdateAsync(entity);
return ToListDto(entity);
}
/// <inheritdoc />
public async Task DeleteAsync(Guid id)
{
var entity = await _locationRepository.GetSingleAsync(x => x.Id == id && x.IsDeleted == false);
if (entity is null)
{
throw new UserFriendlyException("门店不存在");
}
entity.IsDeleted = true;
await _locationRepository.UpdateAsync(entity);
}
/// <inheritdoc />
public Task<IActionResult> DownloadLocationImportTemplateAsync()
{
var opt = _batchImportOptions.Value;
var dir = opt.TemplateDirectory?.Trim();
if (string.IsNullOrWhiteSpace(dir))
{
throw new UserFriendlyException("未配置批量导入模板目录 FoodLabeling:BatchImport:TemplateDirectory");
}
var fileName = opt.LocationTemplateFileName?.Trim();
if (string.IsNullOrWhiteSpace(fileName))
{
throw new UserFriendlyException("未配置模板文件名 FoodLabeling:BatchImport:LocationTemplateFileName");
}
var fullPath = Path.Combine(dir, fileName);
if (!File.Exists(fullPath))
{
throw new UserFriendlyException($"模板文件不存在:{fullPath}");
}
var stream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read);
const string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
return Task.FromResult<IActionResult>(new FileStreamResult(stream, contentType)
{
FileDownloadName = fileName
});
}
/// <inheritdoc />
public async Task<IActionResult> ExportLocationsExcelAsync([FromQuery] LocationGetListInputVo input)
{
var exportFilter = new LocationGetListInputVo
{
Sorting = input.Sorting,
Keyword = input.Keyword,
Partner = input.Partner,
GroupName = input.GroupName,
State = input.State
};
var query = await BuildFilteredQueryAsync(exportFilter);
if (!string.IsNullOrWhiteSpace(exportFilter.Sorting))
{
query = query.OrderBy(exportFilter.Sorting);
}
else
{
query = query.OrderBy(x => x.CreationTime, OrderByType.Desc);
}
var entities = await query.ToListAsync();
var rows = entities.Select(ToListDto).ToList();
var ms = LocationBatchExcelHelper.BuildExportWorkbook(rows);
const string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
var downloadName = $"locations-export-{Clock.Now:yyyyMMdd-HHmmss}.xlsx";
return new FileStreamResult(ms, contentType) { FileDownloadName = downloadName };
}
/// <inheritdoc />
public async Task<LocationBatchImportResultDto> ImportLocationsBatchAsync([FromForm] LocationBatchImportInputVo input)
{
if (input?.File is null || input.File.Length == 0)
{
throw new UserFriendlyException("请上传 Excel 文件(form 字段名:file)");
}
var opt = _batchImportOptions.Value;
if (input.File.Length > opt.MaxUploadBytes)
{
throw new UserFriendlyException($"文件过大,最大允许 {opt.MaxUploadBytes / 1024 / 1024} MB");
}
var ext = Path.GetExtension(input.File.FileName)?.ToLowerInvariant();
if (ext != ".xlsx")
{
throw new UserFriendlyException("仅支持 .xlsx 格式的 Excel 文件");
}
await using var uploadStream = input.File.OpenReadStream();
var parseErrors = new List<LocationBatchImportErrorDto>();
var rows = LocationBatchExcelHelper.ParseImportWorkbook(
uploadStream,
opt.MaxImportRows <= 0 ? 5000 : opt.MaxImportRows,
out var headerErrors);
parseErrors.AddRange(headerErrors);
var result = new LocationBatchImportResultDto();
if (rows.Count == 0 && parseErrors.Count > 0)
{
result.Errors = parseErrors;
result.FailCount = parseErrors.Count;
return result;
}
foreach (var (rowNum, vo) in rows)
{
try
{
await CreateAsync(vo);
result.SuccessCount++;
}
catch (UserFriendlyException ex)
{
result.FailCount++;
result.Errors.Add(new LocationBatchImportErrorDto
{
RowNumber = rowNum,
LocationCode = vo.LocationCode,
Message = ex.Message
});
}
}
result.Errors.InsertRange(0, parseErrors);
return result;
}
/// <inheritdoc />
public async Task<LocationBulkUpdateResultDto> UpdateLocationsBulkAsync([FromBody] LocationBulkUpdateInputVo input)
{
if (input?.Items is null || input.Items.Count == 0)
{
throw new UserFriendlyException("请至少提交一条编辑数据(items 不能为空)");
}
var opt = _batchImportOptions.Value;
var maxItems = opt.MaxBulkUpdateItems <= 0 ? 500 : opt.MaxBulkUpdateItems;
if (input.Items.Count > maxItems)
{
throw new UserFriendlyException($"单次批量编辑最多允许 {maxItems} 条,请分批提交");
}
var effectiveCount = input.Items.Count(static x => x is not null && x.Id != Guid.Empty);
if (effectiveCount == 0)
{
throw new UserFriendlyException("没有有效的门店 Id(请为待保存行填写 id,空行请使用 id 为空 GUID 或从列表中移除)");
}
var result = new LocationBulkUpdateResultDto();
for (var i = 0; i < input.Items.Count; i++)
{
var item = input.Items[i];
if (item is null || item.Id == Guid.Empty)
{
continue;
}
try
{
await UpdateAsync(item.Id, item);
result.SuccessCount++;
}
catch (UserFriendlyException ex)
{
result.FailCount++;
result.Errors.Add(new LocationBulkUpdateErrorDto
{
RowNumber = i + 1,
Id = item.Id,
Message = ex.Message
});
}
}
return result;
}
private async Task<ISugarQueryable<LocationAggregateRoot>> BuildFilteredQueryAsync(LocationGetListInputVo input)
{
var scope = await LocationRegionScopeHelper.ResolveLocationListScopeAsync(CurrentUser, _dbContext);
var keyword = input.Keyword?.Trim();
var partner = input.Partner?.Trim();
var groupName = input.GroupName?.Trim();
var query = _locationRepository._DbQueryable
.Where(x => x.IsDeleted == false);
query = LocationRegionScopeHelper.ApplyLocationListScope(query, scope);
return query
.WhereIF(!string.IsNullOrEmpty(partner), x => x.Partner == partner)
.WhereIF(!string.IsNullOrEmpty(groupName), x => x.GroupName == groupName)
.WhereIF(input.State is not null, x => x.State == input.State)
.WhereIF(!string.IsNullOrEmpty(keyword),
x =>
x.LocationCode.Contains(keyword!) ||
x.LocationName.Contains(keyword!) ||
(x.Street != null && x.Street.Contains(keyword!)) ||
(x.City != null && x.City.Contains(keyword!)) ||
(x.StateCode != null && x.StateCode.Contains(keyword!)) ||
(x.Country != null && x.Country.Contains(keyword!)) ||
(x.ZipCode != null && x.ZipCode.Contains(keyword!)) ||
(x.Phone != null && x.Phone.Contains(keyword!)) ||
(x.Email != null && x.Email.Contains(keyword!)) ||
(x.OperatingHours != null && x.OperatingHours.Contains(keyword!)));
}
private static LocationGetListOutputDto ToListDto(LocationAggregateRoot x) =>
new()
{
Id = x.Id,
Partner = x.Partner,
GroupName = x.GroupName,
LocationCode = x.LocationCode,
LocationName = x.LocationName,
Street = x.Street,
City = x.City,
StateCode = x.StateCode,
Country = x.Country,
ZipCode = x.ZipCode,
Phone = x.Phone,
Email = x.Email,
Latitude = x.Latitude,
Longitude = x.Longitude,
OperatingHours = x.OperatingHours,
State = x.State
};
}