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

import cn.novalon.gym.manage.common.dto.PageRequest;
import cn.novalon.gym.manage.common.dto.PageResponse;
import cn.novalon.gym.manage.sys.core.domain.OperationLog;
import cn.novalon.gym.manage.sys.core.query.OperationLogQuery;
import cn.novalon.gym.manage.sys.core.repository.IOperationLogRepository;
import cn.novalon.gym.manage.sys.core.service.IOperationLogService;
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 OperationLogService implements IOperationLogService {

    private final IOperationLogRepository logRepository;

    public OperationLogService(IOperationLogRepository logRepository) {
        this.logRepository = logRepository;
    }

    @Override
    public Mono<OperationLog> save(OperationLog log) {
        return logRepository.save(log);
    }

    @Override
    public Flux<OperationLog> findAll() {
        return logRepository.findAll();
    }

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

    @Override
    public Flux<OperationLog> findByUsername(String username) {
        return logRepository.findByUsername(username);
    }

    @Override
    public Mono<PageResponse<OperationLog>> findByQueryWithPagination(OperationLogQuery query, PageRequest pageRequest) {
        return logRepository.findByQueryWithPagination(query, pageRequest);
    }

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

    @Override
    public Mono<Long> countToday() {
        LocalDateTime startOfDay = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
        return logRepository.countByCreatedAtAfter(startOfDay);
    }
}
