package com.sundot.airport.blocked.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.sundot.airport.blocked.dto.BlockedAreaDistributionDTO;
import com.sundot.airport.blocked.dto.BlockedBrigadeCertificateDistributionDTO;
import com.sundot.airport.blocked.dto.BlockedBrigadeRateStatsDTO;
import com.sundot.airport.blocked.dto.BlockedBrigadeStatsDTO;
import com.sundot.airport.blocked.dto.BlockedCertificateDistributionDTO;
import com.sundot.airport.blocked.dto.BlockedDailyBrigadeLuggageStatsDTO;
import com.sundot.airport.blocked.dto.BlockedDailyBrigadeRateStatsDTO;
import com.sundot.airport.blocked.dto.BlockedDailyBrigadeStatsDTO;
import com.sundot.airport.blocked.dto.BlockedDailyLuggageStatsDTO;
import com.sundot.airport.blocked.dto.BlockedDailyRateStatsDTO;
import com.sundot.airport.blocked.dto.BlockedDailyStatsDTO;
import com.sundot.airport.blocked.dto.BlockedDashboardQueryDTO;
import com.sundot.airport.blocked.dto.BlockedDashboardResultDTO;
import com.sundot.airport.blocked.dto.BlockedDifficultyDistributionDTO;
import com.sundot.airport.blocked.dto.BlockedDiscriminationDistributionDTO;
import com.sundot.airport.blocked.dto.BlockedItemDistributionDTO;
import com.sundot.airport.blocked.dto.BlockedItemLocationDistributionDTO;
import com.sundot.airport.blocked.dto.BlockedMissCheckReasonDistributionDTO;
import com.sundot.airport.blocked.dto.BlockedOperatingYearsDistributionDTO;
import com.sundot.airport.blocked.dto.BlockedRankingDTO;
import com.sundot.airport.blocked.dto.BlockedRateResultDTO;
import com.sundot.airport.blocked.dto.BlockedTimePeriodStatsDTO;
import com.sundot.airport.blocked.mapper.BlockedDashboardMapper;
import com.sundot.airport.blocked.service.IBlockedDashboardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

/**
 * 查堵大屏Service业务层处理
 * 
 * @author wangxx
 * @date 2026-04-15
 */
@Service
public class BlockedDashboardServiceImpl implements IBlockedDashboardService {

    @Autowired
    private BlockedDashboardMapper blockedDashboardMapper;

    /**
     * 查询查堵总数
     * 
     * @param queryDTO 查询参数
     * @return 查堵统计结果
     */
    @Override
    public BlockedDashboardResultDTO getBlockedTotalCount(BlockedDashboardQueryDTO queryDTO) {
        BlockedDashboardResultDTO result = new BlockedDashboardResultDTO();
        
        // 判断是否选择了大队
        if (queryDTO.getBrigadeId() == null) {
            // 从各大队每日过检行李查堵数据收集表统计
            Integer totalCount = blockedDashboardMapper.selectTotalBlockedCountWithoutBrigade(queryDTO);
            result.setTotalBlockedCount(totalCount != null ? totalCount : 0);
        } else {
            // 从漏检统计表统计
            Integer totalCount = blockedDashboardMapper.selectTotalBlockedCountWithBrigade(queryDTO);
            result.setTotalBlockedCount(totalCount != null ? totalCount : 0);
        }
        
        return result;
    }

    /**
     * 查询各大队查堵总数
     * 
     * @param queryDTO 查询参数
     * @return 各大队查堵统计列表
     */
    @Override
    public List<BlockedBrigadeStatsDTO> getBlockedCountByBrigade(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedBrigadeStatsDTO> resultList = blockedDashboardMapper.selectBlockedCountByBrigade(queryDTO);
        return resultList != null ? resultList : Collections.emptyList();
    }

    /**
     * 查询总查堵万分率(所有大队平均值)
     * 
     * @param queryDTO 查询参数
     * @return 查堵万分率统计结果
     */
    @Override
    public BlockedRateResultDTO getTotalBlockedRate(BlockedDashboardQueryDTO queryDTO) {
        BlockedRateResultDTO result = new BlockedRateResultDTO();
        BigDecimal totalRate = blockedDashboardMapper.selectTotalBlockedRate(queryDTO);
        result.setTotalBlockedRate(totalRate != null ? totalRate : BigDecimal.ZERO);
        return result;
    }

    /**
     * 查询各大队平均查堵万分率(用于条形图)
     * 
     * @param queryDTO 查询参数
     * @return 各大队查堵万分率统计列表
     */
    @Override
    public List<BlockedBrigadeRateStatsDTO> getBlockedRateByBrigade(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedBrigadeRateStatsDTO> resultList = blockedDashboardMapper.selectBlockedRateByBrigade(queryDTO);
        return resultList != null ? resultList : Collections.emptyList();
    }

    /**
     * 查询每日查堵合计件数(用于趋势图)
     * 
     * @param queryDTO 查询参数
     * @return 每日查堵统计列表
     */
    @Override
    public List<BlockedDailyStatsDTO> getBlockedCountByDate(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedDailyStatsDTO> resultList = blockedDashboardMapper.selectBlockedCountByDate(queryDTO);
        return resultList != null ? resultList : Collections.emptyList();
    }

