many of done

This commit is contained in:
Калышев Ян 2023-05-14 19:07:45 +04:00
parent 3ad6e77a70
commit a9284a5971
7 changed files with 55 additions and 32 deletions

View File

@ -20,9 +20,6 @@ repositories {
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mongodb:mongodb-driver-sync:4.9.0'
implementation 'org.mongodb:mongodb-driver-core:4.9.0'
implementation 'org.mongodb:mongodb-jdbc:2.0.1'
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb:3.0.4' implementation 'org.springframework.boot:spring-boot-starter-data-mongodb:3.0.4'
compileOnly 'org.projectlombok:lombok' compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools' developmentOnly 'org.springframework.boot:spring-boot-devtools'

View File

@ -2,9 +2,18 @@ package com.subd.subd.Controllers;
import com.subd.subd.Models.Order; import com.subd.subd.Models.Order;
import com.subd.subd.Repositories.OrderRepository; import com.subd.subd.Repositories.OrderRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.mongodb.core.query.Query;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
@ -13,16 +22,31 @@ import java.util.List;
@RestController @RestController
public class CustomRequest { public class CustomRequest {
private final OrderRepository orderRepository; private final OrderRepository orderRepository;
public CustomRequest(OrderRepository orderRepository) { private final MongoTemplate mongoTemplate;
private static final Logger log = LoggerFactory.getLogger(CustomRequest.class);
public CustomRequest(OrderRepository orderRepository, MongoTemplate mongoTemplate) {
this.orderRepository = orderRepository; this.orderRepository = orderRepository;
this.mongoTemplate = mongoTemplate;
} }
@GetMapping("/custom") @GetMapping("/custom")
public Long performCustomRequest(String date, Double value, String status, String orderBy) throws ParseException { public Long performCustomRequest(@RequestParam("date") String date,
@RequestParam("value") Double value,
@RequestParam("status") String status,
@RequestParam("orderBy") String orderBy) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-M-d"); SimpleDateFormat format = new SimpleDateFormat("yyyy-M-d");
Date date1 = format.parse(date); Date date1 = format.parse(date);
Query query = new Query();
query.addCriteria(Criteria.where("date").gt(date1))
.addCriteria(Criteria.where("value").gt(value))
.addCriteria(Criteria.where("status").is(status));
query.with(Sort.by(Sort.Direction.ASC, orderBy));
Long start = System.nanoTime(); Long start = System.nanoTime();
orderRepository.customAction(date1, value, status, orderBy); // orderRepository.customAction(date1, value, status, orderBy);
List<Order> orders = mongoTemplate.find(query, Order.class);
Long finish = System.nanoTime(); Long finish = System.nanoTime();
for (Order order : orders) {
log.info(order.toString());
}
return finish - start; return finish - start;
} }
} }

View File

