directions1

This commit is contained in:
DyCTaTOR 2024-04-14 17:58:09 +04:00
parent 7ba6f53b0e
commit abd50a63ed
8 changed files with 75 additions and 10 deletions

View File

@ -5,18 +5,29 @@ import java.util.Objects;
import com.example.demo.core.model.BaseEntity;
import com.example.demo.department.model.DepartmentEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
@Entity
@Table(name = "directions")
public class DirectionsEntity extends BaseEntity {
@Column(nullable = false, unique = true, length = 9)
private String code;
@Column(nullable = false, unique = true, length = 80)
private String name;
@ManyToOne
@JoinColumn(name = "departmentId", nullable = false)
private DepartmentEntity department;
@Column(nullable = false, unique = true, length = 80)
private String things;
public DirectionsEntity() {
super();
}
public DirectionsEntity(Long id, String code, String name, DepartmentEntity department, String things) {
super(id);
this.code = code;
this.name = name;
this.department = department;

View File

@ -1,10 +1,31 @@
package com.example.demo.directions.repository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import com.example.demo.core.repository.MapRepository;
import com.example.demo.directions.model.DirectionsEntity;
@Repository
public class DirectionsRepository extends MapRepository<DirectionsEntity> {
public interface DirectionsRepository extends CrudRepository<DirectionsEntity, Long> {
Optional<OrderEntity> findOneByUserIdAndId(long userId, long id);
List<OrderEntity> findByUserId(long userId);
List<OrderEntity> findByUserIdAndTypeId(long userId, long typeId);
// select
// tpe.name,
// coalesce(sum(order.price), 0),
// coalesce(sum(order.count), 0)
// from types as tpe
// left join orders as order on tpe.id = order.type_id and order.user_id = ?
// group by tpe.name order by tpe.id
@Query("select "
+ "t as type, "
+ "coalesce(sum(o.price), 0) as totalPrice, "
+ "coalesce(sum(o.count), 0) as totalCount "
+ "from TypeEntity t left join OrderEntity o on o.type = t and o.user.id = ?1 "
+ "group by t order by t.id")
List<OrderGrouped> getOrdersTotalByType(long userId);
}

View File

@ -1 +1,20 @@
# Server
spring.main.banner-mode=off
server.port=8080
# Logger settings
# Available levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
logging.level.com.example.demo=DEBUG
# JPA Settings
spring.datasource.url=jdbc:h2:file:./data
spring.datasource.username=sa
spring.datasource.password=password
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.hibernate.ddl-auto=create
spring.jpa.open-in-view=false
# spring.jpa.show-sql=true
# spring.jpa.properties.hibernate.format_sql=true
# H2 console
spring.h2.console.enabled=true

View File

@ -1,4 +1,4 @@
package com.example.demo;
package com.example.demo.java;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;

View File

@ -1,4 +1,4 @@
package com.example.demo;
package com.example.demo.java;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;

View File

@ -1,4 +1,4 @@
package com.example.demo;
package com.example.demo.java;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;

View File

@ -1,4 +1,4 @@
package com.example.demo;
package com.example.demo.java;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;

View File

@ -0,0 +1,14 @@
# Server
spring.main.banner-mode=off
# Logger settings
# Available levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
logging.level.com.example.demo=DEBUG
# JPA Settings
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=password
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.hibernate.ddl-auto=create
spring.jpa.open-in-view=false