This commit is contained in:
@@ -2,10 +2,13 @@ package ru.soune.nocopy.entity.user;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
import org.hibernate.annotations.ColumnDefault;
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Entity
|
||||
@Table(name = "protect_check")
|
||||
@@ -25,11 +28,6 @@ public class ProtectedFileCheck {
|
||||
@JoinColumn(name = "user_id", unique = true, nullable = false)
|
||||
private User user;
|
||||
|
||||
@Column(nullable = false)
|
||||
@ColumnDefault(value = "10")
|
||||
@Builder.Default
|
||||
private Integer limitCheck = 10;
|
||||
|
||||
@Column(nullable = false)
|
||||
@Builder.Default
|
||||
private Integer countChecked = 0;
|
||||
@@ -40,14 +38,66 @@ public class ProtectedFileCheck {
|
||||
@Version
|
||||
private Long version;
|
||||
|
||||
public boolean incrementCheck() {
|
||||
if (this.limitCheck <= 0) {
|
||||
return false;
|
||||
}
|
||||
@ElementCollection
|
||||
@CollectionTable(
|
||||
name = "protection_usage_by_type",
|
||||
joinColumns = @JoinColumn(name = "protection_usage_id"))
|
||||
@MapKeyColumn(name = "file_type")
|
||||
@Column(name = "count")
|
||||
@Builder.Default
|
||||
private Map<String, Integer> usageByType = new HashMap<>();
|
||||
|
||||
@Column(name = "period_start_date")
|
||||
@Builder.Default
|
||||
private LocalDate periodStartDate = LocalDate.now();
|
||||
|
||||
@Column(name = "period_count")
|
||||
@Builder.Default
|
||||
private Integer periodCount = 0;
|
||||
|
||||
public synchronized boolean writeCheck(String fileType) {
|
||||
resetPeriodIfNeeded();
|
||||
|
||||
countChecked++;
|
||||
periodCount++;
|
||||
|
||||
usageByType.merge(fileType, 1, Integer::sum);
|
||||
|
||||
lastCheckAt = LocalDateTime.now();
|
||||
|
||||
this.countChecked++;
|
||||
this.limitCheck--;
|
||||
this.lastCheckAt = LocalDateTime.now();
|
||||
return true;
|
||||
}
|
||||
|
||||
private void resetPeriodIfNeeded() {
|
||||
LocalDate today = LocalDate.now();
|
||||
|
||||
if (periodStartDate == null ||
|
||||
ChronoUnit.DAYS.between(periodStartDate, today) >= 30) {
|
||||
|
||||
cleanupOldData();
|
||||
|
||||
periodStartDate = today;
|
||||
periodCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void cleanupOldData() {
|
||||
if (usageByType != null && !usageByType.isEmpty()) {
|
||||
usageByType.clear();
|
||||
periodCount = 0;
|
||||
}
|
||||
|
||||
lastCheckAt = LocalDateTime.now();
|
||||
}
|
||||
|
||||
|
||||
public int getPeriodCount() {
|
||||
resetPeriodIfNeeded();
|
||||
return periodCount;
|
||||
}
|
||||
|
||||
public LocalDate getPeriodStartDate() {
|
||||
resetPeriodIfNeeded();
|
||||
return periodStartDate;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user