package com.tofly.analyse.util.analysis;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.supermap.analyst.networkanalyst.FacilityAnalyst;
import com.supermap.data.CursorType;
import com.supermap.data.Recordset;
import com.tofly.analyse.entity.LayerFiledConfig;
import com.tofly.analyse.entity.analyse.BurstResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * 关阀分析
 *
 * @author zhoux
 */
@Component
public class FormShutoffValveAnalyst {

    @Autowired
    public ConnectionAnalysis connectionAnalysis;

    @Autowired
    private BurstPipeAnalysis burstPipeAnalysis;



    //关阀分析
    public BurstResult formShutoffValveAnalyst(FacilityAnalyst facilityAnalyst, LayerFiledConfig layfiledConfig, String sid, List<Integer> sourceIds) {
        BurstResult brResult = new BurstResult();
        List<String> affectedEdge = new ArrayList<>();
        List<Object> affectedNode = new ArrayList<>();
        List<Object> mustCloseNode = new ArrayList<>();

        //不存在爆管线则做关阀分析
        if(StringUtils.isNotBlank(sid)){
            List<String> iValveNodeIds = Arrays.asList(sid.split(","));
            //有效进行分析的边Id集合
            List<Map<String,String>> lstValidEdgeIds = new ArrayList<Map<String,String> >();
            //分析阀门是否会关闭后产生影响，转换得到模拟爆管分析的边Id
            for(String iValveNodeId:iValveNodeIds) {
                String strSQL = "START_SID= '"+iValveNodeId+"' or END_SID = '"+iValveNodeId+"' ";
                Recordset recConnectedPipe = facilityAnalyst.getAnalystSetting().getNetworkDataset().query(strSQL, CursorType.STATIC);
                recConnectedPipe.moveFirst();
                //障碍两边的弧段只需要取一端，以此阀门做二次关阀分析
                Boolean flag=true;
                while(recConnectedPipe.isEOF() == false ) {
                    //记录关阀后，受影响的待分析源管线
                    if(flag){
                        Map<String,String> map=new HashMap<>();
                        map.put("edgeSid",recConnectedPipe.getString(layfiledConfig.getNo()));
                        map.put("nodeSid",iValveNodeId);
                        lstValidEdgeIds.add(map);
                        flag=false;
                    }
                    recConnectedPipe.moveNext();
                }
                recConnectedPipe.close();
                recConnectedPipe.dispose();
            }
            //进行爆管分析
            if(CollUtil.isNotEmpty(lstValidEdgeIds)){
                for (Map<String,String> lstValidEdgeId : lstValidEdgeIds) {
                    String edgeSid = lstValidEdgeId.get("edgeSid");
                    String nodeSid = lstValidEdgeId.get("nodeSid");
                    BurstResult burstResult = burstPipeAnalysis.BurstPipeAnalysis(edgeSid, facilityAnalyst, layfiledConfig, sourceIds, nodeSid);
                    affectedEdge.addAll(burstResult.getAffectedEdge());
                    affectedNode.addAll(burstResult.getAffectedNode());
                    mustCloseNode.addAll(burstResult.getMustCloseNode());
                }
            }
        }
        if(CollUtil.isNotEmpty(affectedEdge)){
            affectedEdge=affectedEdge.stream().distinct().collect(Collectors.toList());
        }
        if(CollUtil.isNotEmpty(affectedNode)){
            affectedNode=affectedNode.stream().distinct().collect(Collectors.toList());
        }
        if(CollUtil.isNotEmpty(mustCloseNode)){
            mustCloseNode=mustCloseNode.stream().distinct().collect(Collectors.toList());
        }
        brResult.setAffectedEdge(affectedEdge);
        brResult.setAffectedNode(affectedNode);
        brResult.setMustCloseNode(mustCloseNode);
        return brResult;
    }


}