Доработки
This commit is contained in:
parent
ca7afdf94e
commit
1e958a8b94
@ -17,14 +17,14 @@ public class Orders {
|
|||||||
@Temporal(TemporalType.DATE)
|
@Temporal(TemporalType.DATE)
|
||||||
private Date dateOfOrder;
|
private Date dateOfOrder;
|
||||||
|
|
||||||
@ManyToOne(fetch = FetchType.EAGER)
|
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE})
|
||||||
@JoinColumn(name = "supplier_fk")
|
@JoinColumn(name = "supplier_fk")
|
||||||
private Supplier supplier;
|
private Supplier supplier;
|
||||||
@ManyToMany(fetch = FetchType.EAGER, cascade = {
|
@ManyToMany(fetch = FetchType.EAGER, cascade = {
|
||||||
CascadeType.PERSIST,
|
CascadeType.PERSIST,
|
||||||
CascadeType.MERGE
|
CascadeType.MERGE
|
||||||
})
|
})
|
||||||
@JoinTable(joinColumns = @JoinColumn(name = "order_fk"),
|
@JoinTable(name = "ordersAndProducts",joinColumns = @JoinColumn(name = "order_fk"),
|
||||||
inverseJoinColumns = @JoinColumn(name = "product_fk"))
|
inverseJoinColumns = @JoinColumn(name = "product_fk"))
|
||||||
private List<Product> products;
|
private List<Product> products;
|
||||||
|
|
||||||
@ -74,7 +74,6 @@ public class Orders {
|
|||||||
|
|
||||||
if(!Objects.equals(dateOfOrder.toString(), order.dateOfOrder.toString())) return false;
|
if(!Objects.equals(dateOfOrder.toString(), order.dateOfOrder.toString())) return false;
|
||||||
if(!Objects.equals(supplier, order.supplier)) return false;
|
if(!Objects.equals(supplier, order.supplier)) return false;
|
||||||
// if(!Objects.equals(products.size(), order.products.size())) return false;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,10 +6,13 @@ import com.example.demo.supply.models.Supplier;
|
|||||||
import jakarta.persistence.EntityManager;
|
import jakarta.persistence.EntityManager;
|
||||||
import jakarta.persistence.EntityNotFoundException;
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
import jakarta.persistence.PersistenceContext;
|
import jakarta.persistence.PersistenceContext;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.sql.Date;
|
import java.sql.Date;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@ -25,10 +28,34 @@ public class OrderService {
|
|||||||
final Orders order = new Orders(new Date(System.currentTimeMillis()));
|
final Orders order = new Orders(new Date(System.currentTimeMillis()));
|
||||||
order.setSupplier(supplier);
|
order.setSupplier(supplier);
|
||||||
em.persist(order);
|
em.persist(order);
|
||||||
em.merge(supplier);
|
|
||||||
return order;
|
return order;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//поставщики, у которых есть заказ на конкретный товар или несколько товаров
|
||||||
|
@Transactional
|
||||||
|
public List<Supplier> suppliers(List<Product> products){
|
||||||
|
List<Supplier> result = new ArrayList<>();
|
||||||
|
List<Orders> orders = findAllOrders();
|
||||||
|
for(Orders order : orders){
|
||||||
|
int k = 0;
|
||||||
|
for(Product product : products){
|
||||||
|
if(order.getProducts().contains(product)) k++;
|
||||||
|
}
|
||||||
|
if(k == products.size())
|
||||||
|
result.add(order.getSupplier());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Transactional
|
||||||
|
// public List<Supplier> suppliers(List<Product> products){
|
||||||
|
// return em.createQuery("SELECT o.supplier FROM Orders o join o.products as p WHERE p = :products ", Supplier.class)
|
||||||
|
// .setParameter("products", products).getResultList();
|
||||||
|
// return em.createQuery("SELECT o.supplier FROM Orders o WHERE o.products = :products ", Supplier.class)
|
||||||
|
// .setParameter("products", products).getResultList();
|
||||||
|
// }
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Orders addProduct(Long id, Product product) {
|
public Orders addProduct(Long id, Product product) {
|
||||||
if (product == null) {
|
if (product == null) {
|
||||||
@ -36,8 +63,6 @@ public class OrderService {
|
|||||||
}
|
}
|
||||||
final Orders currentOrder = findOrder(id);
|
final Orders currentOrder = findOrder(id);
|
||||||
currentOrder.addProduct(product);
|
currentOrder.addProduct(product);
|
||||||
product.getOrders().add(currentOrder);
|
|
||||||
em.merge(product);
|
|
||||||
return em.merge(currentOrder);
|
return em.merge(currentOrder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@ -35,10 +36,46 @@ public class Tests {
|
|||||||
Orders order = orderService.addOrder(supplierService.findSupplier(supplier.getId()));
|
Orders order = orderService.addOrder(supplierService.findSupplier(supplier.getId()));
|
||||||
order = orderService.addProduct(order.getId(), product1);
|
order = orderService.addProduct(order.getId(), product1);
|
||||||
order = orderService.addProduct(order.getId(), product2);
|
order = orderService.addProduct(order.getId(), product2);
|
||||||
// List<Product> products1 = order.getProducts();
|
|
||||||
|
|
||||||
final Orders order2 = orderService.findOrder(order.getId());
|
final Orders order2 = orderService.findOrder(order.getId());
|
||||||
// List<Product> products2 = order2.getProducts();
|
|
||||||
Assertions.assertEquals(order, order2);
|
Assertions.assertEquals(order, order2);
|
||||||
|
productService.deleteAll();
|
||||||
|
orderService.deleteAll();
|
||||||
|
supplierService.deleteAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test(){
|
||||||
|
productService.deleteAll();
|
||||||
|
orderService.deleteAll();
|
||||||
|
supplierService.deleteAll();
|
||||||
|
/
|
||||||
|
final Product product1 = productService.addProduct("Huawei Band 3", 2000.13);
|
||||||
|
final Product product2 = productService.addProduct("Samsung A2", 22000.56);
|
||||||
|
final Product product3 = productService.addProduct("Redmond f3", 2000.13);
|
||||||
|
final Product product4 = productService.addProduct("Asus red9", 22000.56);
|
||||||
|
|
||||||
|
final Supplier supplier1 = supplierService.addSupplier("SuperSupplier1", 325453);
|
||||||
|
final Supplier supplier2 = supplierService.addSupplier("SuperSupplier2", 545455);
|
||||||
|
final Supplier supplier3 = supplierService.addSupplier("SuperSupplier3", 122122);
|
||||||
|
|
||||||
|
Orders order1 = orderService.addOrder(supplierService.findSupplier(supplier1.getId()));
|
||||||
|
orderService.addProduct(order1.getId(), product1);
|
||||||
|
orderService.addProduct(order1.getId(), product2);
|
||||||
|
|
||||||
|
Orders order2 = orderService.addOrder(supplierService.findSupplier(supplier2.getId()));
|
||||||
|
orderService.addProduct(order2.getId(), product3);
|
||||||
|
|
||||||
|
Orders order3 = orderService.addOrder(supplierService.findSupplier(supplier1.getId()));
|
||||||
|
orderService.addProduct(order3.getId(), product3);
|
||||||
|
orderService.addProduct(order3.getId(), product4);
|
||||||
|
|
||||||
|
List<Product> products = new ArrayList<>();
|
||||||
|
products.add(productService.findProduct(product3.getId()));
|
||||||
|
List<Supplier> suppliers = orderService.suppliers(products);
|
||||||
|
Assertions.assertEquals(suppliers.size(), 2);
|
||||||
|
|
||||||
|
productService.deleteAll();
|
||||||
|
orderService.deleteAll();
|
||||||
|
supplierService.deleteAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package com.example.demo;
|
package com.example.demo;
|
||||||
|
|
||||||
import com.example.demo.supply.models.Supplier;
|
import com.example.demo.supply.models.Supplier;
|
||||||
|
import com.example.demo.supply.services.OrderService;
|
||||||
|
import com.example.demo.supply.services.ProductService;
|
||||||
import com.example.demo.supply.services.SupplierService;
|
import com.example.demo.supply.services.SupplierService;
|
||||||
import jakarta.persistence.EntityNotFoundException;
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
@ -14,6 +16,10 @@ import java.util.List;
|
|||||||
public class TestsSupplier {
|
public class TestsSupplier {
|
||||||
@Autowired
|
@Autowired
|
||||||
private SupplierService supplierService;
|
private SupplierService supplierService;
|
||||||
|
@Autowired
|
||||||
|
private OrderService orderService;
|
||||||
|
@Autowired
|
||||||
|
private ProductService productService;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testSupplier(){
|
void testSupplier(){
|
||||||
|
Loading…
Reference in New Issue
Block a user