package com.zksy.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.base.domain.PipeNetworkBase;
import com.zksy.base.domain.RiskAssessment;
import com.zksy.base.dto.RiskPageInDTO;
import com.zksy.base.mapper.PipeNetworkBaseMapper;
import com.zksy.base.mapper.RiskAssessmentMapper;
import com.zksy.base.service.RiskAssessmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class RiskAssessmentServiceImpl extends ServiceImpl<RiskAssessmentMapper, RiskAssessment>
        implements RiskAssessmentService {

    @Autowired
    private PipeNetworkBaseMapper pipeNetworkBaseMapper;

    @Override
    public Page<RiskAssessment> findByPage(RiskPageInDTO dto) {
        Page<RiskAssessment> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<RiskAssessment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dto.getNetworkId() != null && !dto.getNetworkId().isEmpty(), RiskAssessment::getNetworkId, dto.getNetworkId());
        wrapper.eq(dto.getRiskLevel() != null && !dto.getRiskLevel().isEmpty(), RiskAssessment::getRiskLevel, dto.getRiskLevel());
        wrapper.orderByDesc(RiskAssessment::getAssessmentTime);
        return this.page(page, wrapper);
    }

    @Override
    public List<Map<String, Object>> getFourColorMapData() {
        List<RiskAssessment> riskList = this.list(new LambdaQueryWrapper<RiskAssessment>()
                .orderByDesc(RiskAssessment::getAssessmentTime));

        Set<String> networkIds = riskList.stream()
                .map(RiskAssessment::getNetworkId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());

        List<PipeNetworkBase> pipeList = new ArrayList<>();
        if (!networkIds.isEmpty()) {
            pipeList = pipeNetworkBaseMapper.selectBatchIds(networkIds);
        }
        Map<String, PipeNetworkBase> pipeMap = pipeList.stream()
                .collect(Collectors.toMap(PipeNetworkBase::getNetworkId, p -> p, (k1, k2) -> k1));

        List<Map<String, Object>> result = new ArrayList<>();
        for (RiskAssessment risk : riskList) {
            Map<String, Object> item = new HashMap<>();
            item.put("riskId", risk.getRiskId());
            item.put("networkId", risk.getNetworkId());
            item.put("riskLevel", risk.getRiskLevel());
            item.put("riskScore", risk.getRiskScore());
            item.put("riskColor", risk.getRiskColor());
            PipeNetworkBase pipe = pipeMap.get(risk.getNetworkId());
            if (pipe != null) {
                item.put("networkName", pipe.getNetworkName());
                item.put("longitude", pipe.getLongitude());
                item.put("latitude", pipe.getLatitude());
                item.put("networkCode", pipe.getNetworkCode());
            }
            result.add(item);
        }
        return result;
    }
}
