@@ -151,3 +151,21 @@ INSERT INTO referral_levels (id, name, min_invitees, reward_percentage) VALUES
|
||||
('silver', 'SILVER', 6, 18),
|
||||
('gold', 'GOLD', 16, 22),
|
||||
('platinum', 'PLATINUM', 50, 25);
|
||||
|
||||
-----------------
|
||||
Скрипт,для нумерации для уже сохданных файлов
|
||||
WITH max_id AS (
|
||||
SELECT COALESCE(MAX(support_id), 0) as max_val FROM file_entities
|
||||
),
|
||||
-- Нумеруем только NULL записи, начиная с max_val + 1
|
||||
numbered AS (
|
||||
SELECT
|
||||
f.id,
|
||||
(SELECT max_val FROM max_id) + ROW_NUMBER() OVER (ORDER BY f.created_at, f.id) as new_support_id
|
||||
FROM file_entities f
|
||||
WHERE f.support_id IS NULL
|
||||
)
|
||||
UPDATE file_entities
|
||||
SET support_id = numbered.new_support_id
|
||||
FROM numbered
|
||||
WHERE file_entities.id = numbered.id;
|
||||
@@ -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