Untitled

mail@pastecode.io avatar
unknown
java
a year ago
3.6 kB
1
Indexable
@Override
    @Transactional
    public List<SuppliesDTO> saveMultiple(List<SuppliesDTO> suppliesDTOS) {
        log.debug("Request to save multiple supplies : {}", suppliesDTOS);

        for (SuppliesDTO suppliesDTO : suppliesDTOS)
            suppliesDTO.validateInput();

        Set<Long> idSuppliesGroups = suppliesDTOS.stream().map(SuppliesDTO::getIdSuppliesGroup).collect(Collectors.toSet());
        checkIfSupplyGroupExist(idSuppliesGroups);

        for (SuppliesDTO suppliesDTO : suppliesDTOS) {
            validateCreateMultiple(suppliesDTO);
        }

        Set<Long> idSuppliesGroupInSupplies = suppliesRepository.findIdSuppliesGroupInSupplies();

        Set<Long> intersection = new HashSet<>(idSuppliesGroups);
        intersection.retainAll(idSuppliesGroupInSupplies);

        Set<Long> remainder = new HashSet<>(idSuppliesGroups);
        remainder.removeAll(idSuppliesGroupInSupplies);

        
        List<Supplies> savedAll = new ArrayList<>();
        List<Supplies> suppliesLastGroup = suppliesRepository.findLastSuppliesByGroup(idSuppliesGroups);

        Map<Long, SupplySplitCode> lastCode = new HashMap<>();

        for (Supplies supplies : suppliesLastGroup) {
            String[] split = supplies.getCode().split(Constants.UNDERLINED);

            SupplySplitCode supplySplitCode = new SupplySplitCode();
            supplySplitCode.setCode(split[0]);
            supplySplitCode.setNumber(Integer.parseInt(split[1]));
            lastCode.put(supplies.getIdSuppliesGroup(), supplySplitCode);
        }

        Map<Long, List<SuppliesDTO>> hashMapSupplies = new HashMap<>();

        for (SuppliesDTO suppliesDTO : suppliesDTOS) {
            if (CollectionUtils.isEmpty(hashMapSupplies.get(suppliesDTO.getIdSuppliesGroup()))) {
                List<SuppliesDTO> suppliesDTOList = new ArrayList<>();
                suppliesDTOList.add(suppliesDTO);
                hashMapSupplies.put(suppliesDTO.getIdSuppliesGroup(), suppliesDTOList);
            } else {
                List<SuppliesDTO> suppliesDTOList = hashMapSupplies.get(suppliesDTO.getIdSuppliesGroup());
                suppliesDTOList.add(suppliesDTO);
                hashMapSupplies.put(suppliesDTO.getIdSuppliesGroup(), suppliesDTOList);
            }
        }

        for (Map.Entry<Long, List<SuppliesDTO>> longListEntry : hashMapSupplies.entrySet()) {
            if (!lastCode.containsKey(longListEntry.getKey())) {
                continue;
            }
            SupplySplitCode supplySplitCode = lastCode.get(longListEntry.getKey());

            List<SuppliesDTO> suppliesDTOList = longListEntry.getValue();
            int i = 1;
            for (SuppliesDTO suppliesDTO : suppliesDTOList) {
                String formattedNumber = String.format("%0" + NUM_DIGITS + "d", supplySplitCode.getNumber() + i);
                suppliesDTO.setCode(supplySplitCode.getCode() + Constants.UNDERLINED + formattedNumber);
                i++;
                savedAll.add(suppliesMapper.toCreateSupplies(suppliesDTO));
            }
        }

        savedAll = suppliesRepository.saveAll(savedAll);
        savedAll.forEach(supplies ->
            logService.writeLog(
                supplies.getId(),
                supplies,
                null,
                BaseService.getCurrentUser().getLogin(),
                "SuppliesServiceImpl.saveMultiple"
            )
        );
        return savedAll.stream().map(suppliesMapper::toDto).collect(Collectors.toList());
    }