Commit 7605fbad authored by renjie's avatar renjie

操作日志

parent 2bb22ccb
......@@ -5,12 +5,14 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;
public interface LoginHistoryRepository extends MongoRepository<LoginHistory, String> {
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);
}
......@@ -8,8 +8,8 @@ import org.springframework.data.mongodb.repository.MongoRepository;
public interface OperationHistoryRepository extends MongoRepository<OperationHistory, String> {
OperationHistory findFirstByAccount(String account);
Page<OperationHistory> findByAdministratorOrderByOperationTimeDesc(String administrator, Pageable pageable);
Page<OperationHistory> findByAccountOrderByOperationTimeDesc(String account, Pageable pageable);
Page<OperationHistory> findByAdministratorAndOperationTimeGreaterThanOrderByOperationTimeDesc(String administrator, long time, Pageable pageable);
Page<OperationHistory> findByAccountAndOperationTimeGreaterThanOrderByOperationTimeDesc(String account, long time, Pageable pageable);
int countByAccount(String account);
}
......@@ -17,6 +17,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
......@@ -39,7 +40,7 @@ public class HistoryServiceImpl implements HistoryService {
if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
int num = loginHistoryRepository.countByAccount(username);
if (num > 10)
if (num > 10000)
loginHistoryRepository.delete(loginHistoryRepository.findFirstByAccount(username));
LoginHistory loginHistory = new LoginHistory(loginHistoryDto);
loginHistory.setLoginTime(Instant.now().toEpochMilli());
......@@ -83,10 +84,11 @@ public class HistoryServiceImpl implements HistoryService {
Pageable pageable = PageRequest.of(historyListRequestDto.getPage(), historyListRequestDto.getAmount());
HistoryPageResultDto historyPageResultDto = new HistoryPageResultDto();
Page<LoginHistory> loginHistoryPage;
long time = Instant.now().atZone(ZoneOffset.UTC).minusDays(historyListRequestDto.getDay()).toInstant().toEpochMilli();
if (account.getParent() == null) {
loginHistoryPage = loginHistoryRepository.findByAdministratorOrderByLoginTime(username, pageable);
loginHistoryPage = loginHistoryRepository.findByAdministratorAndLoginTimeGreaterThanOrderByLoginTime(username, time, pageable);
} else {
loginHistoryPage = loginHistoryRepository.findByAccountOrderByLoginTime(username, pageable);
loginHistoryPage = loginHistoryRepository.findByAccountAndLoginTimeGreaterThanOrderByLoginTime(username, time, pageable);
}
List<LoginHistoryDto> loginHistoryDtoList = new ArrayList<>();
......@@ -114,10 +116,11 @@ public class HistoryServiceImpl implements HistoryService {
Pageable pageable = PageRequest.of(historyListRequestDto.getPage(), historyListRequestDto.getAmount());
HistoryPageResultDto historyPageResultDto = new HistoryPageResultDto();
Page<OperationHistory> operationHistories;
long time = Instant.now().atZone(ZoneOffset.UTC).minusDays(historyListRequestDto.getDay()).toInstant().toEpochMilli();
if (account.getParent() == null) {
operationHistories = operationHistoryRepository.findByAdministratorOrderByOperationTimeDesc(username, pageable);
operationHistories = operationHistoryRepository.findByAdministratorAndOperationTimeGreaterThanOrderByOperationTimeDesc(username, time, pageable);
} else {
operationHistories = operationHistoryRepository.findByAccountOrderByOperationTimeDesc(username, pageable);
operationHistories = operationHistoryRepository.findByAccountAndOperationTimeGreaterThanOrderByOperationTimeDesc(username, time, pageable);
}
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