@@ -8,6 +8,7 @@ import org.springframework.data.annotation.LastModifiedDate;
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@@ -17,6 +18,9 @@ import java.time.LocalDateTime;
|
||||
@Table(name = "file_entities")
|
||||
@EntityListeners(AuditingEntityListener.class)
|
||||
public class FileEntity {
|
||||
|
||||
private static AtomicInteger supportIdCounter = new AtomicInteger(0);
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.UUID)
|
||||
private String id;
|
||||
@@ -83,6 +87,10 @@ public class FileEntity {
|
||||
|
||||
@PrePersist
|
||||
public void prePersist() {
|
||||
if (this.supportId == null) {
|
||||
this.supportId = supportIdCounter.incrementAndGet();
|
||||
}
|
||||
|
||||
if (this.status == null) {
|
||||
this.status = FileStatus.ACTIVE;
|
||||
}
|
||||
@@ -101,4 +109,8 @@ public class FileEntity {
|
||||
public void preUpdate() {
|
||||
this.updatedAt = LocalDateTime.now();
|
||||
}
|
||||
|
||||
public static void initializeCounter(int maxId) {
|
||||
supportIdCounter = new AtomicInteger(maxId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package ru.soune.nocopy.entity.file;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
||||
|
||||
@Component
|
||||
public class SupportIdInitializer {
|
||||
|
||||
@Autowired
|
||||
private FileEntityRepository fileRepository;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
Integer maxId = fileRepository.findMaxSupportId();
|
||||
if (maxId != null) {
|
||||
FileEntity.initializeCounter(maxId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,4 +58,7 @@ public interface FileEntityRepository extends JpaRepository<FileEntity, String>
|
||||
|
||||
@Query("SELECT f FROM FileEntity f WHERE f.mimeType = :mimeType")
|
||||
List<FileEntity> findByMimeType(String mimeType);
|
||||
|
||||
@Query("SELECT MAX(f.supportId) FROM FileEntity f")
|
||||
Integer findMaxSupportId();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user