using CMS.Plugin.PipeLineLems.Application.Contracts.Dtos.MyTestEntityNames; using CMS.Plugin.PipeLineLems.Application.Contracts.Services; using CMS.Plugin.PipeLineLems.Domain.MyTestEntityNames; using CMS.Plugin.PipeLineLems.Domain.Shared; using CMS.Plugin.PipeLineLems.Domain.Shared.MyTestEntityNames; using Volo.Abp; using Volo.Abp.Application.Dtos; using Volo.Abp.Data; using Volo.Abp.ObjectExtending; namespace CMS.Plugin.PipeLineLems.Application.Implements; /// public class MesAppService : CMSPluginAppService, IMesAppService { private readonly IMyTestEntityNameRepository _mytestentitynameRepository; /// /// Initializes a new instance of the class. /// /// The task job repository. public MesAppService(IMyTestEntityNameRepository mytestentitynameRepository) { _mytestentitynameRepository = mytestentitynameRepository; } /// public virtual async Task GetAsync(Guid id) { return ObjectMapper.Map(await _mytestentitynameRepository.GetAsync(id)); } /// public virtual async Task> GetListAsync(GetMyTestEntityNamesInput input) { Check.NotNull(input, nameof(input)); if (input.Sorting.IsNullOrWhiteSpace()) { input.Sorting = nameof(MyTestEntityName.Sort); } var specification = new MyTestEntityNameSpecification(input.Name); var count = await _mytestentitynameRepository.GetCountAsync(input.Filter, specification); var list = await _mytestentitynameRepository.GetListAsync(input.Sorting, input.MaxResultCount, input.SkipCount, input.Filter, specification); return new PagedResultDto(count, ObjectMapper.Map, List>(list)); } /// public virtual async Task CreateAsync(MyTestEntityNameCreateDto input) { await CheckCreateOrUpdateDtoAsync(input); var exist = await _mytestentitynameRepository.NameExistAsync(input.Name); if (exist) { throw new UserFriendlyException(L[CMSPluginDomainErrorCodes.NameAlreadyExists, input.Name]); } var maxSort = await _mytestentitynameRepository.GetMaxSortAsync(); var sort = input.Sort ?? maxSort; var mytestentityname = new MyTestEntityName(GuidGenerator.Create(), input.Code, input.Name, sort, input.Remark); input.MapExtraPropertiesTo(mytestentityname, MappingPropertyDefinitionChecks.None); await _mytestentitynameRepository.InsertAsync(mytestentityname); if (input.Sort.HasValue && mytestentityname.Sort != maxSort) { await AdjustSortAsync(mytestentityname.Id, mytestentityname.Sort); } return ObjectMapper.Map(mytestentityname); } /// public virtual async Task UpdateAsync(Guid id, MyTestEntityNameUpdateDto input) { await CheckCreateOrUpdateDtoAsync(input); var mytestentityname = await _mytestentitynameRepository.GetAsync(id); var exist = await _mytestentitynameRepository.NameExistAsync(input.Name, mytestentityname.Id); if (exist) { throw new UserFriendlyException(L[CMSPluginDomainErrorCodes.NameAlreadyExists, input.Name]); } mytestentityname.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp); input.MapExtraPropertiesTo(mytestentityname, MappingPropertyDefinitionChecks.None); mytestentityname.Update(input.Code, input.Name, input.Remark); await _mytestentitynameRepository.UpdateAsync(mytestentityname); return ObjectMapper.Map(mytestentityname); } /// public async Task> CloneAsync(IEnumerable ids) { var mytestentitynames = new List(); if (ids != null) { var sort = await _mytestentitynameRepository.GetMaxSortAsync(); foreach (var id in ids) { var mytestentityname = await _mytestentitynameRepository.FindAsync(id); if (mytestentityname != null) { var name = mytestentityname.Name + MyTestEntityNameConsts.CloneTag; var notExist = false; while (!notExist) { var exist = await _mytestentitynameRepository.NameExistAsync(name); if (exist || mytestentitynames.Any(x => x.Name == name)) { name += MyTestEntityNameConsts.CloneTag; continue; } notExist = true; } mytestentityname = await _mytestentitynameRepository.InsertAsync(mytestentityname.Clone(GuidGenerator.Create(), name, sort++)); mytestentitynames.Add(mytestentityname); } } } return ObjectMapper.Map, List>(mytestentitynames); } /// public virtual Task DeleteAsync(Guid id) { return _mytestentitynameRepository.DeleteAsync(id); } /// public async Task DeleteManyAsync(IEnumerable ids) { foreach (var id in ids) { await DeleteAsync(id); } } /// public virtual async Task AdjustSortAsync(Guid id, int sort) { var list = await _mytestentitynameRepository.GetListAsync(nameof(MyTestEntityName.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 _mytestentitynameRepository.UpdateManyAsync(list); } /// public async Task ImportAsync(MyTestEntityNamesImportModel input) { Check.NotNull(input, nameof(input)); var mytestentitynameCreateDtos = new List<(int RowIndex, MyTestEntityNameCreateDto Item)>(); var mytestentitynameUpdateDtos = new List<(int RowIndex, Guid Id, MyTestEntityNameUpdateDto Item)>(); var mytestentitynames = input.MyTestEntityNames; if (mytestentitynames != null && mytestentitynames.Any()) { #region 导入校验 // 判断名称是否重复,并输出第几行重复 var duplicateMyTestEntityNames = mytestentitynames.GroupBy(x => x.Name).Where(x => x.Count() > 1).ToList(); if (duplicateMyTestEntityNames?.Any() == true) { var duplicateMyTestEntityNameMsgs = duplicateMyTestEntityNames.Select(x => $"第 {string.Join(",", x.Select(x => x.RowIndex))} 行:{x.Key} 名称重复"); var errorMsg = $"导入失败!配置, {string.Join(",", duplicateMyTestEntityNameMsgs)},终止导入"; throw new UserFriendlyException(errorMsg); } #endregion foreach (var mytestentityname in mytestentitynames) { if (mytestentityname.Code.IsNullOrWhiteSpace() && mytestentityname.Name.IsNullOrWhiteSpace()) { continue; } if (mytestentityname.Name.IsNullOrWhiteSpace()) { var errorMsg = $"导入失败!配置,第{mytestentityname.RowIndex}行:MyTestEntityName名称不能为空"; throw new UserFriendlyException(errorMsg); } var oldMyTestEntityName = await _mytestentitynameRepository.FindByNameAsync(mytestentityname.Name); if (oldMyTestEntityName != null) { var mytestentitynameUpdateDto = new MyTestEntityNameUpdateDto { Code = mytestentityname.Code, Name = mytestentityname.Name, Remark = mytestentityname.Remark, }; mytestentitynameUpdateDtos.Add((mytestentityname.RowIndex, oldMyTestEntityName.Id, mytestentitynameUpdateDto)); } else { var mytestentitynameCreateDto = new MyTestEntityNameCreateDto { Code = mytestentityname.Code, Name = mytestentityname.Name, Remark = mytestentityname.Remark, }; mytestentitynameCreateDtos.Add((mytestentityname.RowIndex, mytestentitynameCreateDto)); } } } // 新增 foreach (var mytestentitynameDto in mytestentitynameCreateDtos) { try { await CreateAsync(mytestentitynameDto.Item); } catch (Exception e) { var errorMsg = $"导入失败!配置,第{mytestentitynameDto.RowIndex}行:{e.Message},终止导入"; throw new UserFriendlyException(errorMsg); } } // 更新 foreach (var mytestentitynameDto in mytestentitynameUpdateDtos) { try { await UpdateAsync(mytestentitynameDto.Id, mytestentitynameDto.Item); } catch (Exception e) { var errorMsg = $"导入失败!配置,第{mytestentitynameDto.RowIndex}行:{e.Message},终止导入"; throw new UserFriendlyException(errorMsg); } } } /// public async Task<(Dictionary Sheets, string FileName)> ExportAsync(GetMyTestEntityNamesInput input) { Check.NotNull(input, nameof(input)); if (input.Sorting.IsNullOrWhiteSpace()) { input.Sorting = nameof(MyTestEntityName.Sort); } var specification = new MyTestEntityNameSpecification(input.Name); var list = await _mytestentitynameRepository.GetListAsync(input.Sorting, input.MaxResultCount, input.SkipCount, input.Filter, specification, includeDetails: true); var result = ObjectMapper.Map, List>(list); var sheets = new Dictionary { ["配置"] = result.Select(x => x.GetExportData()).ToList(), }; var fileName = result.Count > 1 ? "MyTestEntityName列表" : result.Count == 1 ? result.First()?.Name : "MyTestEntityName模版"; return (sheets, fileName); } /// /// Checks the create or update dto asynchronous. /// /// The input. protected Task CheckCreateOrUpdateDtoAsync(MyTestEntityNameCreateOrUpdateDtoBase input) { Check.NotNull(input, nameof(input)); Check.NotNullOrWhiteSpace(input.Code, "编号", MyTestEntityNameConsts.MaxCodeLength); Check.NotNullOrWhiteSpace(input.Name, "名称", MyTestEntityNameConsts.MaxNameLength); Check.Length(input.Remark, "备注", MyTestEntityNameConsts.MaxRemarkLength); return Task.CompletedTask; } }