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.exam.dto.*;
import com.sundot.airport.exam.service.IDashboardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <b>功能名：</b>DashboardController<br>
 * <b>说明：</b> 大屏统计Controller <br>
 * <b>著作权：</b> Copyright (C) 2025 SUNDOT CORPORATION<br>
 * <b>修改履历：
 *
 * @author 2025-01-19
 */
@RestController
@RequestMapping("/exam/daily/statistics/dashboard")
public class DashboardController extends BaseController {

    @Autowired
    private IDashboardService dashboardService;

    /**
     * 获取大屏概览数据
     */
    @GetMapping("/overview")
    public AjaxResult getOverview(DashboardQueryDTO query) {
        try {
            DashboardOverviewDTO overview = dashboardService.getOverview(query);
            return AjaxResult.success(overview);
        } catch (Exception e) {
            logger.error("获取大屏概览数据失败", e);
            return error("获取概览数据失败：" + e.getMessage());
        }
    }

    /**
     * 获取任务趋势数据
     */
    @GetMapping("/trend")
    public AjaxResult getTrend(DashboardQueryDTO query) {
        try {
            DashboardTrendDTO trend = dashboardService.getTrend(query);
            return AjaxResult.success(trend);
        } catch (Exception e) {
            logger.error("获取任务趋势数据失败", e);
            return error("获取趋势数据失败：" + e.getMessage());
        }
    }

    /**
     * 获取部门任务完成排行
     */
    @GetMapping("/dept-ranking")
    public AjaxResult getDeptRanking(DashboardQueryDTO query) {
        try {
            List<DashboardDeptRankingDTO> ranking = dashboardService.getDeptRanking(query);
            return AjaxResult.success(ranking);
        } catch (Exception e) {
            logger.error("获取部门排行数据失败", e);
            return error("获取部门排行失败：" + e.getMessage());
        }
    }

    /**
     * 获取角色维度统计数据
     */
    @GetMapping("/role-stats")
    public AjaxResult getRoleStats(DashboardQueryDTO query) {
        try {
            List<DashboardRoleStatsDTO> stats = dashboardService.getRoleStats(query);
            return AjaxResult.success(stats);
        } catch (Exception e) {
            logger.error("获取角色统计数据失败", e);
            return error("获取角色统计失败：" + e.getMessage());
        }
    }

    /**
     * 获取逾期任务分析数据
     */
    @GetMapping("/expired-analysis")
    public AjaxResult getExpiredAnalysis(DashboardQueryDTO query) {
        try {
            DashboardExpiredAnalysisDTO analysis = dashboardService.getExpiredAnalysis(query);
            return AjaxResult.success(analysis);
        } catch (Exception e) {
            logger.error("获取逾期任务分析数据失败", e);
            return error("获取逾期分析失败：" + e.getMessage());
        }
    }

    /**
     * 获取个人任务完成排行榜
     */
    @GetMapping("/user-ranking")
    public AjaxResult getUserRanking(DashboardQueryDTO query) {
        try {
            List<DashboardUserRankingDTO> ranking = dashboardService.getUserRanking(query);
            return AjaxResult.success(ranking);
        } catch (Exception e) {
            logger.error("获取个人排行数据失败", e);
            return error("获取个人排行失败：" + e.getMessage());
        }
    }

    /**
     * 获取当天所有用户的统计数据（分页）
     * 返回当天所有用户的任务统计数据，支持分页功能
     */
    @GetMapping("/all-users-ranking")
    public AjaxResult getAllUsersRanking(DashboardQueryDTO query) {
        try {
            startPage();
            List<DashboardUserRankingDTO> ranking = dashboardService.getAllUsersRanking(query);
            return AjaxResult.success(getDataTable(ranking));
        } catch (Exception e) {
            logger.error("获取全部用户统计数据失败", e);
            return error("获取用户统计数据失败：" + e.getMessage());
        }
    }

    /**
     * 获取最新完成的任务流水
     */
    @GetMapping("/recent-tasks")
    public AjaxResult getRecentTasks(DashboardQueryDTO query) {
        try {
            List<DashboardRecentTaskDTO> tasks = dashboardService.getRecentTasks(query);
            return AjaxResult.success(tasks);
        } catch (Exception e) {
            logger.error("获取实时任务流水失败", e);
            return error("获取任务流水失败：" + e.getMessage());
        }
    }

    /**
     * 用户模糊搜索
     */
    @GetMapping("/search-users")
    public AjaxResult searchUsers(DashboardQueryDTO query) {
        try {
            List<IDashboardService.UserSearchResultDTO> users = dashboardService.searchUsers(query);
            return AjaxResult.success(users);
        } catch (Exception e) {
            logger.error("用户搜索失败", e);
            return error("用户搜索失败：" + e.getMessage());
        }
    }

    /**
     * 获取筛选条件选项
     */
    @GetMapping("/filter-options")
    public AjaxResult getFilterOptions() {
        try {
            IDashboardService.FilterOptionsDTO options = dashboardService.getFilterOptions();
            return AjaxResult.success(options);
        } catch (Exception e) {
            logger.error("获取筛选选项失败", e);
            return error("获取筛选选项失败：" + e.getMessage());
        }
    }
}
