package com.lutao.cloud.oil.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lutao.cloud.common.core.util.MessageInfo;
import com.lutao.cloud.common.core.util.StringUtil;
import com.lutao.cloud.oil.mapper.ProblemRectificationDetailMapper;
import com.lutao.cloud.oil.mapper.SecurityRiskAssessmentMapper;
import com.lutao.cloud.oil.mapper.SecurityRiskAssessmentSummaryMapper;
import com.lutao.cloud.oil.mapper.VideoEquipmentInfoMapper;
import com.lutao.cloud.oil.pojo.entity.VideoInfo;
import com.lutao.cloud.oil.pojo.param.*;
import com.lutao.cloud.oil.pojo.vo.*;
import com.lutao.cloud.oil.service.*;
import com.lutao.entity.PlatformInfo;
import com.lutao.entity.ProblemRectificationDetail;
import com.lutao.entity.SecurityRiskAssessmentSummary;
import com.lutao.entity.VideoEquipmentInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BusinessSecurityRiskAssessmentServiceImpl implements IBusinessSecurityRiskAssessmentService {

    @Resource
    private ProblemRectificationDetailService problemRectificationDetailService;

    @Resource
    private SecurityRiskAssessmentService securityRiskAssessmentService;

    @Resource
    private PlatformInfoService platformInfoService;

    @Resource
    private VideoInfoService videoInfoService;

    @Resource
    private SecurityRiskAssessmentSummaryService securityRiskAssessmentSummaryService;

    @Resource
    private SecurityRiskAssessmentSummaryMapper securityRiskAssessmentSummaryMapper;

    @Resource
    private SecurityRiskAssessmentMapper securityRiskAssessmentMapper;

    @Resource
    private ProblemRectificationDetailMapper problemRectificationDetailMapper;

    @Resource
    private VideoEquipmentInfoMapper videoEquipmentInfoMapper;


    @Override
    public MessageInfo<IPage<SecurityRiskAssessmentSummaryPageVO>> queryRiskAssessmentStatus(SecurityRiskAssessmentParam param, Page page) {
        if (param.getYear() == null) {
            param.setYear(LocalDate.now().getYear() + "");
        }
        IPage<SecurityRiskAssessmentSummaryPageVO> securityRiskAssessmentSummaryVOIPage = securityRiskAssessmentSummaryMapper.querySecurityRiskAssessmentSummaryPage(page, param);
        return MessageInfo.ok(securityRiskAssessmentSummaryVOIPage);
    }

    @Override
    public MessageInfo<IPage<SeaPlatformFacilityVO>> querySeaPlatformFacility(SelectSeaPlatformFacilityParam param, Page page) {

        if ( param != null && param.getExcludeLowRiskFlag()!=null && param.getExcludeLowRiskFlag() == 1){

            String keyWithLongestList = null;
            int maxLength = 0;

            List<SecurityRiskAssessmentSummary> securityRiskAssessmentSummaries = securityRiskAssessmentSummaryService.list(Wrappers.<SecurityRiskAssessmentSummary>lambdaQuery()
                    .eq(SecurityRiskAssessmentSummary::getEvaluationType, 2)
                    .eq(SecurityRiskAssessmentSummary::getEvaluationYear, LocalDate.now().getYear()));
            Map<String, List<SecurityRiskAssessmentSummary>> collect = securityRiskAssessmentSummaries
                    .stream()
                    .filter(item -> item.getRiskRank() != null)
                    .collect(Collectors.groupingBy(SecurityRiskAssessmentSummary::getRiskRank));
            for (Map.Entry<String, List<SecurityRiskAssessmentSummary>> stringListEntry : collect.entrySet()) {
                if (stringListEntry.getValue().size() > maxLength) {
                    keyWithLongestList = stringListEntry.getKey();
                    maxLength = stringListEntry.getValue().size();
                }
            }
            param.setSortRiskLevel(keyWithLongestList);
        }

        IPage<SeaPlatformFacilityVO> seaPlatformFacilityVOIPage = securityRiskAssessmentMapper.selectSeaPlatformFacility(page, param);
        return MessageInfo.ok(seaPlatformFacilityVOIPage);
    }

    @Override
    public MessageInfo<HiddenDangerRectificationVO> queryHiddenDangerRectifyStatus(BaseParam param) {
        SecurityRiskAssessmentParam newParam = new SecurityRiskAssessmentParam();
        BeanUtil.copyProperties(param, newParam);
        if (newParam.getYear() == null){
            newParam.setYear(LocalDate.now().getYear() + "");
        }

       // List<SecurityRiskAssessmentBarListVO> securityRiskAssessmentBarListVO = securityRiskAssessmentMapper.getSecurityRiskAssessmentBarListVO(param);

        //Map<String, Long> collect = securityRiskAssessmentBarListVO.stream().collect(Collectors.groupingBy(SecurityRiskAssessmentBarListVO::getRiskLevel, Collectors.counting()));

        //log.info("collect:{}", collect);

        //暂未顶级数
        Long unclassifiedRisk = 0L;
        //低风险平台数
        Long lowRisk = 0L;
        //中风险平台数
        Long mediumRisk = 0L;
        //中高风险平台数
        Long mediumHighRiskNumber = 0L;
        //高风险平台数
        Long highRiskNumber = 0L;
        // 风险平台列表
        List<SecurityRiskAssessmentSummaryVO> securityRiskAssessmentSummaries = securityRiskAssessmentSummaryMapper.querySecurityRiskAssessmentSummary(newParam);
        for (SecurityRiskAssessmentSummaryVO securityRiskAssessmentSummary : securityRiskAssessmentSummaries) {
            if (securityRiskAssessmentSummary.getRiskRank() == null) {
                unclassifiedRisk++;
            }
            if (StringUtil.isNotBlank(securityRiskAssessmentSummary.getRiskRank())) {
                switch (securityRiskAssessmentSummary.getRiskRank()) {
                    case "004":
                        lowRisk++;
                        break;
                    case "003":
                        mediumRisk++;
                        break;
                    case "002":
                        mediumHighRiskNumber++;
                        break;
                    case "001":
                        highRiskNumber++;
                        break;
                }
            }
        }
        //问题隐患整改率
        HiddenDangerRectificationVO hiddenDangerRectificationRate = securityRiskAssessmentMapper.getHiddenDangerRectificationRate(param);
        hiddenDangerRectificationRate.setHighRiskNumber(highRiskNumber);
        hiddenDangerRectificationRate.setMediumHighRiskNumber(mediumHighRiskNumber);
        hiddenDangerRectificationRate.setMediumRisk(mediumRisk);
        hiddenDangerRectificationRate.setLowRisk(lowRisk);
        hiddenDangerRectificationRate.setUnclassifiedRisk(unclassifiedRisk);
        hiddenDangerRectificationRate.setTotalNumber(highRiskNumber + mediumHighRiskNumber + mediumRisk + lowRisk + unclassifiedRisk);
        HiddenDangerNumberVO hiddenDangerNumber = securityRiskAssessmentMapper.getHiddenDangerNumber(param);
        hiddenDangerRectificationRate.setTotalRectifyRate(

                (double) (hiddenDangerNumber.getLowRiskRectifyNumber() + hiddenDangerNumber.getMediumRiskRectifyNumber() + hiddenDangerNumber.getMediumHighRiskRectifyNumber() + hiddenDangerNumber.getHighRiskRectifyNumber()) /
                        (hiddenDangerNumber.getLowRiskNumber() + hiddenDangerNumber.getMediumRiskNumber() + hiddenDangerNumber.getMediumHighRiskNumber() + hiddenDangerNumber.getHighRiskNumber())

        );


        return MessageInfo.ok(hiddenDangerRectificationRate);
    }

    @Override
    public MessageInfo queryProblemRectifyStatus(Page page, ProblemRectifyStatusParam param) {

        if (param.getYear() == null) {
            param.setYear(LocalDate.now().getYear() + "");
        }

        if (param.getType() == 1) {
            //深评
            IPage<ProblemRectifyStatusVO> problemRectifyStatusVOIPage = problemRectificationDetailMapper.selectProblemRectifyStatus(page, param.getPlatformId(), param.getAppId(), "1", param.getYear(), param.getOilGasName(), param.getOperatingCompany());
            return MessageInfo.ok(problemRectifyStatusVOIPage);
        } else if (param.getType() == 2) {
            //分布检查
            IPage<ProblemRectifyStatusCheckVO> problemRectifyStatusCheckVOIPage = problemRectificationDetailMapper.selectProblemRectifyStatusCheck(page, param.getPlatformId(), param.getAppId(), param.getYear(), param.getOilGasName(), param.getOperatingCompany());
            return MessageInfo.ok(problemRectifyStatusCheckVOIPage);
        } else if (param.getType() == 3) {
            //部级督导
            IPage<ProblemRectifySupervisionVO> problemRectifySupervisionVOIPage = securityRiskAssessmentMapper.selectProblemRectifySupervision(page, param.getPlatformId(), param.getAppId(), param.getYear(), param.getOilGasName(), param.getOperatingCompany());
            return MessageInfo.ok(problemRectifySupervisionVOIPage);
        } else if (param.getType() == 4) {
            //自评
            IPage<ProblemRectifyStatusVO> problemRectifyStatusVOIPage = problemRectificationDetailMapper.selectProblemRectifyStatus(page, param.getPlatformId(), param.getAppId(), "2", param.getYear(), param.getOilGasName(), param.getOperatingCompany());
            return MessageInfo.ok(problemRectifyStatusVOIPage);
        }
        return MessageInfo.ok();
    }

    @Override
    public MessageInfo<ProblemRectifyProportionVO> queryProblemRectifyProportion(BaseParam baseParam) {

        //问题隐患数量
        int problemNumber = problemRectificationDetailService.count(Wrappers.lambdaQuery(ProblemRectificationDetail.class)
                .eq(StringUtil.isNotBlank(baseParam.getPlatformId()), ProblemRectificationDetail::getPlatformId, baseParam.getPlatformId())
                .eq(StringUtil.isNotBlank(baseParam.getAppId()), ProblemRectificationDetail::getAppId, baseParam.getAppId()));

        //整改完成数量
        int rectifyNumber = problemRectificationDetailService.count(Wrappers.lambdaQuery(ProblemRectificationDetail.class)
                .eq(ProblemRectificationDetail::getHiddenProblemsState, "002")
                .eq(StringUtil.isNotBlank(baseParam.getPlatformId()), ProblemRectificationDetail::getPlatformId, baseParam.getPlatformId())
                .eq(StringUtil.isNotBlank(baseParam.getAppId()), ProblemRectificationDetail::getAppId, baseParam.getAppId()));

        ProblemRectifyProportionVO problemRectifyProportionVO = new ProblemRectifyProportionVO()
                .setProblemNumber(problemNumber)
                .setRectifyNumber(rectifyNumber)
                .setRectifyProportion(Math.round(((float) rectifyNumber / problemNumber) * 100.0) / 100.0 + "%");

        return MessageInfo.ok(problemRectifyProportionVO);
    }

    @Override
    public MessageInfo<SecurityRiskAssessmentTimeDistributionVO> querySecurityRiskAssessmentTimeDistribution(BaseParam baseParam) {
        SecurityRiskAssessmentTimeDistributionVO securityRiskAssessmentTimeDistributionVO = securityRiskAssessmentMapper.selectSecurityRiskAssessmentTimeDistribution(baseParam.getPlatformId(), baseParam.getAppId());
        return MessageInfo.ok(securityRiskAssessmentTimeDistributionVO);
    }

    @Override
    public MessageInfo<AssessmentReportVO> getAssessmentStrategy(BaseParam param) {

        SecurityRiskAssessmentParam securityRiskAssessmentParam = new SecurityRiskAssessmentParam();
        BeanUtil.copyProperties(param, securityRiskAssessmentParam);

        if (securityRiskAssessmentParam.getYear() == null) {
            securityRiskAssessmentParam.setYear(LocalDate.now().getYear() + "");
        }
        AssessmentReportVO assessmentReportVO = new AssessmentReportVO();
        //形成评估报告数量
        List<SecurityRiskAssessmentSummaryVO> securityRiskAssessmentSummaries = securityRiskAssessmentSummaryMapper.querySecurityRiskAssessmentSummary(securityRiskAssessmentParam);
        long assessmentReportNumber = securityRiskAssessmentSummaries.stream().filter(item -> item.getEvaluationCode() != null).count();

        int hiddenDangerNumber = securityRiskAssessmentMapper.getHiddenDangerNumberNowYear(param);


        //获取平台总数
//        int platformTotal = platformInfoService.count(Wrappers.lambdaQuery(PlatformInfo.class)
//                .eq(StringUtil.isNotBlank(param.getAppId()), PlatformInfo::getAppId, param.getAppId())
//                .eq(StringUtil.isNotBlank(param.getPlatformId()), PlatformInfo::getPlatformCode, param.getPlatformId()));
        int platformTotal = securityRiskAssessmentSummaries.size();

        //评估进度  形成评估报告/平台总数

        String assessmentProgress = Math.round(((float) assessmentReportNumber / platformTotal) * 100.0) + "%";

        assessmentReportVO.setAssessmentProgress(assessmentProgress)
                .setAssessmentReportNumber((int) assessmentReportNumber)
                .setOneStrategy((int) assessmentReportNumber)
                .setHiddenDangerNumber(hiddenDangerNumber)
                .setPlatformCount(platformTotal)
                .setAssessmentPlatformCount((int) assessmentReportNumber);
        return MessageInfo.ok(assessmentReportVO);
    }

    @Override
    public MessageInfo<SecurityRiskAssessmentPieVO> queryRiskAssessmentStatusPie(SecurityRiskAssessmentParam param) {
        int year = LocalDate.now().getYear();

        param.setYear(year + "");

        List<RiskLevelGroupVO> riskLevelGroup = null;

        //风险组
        riskLevelGroup = securityRiskAssessmentMapper.getRiskLevelGroup(param);
        if (riskLevelGroup.isEmpty()) {
            //年份减一
            param.setYear((year - 1) + "");
            riskLevelGroup = securityRiskAssessmentMapper.getRiskLevelGroup(param);
        }

        SecurityRiskAssessmentPieVO securityRiskAssessmentPieVO = new SecurityRiskAssessmentPieVO();

        securityRiskAssessmentPieVO.setHighRiskNumber(0L);
        securityRiskAssessmentPieVO.setMediumHighRiskNumber(0L);
        securityRiskAssessmentPieVO.setMediumRiskNumber(0L);
        securityRiskAssessmentPieVO.setLowRiskNumber(0L);

        for (RiskLevelGroupVO riskLevelGroupVO : riskLevelGroup) {
            switch (riskLevelGroupVO.getRiskLevel()) {
                case "高风险":
                    securityRiskAssessmentPieVO.setHighRiskNumber(riskLevelGroupVO.getRiskCount() == null ? 0 : riskLevelGroupVO.getRiskCount());
                    break;
                case "中高风险":
                    securityRiskAssessmentPieVO.setMediumHighRiskNumber(riskLevelGroupVO.getRiskCount() == null ? 0 : riskLevelGroupVO.getRiskCount());
                    break;
                case "中风险":
                    securityRiskAssessmentPieVO.setMediumRiskNumber(riskLevelGroupVO.getRiskCount() == null ? 0 : riskLevelGroupVO.getRiskCount());
                    break;
                case "低风险":
                    securityRiskAssessmentPieVO.setLowRiskNumber(riskLevelGroupVO.getRiskCount() == null ? 0 : riskLevelGroupVO.getRiskCount());
                    break;
            }
        }

        securityRiskAssessmentPieVO.setTotal(securityRiskAssessmentPieVO.getHighRiskNumber() + securityRiskAssessmentPieVO.getMediumHighRiskNumber() + securityRiskAssessmentPieVO.getMediumRiskNumber() + securityRiskAssessmentPieVO.getLowRiskNumber());

        securityRiskAssessmentPieVO.setHighRiskNumberProportion(Math.round(((float) securityRiskAssessmentPieVO.getHighRiskNumber() / securityRiskAssessmentPieVO.getTotal()) * 100.0) / 100.0 + "%");
        securityRiskAssessmentPieVO.setMediumHighRiskNumberProportion(Math.round(((float) securityRiskAssessmentPieVO.getMediumHighRiskNumber() / securityRiskAssessmentPieVO.getTotal()) * 100.0) / 100.0 + "%");
        securityRiskAssessmentPieVO.setMediumRiskNumberProportion(Math.round(((float) securityRiskAssessmentPieVO.getMediumRiskNumber() / securityRiskAssessmentPieVO.getTotal()) * 100.0) / 100.0 + "%");
        securityRiskAssessmentPieVO.setLowRiskNumberProportion(Math.round(((float) securityRiskAssessmentPieVO.getLowRiskNumber() / securityRiskAssessmentPieVO.getTotal()) * 100.0) / 100.0 + "%");

        return MessageInfo.ok(securityRiskAssessmentPieVO);
    }

    @Override
    public MessageInfo<ScreenDisplayVideoChartVO> queryScreenDisplayVideoChart() {
        ScreenDisplayVideoChartVO screenDisplayVideoChartVO = new ScreenDisplayVideoChartVO();

        List<ScreenVideoNumberVO> screenVideoNumberVOS = new ArrayList<>();
        //初始化三家 为 中海油 中石油 中石化
        screenVideoNumberVOS.add(new ScreenVideoNumberVO().setAppName("中海油").setVideoTotalNumber(0L));
        screenVideoNumberVOS.add(new ScreenVideoNumberVO().setAppName("中石油").setVideoTotalNumber(0L));
        screenVideoNumberVOS.add(new ScreenVideoNumberVO().setAppName("中石化").setVideoTotalNumber(0L));


        List<ScreenVideoNumberVO> resVideo = videoEquipmentInfoMapper.queryVideoNumberChart();

        //赋值
        for (ScreenVideoNumberVO screenVideoNumberVO : resVideo) {
            for (ScreenVideoNumberVO screenVideoNumberVO1 : screenVideoNumberVOS) {
                if (screenVideoNumberVO.getAppName().equals(screenVideoNumberVO1.getAppName())) {
                    screenVideoNumberVO1.setVideoTotalNumber(screenVideoNumberVO.getVideoTotalNumber());
                }
            }
        }

        List<ScreenVideoNumberOnlineVO> screenVideoNumberOnlineVOS = new ArrayList<>();
        //初始化三家 为 中海油 中石油 中石化
        screenVideoNumberOnlineVOS.add(new ScreenVideoNumberOnlineVO().setAppName("中海油").setOnlineNumber(0).setTotalNumber(0));
        screenVideoNumberOnlineVOS.add(new ScreenVideoNumberOnlineVO().setAppName("中石油").setOnlineNumber(0).setTotalNumber(0));
        screenVideoNumberOnlineVOS.add(new ScreenVideoNumberOnlineVO().setAppName("中石化").setOnlineNumber(0).setTotalNumber(0));

        List<ScreenVideoNumberOnlineVO> onlineChart = videoEquipmentInfoMapper.queryVideoNumberOnlineChart();

        for (ScreenVideoNumberOnlineVO screenVideoNumberOnlineVO : onlineChart) {
            switch (screenVideoNumberOnlineVO.getAppName()) {
                case "zhonghaiyou":
                    screenVideoNumberOnlineVOS.get(0).setOnlineNumber(screenVideoNumberOnlineVO.getOnlineNumber());
                    screenVideoNumberOnlineVOS.get(0).setTotalNumber(screenVideoNumberOnlineVO.getTotalNumber());
                    break;
                case "zhongshiyou":
                    screenVideoNumberOnlineVOS.get(1).setOnlineNumber(screenVideoNumberOnlineVO.getOnlineNumber());
                    screenVideoNumberOnlineVOS.get(1).setTotalNumber(screenVideoNumberOnlineVO.getTotalNumber());
                    break;
                case "zhongshihua":
                    screenVideoNumberOnlineVOS.get(2).setOnlineNumber(screenVideoNumberOnlineVO.getOnlineNumber());
                    screenVideoNumberOnlineVOS.get(2).setTotalNumber(screenVideoNumberOnlineVO.getTotalNumber());
                    break;
            }
        }


        screenDisplayVideoChartVO.setVideoNumberChart(screenVideoNumberVOS);

        screenDisplayVideoChartVO.setVideoNumberOnlineChart(screenVideoNumberOnlineVOS);

        //视频设备总数
        int videoNumber = videoEquipmentInfoMapper.selectCount(null);

        //视频设备在线数
        int videoOnlineNumber = videoEquipmentInfoMapper.selectCount(Wrappers.lambdaQuery(VideoEquipmentInfo.class).eq(VideoEquipmentInfo::getOnlineStatus, "1"));

        //视频接入率
        screenDisplayVideoChartVO.setVideoAccessRate("100%");

        //视频在线率
        screenDisplayVideoChartVO.setVideoOnlineRate("100%");

        //视频路数在线率
        screenDisplayVideoChartVO.setVideoRoadOnlineRate(Math.round(((float) videoOnlineNumber / videoNumber) * 100.0) + "%");


        return MessageInfo.ok(screenDisplayVideoChartVO);
    }

    @Override
    public MessageInfo<ScreenDisplayVideoChartVO> queryScreenDisplayVideoChartV2() {

        ScreenDisplayVideoChartVO screenDisplayVideoChartVO = new ScreenDisplayVideoChartVO();

        //视频总数
        long videoNumber = 0L;

        long videoOnlineNumber = 0L;

        HashMap<String, Long> oilCount = new HashMap<>();

        oilCount.put("中海油", 0L);
        oilCount.put("中石油", 0L);
        oilCount.put("中石化", 0L);

        HashMap<String, Long> oilOnlineCount = new HashMap<>();

        oilOnlineCount.put("中海油", 0L);
        oilOnlineCount.put("中石油", 0L);
        oilOnlineCount.put("中石化", 0L);

        HashMap<String, Float> oilOnlineRate = new HashMap<>();
        oilOnlineRate.put("中海油", 0F);
        oilOnlineRate.put("中石油", 0F);
        oilOnlineRate.put("中石化", 0F);

        List<VideoInfo> videoInfos = videoInfoService.list();
        List<VideoInfo> threeOil = videoInfos.stream().filter(item -> "中海油".equals(item.getName()) || "中石油".equals(item.getName()) || "中石化".equals(item.getName())).collect(Collectors.toList());

        List<VideoInfo> platformList = videoInfos.stream().filter(item -> item.getCameraIndexCode() == null).collect(Collectors.toList());

        List<VideoInfo> cameralist = videoInfos.stream().filter(item -> item.getCameraIndexCode() != null).collect(Collectors.toList());

        for (VideoInfo videoInfo : threeOil) {

            String name = videoInfo.getName();
            long oilCameraCount = 0L;
            long oilCameraOnlineCount = 0L;

            List<String> indexCodes = fetchSubRegions(videoInfo.getIndexCode(), platformList);

            //获取下面得子区域
            for (String indexCode : indexCodes) {
                List<VideoInfo> totalSize = cameralist.stream().filter(item -> indexCode.equals(item.getParentIndexCode())).collect(Collectors.toList());
                List<VideoInfo> onlineSize = cameralist.stream().filter(item -> item.getStatus() != null).filter(item -> indexCode.equals(item.getParentIndexCode())).filter(item -> item.getStatus().equals("1")).collect(Collectors.toList());
                videoNumber += totalSize.size();
                videoOnlineNumber += onlineSize.size();
                oilCameraCount += totalSize.size();
                oilCameraOnlineCount += onlineSize.size();
            }

            oilCount.put(name, oilCameraCount);
            oilOnlineCount.put(name, oilCameraOnlineCount);
            oilOnlineRate.put(name, ((float) oilCameraOnlineCount / oilCameraCount));
        }

        //视频接入率
        screenDisplayVideoChartVO.setVideoAccessRate("100%");

        //视频在线率
        screenDisplayVideoChartVO.setVideoOnlineRate("100%");

        //视频路数在线率
        screenDisplayVideoChartVO.setVideoRoadOnlineRate(Math.round(((float) videoOnlineNumber / videoNumber) * 100.0) + "%");

        screenDisplayVideoChartVO.setVideoRoadNumber(Math.toIntExact(videoNumber));
        screenDisplayVideoChartVO.setVideoRoadOnlineNumber(Math.toIntExact(videoOnlineNumber));

        List<ScreenVideoNumberVO> screenVideoNumberVOS = new ArrayList<>();
        //初始化三家 为 中海油 中石油 中石化
        screenVideoNumberVOS.add(new ScreenVideoNumberVO().setAppName("中海油").setVideoTotalNumber(oilCount.get("中海油")));
        screenVideoNumberVOS.add(new ScreenVideoNumberVO().setAppName("中石油").setVideoTotalNumber(oilCount.get("中石油")));
        screenVideoNumberVOS.add(new ScreenVideoNumberVO().setAppName("中石化").setVideoTotalNumber(oilCount.get("中石化")));

        screenDisplayVideoChartVO.setVideoNumberChart(screenVideoNumberVOS);

        List<ScreenVideoNumberOnlineVO> screenVideoNumberOnlineVOS = new ArrayList<>();
        //初始化三家 为 中海油 中石油 中石化
        screenVideoNumberOnlineVOS.add(new ScreenVideoNumberOnlineVO().setAppName("中海油").setOnlineNumber(Math.toIntExact(oilOnlineCount.get("中海油"))).setTotalNumber(Math.toIntExact(oilCount.get("中海油"))));
        screenVideoNumberOnlineVOS.add(new ScreenVideoNumberOnlineVO().setAppName("中石油").setOnlineNumber(Math.toIntExact(oilOnlineCount.get("中石油"))).setTotalNumber(Math.toIntExact(oilCount.get("中石油"))));
        screenVideoNumberOnlineVOS.add(new ScreenVideoNumberOnlineVO().setAppName("中石化").setOnlineNumber(Math.toIntExact(oilOnlineCount.get("中石化"))).setTotalNumber(Math.toIntExact(oilCount.get("中石化"))));

        screenDisplayVideoChartVO.setVideoNumberChart(screenVideoNumberVOS);
        screenDisplayVideoChartVO.setVideoNumberOnlineChart(screenVideoNumberOnlineVOS);

        List<ScreenVideoNumberOnlineRateVO> screenVideoNumberOnlineRateVOS = new ArrayList<>();
        screenVideoNumberOnlineRateVOS.add(new ScreenVideoNumberOnlineRateVO().setAppName("中海油").setVideoOnlineRate(oilOnlineRate.get("中海油")));
        screenVideoNumberOnlineRateVOS.add(new ScreenVideoNumberOnlineRateVO().setAppName("中石油").setVideoOnlineRate(oilOnlineRate.get("中石油")));
        screenVideoNumberOnlineRateVOS.add(new ScreenVideoNumberOnlineRateVO().setAppName("中石化").setVideoOnlineRate(oilOnlineRate.get("中石化")));
        screenDisplayVideoChartVO.setVideoOnlineRateChart(screenVideoNumberOnlineRateVOS);

        return MessageInfo.ok(screenDisplayVideoChartVO);
    }

    public List<String> fetchSubRegions(String parentIndexCode, List<VideoInfo> platformList) {
        List<String> indexCodes = new ArrayList<>();

        List<VideoInfo> collect = platformList.stream().filter(item -> parentIndexCode.equals(item.getParentIndexCode())).collect(Collectors.toList());


        if (collect.isEmpty()) {
            indexCodes.add(parentIndexCode);
        } else {
            for (VideoInfo videoInfo : collect) {
                // 递归获取子区域
                indexCodes.addAll(fetchSubRegions(videoInfo.getIndexCode(), platformList));
            }
        }
        return indexCodes;
    }


    @Override
    public MessageInfo<HiddenDangerNumberVO> queryHiddenDangerRectifyProportion(BaseParam param) {
        if (param.getYear() == null) {
            //本年度评估数量
            param.setYear(LocalDate.now().getYear() + "");
        }
        HiddenDangerNumberVO hiddenDangerNumber = securityRiskAssessmentMapper.getHiddenDangerNumber(param);

        //低风险
        hiddenDangerNumber.setLowRiskRectifyRate(hiddenDangerNumber.getLowRiskRectifyNumber() + "/" + hiddenDangerNumber.getLowRiskNumber());

        //中风险
        hiddenDangerNumber.setMediumRiskRectifyRate(hiddenDangerNumber.getMediumRiskRectifyNumber() + "/" + hiddenDangerNumber.getMediumRiskNumber());

        //较高风险
        hiddenDangerNumber.setMediumHighRiskRectifyRate(hiddenDangerNumber.getMediumHighRiskRectifyNumber() + "/" + hiddenDangerNumber.getMediumHighRiskNumber());

        //高风险
        hiddenDangerNumber.setHighRiskRectifyRate(hiddenDangerNumber.getHighRiskRectifyNumber() + "/" + hiddenDangerNumber.getHighRiskNumber());


        //总的风险数量
        int total = hiddenDangerNumber.getLowRiskNumber() + hiddenDangerNumber.getMediumRiskNumber() + hiddenDangerNumber.getMediumHighRiskNumber() + hiddenDangerNumber.getHighRiskNumber();

        int totalRectify = hiddenDangerNumber.getLowRiskRectifyNumber() + hiddenDangerNumber.getMediumRiskRectifyNumber() + hiddenDangerNumber.getMediumHighRiskRectifyNumber() + hiddenDangerNumber.getHighRiskRectifyNumber();

        hiddenDangerNumber.setPlatformRectifyRate(totalRectify+"/"+total);

        return MessageInfo.ok(hiddenDangerNumber);
    }

    @Override
    public MessageInfo<SecurityRiskAssessmentBarVO> queryRiskAssessmentStatusBar(SecurityRiskAssessmentParam param) {

        if (param.getYear() == null){
            param.setYear(LocalDate.now().getYear()+"");
        }
        List<SecurityRiskAssessmentSummaryVO> securityRiskAssessmentSummaries = securityRiskAssessmentSummaryMapper.querySecurityRiskAssessmentSummary(param);//
        String[] riskLevels = {"高风险", "较高风险", "中风险", "低风险", "暂未定级"};
        String[] platformTypes = {"1", "2", "3", "4"};

        // 创建一个Map来存储结果
        Map<String, Map<String, Long>> countsByRiskAndPlatform = new HashMap<>();

        for (String riskLevel : riskLevels) {
            Map<String, Long> platformCounts = new HashMap<>();
            for (String platformType : platformTypes) {
                long count = securityRiskAssessmentSummaries.stream()
                        .filter(item -> item.getRiskLevel().equals(riskLevel))
                        .filter(item -> item.getPlatformType().equals(platformType))
                        .count();
                platformCounts.put(platformType, count);
            }
            countsByRiskAndPlatform.put(riskLevel, platformCounts);
        }

        //1固定平台 2FPSO 3钻井平台 4人工岛

        //低风险固定平台数量
        Long lowRiskFixedPlatformNumber = countsByRiskAndPlatform.getOrDefault("低风险", new HashMap<>()).getOrDefault("1", 0L);
        //低风险FPSO数量
        Long lowRiskFPSONumber = countsByRiskAndPlatform.getOrDefault("低风险", new HashMap<>()).getOrDefault("2", 0L);
        //低风险钻井平台数量
        Long lowRiskDrillingPlatformNumber = countsByRiskAndPlatform.getOrDefault("低风险", new HashMap<>()).getOrDefault("3", 0L);
        //低风险人工岛数量
        Long lowRiskArtificialIslandNumber = countsByRiskAndPlatform.getOrDefault("低风险", new HashMap<>()).getOrDefault("4", 0L);
        //中风险固定平台数量
        Long mediumRiskFixedPlatformNumber = countsByRiskAndPlatform.getOrDefault("中风险", new HashMap<>()).getOrDefault("1", 0L);
        //中风险FPSO数量
        Long mediumRiskFPSONumber = countsByRiskAndPlatform.getOrDefault("中风险", new HashMap<>()).getOrDefault("2", 0L);
        //中风险钻井平台数量
        Long mediumRiskDrillingPlatformNumber = countsByRiskAndPlatform.getOrDefault("中风险", new HashMap<>()).getOrDefault("3", 0L);
        //中风险人工岛数量
        Long mediumRiskArtificialIslandNumber = countsByRiskAndPlatform.getOrDefault("中风险", new HashMap<>()).getOrDefault("4", 0L);
        //较高风险固定平台数量
        Long mediumHighRiskFixedPlatformNumber = countsByRiskAndPlatform.getOrDefault("较高风险", new HashMap<>()).getOrDefault("1", 0L);
        //较高风险FPSO数量
        Long mediumHighRiskFPSONumber = countsByRiskAndPlatform.getOrDefault("较高风险", new HashMap<>()).getOrDefault("2", 0L);
        //较高风险钻井平台数量
        Long mediumHighRiskDrillingPlatformNumber = countsByRiskAndPlatform.getOrDefault("较高风险", new HashMap<>()).getOrDefault("3", 0L);
        //较高风险人工岛数量
        Long mediumHighRiskArtificialIslandNumber = countsByRiskAndPlatform.getOrDefault("较高风险", new HashMap<>()).getOrDefault("4", 0L);
        //高风险固定平台数量
        Long highRiskFixedPlatformNumber = countsByRiskAndPlatform.getOrDefault("高风险", new HashMap<>()).getOrDefault("1", 0L);
        //高风险FPSO数量
        Long highRiskFPSONumber = countsByRiskAndPlatform.getOrDefault("高风险", new HashMap<>()).getOrDefault("2", 0L);
        //高风险钻井平台数量
        Long highRiskDrillingPlatformNumber = countsByRiskAndPlatform.getOrDefault("高风险", new HashMap<>()).getOrDefault("3", 0L);
        //高风险人工岛数量
        Long highRiskArtificialIslandNumber = countsByRiskAndPlatform.getOrDefault("高风险", new HashMap<>()).getOrDefault("4", 0L);

        //暂未定级固定平台数量
        Long unFixedPlatformNumber = countsByRiskAndPlatform.getOrDefault("暂未定级", new HashMap<>()).getOrDefault("1", 0L);
        //暂未定级FPSO数量
        Long unFPSONumber = countsByRiskAndPlatform.getOrDefault("暂未定级", new HashMap<>()).getOrDefault("2", 0L);
        //暂未定级钻井平台数量
        Long unDrillingPlatformNumber = countsByRiskAndPlatform.getOrDefault("暂未定级", new HashMap<>()).getOrDefault("3", 0L);
        //暂未定级人工岛数量
        Long unArtificialIslandNumber = countsByRiskAndPlatform.getOrDefault("暂未定级", new HashMap<>()).getOrDefault("4", 0L);


        SecurityRiskAssessmentBarVO securityRiskAssessmentBarVO = new SecurityRiskAssessmentBarVO()
                .setHighRiskFixedPlatformNumber(highRiskFixedPlatformNumber)
                .setHighRiskFPSONumber(highRiskFPSONumber)
                .setHighRiskDrillingPlatformNumber(highRiskDrillingPlatformNumber)
                .setHighRiskArtificialIslandNumber(highRiskArtificialIslandNumber)
                .setMediumHighRiskFixedPlatformNumber(mediumHighRiskFixedPlatformNumber)
                .setMediumHighRiskFPSONumber(mediumHighRiskFPSONumber)
                .setMediumHighRiskDrillingPlatformNumber(mediumHighRiskDrillingPlatformNumber)
                .setMediumHighRiskArtificialIslandNumber(mediumHighRiskArtificialIslandNumber)
                .setMediumRiskFixedPlatformNumber(mediumRiskFixedPlatformNumber)
                .setMediumRiskFPSONumber(mediumRiskFPSONumber)
                .setMediumRiskDrillingPlatformNumber(mediumRiskDrillingPlatformNumber)
                .setMediumRiskArtificialIslandNumber(mediumRiskArtificialIslandNumber)
                .setLowRiskFixedPlatformNumber(lowRiskFixedPlatformNumber)
                .setLowRiskFPSONumber(lowRiskFPSONumber)
                .setLowRiskDrillingPlatformNumber(lowRiskDrillingPlatformNumber)
                .setLowRiskArtificialIslandNumber(lowRiskArtificialIslandNumber)
                .setUnFixedPlatformNumber(unFixedPlatformNumber)
                .setUnFPSONumber(unFPSONumber)
                .setUnDrillingPlatformNumber(unDrillingPlatformNumber)
                .setUnArtificialIslandNumber(unArtificialIslandNumber);
        return MessageInfo.ok(securityRiskAssessmentBarVO);
    }

    @Override
    public MessageInfo<ProblemCheckPieVO> queryProblemCheckPie(ProblemCheckPieParam param) {

        if (param.getYear() == null){
            param.setYear(LocalDate.now().getYear() + "");
        }

        List<FourProblemCheckVO> fourProblemCheckVOS = new ArrayList<>();
        //深度评估:1 ; 分部检查:2 ;部级督导:3；自评：4
        if (param.getType() == 1){
            //深度评估
            fourProblemCheckVOS = problemRectificationDetailMapper.deepEvaluation(param);
        }else if (param.getType() == 2){
            fourProblemCheckVOS = problemRectificationDetailMapper.divisionEvaluation(param);
        } else if (param.getType() == 3) {
            fourProblemCheckVOS = problemRectificationDetailMapper.ministerEvaluation(param);
        }else if (param.getType() == 4) {
            fourProblemCheckVOS = problemRectificationDetailMapper.selfEvaluation(param);
        }

        ProblemCheckPieVO problemCheckPieVO = new ProblemCheckPieVO();

        if (!fourProblemCheckVOS.isEmpty()){
            //001待整治、002已整治
            for (FourProblemCheckVO fourProblemCheckVO : fourProblemCheckVOS) {
                if (fourProblemCheckVO.getRectifyStatus().equals("001")){
                    problemCheckPieVO.setUnRectifyNumber(fourProblemCheckVO.getNumber());
                }else if (fourProblemCheckVO.getRectifyStatus().equals("002")){
                    problemCheckPieVO.setRectifyNumber(fourProblemCheckVO.getNumber());
                }
            }
            if (problemCheckPieVO.getUnRectifyNumber() == null){
                problemCheckPieVO.setUnRectifyProportion("0%");
                problemCheckPieVO.setUnRectifyNumber(0);
            }else {
                problemCheckPieVO.setUnRectifyProportion(problemCheckPieVO.getUnRectifyNumber() / (problemCheckPieVO.getUnRectifyNumber() + problemCheckPieVO.getRectifyNumber()) * 100 + "%");
            }

            if (problemCheckPieVO.getRectifyNumber() == null){
                problemCheckPieVO.setRectifyProportion("0%");
                problemCheckPieVO.setRectifyNumber(0);
            }else {
                problemCheckPieVO.setRectifyProportion(problemCheckPieVO.getRectifyNumber() / (problemCheckPieVO.getUnRectifyNumber() + problemCheckPieVO.getRectifyNumber()) * 100 + "%");
            }
        }else {
            problemCheckPieVO.setUnRectifyProportion("0%");
            problemCheckPieVO.setRectifyProportion("0%");
            problemCheckPieVO.setUnRectifyNumber(0);
            problemCheckPieVO.setRectifyNumber(0);
        }


        // problemRectificationDetailMapper.
        return MessageInfo.ok(problemCheckPieVO);
    }
}
