package cn.novalon.gym.manage.sys.core.service.impl;

import cn.novalon.gym.manage.sys.core.domain.SysExceptionLog;
import cn.novalon.gym.manage.sys.core.repository.ISysExceptionLogRepository;
import cn.novalon.gym.manage.sys.core.service.ISysExceptionLogService;
import cn.novalon.gym.manage.common.dto.PageRequest;
import cn.novalon.gym.manage.common.dto.PageResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;

/**
 * 异常日志服务实现类
 * 
 * @author 张翔
 * @date 2026-03-14
 */
@Service
public class SysExceptionLogService implements ISysExceptionLogService {

    private static final Logger logger = LoggerFactory.getLogger(SysExceptionLogService.class);
    private final ISysExceptionLogRepository repository;

    public SysExceptionLogService(ISysExceptionLogRepository repository) {
        this.repository = repository;
    }

    @Override
    public Flux<SysExceptionLog> findAll() {
        return repository.findAllByOrderByCreateTimeDesc();
    }

    @Override
    public Flux<SysExceptionLog> findByUsername(String username) {
        return repository.findByUsernameOrderByCreateTimeDesc(username);
    }

    @Override
    public Flux<SysExceptionLog> findByCreateTimeBetween(LocalDateTime startTime, LocalDateTime endTime) {
        return repository.findByCreateTimeBetweenOrderByCreateTimeDesc(startTime, endTime);
    }

    @Override
    public Mono<SysExceptionLog> save(SysExceptionLog exceptionLog) {
        return repository.save(exceptionLog);
    }

    @Override
    public Mono<SysExceptionLog> findById(Long id) {
        return repository.findById(id);
    }

    @Override
    public Mono<PageResponse<SysExceptionLog>> findExceptionLogsByPage(PageRequest pageRequest) {
        logger.info("分页查询异常日志: page={}, size={}", pageRequest.getPage(), pageRequest.getSize());
        return repository.findExceptionLogsByPage(pageRequest);
    }

    @Override
    public Mono<Long> count() {
        return repository.count();
    }
}
