package cn.novalon.gym.manage.sys.handler.stats;

import cn.novalon.gym.manage.sys.core.service.ISysUserService;
import cn.novalon.gym.manage.sys.core.service.ISysRoleService;
import cn.novalon.gym.manage.sys.core.service.IOperationLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

/**
 * 统计数据处理器
 * 
 * @author 张翔
 * @date 2026-03-14
 */
@Component
@Tag(name = "统计信息", description = "系统统计相关操作")
public class StatsHandler {

    private final ISysUserService userService;
    private final ISysRoleService roleService;
    private final IOperationLogService operationLogService;

    public StatsHandler(ISysUserService userService, ISysRoleService roleService, IOperationLogService operationLogService) {
        this.userService = userService;
        this.roleService = roleService;
        this.operationLogService = operationLogService;
    }

    @Operation(summary = "获取系统概览", description = "获取系统统计概览信息")
    public Mono<ServerResponse> getOverview(ServerRequest request) {
        return Mono.zip(
                userService.count(),
                roleService.count(),
                operationLogService.count(),
                operationLogService.countToday()
        ).flatMap(tuple -> {
            OverviewStats stats = new OverviewStats();
            stats.setUserCount(tuple.getT1());
            stats.setRoleCount(tuple.getT2());
            stats.setOperationLogCount(tuple.getT3());
            stats.setTodayOperationCount(tuple.getT4());
            return ServerResponse.ok().bodyValue(stats);
        });
    }

    public static class OverviewStats {
        private Long userCount;
        private Long roleCount;
        private Long operationLogCount;
        private Long todayOperationCount;

        public Long getUserCount() {
            return userCount;
        }

        public void setUserCount(Long userCount) {
            this.userCount = userCount;
        }

        public Long getRoleCount() {
            return roleCount;
        }

        public void setRoleCount(Long roleCount) {
            this.roleCount = roleCount;
        }

        public Long getOperationLogCount() {
            return operationLogCount;
        }

        public void setOperationLogCount(Long operationLogCount) {
            this.operationLogCount = operationLogCount;
        }

        public Long getTodayOperationCount() {
            return todayOperationCount;
        }

        public void setTodayOperationCount(Long todayOperationCount) {
            this.todayOperationCount = todayOperationCount;
        }
    }
}
