using CMS.Plugin.WareCmsUtilityApi.Application.Contracts.Dtos.Samples;
|
using CMS.Plugin.WareCmsUtilityApi.Application.Contracts.Services;
|
using CMS.Plugin.WareCmsUtilityApi.Domain.Samples;
|
using CMS.Plugin.WareCmsUtilityApi.Domain.Shared;
|
using CMS.Plugin.WareCmsUtilityApi.Domain.Shared.Samples;
|
using Volo.Abp;
|
using Volo.Abp.Application.Dtos;
|
using Volo.Abp.Data;
|
using Volo.Abp.ObjectExtending;
|
|
namespace CMS.Plugin.WareCmsUtilityApi.Application.Implements;
|
|
/// <inheritdoc />
|
public class SampleAppService : CMSPluginAppService, ISampleAppService
|
{
|
private readonly ISampleRepository _sampleRepository;
|
|
/// <summary>
|
/// Initializes a new instance of the <see cref="SampleAppService"/> class.
|
/// </summary>
|
/// <param name="sampleRepository">The task job repository.</param>
|
public SampleAppService(ISampleRepository sampleRepository)
|
{
|
_sampleRepository = sampleRepository;
|
}
|
|
/// <inheritdoc />
|
public virtual async Task<SampleDto> GetAsync(Guid id)
|
{
|
return ObjectMapper.Map<Sample, SampleDto>(await _sampleRepository.GetAsync(id));
|
}
|
|
/// <inheritdoc />
|
public virtual async Task<PagedResultDto<SampleDto>> GetListAsync(GetSamplesInput input)
|
{
|
Check.NotNull(input, nameof(input));
|
|
if (input.Sorting.IsNullOrWhiteSpace())
|
{
|
input.Sorting = nameof(Sample.Sort);
|
}
|
|
var specification = new SampleSpecification(input.Name);
|
var count = await _sampleRepository.GetCountAsync(input.Filter, specification);
|
var list = await _sampleRepository.GetListAsync(input.Sorting, input.MaxResultCount, input.SkipCount, input.Filter, specification);
|
|
return new PagedResultDto<SampleDto>(count, ObjectMapper.Map<List<Sample>, List<SampleDto>>(list));
|
}
|
|
/// <inheritdoc />
|
public virtual async Task<SampleDto> CreateAsync(SampleCreateDto input)
|
{
|
await CheckCreateOrUpdateDtoAsync(input);
|
|
var exist = await _sampleRepository.NameExistAsync(input.Name);
|
if (exist)
|
{
|
throw new UserFriendlyException(L[CMSPluginDomainErrorCodes.NameAlreadyExists, input.Name]);
|
}
|
|
var maxSort = await _sampleRepository.GetMaxSortAsync();
|
var sort = input.Sort ?? maxSort;
|
var sample = new Sample(GuidGenerator.Create(), input.Code, input.Name, sort, input.Remark);
|
input.MapExtraPropertiesTo(sample, MappingPropertyDefinitionChecks.None);
|
|
await _sampleRepository.InsertAsync(sample);
|
|
if (input.Sort.HasValue && sample.Sort != maxSort)
|
{
|
await AdjustSortAsync(sample.Id, sample.Sort);
|
}
|
|
return ObjectMapper.Map<Sample, SampleDto>(sample);
|
}
|
|
/// <inheritdoc />
|
public virtual async Task<SampleDto> UpdateAsync(Guid id, SampleUpdateDto input)
|
{
|
await CheckCreateOrUpdateDtoAsync(input);
|
|
var sample = await _sampleRepository.GetAsync(id);
|
var exist = await _sampleRepository.NameExistAsync(input.Name, sample.Id);
|
if (exist)
|
{
|
throw new UserFriendlyException(L[CMSPluginDomainErrorCodes.NameAlreadyExists, input.Name]);
|
}
|
|
sample.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp);
|
input.MapExtraPropertiesTo(sample, MappingPropertyDefinitionChecks.None);
|
|
sample.Update(input.Code, input.Name, input.Remark);
|
|
await _sampleRepository.UpdateAsync(sample);
|
|
return ObjectMapper.Map<Sample, SampleDto>(sample);
|
}
|
|
/// <inheritdoc />
|
public async Task<List<SampleDto>> CloneAsync(IEnumerable<Guid> ids)
|
{
|
var samples = new List<Sample>();
|
if (ids != null)
|
{
|
var sort = await _sampleRepository.GetMaxSortAsync();
|
foreach (var id in ids)
|
{
|
var sample = await _sampleRepository.FindAsync(id);
|
if (sample != null)
|
{
|
var name = sample.Name + SampleConsts.CloneTag;
|
var notExist = false;
|
while (!notExist)
|
{
|
var exist = await _sampleRepository.NameExistAsync(name);
|
if (exist || samples.Any(x => x.Name == name))
|
{
|
name += SampleConsts.CloneTag;
|
continue;
|
}
|
|
notExist = true;
|
}
|
|
sample = await _sampleRepository.InsertAsync(sample.Clone(GuidGenerator.Create(), name, sort++));
|
samples.Add(sample);
|
}
|
}
|
}
|
|
return ObjectMapper.Map<List<Sample>, List<SampleDto>>(samples);
|
}
|
|
/// <inheritdoc />
|
public virtual Task DeleteAsync(Guid id)
|
{
|
return _sampleRepository.DeleteAsync(id);
|
}
|
|
/// <inheritdoc />
|
public async Task DeleteManyAsync(IEnumerable<Guid> ids)
|
{
|
foreach (var id in ids)
|
{
|
await DeleteAsync(id);
|
}
|
}
|
|
/// <inheritdoc />
|
public virtual async Task AdjustSortAsync(Guid id, int sort)
|
{
|
var list = await _sampleRepository.GetListAsync(nameof(Sample.Sort));
|
if (list != null && list.Any())
|
{
|
var initSort = 1;
|
list.ForEach(x => x.AdjustSort(initSort++));
|
var entity = list.FirstOrDefault(x => x.Id == id);
|
if (entity != null)
|
{
|
if (sort == 1)
|
{
|
list.Where(x => x.Id != id).ToList()?.ForEach(x => x.AdjustSort(x.Sort + 1));
|
}
|
else if (entity.Sort > sort)
|
{
|
list.Where(x => x.Id != id && x.Sort >= sort).ToList()?.ForEach(x => x.AdjustSort(x.Sort + 1));
|
list.Where(x => x.Id != id && x.Sort < sort).ToList()?.ForEach(x => x.AdjustSort(x.Sort - 1));
|
}
|
else if (entity.Sort < sort)
|
{
|
list.Where(x => x.Id != id && x.Sort > sort).ToList()?.ForEach(x => x.AdjustSort(x.Sort + 1));
|
list.Where(x => x.Id != id && x.Sort <= sort).ToList()?.ForEach(x => x.AdjustSort(x.Sort - 1));
|
}
|
|
entity.AdjustSort(sort);
|
}
|
}
|
|
await _sampleRepository.UpdateManyAsync(list);
|
}
|
|
/// <inheritdoc />
|
public async Task ImportAsync(SamplesImportModel input)
|
{
|
Check.NotNull(input, nameof(input));
|
|
var sampleCreateDtos = new List<(int RowIndex, SampleCreateDto Item)>();
|
var sampleUpdateDtos = new List<(int RowIndex, Guid Id, SampleUpdateDto Item)>();
|
var samples = input.Samples;
|
|
if (samples != null && samples.Any())
|
{
|
#region 导入校验
|
|
// 判断名称是否重复,并输出第几行重复
|
var duplicateSamples = samples.GroupBy(x => x.Name).Where(x => x.Count() > 1).ToList();
|
if (duplicateSamples?.Any() == true)
|
{
|
var duplicateSampleMsgs = duplicateSamples.Select(x => $"第 {string.Join(",", x.Select(x => x.RowIndex))} 行:{x.Key} 名称重复");
|
var errorMsg = $"导入失败!配置, {string.Join(",", duplicateSampleMsgs)},终止导入";
|
throw new UserFriendlyException(errorMsg);
|
}
|
|
#endregion
|
|
foreach (var sample in samples)
|
{
|
if (sample.Code.IsNullOrWhiteSpace() && sample.Name.IsNullOrWhiteSpace())
|
{
|
continue;
|
}
|
|
if (sample.Name.IsNullOrWhiteSpace())
|
{
|
var errorMsg = $"导入失败!配置,第{sample.RowIndex}行:Sample名称不能为空";
|
throw new UserFriendlyException(errorMsg);
|
}
|
|
var oldSample = await _sampleRepository.FindByNameAsync(sample.Name);
|
if (oldSample != null)
|
{
|
var sampleUpdateDto = new SampleUpdateDto
|
{
|
Code = sample.Code,
|
Name = sample.Name,
|
Remark = sample.Remark,
|
};
|
|
sampleUpdateDtos.Add((sample.RowIndex, oldSample.Id, sampleUpdateDto));
|
}
|
else
|
{
|
var sampleCreateDto = new SampleCreateDto
|
{
|
Code = sample.Code,
|
Name = sample.Name,
|
Remark = sample.Remark,
|
};
|
|
sampleCreateDtos.Add((sample.RowIndex, sampleCreateDto));
|
}
|
}
|
}
|
|
// 新增
|
foreach (var sampleDto in sampleCreateDtos)
|
{
|
try
|
{
|
await CreateAsync(sampleDto.Item);
|
}
|
catch (Exception e)
|
{
|
var errorMsg = $"导入失败!配置,第{sampleDto.RowIndex}行:{e.Message},终止导入";
|
throw new UserFriendlyException(errorMsg);
|
}
|
}
|
|
// 更新
|
foreach (var sampleDto in sampleUpdateDtos)
|
{
|
try
|
{
|
await UpdateAsync(sampleDto.Id, sampleDto.Item);
|
}
|
catch (Exception e)
|
{
|
var errorMsg = $"导入失败!配置,第{sampleDto.RowIndex}行:{e.Message},终止导入";
|
throw new UserFriendlyException(errorMsg);
|
}
|
}
|
}
|
|
/// <inheritdoc />
|
public async Task<(Dictionary<string, object> Sheets, string FileName)> ExportAsync(GetSamplesInput input)
|
{
|
Check.NotNull(input, nameof(input));
|
|
if (input.Sorting.IsNullOrWhiteSpace())
|
{
|
input.Sorting = nameof(Sample.Sort);
|
}
|
|
var specification = new SampleSpecification(input.Name);
|
var list = await _sampleRepository.GetListAsync(input.Sorting, input.MaxResultCount, input.SkipCount, input.Filter, specification, includeDetails: true);
|
var result = ObjectMapper.Map<List<Sample>, List<SampleDto>>(list);
|
|
var sheets = new Dictionary<string, object>
|
{
|
["配置"] = result.Select(x => x.GetExportData()).ToList(),
|
};
|
|
var fileName = result.Count > 1 ? "Sample列表" : result.Count == 1 ? result.First()?.Name : "Sample模版";
|
return (sheets, fileName);
|
}
|
|
/// <summary>
|
/// Checks the create or update dto asynchronous.
|
/// </summary>
|
/// <param name="input">The input.</param>
|
protected Task CheckCreateOrUpdateDtoAsync(SampleCreateOrUpdateDtoBase input)
|
{
|
Check.NotNull(input, nameof(input));
|
Check.NotNullOrWhiteSpace(input.Code, "编号", SampleConsts.MaxCodeLength);
|
Check.NotNullOrWhiteSpace(input.Name, "名称", SampleConsts.MaxNameLength);
|
Check.Length(input.Remark, "备注", SampleConsts.MaxRemarkLength);
|
return Task.CompletedTask;
|
}
|
}
|