@ -3,6 +3,7 @@ package com.subd.subd.Models;
import com.subd.subd.Dtos.CarDto; import com.subd.subd.Dtos.CarDto;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.DocumentReference;
import java.util.Objects; import java.util.Objects;
@ -12,8 +13,7 @@ public class Car {
private String id; private String id;
private String gosNumber; private String gosNumber;
private String vin; private String vin;
@OneToOne() @DocumentReference()
@JoinColumn(name = "Driver_id", referencedColumnName = "id")
private Driver driver; private Driver driver;
public Car() {} public Car() {}
public Car(String gosNumber, String vin, Driver driver) { public Car(String gosNumber, String vin, Driver driver) {

View File

@ -1,8 +1,10 @@
package com.subd.subd.Models; package com.subd.subd.Models;
import jakarta.annotation.Nullable;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Unwrapped;
import org.springframework.lang.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -14,9 +16,8 @@ public class Client {
private String id; private String id;
private String name; private String name;
private String phone; private String phone;
@Nullable @Unwrapped.Nullable
private String email; private String email;
@OneToMany(cascade = {CascadeType.MERGE})
private List<Order> orders; private List<Order> orders;
public Client() {} public Client() {}
public Client(String name, String phone, @Nullable String email) { public Client(String name, String phone, @Nullable String email) {

View File

@ -1,8 +1,12 @@
package com.subd.subd.Models; package com.subd.subd.Models;
import jakarta.annotation.Nullable;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.DocumentReference;
import org.springframework.data.mongodb.core.mapping.Unwrapped;
import org.springframework.lang.Nullable;
import java.util.Date; import java.util.Date;
import java.util.Objects; import java.util.Objects;
@ -14,18 +18,13 @@ public class Order {
private Double value; private Double value;
private String status; private String status;
private Date date; private Date date;
@ManyToOne( cascade = {CascadeType.MERGE}, fetch = FetchType.EAGER) @DocumentReference
@JoinColumn(name = "Client_id", nullable = true)
private Client client; private Client client;
@OneToOne() @DocumentReference
@JoinColumn(name = "sourcePickUpPoint_id", referencedColumnName = "id")
private PickUpPoint sourcePickUpPoint; private PickUpPoint sourcePickUpPoint;
@Nullable @DocumentReference
@OneToOne()
@JoinColumn(name = "destPickUpPoint_id", referencedColumnName = "id")
private PickUpPoint destPickUpPoint; private PickUpPoint destPickUpPoint;
@OneToOne() @DocumentReference
@JoinColumn(name = "Car_id", referencedColumnName = "id")
private Car car; private Car car;
public Order() {} public Order() {}
public Order(Double value, String status, Date date, Client client, PickUpPoint sourcePickUpPoint, @Nullable PickUpPoint destPickUpPoint, Car car) { public Order(Double value, String status, Date date, Client client, PickUpPoint sourcePickUpPoint, @Nullable PickUpPoint destPickUpPoint, Car car) {

View File

@ -1,21 +1,23 @@
package com.subd.subd.Repositories; package com.subd.subd.Repositories;
import com.subd.subd.Models.Order; import com.subd.subd.Models.Order;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
public interface OrderRepository extends JpaRepository<Order, Long> { public interface OrderRepository extends MongoRepository<Order, String> {
@Query(value = "SELECT \"order\".ID, \"order\".DATE, \"order\".STATUS, \"car\".GOS_NUMBER, \"driver\".NAME as driver_name, \"client\".NAME as client_name\n" + @Query(value = """
"FROM \"order\"\n" + db.Order.find({date: {$gt: ISODate("2000-01-01")}, value: {$gt: 1}, status: "accepted"}).sort({date: 1}).forEach(function(order){
"JOIN CAR ON CAR.ID = \"order\".CAR_ID\n" + var client = db.Client.findOne({_id: order.client});
"JOIN DRIVER ON CAR.DRIVER_ID = DRIVER.ID\n" + var car=db.Car.findOne({_id: order.car});
"JOIN CLIENT ON CLIENT.ID = \"order\".CLIENT_ID\n" + car.driver = db.Driver.findOne({_id: car.driver});
"WHERE \"order\".DATE > :date AND \"order\".VALUE > :value AND \"order\".STATUS like :status\n" + var sourcePickUpPoint = db.PickUpPoint.findOne({_id: order.sourcePickUpPoint});
"ORDER BY :orderBy", nativeQuery = true) var destPickUpPoint = db.PickUpPoint.findOne({_id: order.destPickUpPoint});
print(order._id, order.value, order.status, order.date, client, car, sourcePickUpPoint, destPickUpPoint);
})""")
List<Object> customAction(@Param("date") Date date, List<Object> customAction(@Param("date") Date date,
@Param("value") Double value, @Param("value") Double value,
@Param("status") String status, @Param("status") String status,

View File

@ -42,7 +42,7 @@
let count = document.getElementById('measureCount').value; let count = document.getElementById('measureCount').value;
this.wholeTime = 0; this.wholeTime = 0;
for (let i = 0; i < count; i++) { for (let i = 0; i < count; i++) {
const response = axios.get("http://localhost:8080/custom?date=2023-2-1&value=1000&status=delivered&orderBy=date") const response = axios.get("http://localhost:8080/custom?date=2000-2-1&value=1000&status=delivered&orderBy=date")
.then(data => { .then(data => {
this.wholeTime += data.data; this.wholeTime += data.data;
}); });