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

import cn.hutool.core.collection.CollUtil;
import com.sundot.airport.blocked.domain.BlockedMissCheckStatistics;
import com.sundot.airport.blocked.mapper.BlockedMissCheckStatisticsMapper;
import com.sundot.airport.blocked.service.IBlockedMissCheckStatisticsScreenService;
import com.sundot.airport.common.core.domain.BlockedMissCheckStatisticsQueryParamDto;
import com.sundot.airport.common.core.domain.entity.SysDictData;
import com.sundot.airport.common.dto.BaseCommonDto;
import com.sundot.airport.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 漏检统计大屏Service业务层处理
 *
 * @author wangxx
 * @date 2026-04-13
 */
@Service
public class BlockedMissCheckStatisticsScreenServiceImpl implements IBlockedMissCheckStatisticsScreenService {

    @Autowired
    private BlockedMissCheckStatisticsMapper blockedMissCheckStatisticsMapper;
    @Autowired
    private ISysDictDataService sysDictDataService;

    /**
     * 月考成绩统计
     *
     * @param dto 漏检统计查询参数
     * @return 月考成绩统计
     */
    @Override
    public List<BaseCommonDto> monthlyAssessment(BlockedMissCheckStatisticsQueryParamDto dto) {
        List<BaseCommonDto> result = new ArrayList<>();
        SysDictData dictData = new SysDictData();
        dictData.setDictType("blocked_monthly_exam_result");
        List<SysDictData> sysDictDataList = sysDictDataService.selectDictDataList(dictData);
        BlockedMissCheckStatistics query = new BlockedMissCheckStatistics();
        query.setReviewDate(dto.getSpecifiedDate());
        query.setStartDate(dto.getStartDate());
        query.setEndDate(dto.getEndDate());
        query.setTerminalId(dto.getTerminalId());
        query.setTerminalName(dto.getTerminalName());
        query.setAreaId(dto.getAreaId());
        query.setAreaName(dto.getAreaName());
        query.setChannelId(dto.getChannelId());
        query.setChannelName(dto.getChannelName());
        query.setSupervisorId(dto.getSupervisorId());
        query.setSupervisorName(dto.getSupervisorName());
        query.setTeamLeaderId(dto.getTeamLeaderId());
        query.setTeamLeaderName(dto.getTeamLeaderName());
        query.setMissCheckItem(dto.getMissCheckItem());
        query.setBrigadeId(dto.getBrigadeId());
        query.setBrigadeName(dto.getBrigadeName());
        List<BlockedMissCheckStatistics> blockedMissCheckStatisticsList = blockedMissCheckStatisticsMapper.selectBlockedMissCheckStatisticsList(query);
        Map<String, Long> monthlyAssessmentMap = blockedMissCheckStatisticsList.stream()
                .filter(blockedMissCheckStatistics -> blockedMissCheckStatistics.getMonthlyAssessment() != null)
                .collect(Collectors.groupingBy(BlockedMissCheckStatistics::getMonthlyAssessment, Collectors.counting()));
        sysDictDataList.forEach(sysDictData -> {
            BaseCommonDto item = new BaseCommonDto();
            item.setCode(sysDictData.getDictValue());
            item.setName(sysDictData.getDictLabel());
            item.setTotal(BigDecimal.valueOf(monthlyAssessmentMap.getOrDefault(sysDictData.getDictValue(), 0L)));
            result.add(item);
        });
        BigDecimal sum = result.stream().map(BaseCommonDto::getTotal).reduce(BigDecimal.ZERO, BigDecimal::add);
        result.forEach(item -> {
            item.setScale(sum.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : item.getTotal().divide(sum, 4, RoundingMode.HALF_UP));
            item.setScaleDesc(item.getScale().multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP) + "%");
        });
        return result;
    }

    /**
     * 本月自测有无漏检统计
     *
     * @param dto 漏检统计查询参数
     * @return 本月自测有无漏检统计
     */
    @Override
    public List<BaseCommonDto> selfTestHasMissCheck(BlockedMissCheckStatisticsQueryParamDto dto) {
        List<BaseCommonDto> result = new ArrayList<>();
        BlockedMissCheckStatistics query = new BlockedMissCheckStatistics();
        query.setReviewDate(dto.getSpecifiedDate());
        query.setStartDate(dto.getStartDate());
        query.setEndDate(dto.getEndDate());
        query.setTerminalId(dto.getTerminalId());
        query.setTerminalName(dto.getTerminalName());
        query.setAreaId(dto.getAreaId());
        query.setAreaName(dto.getAreaName());
        query.setChannelId(dto.getChannelId());
        query.setChannelName(dto.getChannelName());
        query.setSupervisorId(dto.getSupervisorId());
        query.setSupervisorName(dto.getSupervisorName());
        query.setTeamLeaderId(dto.getTeamLeaderId());
        query.setTeamLeaderName(dto.getTeamLeaderName());
        query.setMissCheckItem(dto.getMissCheckItem());
        query.setBrigadeId(dto.getBrigadeId());
        query.setBrigadeName(dto.getBrigadeName());
        List<BlockedMissCheckStatistics> blockedMissCheckStatisticsList = blockedMissCheckStatisticsMapper.selectBlockedMissCheckStatisticsList(query);
        Map<Integer, Long> selfTestHasMissCheckMap = blockedMissCheckStatisticsList.stream()
                .filter(blockedMissCheckStatistics -> blockedMissCheckStatistics.getSelfTestHasMissCheck() != null)
                .collect(Collectors.groupingBy(BlockedMissCheckStatistics::getSelfTestHasMissCheck, Collectors.counting()));
        selfTestHasMissCheckMap.forEach((key, value) -> {
            BaseCommonDto item = new BaseCommonDto();
            item.setName(String.valueOf(key));
            item.setTotal(BigDecimal.valueOf(value));
            result.add(item);
        });
        BigDecimal sum = result.stream().map(BaseCommonDto::getTotal).reduce(BigDecimal.ZERO, BigDecimal::add);
        result.forEach(item -> {
            item.setScale(sum.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : item.getTotal().divide(sum, 4, RoundingMode.HALF_UP));
            item.setScaleDesc(item.getScale().multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP) + "%");
        });
        return result;
    }

}
