Untitled
unknown
plain_text
2 years ago
6.4 kB
6
Indexable
package br.com.senior.erpxcomven.pedido.service.impl; import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.UUID; import javax.inject.Inject; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import br.com.senior.erpxcomven.pedido.E120pedEntity; import br.com.senior.erpxcomven.pedido.EnumAggregationStatus; import br.com.senior.erpxcomven.pedido.EnumExternalAggregationType; import br.com.senior.erpxcomven.pedido.ExternalOrderAggregationEntity; import br.com.senior.erpxcomven.pedido.repository.E120infRepository; import br.com.senior.erpxcomven.pedido.repository.E120pedBasePedidoRepository; import br.com.senior.erpxcomven.pedido.repository.ExternalOrderAggregationRepository; import br.com.senior.erpxcomven.pedido.rms.components.RmsRoteirosCaller; import br.com.senior.erpxcomven.pedido.rms.dto.BuscaRoteirosPorLoteInput; import br.com.senior.erpxcomven.pedido.rms.dto.BuscaRoteirosPorLoteOutput; import br.com.senior.erpxcomven.pedido.rms.dto.RecBuscaRoteirosPorLote; import br.com.senior.erpxcomven.pedido.service.GetRoutingLicensePlateService; import br.com.senior.erpxcomven.utils.TranslationConstants; import br.com.senior.messaging.ErrorCategory; import br.com.senior.messaging.model.ServiceException; import br.com.senior.platform.translationhub.api.TranslationHubApi; import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicLong; @Service public class GetRoutingLicensePlateServiceImpl implements GetRoutingLicensePlateService { private static final Long MAX_LIMIT = 100L; @Inject private ExternalOrderAggregationRepository externalOrderAggregationRepository; @Inject private RmsRoteirosCaller rmsRoteirosCaller; @Inject private E120pedBasePedidoRepository e120pedBasePedidoRepository; @Inject private E120infRepository e120infRepository; @Inject private TranslationHubApi translation; @Override @Transactional public Long getRoutingLicensePlate(List<String> orderIds) { List<String> invalidOrders = new ArrayList<>(); List<Long> codigosAtividades = new ArrayList<>(); List<ExternalOrderAggregationEntity> aggregations = new ArrayList<>(); orderIds.stream().forEach(orderId -> retrieveOrderAndRmsIntegrations(invalidOrders, codigosAtividades, aggregations, orderId)); if (CollectionUtils.isNotEmpty(invalidOrders)) { throw new ServiceException(ErrorCategory.BAD_REQUEST, translation.getFormattedMessage(TranslationConstants.ORDER_INVALID_ROUTING_PLATE_SEARCH, String.join(",", invalidOrders))); } var pedidosAtualizados = new AtomicLong(); if (CollectionUtils.isNotEmpty(codigosAtividades)) { BuscaRoteirosPorLoteOutput output = buscaRoteiros(codigosAtividades); if (Objects.nonNull(output) && CollectionUtils.isNotEmpty(output.getContents())) { output.getContents().stream().forEach(roteiro -> { if(CollectionUtils.isNotEmpty(roteiro.getAtividades())){ updatePlateAndRmsIntegration(aggregations, pedidosAtualizados, roteiro); } }); } } return pedidosAtualizados.get(); } /* * Método que busca os pedidos que integram com o RMS na situação integrado e popula as listas de atividades, externalOrderAggregations, e pedidos invalidos */ private void retrieveOrderAndRmsIntegrations(List<String> invalidOrders, List<Long> codigosAtividades, List<ExternalOrderAggregationEntity> aggregations, String orderId) { E120pedEntity e120ped = e120pedBasePedidoRepository.findById(UUID.fromString(orderId)) .orElseThrow(() -> new ServiceException(ErrorCategory.BAD_REQUEST, translation.getFormattedMessage(TranslationConstants.SALE_NOT_FOUND_ID_X, orderId))); ExternalOrderAggregationEntity externalAggregation = externalOrderAggregationRepository.findByE120pedAndAggregationType(e120ped, EnumExternalAggregationType.RMS).orElse(null); if (Objects.isNull(externalAggregation) || EnumAggregationStatus.INTEGRADO != externalAggregation.getStatus()) { invalidOrders.add(String.valueOf(e120ped.getNumPed())); } else { if (StringUtils.isNotEmpty(externalAggregation.getExternalCode())) { aggregations.add(externalAggregation); codigosAtividades.add(Long.valueOf(externalAggregation.getExternalCode())); } } } /* * Método usado para filtrar o retorno da api de roteiros em busca das atividades e popular as placas dos pedidos relacionados a essa atividade */ private void updatePlateAndRmsIntegration(List<ExternalOrderAggregationEntity> aggregations, AtomicLong pedidosAtualizados, RecBuscaRoteirosPorLote roteiro) { roteiro.getAtividades().stream().forEach(atividade -> aggregations.stream().filter(externalOrder -> externalOrder.getExternalCode().equals(String.valueOf(atividade.getCdAtividade()))) .forEach(externalOrder -> { e120infRepository.updatePlaVeiById(externalOrder.getE120ped().getId(), roteiro.getNrPlaca()); updateRmsIntegration(externalOrder); pedidosAtualizados.getAndIncrement(); }) ); } private BuscaRoteirosPorLoteOutput buscaRoteiros(List<Long> codigosAtividades) { return rmsRoteirosCaller.buscaRoteirosPorLote(BuscaRoteirosPorLoteInput.builder() .desconsiderarRotNaoEntregue(false) .limit(MAX_LIMIT) .offset(0L) .listaCdAtividade(codigosAtividades) .build() ); } private void updateRmsIntegration(ExternalOrderAggregationEntity externalOrderAggregation) { externalOrderAggregation.setStatus(EnumAggregationStatus.FINALIZADO); externalOrderAggregation.setErrorMessage(null); externalOrderAggregationRepository.save(externalOrderAggregation); } }
Editor is loading...