diff --git a/src/main/java/ip/labwork/student/controller/OrderController.java b/src/main/java/ip/labwork/student/controller/OrderController.java index d72cb19..1cea0be 100644 --- a/src/main/java/ip/labwork/student/controller/OrderController.java +++ b/src/main/java/ip/labwork/student/controller/OrderController.java @@ -50,10 +50,9 @@ public class OrderController { @RequestParam("count") Integer[] count, @RequestParam("prod") Long[] prod){ orderService.updateOrder(id, date, price); - orderProductsService.removeAll(id, prod); Order order = orderService.findOrder(id); for(int i = 0; i < prod.length; i++){ - orderProductsService.addOrderProducts(order, productService.findProduct(prod[i]),count[i]); + orderProductsService.update(order, productService.findProduct(prod[i]),count[i], prod); } return order; } diff --git a/src/main/java/ip/labwork/student/controller/ProductController.java b/src/main/java/ip/labwork/student/controller/ProductController.java index 881d548..15cd412 100644 --- a/src/main/java/ip/labwork/student/controller/ProductController.java +++ b/src/main/java/ip/labwork/student/controller/ProductController.java @@ -43,10 +43,10 @@ public class ProductController { @RequestParam("count") Integer[] count, @RequestParam("comp") Long[] comp){ productService.updateProduct(id, name, price); - productComponentsService.removeAll(id, comp); + Product product = productService.findProduct(id); for(int i = 0; i < comp.length; i++){ - productComponentsService.addProductComponents(product, componentService.findComponent(comp[i]),count[i]); + productComponentsService.update(product, componentService.findComponent(comp[i]),count[i], comp); } return product; } diff --git a/src/main/java/ip/labwork/student/service/OrderProductsService.java b/src/main/java/ip/labwork/student/service/OrderProductsService.java index cfdd10e..1978ba5 100644 --- a/src/main/java/ip/labwork/student/service/OrderProductsService.java +++ b/src/main/java/ip/labwork/student/service/OrderProductsService.java @@ -7,6 +7,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.Objects; @Service public class OrderProductsService { @@ -74,4 +75,38 @@ public class OrderProductsService { em.remove(temp.get(i)); } } + + public void update(Order order, Product product, Integer count, Long[] prod) { + for (int i = 0; i < prod.length; i++){ + List tem = em.createQuery("select o from OrderProducts o where o.id.orderId = " + order.getId() + " and o.id.productId = " + product.getId(), OrderProducts.class) + .getResultList(); + if (tem.size() != 0){ + final OrderProducts orderProducts = tem.get(0); + orderProducts.setCount(count); + em.merge(orderProducts); + }else{ + final OrderProducts orderProducts = new OrderProducts(order, product, count); + order.addProduct(orderProducts); + product.addOrder(orderProducts); + em.persist(orderProducts); + } + } + List newList = em.createQuery("select o from OrderProducts o where o.id.orderId = " + order.getId(), OrderProducts.class).getResultList(); + + for(int i =0; i < newList.size(); i++){ + boolean flag = false; + for (int j = 0; j < prod.length; j++){ + + if (Objects.equals(newList.get(i).getId().getProductId(), prod[j])) { + flag = true; + break; + } + } + if (!flag){ + newList.get(i).getProduct().removeOrder(newList.get(i)); + newList.get(i).getOrder().removeProducts(newList.get(i)); + em.remove(newList.get(i)); + } + } + } } diff --git a/src/main/java/ip/labwork/student/service/ProductComponentsService.java b/src/main/java/ip/labwork/student/service/ProductComponentsService.java index 12ffc65..5975191 100644 --- a/src/main/java/ip/labwork/student/service/ProductComponentsService.java +++ b/src/main/java/ip/labwork/student/service/ProductComponentsService.java @@ -11,6 +11,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; +import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -28,33 +29,12 @@ public class ProductComponentsService { return productComponents; } - /* @Transactional(readOnly = true) - public Component findProductComponent(Long productId, Long componentId) { - em.find(Product.class, id); - List productComponentsList = em.createQuery("select pc from ProductComponents pc", ProductComponents.class) - .getResultList(); - final ProductComponentsKey productComponentsKey = new ProductComponentsKey(id,) - final ProductComponents productComponents = em.find(ProductComponents.class, id); - if (component == null) { - throw new EntityNotFoundException(String.format("Component with id [%s] is not found", id)); - } - return component; - }*/ - @Transactional(readOnly = true) public List findAllProductComponents(Long id) { return em.createQuery("select p from ProductComponents p where id.productId = " + id, ProductComponents.class) .getResultList(); } - /* @Transactional - public ProductComponents updateProduct(Product product, Component component, Integer Count) { - - //final Component currentComponent = fin(id); - currentComponent.setComponentName(ProductName); - currentComponent.setPrice(Count); - return em.merge(currentComponent); - }*/ @Transactional public void deleteProduct(Product product) { int size = product.getComponents().size(); @@ -71,13 +51,43 @@ public class ProductComponentsService { } @Transactional public void removeAll(Long id, Long[] compid) { - /*em.createQuery("delete from ProductComponents p where p.id.productId = " + id + " and p.id.componentId not in "+ compid).executeUpdate(); - Product product = em.find(Product.class, id); - product.getComponents().clear(); - int s = 5;*/ List temp = findAllProductComponents(id); for(int i = 0; i < temp.size(); i++){ em.remove(temp.get(i)); } } + @Transactional + public void update(Product product, Component component, Integer count, Long[] comp) { + for (int i = 0; i < comp.length; i++){ + List tem = em.createQuery("select p from ProductComponents p where p.id.productId = " + product.getId() + " and p.id.componentId = " + component.getId(), ProductComponents.class) + .getResultList(); + if (tem.size() != 0){ + final ProductComponents productComponents = tem.get(0); + productComponents.setCount(count); + em.merge(productComponents); + }else{ + final ProductComponents productComponents = new ProductComponents(component, product, count); + product.addComponent(productComponents); + component.addProduct(productComponents); + em.persist(productComponents); + } + } + List newList = em.createQuery("select p from ProductComponents p where p.id.productId = " + product.getId(), ProductComponents.class).getResultList(); + + for(int i =0; i < newList.size(); i++){ + boolean flag = false; + for (int j = 0; j < comp.length; j++){ + + if (Objects.equals(newList.get(i).getId().getComponentId(), comp[j])) { + flag = true; + break; + } + } + if (!flag){ + newList.get(i).getComponent().removeProduct(newList.get(i)); + newList.get(i).getProduct().removeComponent(newList.get(i)); + em.remove(newList.get(i)); + } + } + } }