Untitled
unknown
php
a year ago
5.1 kB
1
Indexable
Never
@Override @Transactional public List<SuppliesDTO> saveMultiple(List<SuppliesDTO> suppliesDTOS) { log.debug("Request to save multiple supplies: {}", suppliesDTOS); // Validate input for all supplies suppliesDTOS.forEach(SuppliesDTO::validateInput); // Get unique supply group IDs Set<Long> idSuppliesGroups = suppliesDTOS.stream() .map(SuppliesDTO::getIdSuppliesGroup) .collect(Collectors.toSet()); // Check if supply groups exist checkIfSupplyGroupsExist(idSuppliesGroups); // Find supply groups that already have supplies Set<Long> idSuppliesGroupInSupplies = new HashSet<>(suppliesRepository.findIdSuppliesGroupInSupplies()); // Separate supply groups into intersection and remainder Set<Long> intersection = new HashSet<>(idSuppliesGroups); intersection.retainAll(idSuppliesGroupInSupplies); Set<Long> remainder = new HashSet<>(idSuppliesGroups); remainder.removeAll(idSuppliesGroupInSupplies); List<Supplies> savedAll = new ArrayList<>(); // Process supplies for intersecting supply groups if (!intersection.isEmpty()) { Map<Long, SupplySplitCode> lastCode = getLastSupplyCodes(intersection); Map<Long, List<SuppliesDTO>> hashMapSupplies = groupSuppliesByGroupId(suppliesDTOS, intersection); for (Map.Entry<Long, List<SuppliesDTO>> entry : hashMapSupplies.entrySet()) { Long groupId = entry.getKey(); if (!lastCode.containsKey(groupId)) { continue; } SupplySplitCode supplySplitCode = lastCode.get(groupId); List<SuppliesDTO> suppliesDTOList = entry.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)); } } } // Process supplies for remaining supply groups if (!remainder.isEmpty()) { Map<Long, String> supplyGroupCodes = getSupplyGroupCodes(remainder); Map<Long, List<SuppliesDTO>> map = groupSuppliesByGroupId(suppliesDTOS, remainder); for (Map.Entry<Long, List<SuppliesDTO>> entry : map.entrySet()) { List<SuppliesDTO> suppliesDTOList = entry.getValue(); String supplyGroupCode = supplyGroupCodes.get(entry.getKey()); int i = 1; 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)); } } } // Save all supplies and log the changes 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()); } private Map<Long, SupplySplitCode> getLastSupplyCodes(Set<Long> supplyGroupIds) { List<Supplies> suppliesLastGroup = suppliesRepository.findLastSuppliesByGroup(supplyGroupIds); Map<Long, SupplySplitCode> lastCode = new HashMap<>(); for (Supplies supplies : suppliesLastGroup) { String supplyGroupCode = supplies.getCode(); int lastUnderlineIndex = supplyGroupCode.lastIndexOf(Constants.UNDERLINED); SupplySplitCode supplySplitCode = new SupplySplitCode(); supplySplitCode.setCode(supplyGroupCode.substring(0, lastUnderlineIndex)); supplySplitCode.setNumber(Integer.parseInt(supplyGroupCode.substring(lastUnderlineIndex + 1))); lastCode.put(supplies.getIdSuppliesGroup(), supplySplitCode); } return lastCode; } private Map<Long, List<SuppliesDTO>> groupSuppliesByGroupId(List<SuppliesDTO> suppliesDTOList, Set<Long> groupIds) { Map<Long, List<SuppliesDTO>> map = new HashMap<>(); for (SuppliesDTO suppliesDTO : suppliesDTOList) { if (groupIds.contains(suppliesDTO.getIdSuppliesGroup())) { map.computeIfAbsent(suppliesDTO.getIdSuppliesGroup(), k -> new ArrayList<>()).add(suppliesDTO); } } return map; } private Map<Long, String> getSupplyGroupCodes(Set<Long> groupIds) { List<SuppliesGroup> suppliesGroups = suppliesGroupRepository.findActiveSupplyGroups(groupIds); Map<Long, String> hashMap = new HashMap<>(); for (SuppliesGroup suppliesGroup : suppliesGroups) { hashMap.put(suppliesGroup.getId(), suppliesGroup.getCode()); } return hashMap; }