From 2e5df4f22c9dd1997eb61ce1e92b483a4a9ccd4e Mon Sep 17 00:00:00 2001 From: mrfoxygmfr Date: Mon, 24 Mar 2025 12:21:16 +0300 Subject: feat(db/models): addProduct method for Operation --- .../warehouse_accounting/db/models/Operation.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/main') diff --git a/src/main/java/ru/mrfoxygmfr/warehouse_accounting/db/models/Operation.java b/src/main/java/ru/mrfoxygmfr/warehouse_accounting/db/models/Operation.java index 22116f8..31ec626 100644 --- a/src/main/java/ru/mrfoxygmfr/warehouse_accounting/db/models/Operation.java +++ b/src/main/java/ru/mrfoxygmfr/warehouse_accounting/db/models/Operation.java @@ -6,7 +6,9 @@ import org.hibernate.annotations.CreationTimestamp; import org.hibernate.annotations.UpdateTimestamp; import java.sql.Timestamp; +import java.util.HashSet; import java.util.Set; +import java.util.stream.Collectors; @Entity @Table(name = "operations") @@ -61,4 +63,21 @@ public class Operation { @OneToMany(mappedBy = "operation", cascade=CascadeType.ALL, orphanRemoval = true) @ToString.Exclude private Set products; + + public void updateProduct(OperationProduct product) { + if (products == null) { + products = new HashSet<>(); + } + + products = products.parallelStream() + .filter((p) -> p.getProduct() != product.getProduct()) + .collect(Collectors.toSet()); + + if (product.getAmount() == 0) { + return; + } + + products.add(product); + product.setOperation(this); + } } -- cgit mrf-deployment