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 {
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'
compileOnly 'org.projectlombok:lombok'
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.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.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.mongodb.core.query.Query;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@ -13,16 +22,31 @@ import java.util.List;
@RestController
public class CustomRequest {
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.mongoTemplate = mongoTemplate;
}
@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");
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();
orderRepository.customAction(date1, value, status, orderBy);
// orderRepository.customAction(date1, value, status, orderBy);
List<Order> orders = mongoTemplate.find(query, Order.class);
Long finish = System.nanoTime();
for (Order order : orders) {
log.info(order.toString());
}
return finish - start;
}
}

View File

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

View File

@ -1,8 +1,10 @@
package com.subd.subd.Models;
import jakarta.annotation.Nullable;
import org.springframework.data.annotation.Id;
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.List;
@ -14,9 +16,8 @@ public class Client {
private String id;
private String name;
private String phone;
@Nullable
@Unwrapped.Nullable
private String email;
@OneToMany(cascade = {CascadeType.MERGE})
private List<Order> orders;
public Client() {}
public Client(String name, String phone, @Nullable String email) {

View File

@ -1,8 +1,12 @@
package com.subd.subd.Models;
import jakarta.annotation.Nullable;
import org.springframework.data.annotation.Id;
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.Objects;
@ -14,18 +18,13 @@ public class Order {
private Double value;
private String status;
private Date date;
@ManyToOne( cascade = {CascadeType.MERGE}, fetch = FetchType.EAGER)
@JoinColumn(name = "Client_id", nullable = true)
@DocumentReference
private Client client;
@OneToOne()
@JoinColumn(name = "sourcePickUpPoint_id", referencedColumnName = "id")
@DocumentReference
private PickUpPoint sourcePickUpPoint;
@Nullable
@OneToOne()
@JoinColumn(name = "destPickUpPoint_id", referencedColumnName = "id")
@DocumentReference
private PickUpPoint destPickUpPoint;
@OneToOne()
@JoinColumn(name = "Car_id", referencedColumnName = "id")
@DocumentReference
private Car car;
public Order() {}
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;
import com.subd.subd.Models.Order;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.Date;
import java.util.List;
public interface OrderRepository extends JpaRepository<Order, Long> {
@Query(value = "SELECT \"order\".ID, \"order\".DATE, \"order\".STATUS, \"car\".GOS_NUMBER, \"driver\".NAME as driver_name, \"client\".NAME as client_name\n" +
"FROM \"order\"\n" +
"JOIN CAR ON CAR.ID = \"order\".CAR_ID\n" +
"JOIN DRIVER ON CAR.DRIVER_ID = DRIVER.ID\n" +
"JOIN CLIENT ON CLIENT.ID = \"order\".CLIENT_ID\n" +
"WHERE \"order\".DATE > :date AND \"order\".VALUE > :value AND \"order\".STATUS like :status\n" +
"ORDER BY :orderBy", nativeQuery = true)
public interface OrderRepository extends MongoRepository<Order, String> {
@Query(value = """
db.Order.find({date: {$gt: ISODate("2000-01-01")}, value: {$gt: 1}, status: "accepted"}).sort({date: 1}).forEach(function(order){
var client = db.Client.findOne({_id: order.client});
var car=db.Car.findOne({_id: order.car});
car.driver = db.Driver.findOne({_id: car.driver});
var sourcePickUpPoint = db.PickUpPoint.findOne({_id: order.sourcePickUpPoint});
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,
@Param("value") Double value,
@Param("status") String status,

View File

@ -42,7 +42,7 @@
let count = document.getElementById('measureCount').value;
this.wholeTime = 0;
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 => {
this.wholeTime += data.data;
});