Commit 7605fbad authored by renjie's avatar renjie

操作日志

parent 2bb22ccb
...@@ -5,12 +5,14 @@ import org.springframework.data.domain.Page; ...@@ -5,12 +5,14 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;
public interface LoginHistoryRepository extends MongoRepository<LoginHistory, String> { public interface LoginHistoryRepository extends MongoRepository<LoginHistory, String> {
LoginHistory findFirstByAccount(String account); LoginHistory findFirstByAccount(String account);
Page<LoginHistory> findByAdministratorOrderByLoginTime(String administrator, Pageable pageable); Page<LoginHistory> findByAdministratorAndLoginTimeGreaterThanOrderByLoginTime(String administrator, long time, Pageable pageable);
Page<LoginHistory> findByAccountOrderByLoginTime(String account, Pageable pageable); Page<LoginHistory> findByAccountAndLoginTimeGreaterThanOrderByLoginTime(String account, long time, Pageable pageable);
int countByAccount(String account); int countByAccount(String account);
} }
...@@ -8,8 +8,8 @@ import org.springframework.data.mongodb.repository.MongoRepository; ...@@ -8,8 +8,8 @@ import org.springframework.data.mongodb.repository.MongoRepository;
public interface OperationHistoryRepository extends MongoRepository<OperationHistory, String> { public interface OperationHistoryRepository extends MongoRepository<OperationHistory, String> {
OperationHistory findFirstByAccount(String account); OperationHistory findFirstByAccount(String account);
Page<OperationHistory> findByAdministratorOrderByOperationTimeDesc(String administrator, Pageable pageable); Page<OperationHistory> findByAdministratorAndOperationTimeGreaterThanOrderByOperationTimeDesc(String administrator, long time, Pageable pageable);
Page<OperationHistory> findByAccountOrderByOperationTimeDesc(String account, Pageable pageable); Page<OperationHistory> findByAccountAndOperationTimeGreaterThanOrderByOperationTimeDesc(String account, long time, Pageable pageable);
int countByAccount(String account); int countByAccount(String account);
} }
...@@ -17,6 +17,7 @@ import org.springframework.data.domain.Pageable; ...@@ -17,6 +17,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.Instant; import java.time.Instant;
import java.time.ZoneOffset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -39,7 +40,7 @@ public class HistoryServiceImpl implements HistoryService { ...@@ -39,7 +40,7 @@ public class HistoryServiceImpl implements HistoryService {
if (account == null) if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST); throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
int num = loginHistoryRepository.countByAccount(username); int num = loginHistoryRepository.countByAccount(username);
if (num > 10) if (num > 10000)
loginHistoryRepository.delete(loginHistoryRepository.findFirstByAccount(username)); loginHistoryRepository.delete(loginHistoryRepository.findFirstByAccount(username));
LoginHistory loginHistory = new LoginHistory(loginHistoryDto); LoginHistory loginHistory = new LoginHistory(loginHistoryDto);
loginHistory.setLoginTime(Instant.now().toEpochMilli()); loginHistory.setLoginTime(Instant.now().toEpochMilli());
...@@ -83,10 +84,11 @@ public class HistoryServiceImpl implements HistoryService { ...@@ -83,10 +84,11 @@ public class HistoryServiceImpl implements HistoryService {
Pageable pageable = PageRequest.of(historyListRequestDto.getPage(), historyListRequestDto.getAmount()); Pageable pageable = PageRequest.of(historyListRequestDto.getPage(), historyListRequestDto.getAmount());
HistoryPageResultDto historyPageResultDto = new HistoryPageResultDto(); HistoryPageResultDto historyPageResultDto = new HistoryPageResultDto();
Page<LoginHistory> loginHistoryPage; Page<LoginHistory> loginHistoryPage;
long time = Instant.now().atZone(ZoneOffset.UTC).minusDays(historyListRequestDto.getDay()).toInstant().toEpochMilli();
if (account.getParent() == null) { if (account.getParent() == null) {
loginHistoryPage = loginHistoryRepository.findByAdministratorOrderByLoginTime(username, pageable); loginHistoryPage = loginHistoryRepository.findByAdministratorAndLoginTimeGreaterThanOrderByLoginTime(username, time, pageable);
} else { } else {
loginHistoryPage = loginHistoryRepository.findByAccountOrderByLoginTime(username, pageable); loginHistoryPage = loginHistoryRepository.findByAccountAndLoginTimeGreaterThanOrderByLoginTime(username, time, pageable);
} }
List<LoginHistoryDto> loginHistoryDtoList = new ArrayList<>(); List<LoginHistoryDto> loginHistoryDtoList = new ArrayList<>();
...@@ -114,10 +116,11 @@ public class HistoryServiceImpl implements HistoryService { ...@@ -114,10 +116,11 @@ public class HistoryServiceImpl implements HistoryService {
Pageable pageable = PageRequest.of(historyListRequestDto.getPage(), historyListRequestDto.getAmount()); Pageable pageable = PageRequest.of(historyListRequestDto.getPage(), historyListRequestDto.getAmount());
HistoryPageResultDto historyPageResultDto = new HistoryPageResultDto(); HistoryPageResultDto historyPageResultDto = new HistoryPageResultDto();
Page<OperationHistory> operationHistories; Page<OperationHistory> operationHistories;
long time = Instant.now().atZone(ZoneOffset.UTC).minusDays(historyListRequestDto.getDay()).toInstant().toEpochMilli();
if (account.getParent() == null) { if (account.getParent() == null) {
operationHistories = operationHistoryRepository.findByAdministratorOrderByOperationTimeDesc(username, pageable); operationHistories = operationHistoryRepository.findByAdministratorAndOperationTimeGreaterThanOrderByOperationTimeDesc(username, time, pageable);
} else { } else {
operationHistories = operationHistoryRepository.findByAccountOrderByOperationTimeDesc(username, pageable); operationHistories = operationHistoryRepository.findByAccountAndOperationTimeGreaterThanOrderByOperationTimeDesc(username, time, pageable);
} }
List<OperationHistoryDto> operationHistoryDtos = new ArrayList<>(); List<OperationHistoryDto> operationHistoryDtos = new ArrayList<>();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment