Untitled

mail@pastecode.io avatar
unknown
java
a year ago
5.7 kB
2
Indexable
Never
@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<>();

        if (!intersection.isEmpty()) {
            List<Supplies> suppliesLastGroup = suppliesRepository.findLastSuppliesByGroup(intersection);
            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[split.length - 1]));
                lastCode.put(supplies.getIdSuppliesGroup(), supplySplitCode);
            }

            Map<Long, List<SuppliesDTO>> hashMapSupplies = new HashMap<>();
            for (SuppliesDTO suppliesDTO : suppliesDTOS) {
                if (!intersection.contains(suppliesDTO.getIdSuppliesGroup())) continue;

                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));
                }
            }
        }

        if (!remainder.isEmpty()) {
            List<SuppliesGroup> suppliesGroups = suppliesGroupRepository.findActiveSupplyGroups(remainder);
            Map<Long, String> hashMap = new HashMap<>();
            for (SuppliesGroup suppliesGroup : suppliesGroups) {
                hashMap.put(suppliesGroup.getId(), suppliesGroup.getCode());
            }

            Map<Long, List<SuppliesDTO>> map = new HashMap<>();
            for (SuppliesDTO suppliesDTO : suppliesDTOS) {
                if (!remainder.contains(suppliesDTO.getIdSuppliesGroup())) continue;

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

            for (Map.Entry<Long, List<SuppliesDTO>> longListEntry : map.entrySet()) {
                List<SuppliesDTO> suppliesDTOList = longListEntry.getValue();

                int i = 1;
                String supplyGroupCode = hashMap.get(longListEntry.getKey());
                for (SuppliesDTO suppliesDTO : suppliesDTOList) {
                    String formattedNumber = String.format("%0" + NUM_DIGITS + "d", i);
                    suppliesDTO.setCode(supplyGroupCode + 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());
    }