#1 - доработка фильтров
This commit is contained in:
@@ -16,7 +16,7 @@ public class CocktailFilterRequestDto {
|
||||
private List<String> category;
|
||||
private List<String> alcohol;
|
||||
private List<String> tags;
|
||||
private Integer iCount;
|
||||
private Integer notHaveCount;
|
||||
private List<String> ingredient;
|
||||
private String inMenu;
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import ru.kayashov.bar.controller.IngredientController;
|
||||
import ru.kayashov.bar.controller.dto.cocktail.CocktailFilterRequestDto;
|
||||
import ru.kayashov.bar.controller.dto.cocktail.CocktailForIngredientModalDto;
|
||||
import ru.kayashov.bar.controller.dto.cocktail.CocktailForListResponseDto;
|
||||
@@ -57,6 +56,8 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@@ -162,8 +163,9 @@ public class CocktailService {
|
||||
}
|
||||
}
|
||||
|
||||
if (dto.getICount() != null) {
|
||||
log.info("iii");
|
||||
if (dto.getNotHaveCount() != null) {
|
||||
List<Long> approveCocktail = findICountCocktailIds(dto.getNotHaveCount(), visitor, dto.getIngredient());
|
||||
predicates.add(root.get("id").in(approveCocktail));
|
||||
}
|
||||
//
|
||||
// if (!dto.getSortField().equals("name") || !dto.getSortOrder().equals("asc")) {
|
||||
@@ -189,10 +191,46 @@ public class CocktailService {
|
||||
List<CocktailEntity> cocktailEntities = query.getResultList();
|
||||
|
||||
log.info("Найдено {} коктейлей", cocktailEntities.size());
|
||||
// return Collections.emptyList();
|
||||
return cocktailEntities;
|
||||
}
|
||||
|
||||
private List<Long> findICountCocktailIds(Integer iCount, Visitor visitor, List<String> ingredientFilter) {
|
||||
List<Long> allowedIngredient = visitor.getResidents().stream()
|
||||
.filter(BarResident::getActive)
|
||||
.map(BarResident::getBar)
|
||||
.map(BarEntity::getIngredients)
|
||||
.flatMap(List::stream)
|
||||
.map(BarIngredientStorage::getIngredient)
|
||||
.map(IngredientEntity::getId)
|
||||
.toList();
|
||||
|
||||
Stream<List<ReceiptEntity>> receiptStream = receiptRepository.findAll()
|
||||
.stream()
|
||||
.collect(Collectors.groupingBy(k -> k.getCocktail().getId()))
|
||||
.values()
|
||||
.stream()
|
||||
.filter(l -> getCountNotHaveIngredient(allowedIngredient, l).equals(iCount));
|
||||
|
||||
if (!ingredientFilter.isEmpty()) {
|
||||
receiptStream = receiptStream.filter(l -> findIngredientInReceipts(ingredientFilter, l));
|
||||
}
|
||||
return receiptStream
|
||||
.map(l -> l.get(0))
|
||||
.map(ReceiptEntity::getCocktail)
|
||||
.map(CocktailEntity::getId)
|
||||
.toList();
|
||||
}
|
||||
|
||||
private boolean findIngredientInReceipts(List<String> ingredientFilter, List<ReceiptEntity> receipts) {
|
||||
return receipts.stream().anyMatch(r -> ingredientFilter.contains(r.getIngredient().getName()));
|
||||
}
|
||||
|
||||
private Integer getCountNotHaveIngredient(List<Long> allowedIngredientIds, List<ReceiptEntity> receipts) {
|
||||
return Math.toIntExact(receipts.size() - receipts.stream()
|
||||
.filter(r -> allowedIngredientIds.contains(r.getIngredient().getId()))
|
||||
.count());
|
||||
}
|
||||
|
||||
private List<Long> getStopListIds(Visitor visitor) {
|
||||
return visitor.getResidents()
|
||||
.stream()
|
||||
|
||||
Reference in New Issue
Block a user