    /**
     * 查询每日各大队查堵合计件数(用于大队对比表)
     * 
     * @param queryDTO 查询参数
     * @return 每日各大队查堵统计列表
     */
    @Override
    public List<BlockedDailyBrigadeStatsDTO> getBlockedCountByDateAndBrigade(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedDailyBrigadeStatsDTO> resultList = blockedDashboardMapper.selectBlockedCountByDateAndBrigade(queryDTO);
        return resultList != null ? resultList : Collections.emptyList();
    }

    /**
     * 查询每日平均查堵万分率(用于趋势图)
     * 
     * @param queryDTO 查询参数
     * @return 每日查堵万分率统计列表
     */
    @Override
    public List<BlockedDailyRateStatsDTO> getBlockedRateByDate(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedDailyRateStatsDTO> resultList = blockedDashboardMapper.selectBlockedRateByDate(queryDTO);
        return resultList != null ? resultList : Collections.emptyList();
    }

    /**
     * 查询每日各大队平均查堵万分率(用于大队对比表)
     * 
     * @param queryDTO 查询参数
     * @return 每日各大队查堵万分率统计列表
     */
    @Override
    public List<BlockedDailyBrigadeRateStatsDTO> getBlockedRateByDateAndBrigade(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedDailyBrigadeRateStatsDTO> resultList = blockedDashboardMapper.selectBlockedRateByDateAndBrigade(queryDTO);
        return resultList != null ? resultList : Collections.emptyList();
    }

    /**
     * 查询每日过检行李合计(用于趋势图)
     * 
     * @param queryDTO 查询参数
     * @return 每日过检行李统计列表
     */
    @Override
    public List<BlockedDailyLuggageStatsDTO> getTotalLuggageCountByDate(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedDailyLuggageStatsDTO> resultList = blockedDashboardMapper.selectTotalLuggageCountByDate(queryDTO);
        return resultList != null ? resultList : Collections.emptyList();
    }

    /**
     * 查询每日各大队过检行李合计(用于大队对比表)
     * 
     * @param queryDTO 查询参数
     * @return 每日各大队过检行李统计列表
     */
    @Override
    public List<BlockedDailyBrigadeLuggageStatsDTO> getTotalLuggageCountByDateAndBrigade(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedDailyBrigadeLuggageStatsDTO> resultList = blockedDashboardMapper.selectTotalLuggageCountByDateAndBrigade(queryDTO);
        return resultList != null ? resultList : Collections.emptyList();
    }

    /**
     * 查询查堵物品分布(用于扇形图)
     * 
     * @param queryDTO 查询参数
     * @return 查堵物品分布统计列表
     */
    @Override
    public List<BlockedItemDistributionDTO> getItemDistribution(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedItemDistributionDTO> resultList = blockedDashboardMapper.selectItemDistribution(queryDTO);
        return resultList != null ? resultList : Collections.emptyList();
    }

    /**
     * 查询区域查堵数据分布(用于双轴图表)
     * 
     * @param queryDTO 查询参数
     * @return 区域查堵数据分布统计
     */
    @Override
    public BlockedAreaDistributionDTO getAreaDistribution(BlockedDashboardQueryDTO queryDTO) {
        return blockedDashboardMapper.selectAreaDistribution(queryDTO);
    }

    /**
     * 查询查堵类型分布(用于分组柱状图)
     * 
     * @param queryDTO 查询参数
     * @return 查堵类型分布统计列表
     */
    @Override
    public List<BlockedDiscriminationDistributionDTO> getDiscriminationDistribution(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedDiscriminationDistributionDTO> resultList = blockedDashboardMapper.selectDiscriminationDistribution(queryDTO);
        return resultList != null ? resultList : Collections.emptyList();
    }

    /**
     * 查询查堵时间段过检行李数及万分率(用于双轴图表)
     * 
     * @param queryDTO 查询参数
     * @return 时间段统计列表
     */
    @Override
    public List<BlockedTimePeriodStatsDTO> getTimePeriodStats(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedTimePeriodStatsDTO> resultList = blockedDashboardMapper.selectTimePeriodStats(queryDTO);
        return resultList != null ? resultList : Collections.emptyList();
    }

    /**
     * 查询每日过检行李数及万分率(用于双轴图表)
     * 
     * @param queryDTO 查询参数
     * @return 每日统计列表
     */
    @Override
    public List<BlockedTimePeriodStatsDTO> getDailyLuggageAndRate(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedTimePeriodStatsDTO> resultList = blockedDashboardMapper.selectDailyLuggageAndRate(queryDTO);
        return resultList != null ? resultList : Collections.emptyList();
    }

    /**
     * 查询查堵-AI复查图像总数(用于双系列图表)
     * 
     * @param queryDTO 查询参数
     * @return AI图像统计列表
     */
    @Override
    public List<BlockedDailyStatsDTO> getAiImageStats(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedDailyStatsDTO> resultList = blockedDashboardMapper.selectAiImageStats(queryDTO);
        return resultList != null ? resultList : Collections.emptyList();
    }

