package com.sundot.airport.exam.controller;

import com.sundot.airport.common.core.controller.BaseController;
import com.sundot.airport.common.core.domain.AjaxResult;
import com.sundot.airport.common.core.domain.BaseLargeScreenQueryParamDto;
import com.sundot.airport.exam.dto.AccuracyAggregateStatisticsDTO;
import com.sundot.airport.exam.dto.AccuracyStatisticsDTO;
import com.sundot.airport.exam.dto.AccuracyStatisticsQueryDTO;
import com.sundot.airport.exam.service.IAccuracyStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <b>功能名：</b>AccuracyStatisticsController<br>
 * <b>说明：</b> 抽问抽答正确率统计Controller <br>
 * <b>著作权：</b> Copyright (C) 2026 SUNDOT CORPORATION<br>
 *
 * @author Simon Lin
 */
@RestController
@RequestMapping("/exam/daily/accuracy-statistics")
public class AccuracyStatisticsController extends BaseController {

    @Autowired
    private IAccuracyStatisticsService accuracyStatisticsService;

    /**
     * 获取当前用户或指定部门的正确率统计
     */
    @Cacheable(
            value = "statistics_data",
            keyGenerator = "statisticsKeyGenerator",
            unless = "!T(com.sundot.airport.common.cache.StatisticsCacheConditionUtil).isSuccess(#result) || T(com.sundot.airport.common.cache.StatisticsCacheConditionUtil).isEmptyData(#result)"
    )
    @GetMapping
    public AjaxResult getAccuracyStatistics(AccuracyStatisticsQueryDTO query) {
        try {
            AccuracyStatisticsDTO statistics = accuracyStatisticsService.getAccuracyStatistics(query);
            return AjaxResult.success(statistics);
        } catch (Exception e) {
            logger.error("查询正确率统计失败", e);
            return error("查询正确率统计失败：" + e.getMessage());
        }
    }

    /**
     * 获取指定用户的正确率统计
     */
    @Cacheable(
            value = "statistics_data",
            keyGenerator = "statisticsKeyGenerator",
            unless = "!T(com.sundot.airport.common.cache.StatisticsCacheConditionUtil).isSuccess(#result) || T(com.sundot.airport.common.cache.StatisticsCacheConditionUtil).isEmptyData(#result)"
    )
    @GetMapping("/user/{userId}")
    public AjaxResult getUserAccuracyStatistics(@PathVariable Long userId, AccuracyStatisticsQueryDTO query) {
        try {
            AccuracyStatisticsDTO statistics = accuracyStatisticsService.getUserAccuracyStatistics(userId, query);
            return AjaxResult.success(statistics);
        } catch (Exception e) {
            logger.error("查询用户正确率统计失败 - userId: {}", userId, e);
            return error("查询用户正确率统计失败：" + e.getMessage());
        }
    }

    /**
     * 获取聚合统计数据（管理员视角）
     */
    @Cacheable(
            value = "statistics_data",
            keyGenerator = "statisticsKeyGenerator",
            unless = "!T(com.sundot.airport.common.cache.StatisticsCacheConditionUtil).isSuccess(#result) || T(com.sundot.airport.common.cache.StatisticsCacheConditionUtil).isEmptyData(#result)"
    )
    @PreAuthorize("@ss.hasPermi('exam:daily:statistics:admin') or @ss.hasRole('test') or @ss.hasRole('zhijianke')")
    @GetMapping("/aggregate")
    public AjaxResult getAggregateStatistics(AccuracyStatisticsQueryDTO query) {
        try {
            AccuracyAggregateStatisticsDTO statistics = accuracyStatisticsService.getAggregateStatistics(query);
            return AjaxResult.success(statistics);
        } catch (Exception e) {
            logger.error("查询聚合统计数据失败", e);
            return error("查询聚合统计数据失败：" + e.getMessage());
        }
    }

    /**
     * 获取首页正确率排名
     */
    @Cacheable(
            value = "statistics_data",
            keyGenerator = "statisticsKeyGenerator",
            unless = "!T(com.sundot.airport.common.cache.StatisticsCacheConditionUtil).isSuccess(#result) || T(com.sundot.airport.common.cache.StatisticsCacheConditionUtil).isEmptyData(#result)"
    )
    @GetMapping("/ranking")
    public AjaxResult getAccuracyRanking(BaseLargeScreenQueryParamDto dto) {
        try {
            return AjaxResult.success(accuracyStatisticsService.getAccuracyRanking(dto));
        } catch (Exception e) {
            logger.error("查询正确率排名失败", e);
            return error("查询正确率排名失败：" + e.getMessage());
        }
    }
}
