using CMS.Plugin.HIAWms.Application.Contracts.Dtos.WmsPlaces; using CMS.Plugin.HIAWms.Application.Contracts.Services; using CMS.Plugin.HIAWms.Domain.WmsPlaces; using CMS.Plugin.HIAWms.Domain.Shared; using CMS.Plugin.HIAWms.Domain.Shared.WmsPlaces; using Volo.Abp; using Volo.Abp.Application.Dtos; using Volo.Abp.Data; using Volo.Abp.ObjectExtending; using CMS.Plugin.HIAWms.Domain.Shared.Util; namespace CMS.Plugin.HIAWms.Application.Implements; /// public class WmsPlaceAppService : CMSPluginAppService, IWmsPlaceAppService { private readonly IWmsPlaceRepository _wmsplaceRepository; /// /// Initializes a new instance of the class. /// /// The task job repository. public WmsPlaceAppService(IWmsPlaceRepository wmsplaceRepository) { _wmsplaceRepository = wmsplaceRepository; } /// public virtual async Task GetAsync(Guid id) { return ObjectMapper.Map(await _wmsplaceRepository.GetAsync(id)); } /// public virtual async Task> GetListAsync(GetWmsPlacesInput input) { Check.NotNull(input, nameof(input)); if (input.Sorting.IsNullOrWhiteSpace()) { input.Sorting = nameof(WmsPlace.Sort); } var specification = new WmsPlaceSpecification(input.Name); var place = ObjectMapper.Map(input); var count = await _wmsplaceRepository.GetCountAsync(place,input.Filter, specification); var list = await _wmsplaceRepository.GetListAsync(place, input.Sorting, input.MaxResultCount, input.SkipCount, input.Filter, specification); return new PagedResultDto(count, ObjectMapper.Map, List>(list)); } /// public virtual async Task CreateAsync(WmsPlaceCreateDto input) { await CheckCreateOrUpdateDtoAsync(input); var exist = await _wmsplaceRepository.NameExistAsync(input.PlaceNo); if (exist) { throw new UserFriendlyException(L[CMSPluginDomainErrorCodes.NameAlreadyExists, input.PlaceNo]); } var maxSort = await _wmsplaceRepository.GetMaxSortAsync(); var sort = input.Sort ?? maxSort; var wmsplace = ObjectMapper.Map(input); input.MapExtraPropertiesTo(wmsplace, MappingPropertyDefinitionChecks.None); wmsplace.Sort = sort; await _wmsplaceRepository.InsertAsync(wmsplace); if (input.Sort.HasValue && wmsplace.Sort != maxSort) { await AdjustSortAsync(wmsplace.Id, wmsplace.Sort); } return ObjectMapper.Map(wmsplace); } /// public virtual async Task UpdateAsync(Guid id, WmsPlaceUpdateDto input) { await CheckCreateOrUpdateDtoAsync(input); var wmsplace = await _wmsplaceRepository.GetAsync(id); var exist = await _wmsplaceRepository.NameExistAsync(input.PlaceNo, wmsplace.Id); if (exist) { throw new UserFriendlyException(L[CMSPluginDomainErrorCodes.NameAlreadyExists, input.PlaceNo]); } wmsplace.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp); input.MapExtraPropertiesTo(wmsplace, MappingPropertyDefinitionChecks.None); wmsplace.StorageTypeNo = input.StorageTypeNo; wmsplace.PlaceStatus = input.PlaceStatus; wmsplace.AreaCode = input.AreaCode; wmsplace.Aisle = input.Aisle; wmsplace.RowNo = input.RowNo; wmsplace.ColumnNo = input.ColumnNo; wmsplace.LayerNo = input.LayerNo; wmsplace.Islock = input.Islock; wmsplace.EmptyContainer = input.EmptyContainer; wmsplace.RedundantField1 = input.RedundantField1; wmsplace.RedundantField2 = input.RedundantField2; wmsplace.RedundantField3 = input.RedundantField3; wmsplace.Remark = input.Remark; await _wmsplaceRepository.UpdateAsync(wmsplace); return ObjectMapper.Map(wmsplace); } /// public async Task> CloneAsync(IEnumerable ids) { var wmsplaces = new List(); if (ids != null) { var sort = await _wmsplaceRepository.GetMaxSortAsync(); foreach (var id in ids) { var wmsplace = await _wmsplaceRepository.FindAsync(id); if (wmsplace != null) { var name = wmsplace.PlaceNo + WmsPlaceConsts.CloneTag; var notExist = false; while (!notExist) { var exist = await _wmsplaceRepository.NameExistAsync(name); if (exist || wmsplaces.Any(x => x.PlaceNo == name)) { name += WmsPlaceConsts.CloneTag; continue; } notExist = true; } //wmsplace = await _wmsplaceRepository.InsertAsync(wmsplace.Clone(GuidGenerator.Create(), name, sort++)); wmsplaces.Add(wmsplace); } } } return ObjectMapper.Map, List>(wmsplaces); } /// public virtual Task DeleteAsync(Guid id) { return _wmsplaceRepository.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 _wmsplaceRepository.GetListAsync(null,nameof(WmsPlace.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 _wmsplaceRepository.UpdateManyAsync(list); } /// public async Task ImportAsync(WmsPlacesImportModel input) { Check.NotNull(input, nameof(input)); var wmsplaceCreateDtos = new List<(int RowIndex, WmsPlaceCreateDto Item)>(); var wmsplaceUpdateDtos = new List<(int RowIndex, Guid Id, WmsPlaceUpdateDto Item)>(); var wmsplaces = input.WmsPlaces; if (wmsplaces != null && wmsplaces.Any()) { #region 导入校验 // 判断名称是否重复,并输出第几行重复 var duplicateWmsPlaces = wmsplaces.GroupBy(x => x.PlaceNo).Where(x => x.Count() > 1).ToList(); if (duplicateWmsPlaces?.Any() == true) { var duplicateWmsPlaceMsgs = duplicateWmsPlaces.Select(x => $"第 {string.Join(",", x.Select(x => x.RowIndex))} 行:{x.Key} 名称重复"); var errorMsg = $"导入失败!配置, {string.Join(",", duplicateWmsPlaceMsgs)},终止导入"; throw new UserFriendlyException(errorMsg); } #endregion foreach (var wmsplace in wmsplaces) { if (wmsplace.PlaceNo.IsNullOrWhiteSpace()) { continue; } if (wmsplace.PlaceNo.IsNullOrWhiteSpace()) { var errorMsg = $"导入失败!配置,第{wmsplace.RowIndex}行:WmsPlace名称不能为空"; throw new UserFriendlyException(errorMsg); } var oldWmsPlace = await _wmsplaceRepository.FindByNameAsync(wmsplace.PlaceNo); if (oldWmsPlace != null) { var wmsplaceUpdateDto = new WmsPlaceUpdateDto { PlaceNo = wmsplace.PlaceNo, PlaceStatus = wmsplace.PlaceStatus, Remark = wmsplace.Remark, }; wmsplaceUpdateDtos.Add((wmsplace.RowIndex, oldWmsPlace.Id, wmsplaceUpdateDto)); } else { var wmsplaceCreateDto = new WmsPlaceCreateDto { PlaceNo = wmsplace.PlaceNo, PlaceStatus = wmsplace.PlaceStatus, Remark = wmsplace.Remark, }; wmsplaceCreateDtos.Add((wmsplace.RowIndex, wmsplaceCreateDto)); } } } // 新增 foreach (var wmsplaceDto in wmsplaceCreateDtos) { try { await CreateAsync(wmsplaceDto.Item); } catch (Exception e) { var errorMsg = $"导入失败!配置,第{wmsplaceDto.RowIndex}行:{e.Message},终止导入"; throw new UserFriendlyException(errorMsg); } } // 更新 foreach (var wmsplaceDto in wmsplaceUpdateDtos) { try { await UpdateAsync(wmsplaceDto.Id, wmsplaceDto.Item); } catch (Exception e) { var errorMsg = $"导入失败!配置,第{wmsplaceDto.RowIndex}行:{e.Message},终止导入"; throw new UserFriendlyException(errorMsg); } } } /// public async Task<(Dictionary Sheets, string FileName)> ExportAsync(GetWmsPlacesInput input) { Check.NotNull(input, nameof(input)); if (input.Sorting.IsNullOrWhiteSpace()) { input.Sorting = nameof(WmsPlace.Sort); } var specification = new WmsPlaceSpecification(input.Name); var place = ObjectMapper.Map(input); var list = await _wmsplaceRepository.GetListAsync(place, input.Sorting, input.MaxResultCount, input.SkipCount, input.Filter, specification, includeDetails: true); var result = ObjectMapper.Map, List>(list); var sheets = new Dictionary { ["配置"] = ExportHelper.ConvertListToExportData(result), }; var fileName = result.Count > 1 ? "WmsPlace列表" : result.Count == 1 ? result.First()?.PlaceNo : "WmsPlace模版"; return (sheets, fileName); } /// /// Checks the create or update dto asynchronous. /// /// The input. protected Task CheckCreateOrUpdateDtoAsync(WmsPlaceCreateOrUpdateDtoBase input) { Check.NotNull(input, nameof(input)); Check.NotNullOrWhiteSpace(input.PlaceNo, "编号", WmsPlaceConsts.MaxCodeLength); Check.Length(input.Remark, "备注", WmsPlaceConsts.MaxRemarkLength); return Task.CompletedTask; } }