package com.inspect.analysis.service.impl;

import com.inspect.analysis.domain.AlgPatrolPoint;
import com.inspect.analysis.domain.AnalyseSummaryAlarmModel;
import com.inspect.analysis.domain.ResultAnalysis;
import com.inspect.analysis.domain.ResultAnalysisSummaryDTO;
import com.inspect.analysis.mapper.ResultAnalysisMapper;
import com.inspect.analysis.service.IResultAnalysisService;
import com.inspect.base.core.utils.StringUtils;
import com.inspect.partrolresult.domain.PatrolResult;
import com.inspect.task.domain.PatrolData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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 ResultAnalysisServiceImpl implements IResultAnalysisService {
    private final ResultAnalysisMapper resultAnalysisMapper;

    public ResultAnalysisServiceImpl(ResultAnalysisMapper resultAnalysisMapper) {
        this.resultAnalysisMapper = resultAnalysisMapper;
    }

    public ResultAnalysis selectResultAnalysisByLineId(Long lineId) {
        return this.resultAnalysisMapper.selectResultAnalysisByLineId(lineId);
    }

    public int selectResultAnalysisByMainId(Long lineId) {
        return this.resultAnalysisMapper.selectResultAnalysisByMainId(lineId);
    }

    @Override
    public int selectResultAnalysisByMainIds(List<Long> lineIds) {
        return this.resultAnalysisMapper.selectResultAnalysisByMainIds(lineIds);
    }

    public List<ResultAnalysis> selectResultAnalysisList(ResultAnalysis resultAnalysis) {
        return this.resultAnalysisMapper.selectResultAnalysisList(resultAnalysis);
    }

    public List<ResultAnalysis> selectResultAnalysisListByMainId(ResultAnalysis resultAnalysis) {
        return this.resultAnalysisMapper.selectResultAnalysisListByMainId(resultAnalysis);
    }

    public List<PatrolResult> selectResultAnd(ResultAnalysis resultAnalysis) {
        return this.resultAnalysisMapper.selectResultAnd(resultAnalysis);
    }

    public Map<String, String> selectAlgorithmInfo(String algSubtypeCode) {
        Map<String, String> map = this.resultAnalysisMapper.selectDeviceInfo(algSubtypeCode);
        if (map == null) {
            map = this.resultAnalysisMapper.selectDeviceInfoByBj(algSubtypeCode);
        }

        return map;
    }

    public Map<String, String> selectDeviceInfo(String pointId) {
        return this.resultAnalysisMapper.selectDeviceInfo(pointId);
    }

    public Map<String, String> selectPointInfoById(String deviceId) {
        return this.resultAnalysisMapper.selectPointInfoById(deviceId);
    }

    public List<Map<String, String>> selectChannelByEqId(String eqId) {
        return this.resultAnalysisMapper.selectChannelByEqId(eqId);
    }

    public Map<String, Object> selectPointInfoByCode(String deviceCode) {
        return this.resultAnalysisMapper.selectPointInfoByCode(deviceCode);
    }

    public Map<String, Object> selectEqInfoByCode(String eqCode) {
        return this.resultAnalysisMapper.selectEqInfoByCode(eqCode);
    }

    public List<Map<String, Object>> selectEqInfoBySendCode(String SendCode) {
        return this.resultAnalysisMapper.selectEqInfoBySendCode(SendCode);
    }

    public Map<String, String> selectStaPointInfoById(String deviceId) {
        return this.resultAnalysisMapper.selectStadPointInfoById(deviceId);
    }

    public int insertResultAnalysis(ResultAnalysis resultAnalysis) {
        return this.resultAnalysisMapper.insertResultAnalysis(resultAnalysis);
    }

    public int updateResultAnalysis(ResultAnalysis resultAnalysis) {
        return this.resultAnalysisMapper.updateResultAnalysis(resultAnalysis);
    }

    @Override
    public void updateResultAnalysisByCondition(ResultAnalysis analysis) {
        this.resultAnalysisMapper.updateResultAnalysisByCondition(analysis);
    }

    @Override
    public List<ResultAnalysisSummaryDTO> countResultAnalysisSummary(List<String> taskPatrolledIds) {
        if (taskPatrolledIds.size() == 0) {
            return null;
        }
        return resultAnalysisMapper.countResultAnalysisSummary(taskPatrolledIds);
    }

    public int deleteResultAnalysisByLineIds(Long[] lineIds) {
        return this.resultAnalysisMapper.deleteResultAnalysisByLineIds(lineIds);
    }

    public int deleteResultAnalysisByLineId(Long lineId) {
        return this.resultAnalysisMapper.deleteResultAnalysisByLineId(lineId);
    }

    public ResultAnalysis selectTaskAbnormalData(ResultAnalysis resultAnalysis) {
        return this.resultAnalysisMapper.selectTaskAbnormalData(resultAnalysis);
    }

    @Override
    public Map<String, ResultAnalysis> selectTaskAbnormalDataBatch(List<String> taskPatrolledIds) {
        if (taskPatrolledIds == null || taskPatrolledIds.isEmpty()) {
            return new HashMap<>();
        }

        List<Map<String, Object>> resultList = resultAnalysisMapper.selectTaskAbnormalDataBatch(taskPatrolledIds);

        return resultList.stream().collect(Collectors.toMap(
                item -> (String) item.get("taskPatrolledId"),
                item -> {
                    ResultAnalysis analysis = new ResultAnalysis();
                    analysis.setTaskPatrolledId((String) item.get("taskPatrolledId"));
                    analysis.setCsQxNum(((Number) item.get("csQxNum")).intValue());
                    analysis.setAiQxNum(((Number) item.get("aiQxNum")).intValue());
                    analysis.setCsYcNum(((Number) item.get("csYcNum")).intValue());
                    analysis.setAiYcNum(((Number) item.get("aiYcNum")).intValue());
                    return analysis;
                }
        ));
    }

    @Override
    public List<ResultAnalysis> selectResultAnalysisListBatch(List<String> taskPatrolIds, String resultType) {
        return resultAnalysisMapper.selectResultAnalysisListBatch(taskPatrolIds, resultType);
    }

    public List<AlgPatrolPoint> selectResultAnalyseForAlarm(AnalyseSummaryAlarmModel alarmModel) {
        return this.resultAnalysisMapper.selectResultAnalyseForAlarm(alarmModel);
    }

    public List<PatrolData> selectResultAnalysisListForAlarm(AnalyseSummaryAlarmModel alarmModel) {
        List<PatrolData> patrolDataList = this.resultAnalysisMapper.selectResultAnalysisListForAlarm(alarmModel);
        if (patrolDataList == null || patrolDataList.isEmpty()) {
            return patrolDataList;
        }
        Map<String, List<PatrolData>> collect = patrolDataList.stream().filter(item -> StringUtils.isNotEmpty(item.getDesc()) && !item.getDesc().contains("失败")).collect(Collectors.groupingBy(item -> item.getAlgType() + "_" + item.getObjectId()));
        List<PatrolData> resultList = new ArrayList<>();
        collect.forEach((key, value) -> {
            PatrolData patrolData = new PatrolData();
            String algType = key.split("_")[0];
            String objectId = key.split("_")[1];
            patrolData.setAlgType(algType);
            patrolData.setObjectId(objectId);
            for (PatrolData data : value) {
                // 初筛有缺陷 展示初筛结果图片
                if ("1".equals(data.getFilter()) && StringUtils.isEmpty(patrolData.getImgAnalyse())) {
                    patrolData.setImgAnalyse(data.getImgAnalyse());
                }
                if ("1".equals(data.getFilter()) && StringUtils.isEmpty(patrolData.getImg())) {
                    patrolData.setImg(data.getImg());
                }
                // 大模型有缺陷 展示大模型结果图片
                if ("0".equals(data.getFilter())) {
                    patrolData.setImgAnalyse(data.getImgAnalyse());
                    patrolData.setImg(data.getImg());
                }
            }
            patrolData.setPointName(value.stream().map(item -> item.getPointName() != null ? item.getPointName() : "").distinct().findFirst().orElse(""));
            patrolData.setPatrolTime(value.stream().map(item -> item.getPatrolTime() != null ? item.getPatrolTime() : "").distinct().findFirst().orElse(""));
            patrolData.setPointStatus(value.stream().map(item -> item.getPointStatus() != null ? item.getPointStatus() : "").distinct().findFirst().orElse(""));
            patrolData.setWarnStatus(value.stream().map(item -> item.getWarnStatus() != null ? item.getWarnStatus() : "").distinct().findFirst().orElse(""));
            patrolData.setDesc(value.stream().map(PatrolData::getDesc).distinct().collect(Collectors.joining(",")));
            patrolData.setImgType(value.stream().map(item -> item.getImgType() != null ? item.getImgType() : "").distinct().findFirst().orElse(""));
            patrolData.setLineId(value.stream().map(item -> item.getLineId() != null ? item.getLineId() : "").distinct().findFirst().orElse(""));
            patrolData.setDataType(value.stream().map(item -> item.getDataType() != null ? item.getDataType() : "").distinct().findFirst().orElse(""));
            patrolData.setResStatus(value.stream().map(item -> item.getResStatus() != null ? item.getResStatus() : "").distinct().findFirst().orElse(""));
            patrolData.setSuggestion(value.stream().map(item -> item.getSuggestion() != null ? item.getSuggestion() : "").distinct().findFirst().orElse(""));
            resultList.add(patrolData);

        });
        return resultList;
    }


    public Long getDefectCountGroupByPoint(String defectFilter, String defectIsToday, String type) {
        return this.resultAnalysisMapper.selectResultAnalysisListDefectInfo(defectFilter, defectIsToday, type);
    }

}