    /**
     * 查询查堵-AI漏判图像总数(用于单系列图表)
     * 
     * @param queryDTO 查询参数
     * @return AI漏判图像统计列表
     */
    @Override
    public List<BlockedDailyStatsDTO> getAiMissImageStats(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedDailyStatsDTO> resultList = blockedDashboardMapper.selectAiMissImageStats(queryDTO);
        return resultList != null ? resultList : Collections.emptyList();
    }

    /**
     * 查询查堵-AI误判图像总数(用于单系列图表)
     * 
     * @param queryDTO 查询参数
     * @return AI误判图像统计列表
     */
    @Override
    public List<BlockedDailyStatsDTO> getAiErrorImageStats(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedDailyStatsDTO> resultList = blockedDashboardMapper.selectAiErrorImageStats(queryDTO);
        return resultList != null ? resultList : Collections.emptyList();
    }

    /**
     * 查询查堵-主管排行榜
     * 
     * @param queryDTO 查询参数
     * @return 主管排行榜列表
     */
    @Override
    public List<BlockedRankingDTO> getSupervisorRanking(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedRankingDTO> resultList = blockedDashboardMapper.selectSupervisorRanking(queryDTO);
        if (CollectionUtil.isNotEmpty(resultList)) {
            // 计算排名
            for (int i = 0; i < resultList.size(); i++) {
                resultList.get(i).setRank(i + 1);
            }
        }
        return resultList != null ? resultList : Collections.emptyList();
    }

    /**
     * 查询查堵-班组排行榜
     * 
     * @param queryDTO 查询参数
     * @return 班组排行榜列表
     */
    @Override
    public List<BlockedRankingDTO> getTeamLeaderRanking(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedRankingDTO> resultList = blockedDashboardMapper.selectTeamLeaderRanking(queryDTO);
        if (CollectionUtil.isNotEmpty(resultList)) {
            // 计算排名
            for (int i = 0; i < resultList.size(); i++) {
                resultList.get(i).setRank(i + 1);
            }
        }
        return resultList != null ? resultList : Collections.emptyList();
    }

    /**
     * 查询查堵-人员排行榜
     * 
     * @param queryDTO 查询参数
     * @return 人员排行榜列表
     */
    @Override
    public List<BlockedRankingDTO> getReviewedUserRanking(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedRankingDTO> resultList = blockedDashboardMapper.selectReviewedUserRanking(queryDTO);
        if (CollectionUtil.isNotEmpty(resultList)) {
            // 计算排名
            for (int i = 0; i < resultList.size(); i++) {
                resultList.get(i).setRank(i + 1);
            }
        }
        return resultList != null ? resultList : Collections.emptyList();
    }

    /**
     * 查询查堵-物品位置分布(用于扇形图)
     * 
     * @param queryDTO 查询参数
     * @return 物品位置分布统计列表
     */
    @Override
    public List<BlockedItemLocationDistributionDTO> getItemLocationDistribution(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedItemLocationDistributionDTO> resultList = blockedDashboardMapper.selectItemLocationDistribution(queryDTO);
        return resultList != null ? resultList : Collections.emptyList();
    }

    /**
     * 查询查堵-原因分类分布(用于扇形图)
     * 
     * @param queryDTO 查询参数
     * @return 原因分类分布统计列表
     */
    @Override
    public List<BlockedMissCheckReasonDistributionDTO> getMissCheckReasonDistribution(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedMissCheckReasonDistributionDTO> resultList = blockedDashboardMapper.selectMissCheckReasonDistribution(queryDTO);
        return resultList != null ? resultList : Collections.emptyList();
    }

    /**
     * 查询查堵-图像难易程度分布(用于扇形图)
     * 
     * @param queryDTO 查询参数
     * @return 难易程度分布统计列表
     */
    @Override
    public List<BlockedDifficultyDistributionDTO> getDifficultyDistribution(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedDifficultyDistributionDTO> resultList = blockedDashboardMapper.selectDifficultyDistribution(queryDTO);
        return resultList != null ? resultList : Collections.emptyList();
    }

    /**
     * 查询查堵-开机人员持证比例(用于扇形图)
     * 
     * @param queryDTO 查询参数
     * @return 证书级别分布统计列表
     */
    @Override
    public List<BlockedCertificateDistributionDTO> getCertificateDistribution(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedCertificateDistributionDTO> resultList = blockedDashboardMapper.selectCertificateDistribution(queryDTO);
        return resultList != null ? resultList : Collections.emptyList();
    }

    /**
     * 查询大队开机人员证书分布(用于柱状图)
     * 
     * @param queryDTO 查询参数
     * @return 大队证书分布统计列表
     */
    @Override
    public List<BlockedBrigadeCertificateDistributionDTO> getBrigadeCertificateDistribution(BlockedDashboardQueryDTO queryDTO) {
        List<BlockedBrigadeCertificateDistributionDTO> resultList = blockedDashboardMapper.selectBrigadeCertificateDistribution(queryDTO);
        return resultList != null ? resultList : Collections.emptyList();
    }
}
