diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/ru/mrfoxygmfr/warehouse_accounting/db/models/Operation.java | 19 |
1 files changed, 19 insertions, 0 deletions
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<OperationProduct> 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); + } } |