package com.hdkj.lt.bf.application;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hdkj.fhzg.optimization.response.OperationMetrics;
import com.hdkj.fhzg.optimization.response.StationGridMetrics;
import com.hdkj.fhzg.optimization.response.StationSvgResponse;
import com.hdkj.lt.bf.entity.FhzgOptimizationFeederMetrics;
import com.hdkj.lt.bf.entity.convert.EntityToVOConvertor;
import com.hdkj.lt.bf.service.FhzgOptimizationFeederMetricsService;
import com.hdkj.lt.bf.service.IXlRelService;
import com.hdkj.lt.core.bizms.modle.po.DwdShbDsFeederBase;
import com.hdkj.lt.core.bizms.modle.po.XlRel;
import com.hdkj.lt.core.sys.service.DwdShbDsFeederBaseService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author rj
 * @date 2026/5/25 15:36
 * @description
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class OptimizationApplication {

    private final IXlRelService xlRelService;
    private final DwdShbDsFeederBaseService feederBaseService;
    private final FhzgOptimizationFeederMetricsService feederMetricsService;

    public List<StationSvgResponse> getStationSvg(String maintOrg) {
        List<DwdShbDsFeederBase> feederList = feederBaseService.lambdaQuery().eq(DwdShbDsFeederBase::getPsrState, "20")
                .eq(DwdShbDsFeederBase::getPubPrivFlag, "0")
                .eq(DwdShbDsFeederBase::getMaintOrg, maintOrg).list();
        Map<String, String> stationMap = feederList.stream().collect(Collectors.toMap(DwdShbDsFeederBase::getStartStationName, DwdShbDsFeederBase::getStartStation, (i, j) -> i));
        List<String> feederIdList = feederList.stream().map(DwdShbDsFeederBase::getPsrId).collect(Collectors.toList());
        //有联络的线路
        List<XlRel> contactFeederlist = xlRelService.list(new LambdaQueryWrapper<XlRel>().in(XlRel::getXlIdOne, feederIdList));
        for (XlRel xlRel : contactFeederlist) {
            xlRel.setBdzIdOne(stationMap.get(xlRel.getBdzNameOne()));
            xlRel.setBdzIdTwo(stationMap.get(xlRel.getBdzNameTwo()));
        }
        Set<String> contactFeederIdlist = contactFeederlist.stream().map(XlRel::getXlIdOne).collect(Collectors.toSet());
        //单辐射线路
        List<DwdShbDsFeederBase> collect = feederList.stream().filter(i -> !contactFeederIdlist.contains(i.getPsrId())).collect(Collectors.toList());
        for (DwdShbDsFeederBase dwdShbDsFeederBase : collect) {
            XlRel xlRel = new XlRel();
            xlRel.setId(String.valueOf(IdUtil.getSnowflakeNextId()));
            xlRel.setXlIdOne(dwdShbDsFeederBase.getPsrId());
            xlRel.setXlNameOne(dwdShbDsFeederBase.getName());
            xlRel.setBdzIdOne(dwdShbDsFeederBase.getStartStation());
            xlRel.setBdzNameOne(dwdShbDsFeederBase.getStartStationName());
            contactFeederlist.add(xlRel);
        }
        return EntityToVOConvertor.INSTANCE.toStationSvgResponseList(contactFeederlist);
    }


    public OperationMetrics getStationOperationMetrics(String stationId) {
        FhzgOptimizationFeederMetrics byId = feederMetricsService.getById(1);
        String json = byId.getJsonResult();
        return JSON.parseObject(json, OperationMetrics.class);
    }

    public OperationMetrics getFeederOperationMetrics(String feederId) {
        FhzgOptimizationFeederMetrics byId = feederMetricsService.getById(2);
        String json = byId.getJsonResult();
        return JSON.parseObject(json, OperationMetrics.class);
    }

    public StationGridMetrics getStationGridMetrics(String stationId) {
        FhzgOptimizationFeederMetrics byId = feederMetricsService.getById(3);
        String json = byId.getJsonResult();
        return JSON.parseObject(json, StationGridMetrics.class);
    }
}
