Всё работает, но надо сделать ☆☆refactoring☆☆

This commit is contained in:
Nikita Sergeev 2023-03-17 00:15:17 +04:00
parent 70a17c4b7c
commit 1b4d36a1e2
4 changed files with 73 additions and 29 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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<OrderProducts> 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<OrderProducts> 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));
}
}
}
}

View File

@ -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<ProductComponents> 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<ProductComponents> 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<ProductComponents> 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<ProductComponents> 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<ProductComponents> 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));
}
}
}
}